-
Notifications
You must be signed in to change notification settings - Fork 266
/
Vlpp.Linux.cpp
224 lines (186 loc) · 6.08 KB
/
Vlpp.Linux.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/***********************************************************************
THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY
DEVELOPER: Zihan Chen(vczh)
***********************************************************************/
#include "Vlpp.h"
/***********************************************************************
.\CONSOLE.LINUX.CPP
***********************************************************************/
/***********************************************************************
Author: Zihan Chen (vczh)
Licensed under https://github.com/vczh-libraries/License
***********************************************************************/
#include <iostream>
#include <string>
#ifndef VCZH_GCC
static_assert(false, "Do not build this file for Windows applications.");
#endif
namespace vl
{
namespace console
{
/***********************************************************************
Console
***********************************************************************/
void Console::Write(const wchar_t* string, vint length)
{
std::wstring s(string, string + length);
std::wcout << s << std::ends;
}
WString Console::Read()
{
std::wstring s;
std::getline(std::wcin, s, L'\n');
return s.c_str();
}
void Console::SetColor(bool red, bool green, bool blue, bool light)
{
int color = (blue ? 1 : 0) * 4 + (green ? 1 : 0) * 2 + (red ? 1 : 0);
if (light)
wprintf(L"\x1B[00;3%dm", color);
else
wprintf(L"\x1B[01;3%dm", color);
}
void Console::SetTitle(const WString& string)
{
}
}
}
/***********************************************************************
.\PRIMITIVES\DATETIME.LINUX.CPP
***********************************************************************/
/***********************************************************************
Author: Zihan Chen (vczh)
Licensed under https://github.com/vczh-libraries/License
***********************************************************************/
#include <time.h>
#include <memory.h>
#include <sys/time.h>
#ifndef VCZH_GCC
static_assert(false, "Do not build this file for Windows applications.");
#endif
namespace vl
{
/***********************************************************************
DateTime
***********************************************************************/
DateTime ConvertTMToDateTime(tm* timeinfo, vint milliseconds)
{
time_t timer = mktime(timeinfo);
DateTime dt;
dt.year = timeinfo->tm_year + 1900;
dt.month = timeinfo->tm_mon + 1;
dt.day = timeinfo->tm_mday;
dt.dayOfWeek = timeinfo->tm_wday;
dt.hour = timeinfo->tm_hour;
dt.minute = timeinfo->tm_min;
dt.second = timeinfo->tm_sec;
dt.milliseconds = milliseconds;
// in Linux and macOS, filetime will be mktime(t) * 1000 + gettimeofday().tv_usec / 1000
dt.filetime = (vuint64_t)timer * 1000 + milliseconds;
dt.totalMilliseconds = (vuint64_t)timer * 1000 + milliseconds;
return dt;
}
DateTime DateTime::LocalTime()
{
struct timeval tv;
gettimeofday(&tv, nullptr);
tm* timeinfo = localtime(&tv.tv_sec);
return ConvertTMToDateTime(timeinfo, tv.tv_usec / 1000);
}
DateTime DateTime::UtcTime()
{
struct timeval tv;
gettimeofday(&tv, nullptr);
tm* timeinfo = gmtime(&tv.tv_sec);
return ConvertTMToDateTime(timeinfo, tv.tv_usec / 1000);
}
DateTime DateTime::FromDateTime(vint _year, vint _month, vint _day, vint _hour, vint _minute, vint _second, vint _milliseconds)
{
tm timeinfo;
memset(&timeinfo, 0, sizeof(timeinfo));
timeinfo.tm_year = _year - 1900;
timeinfo.tm_mon = _month - 1;
timeinfo.tm_mday = _day;
timeinfo.tm_hour = _hour;
timeinfo.tm_min = _minute;
timeinfo.tm_sec = _second;
timeinfo.tm_isdst = -1;
return ConvertTMToDateTime(&timeinfo, _milliseconds);
}
DateTime DateTime::FromFileTime(vuint64_t filetime)
{
time_t timer = (time_t)(filetime / 1000);
tm* timeinfo = localtime(&timer);
return ConvertTMToDateTime(timeinfo, filetime % 1000);
}
DateTime DateTime::ToLocalTime()
{
time_t localTimer = time(nullptr);
time_t utcTimer = mktime(gmtime(&localTimer));
time_t timer = (time_t)(filetime / 1000) + localTimer - utcTimer;
tm* timeinfo = localtime(&timer);
return ConvertTMToDateTime(timeinfo, milliseconds);
}
DateTime DateTime::ToUtcTime()
{
time_t timer = (time_t)(filetime / 1000);
tm* timeinfo = gmtime(&timer);
return ConvertTMToDateTime(timeinfo, milliseconds);
}
DateTime DateTime::Forward(vuint64_t milliseconds)
{
return FromFileTime(filetime + milliseconds);
}
DateTime DateTime::Backward(vuint64_t milliseconds)
{
return FromFileTime(filetime - milliseconds);
}
}
/***********************************************************************
.\STRINGS\CONVERSION.LINUX.CPP
***********************************************************************/
/***********************************************************************
Author: Zihan Chen (vczh)
Licensed under https://github.com/vczh-libraries/License
***********************************************************************/
#include <stdio.h>
#include <ctype.h>
#include <wctype.h>
namespace vl
{
/***********************************************************************
String Conversions (buffer walkthrough)
***********************************************************************/
vint _wtoa(const wchar_t* w, char* a, vint chars)
{
return wcstombs(a, w, chars - 1) + 1;
}
vint _atow(const char* a, wchar_t* w, vint chars)
{
return mbstowcs(w, a, chars - 1) + 1;
}
}
/***********************************************************************
.\UNITTEST\UNITTEST.LINUX.CPP
***********************************************************************/
/***********************************************************************
Author: Zihan Chen (vczh)
Licensed under https://github.com/vczh-libraries/License
***********************************************************************/
#ifndef VCZH_GCC
static_assert(false, "Do not build this file for Windows applications.");
#endif
namespace vl
{
namespace unittest
{
/***********************************************************************
UnitTest
***********************************************************************/
bool UnitTest::IsDebuggerAttached()
{
return false;
}
}
}