mirror of
https://github.com/vczh-libraries/Release.git
synced 2026-02-05 19:40:03 +08:00
283 lines
8.0 KiB
C++
283 lines
8.0 KiB
C++
/***********************************************************************
|
|
THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY
|
|
DEVELOPER: Zihan Chen(vczh)
|
|
***********************************************************************/
|
|
#include "Vlpp.h"
|
|
|
|
/***********************************************************************
|
|
.\CONSOLE.WINDOWS.CPP
|
|
***********************************************************************/
|
|
/***********************************************************************
|
|
Author: Zihan Chen (vczh)
|
|
Licensed under https://github.com/vczh-libraries/License
|
|
***********************************************************************/
|
|
|
|
#include <Windows.h>
|
|
|
|
#ifndef VCZH_MSVC
|
|
static_assert(false, "Do not build this file for non-Windows applications.");
|
|
#endif
|
|
|
|
namespace vl
|
|
{
|
|
namespace console
|
|
{
|
|
|
|
/***********************************************************************
|
|
Console
|
|
***********************************************************************/
|
|
|
|
void Console::Write(const wchar_t* string, vint length)
|
|
{
|
|
HANDLE outHandle = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
DWORD fileMode = 0;
|
|
DWORD written = 0;
|
|
if ((GetFileType(outHandle) & FILE_TYPE_CHAR) && GetConsoleMode(outHandle, &fileMode))
|
|
{
|
|
WriteConsole(outHandle, string, (int)length, &written, 0);
|
|
}
|
|
else
|
|
{
|
|
int codePage = GetConsoleOutputCP();
|
|
int charCount = WideCharToMultiByte(codePage, 0, string, -1, 0, 0, 0, 0);
|
|
char* codePageBuffer = new char[charCount];
|
|
WideCharToMultiByte(codePage, 0, string, -1, codePageBuffer, charCount, 0, 0);
|
|
WriteFile(outHandle, codePageBuffer, charCount - 1, &written, 0);
|
|
delete[] codePageBuffer;
|
|
}
|
|
}
|
|
|
|
WString Console::Read()
|
|
{
|
|
WString result;
|
|
DWORD count;
|
|
for (;;)
|
|
{
|
|
wchar_t buffer;
|
|
ReadConsole(GetStdHandle(STD_INPUT_HANDLE), &buffer, 1, &count, 0);
|
|
if (buffer == L'\r')
|
|
{
|
|
ReadConsole(GetStdHandle(STD_INPUT_HANDLE), &buffer, 1, &count, 0);
|
|
break;
|
|
}
|
|
else if (buffer == L'\n')
|
|
{
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
result = result + WString::FromChar(buffer);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
void Console::SetColor(bool red, bool green, bool blue, bool light)
|
|
{
|
|
WORD attribute = 0;
|
|
if (red)attribute |= FOREGROUND_RED;
|
|
if (green)attribute |= FOREGROUND_GREEN;
|
|
if (blue)attribute |= FOREGROUND_BLUE;
|
|
if (light)attribute |= FOREGROUND_INTENSITY;
|
|
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), attribute);
|
|
SetConsoleTextAttribute(GetStdHandle(STD_INPUT_HANDLE), attribute);
|
|
}
|
|
|
|
void Console::SetTitle(const WString& string)
|
|
{
|
|
SetConsoleTitle(string.Buffer());
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/***********************************************************************
|
|
.\PRIMITIVES\DATETIME.WINDOWS.CPP
|
|
***********************************************************************/
|
|
/***********************************************************************
|
|
Author: Zihan Chen (vczh)
|
|
Licensed under https://github.com/vczh-libraries/License
|
|
***********************************************************************/
|
|
|
|
#include <time.h>
|
|
|
|
#ifndef VCZH_MSVC
|
|
static_assert(false, "Do not build this file for non-Windows applications.");
|
|
#endif
|
|
|
|
namespace vl
|
|
{
|
|
|
|
/***********************************************************************
|
|
DateTime
|
|
***********************************************************************/
|
|
|
|
DateTime SystemTimeToDateTime(const SYSTEMTIME& systemTime)
|
|
{
|
|
DateTime dateTime;
|
|
dateTime.year = systemTime.wYear;
|
|
dateTime.month = systemTime.wMonth;
|
|
dateTime.dayOfWeek = systemTime.wDayOfWeek;
|
|
dateTime.day = systemTime.wDay;
|
|
dateTime.hour = systemTime.wHour;
|
|
dateTime.minute = systemTime.wMinute;
|
|
dateTime.second = systemTime.wSecond;
|
|
dateTime.milliseconds = systemTime.wMilliseconds;
|
|
|
|
FILETIME fileTime;
|
|
SystemTimeToFileTime(&systemTime, &fileTime);
|
|
ULARGE_INTEGER largeInteger;
|
|
largeInteger.HighPart = fileTime.dwHighDateTime;
|
|
largeInteger.LowPart = fileTime.dwLowDateTime;
|
|
dateTime.filetime = largeInteger.QuadPart;
|
|
dateTime.totalMilliseconds = dateTime.filetime / 10000;
|
|
|
|
return dateTime;
|
|
}
|
|
|
|
SYSTEMTIME DateTimeToSystemTime(const DateTime& dateTime)
|
|
{
|
|
ULARGE_INTEGER largeInteger;
|
|
largeInteger.QuadPart = dateTime.filetime;
|
|
FILETIME fileTime;
|
|
fileTime.dwHighDateTime = largeInteger.HighPart;
|
|
fileTime.dwLowDateTime = largeInteger.LowPart;
|
|
|
|
SYSTEMTIME systemTime;
|
|
FileTimeToSystemTime(&fileTime, &systemTime);
|
|
return systemTime;
|
|
}
|
|
|
|
DateTime DateTime::LocalTime()
|
|
{
|
|
SYSTEMTIME systemTime;
|
|
GetLocalTime(&systemTime);
|
|
return SystemTimeToDateTime(systemTime);
|
|
}
|
|
|
|
DateTime DateTime::UtcTime()
|
|
{
|
|
SYSTEMTIME utcTime;
|
|
GetSystemTime(&utcTime);
|
|
return SystemTimeToDateTime(utcTime);
|
|
}
|
|
|
|
DateTime DateTime::FromDateTime(vint _year, vint _month, vint _day, vint _hour, vint _minute, vint _second, vint _milliseconds)
|
|
{
|
|
SYSTEMTIME systemTime;
|
|
memset(&systemTime, 0, sizeof(systemTime));
|
|
systemTime.wYear = (WORD)_year;
|
|
systemTime.wMonth = (WORD)_month;
|
|
systemTime.wDay = (WORD)_day;
|
|
systemTime.wHour = (WORD)_hour;
|
|
systemTime.wMinute = (WORD)_minute;
|
|
systemTime.wSecond = (WORD)_second;
|
|
systemTime.wMilliseconds = (WORD)_milliseconds;
|
|
|
|
FILETIME fileTime;
|
|
SystemTimeToFileTime(&systemTime, &fileTime);
|
|
FileTimeToSystemTime(&fileTime, &systemTime);
|
|
return SystemTimeToDateTime(systemTime);
|
|
}
|
|
|
|
DateTime DateTime::FromFileTime(vuint64_t filetime)
|
|
{
|
|
ULARGE_INTEGER largeInteger;
|
|
largeInteger.QuadPart = filetime;
|
|
FILETIME fileTime;
|
|
fileTime.dwHighDateTime = largeInteger.HighPart;
|
|
fileTime.dwLowDateTime = largeInteger.LowPart;
|
|
|
|
SYSTEMTIME systemTime;
|
|
FileTimeToSystemTime(&fileTime, &systemTime);
|
|
return SystemTimeToDateTime(systemTime);
|
|
}
|
|
|
|
DateTime DateTime::ToLocalTime()
|
|
{
|
|
SYSTEMTIME utcTime = DateTimeToSystemTime(*this);
|
|
SYSTEMTIME localTime;
|
|
SystemTimeToTzSpecificLocalTime(NULL, &utcTime, &localTime);
|
|
return SystemTimeToDateTime(localTime);
|
|
}
|
|
|
|
DateTime DateTime::ToUtcTime()
|
|
{
|
|
SYSTEMTIME localTime = DateTimeToSystemTime(*this);
|
|
SYSTEMTIME utcTime;
|
|
TzSpecificLocalTimeToSystemTime(NULL, &localTime, &utcTime);
|
|
return SystemTimeToDateTime(utcTime);
|
|
}
|
|
|
|
DateTime DateTime::Forward(vuint64_t milliseconds)
|
|
{
|
|
return FromFileTime(filetime + milliseconds * 10000);
|
|
}
|
|
|
|
DateTime DateTime::Backward(vuint64_t milliseconds)
|
|
{
|
|
return FromFileTime(filetime - milliseconds * 10000);
|
|
}
|
|
}
|
|
|
|
|
|
/***********************************************************************
|
|
.\STRINGS\CONVERSION.WINDOWS.CPP
|
|
***********************************************************************/
|
|
/***********************************************************************
|
|
Author: Zihan Chen (vczh)
|
|
Licensed under https://github.com/vczh-libraries/License
|
|
***********************************************************************/
|
|
|
|
|
|
#ifndef VCZH_MSVC
|
|
static_assert(false, "Do not build this file for non-Windows applications.");
|
|
#endif
|
|
|
|
namespace vl
|
|
{
|
|
/***********************************************************************
|
|
String Conversions (buffer walkthrough)
|
|
***********************************************************************/
|
|
|
|
vint _wtoa(const wchar_t* w, char* a, vint chars)
|
|
{
|
|
return WideCharToMultiByte(CP_THREAD_ACP, 0, w, -1, a, (int)(a ? chars : 0), 0, 0);
|
|
}
|
|
|
|
vint _atow(const char* a, wchar_t* w, vint chars)
|
|
{
|
|
return MultiByteToWideChar(CP_THREAD_ACP, 0, a, -1, w, (int)(w ? chars : 0));
|
|
}
|
|
}
|
|
|
|
|
|
/***********************************************************************
|
|
.\UNITTEST\UNITTEST.WINDOWS.CPP
|
|
***********************************************************************/
|
|
/***********************************************************************
|
|
Author: Zihan Chen (vczh)
|
|
Licensed under https://github.com/vczh-libraries/License
|
|
***********************************************************************/
|
|
|
|
|
|
#ifndef VCZH_MSVC
|
|
static_assert(false, "Do not build this file for non-Windows applications.");
|
|
#endif
|
|
|
|
namespace vl
|
|
{
|
|
namespace unittest
|
|
{
|
|
/***********************************************************************
|
|
UnitTest
|
|
***********************************************************************/
|
|
|
|
bool UnitTest::IsDebuggerAttached()
|
|
{
|
|
return IsDebuggerPresent();
|
|
}
|
|
}
|
|
}
|
|
|