2010-05-21 03:13:36 +02:00
|
|
|
// Copyright (c) 2010, Amar Takhar <verm@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
/// @file util.cpp
|
|
|
|
/// @brief Windows utility methods.
|
|
|
|
/// @ingroup libaegisub windows
|
|
|
|
|
|
|
|
#ifndef LAGI_PRE
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
2010-06-03 03:09:41 +02:00
|
|
|
|
|
|
|
#include <windows.h>
|
2010-05-21 03:13:36 +02:00
|
|
|
#endif
|
|
|
|
|
2012-01-12 23:31:54 +01:00
|
|
|
#include "libaegisub/access.h"
|
2010-06-09 04:07:43 +02:00
|
|
|
#include "libaegisub/charset_conv_win.h"
|
2012-01-12 23:31:54 +01:00
|
|
|
#include "libaegisub/types.h"
|
2010-05-21 03:13:36 +02:00
|
|
|
#include "libaegisub/util.h"
|
2010-05-22 23:09:37 +02:00
|
|
|
#include "libaegisub/util_win.h"
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
namespace agi {
|
|
|
|
namespace util {
|
|
|
|
|
2010-06-03 22:32:25 +02:00
|
|
|
using agi::charset::ConvertW;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
const std::string DirName(const std::string& path) {
|
2010-10-07 19:18:00 +02:00
|
|
|
std::string::size_type pos = path.rfind('/');
|
|
|
|
|
|
|
|
if (pos == std::string::npos) pos = path.rfind('\\');
|
|
|
|
if (pos == std::string::npos) return ".";
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2010-10-07 19:18:00 +02:00
|
|
|
return path.substr(0, pos+1);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Rename(const std::string& from, const std::string& to) {
|
|
|
|
acs::CheckFileWrite(from);
|
|
|
|
|
|
|
|
try {
|
|
|
|
acs::CheckFileWrite(to);
|
2012-01-12 23:31:54 +01:00
|
|
|
} catch (FileNotFoundError const&) {
|
2010-05-21 03:13:36 +02:00
|
|
|
acs::CheckDirWrite(DirName(to));
|
|
|
|
}
|
|
|
|
|
2012-10-21 17:08:13 +02:00
|
|
|
if (!MoveFileEx(ConvertW(from).c_str(), ConvertW(to).c_str(), MOVEFILE_REPLACE_EXISTING))
|
|
|
|
throw agi::FileNotAccessibleError("Can not overwrite file: " + ErrorString(GetLastError()));
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2010-05-22 23:09:37 +02:00
|
|
|
std::string ErrorString(DWORD error) {
|
2010-06-03 22:32:25 +02:00
|
|
|
LPWSTR lpstr = NULL;
|
2010-05-22 23:09:37 +02:00
|
|
|
|
2010-06-03 22:32:25 +02:00
|
|
|
if(FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, reinterpret_cast<LPWSTR>(&lpstr), 0, NULL) == 0) {
|
2010-05-22 23:09:37 +02:00
|
|
|
/// @todo Return the actual 'unknown error' string from windows.
|
2010-06-03 22:32:25 +02:00
|
|
|
return "Unknown Error";
|
2010-05-22 23:09:37 +02:00
|
|
|
}
|
|
|
|
|
2010-06-03 22:32:25 +02:00
|
|
|
std::string str = ConvertW(lpstr);
|
2010-05-22 23:09:37 +02:00
|
|
|
LocalFree(lpstr);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2010-06-03 03:09:41 +02:00
|
|
|
/// @brief Get seconds and microseconds.
|
2010-06-09 04:07:43 +02:00
|
|
|
/// @param tv[out] agi_timeval struct
|
2010-06-03 03:09:41 +02:00
|
|
|
/// This code is from http://www.suacommunity.com/dictionary/gettimeofday-entry.php
|
2010-06-09 04:07:43 +02:00
|
|
|
void time_log(agi_timeval &tv) {
|
2010-06-03 03:09:41 +02:00
|
|
|
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
|
|
|
|
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
|
|
|
|
#else
|
|
|
|
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Define a structure to receive the current Windows filetime
|
|
|
|
FILETIME ft;
|
2010-06-05 20:16:16 +02:00
|
|
|
|
2010-06-03 03:09:41 +02:00
|
|
|
// Initialize the present time to 0 and the timezone to UTC
|
|
|
|
unsigned __int64 tmpres = 0;
|
|
|
|
|
|
|
|
GetSystemTimeAsFileTime(&ft);
|
2010-06-05 20:16:16 +02:00
|
|
|
|
|
|
|
// The GetSystemTimeAsFileTime returns the number of 100 nanosecond
|
|
|
|
// intervals since Jan 1, 1601 in a structure. Copy the high bits to
|
2010-06-03 03:09:41 +02:00
|
|
|
// the 64 bit tmpres, shift it left by 32 then or in the low 32 bits.
|
|
|
|
tmpres |= ft.dwHighDateTime;
|
|
|
|
tmpres <<= 32;
|
|
|
|
tmpres |= ft.dwLowDateTime;
|
2010-06-05 20:16:16 +02:00
|
|
|
|
2010-06-03 03:09:41 +02:00
|
|
|
// Convert to microseconds by dividing by 10
|
|
|
|
tmpres /= 10;
|
2010-06-05 20:16:16 +02:00
|
|
|
|
|
|
|
// The Unix epoch starts on Jan 1 1970. Need to subtract the difference
|
2010-06-03 03:09:41 +02:00
|
|
|
// in seconds from Jan 1 1601.
|
|
|
|
tmpres -= DELTA_EPOCH_IN_MICROSECS;
|
2010-06-05 20:16:16 +02:00
|
|
|
|
|
|
|
// Finally change microseconds to seconds and place in the seconds value.
|
2010-06-03 03:09:41 +02:00
|
|
|
// The modulus picks up the microseconds.
|
|
|
|
tv.tv_sec = (long)(tmpres / 1000000UL);
|
|
|
|
tv.tv_usec = (long)(tmpres % 1000000UL);
|
|
|
|
}
|
2010-05-22 23:09:37 +02:00
|
|
|
|
2011-07-26 21:51:07 +02:00
|
|
|
uint64_t freespace(std::string const& path, PathType type) {
|
|
|
|
if (type == TypeFile)
|
|
|
|
return freespace(DirName(path));
|
|
|
|
|
|
|
|
ULARGE_INTEGER bytes_available;
|
|
|
|
if (GetDiskFreeSpaceEx(ConvertW(path).c_str(), &bytes_available, 0, 0))
|
|
|
|
return bytes_available.QuadPart;
|
|
|
|
|
|
|
|
acs::CheckDirRead(path);
|
|
|
|
|
|
|
|
/// @todo GetLastError -> Exception mapping
|
|
|
|
throw "Unknown error getting free space";
|
|
|
|
}
|
|
|
|
|
2010-05-21 03:13:36 +02:00
|
|
|
} // namespace io
|
|
|
|
} // namespace agi
|