This repository has been archived by the owner on Jan 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
288 lines (241 loc) · 7.78 KB
/
utils.c
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#define _CRT_SECURE_NO_WARNINGS
#include "utils.h"
const int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int show_menu(char *title, MenuList menu_list[], int count) {
// 菜单项
int currentChoice = 0;
char key;
while (1) {
system("cls");
printf("\n========%s========\n", title);
// 显示菜单项
for (int i = 0; i < count; i++) {
if (i == currentChoice) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x70);
printf("[%c] %s", 'A' + i, menu_list[i].name);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x07);
printf("\n");
} else {
printf("[%c] %s\n", 'A' + i, menu_list[i].name);
}
}
printf("\n");
printf("使用方向键↑↓选择, 回车确认, 或直接按选项字母, ESC返回\n");
// 读取按键
key = _getch();
if (key == KEY_ESC) {
return -1;
} else if (_kbhit()) {
key = _getch();
switch (key) {
case KEY_UP:
currentChoice = (currentChoice - 1 + count) % count;
break;
case KEY_DOWN:
currentChoice = (currentChoice + 1) % count;
break;
}
} else if (key == KEY_ENTER) {
return menu_list[currentChoice].value;
} else if (toupper(key) >= 'A' && toupper(key) <= 'A' + count - 1) {
currentChoice = toupper(key) - 'A';
return menu_list[currentChoice].value;
}
continue;
}
return 0;
}
Date get_today() {
time_t now = time(NULL);
struct tm *tm = localtime(&now);
return (Date){
.year = tm->tm_year + 1900, .month = tm->tm_mon + 1, .day = tm->tm_mday};
}
Date get_next_day(Date date) {
Date next_day = date;
next_day.day++;
int is_leap_year =
(date.year % 4 == 0 && date.year % 100 != 0) || (date.year % 400 == 0);
int max_days = days_in_month[next_day.month - 1];
if (next_day.month == 2 && is_leap_year) {
max_days++;
}
if (next_day.day > max_days) {
next_day.day = 1;
next_day.month++;
if (next_day.month > 12) {
next_day.month = 1;
next_day.year++;
}
}
return next_day;
}
int time_diff(Date date) {
Date today = get_today();
struct tm tm1 = {.tm_year = today.year - 1900,
.tm_mon = today.month - 1,
.tm_mday = today.day,
.tm_hour = 0,
.tm_min = 0,
.tm_sec = 0};
struct tm tm2 = {.tm_year = date.year - 1900,
.tm_mon = date.month - 1,
.tm_mday = date.day,
.tm_hour = 0,
.tm_min = 0,
.tm_sec = 0};
time_t time1 = mktime(&tm1);
time_t time2 = mktime(&tm2);
return difftime(time2, time1) / 86400;
}
char *http_get(const wchar_t *server, const wchar_t *path, size_t *resultSize) {
// 初始化 WinHTTP 会话
HINTERNET hSession =
WinHttpOpen(L"WinHTTP Example/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (!hSession) {
wprintf(L"WinHttpOpen failed with error: %d\n", GetLastError());
return NULL;
}
// 连接到服务器
HINTERNET hConnect =
WinHttpConnect(hSession, server, INTERNET_DEFAULT_HTTPS_PORT, 0);
if (!hConnect) {
wprintf(L"WinHttpConnect failed with error: %d\n", GetLastError());
WinHttpCloseHandle(hSession);
return NULL;
}
// 创建请求句柄
HINTERNET hRequest =
WinHttpOpenRequest(hConnect, L"GET", path, NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE);
if (!hRequest) {
wprintf(L"WinHttpOpenRequest failed with error: %d\n", GetLastError());
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return NULL;
}
// 发送请求
if (!WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0,
WINHTTP_NO_REQUEST_DATA, 0, 0, 0)) {
wprintf(L"WinHttpSendRequest failed with error: %d\n", GetLastError());
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return NULL;
}
// 接收响应
if (!WinHttpReceiveResponse(hRequest, NULL)) {
wprintf(L"WinHttpReceiveResponse failed with error: %d\n", GetLastError());
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return NULL;
}
// 读取数据
DWORD dwSize = 0, dwDownloaded = 0;
BYTE *responseBuffer = NULL;
DWORD totalSize = 0;
do {
// 查询响应数据块大小
if (!WinHttpQueryDataAvailable(hRequest, &dwSize)) {
wprintf(L"WinHttpQueryDataAvailable failed with error: %d\n",
GetLastError());
break;
}
if (dwSize == 0) break;
// 分配缓冲区
BYTE *tempBuffer = (BYTE *)malloc(dwSize);
if (!tempBuffer) {
wprintf(L"Memory allocation failed\n");
break;
}
// 读取数据
if (!WinHttpReadData(hRequest, tempBuffer, dwSize, &dwDownloaded)) {
wprintf(L"WinHttpReadData failed with error: %d\n", GetLastError());
free(tempBuffer);
break;
}
// 将数据拼接到响应缓冲区
responseBuffer = (BYTE *)realloc(responseBuffer, totalSize + dwDownloaded);
if (!responseBuffer) {
wprintf(L"Memory reallocation failed\n");
free(tempBuffer);
break;
}
memcpy(responseBuffer + totalSize, tempBuffer, dwDownloaded);
totalSize += dwDownloaded;
free(tempBuffer);
} while (dwSize > 0);
// 关闭句柄
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
if (totalSize == 0) {
wprintf(L"No response data received.\n");
free(responseBuffer);
return NULL;
}
// UTF-8 转换为 GBK
int utf8Length = (int)totalSize;
int wideLength = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)responseBuffer,
utf8Length, NULL, 0);
if (wideLength == 0) {
wprintf(L"MultiByteToWideChar failed with error: %d\n", GetLastError());
free(responseBuffer);
return NULL;
}
wchar_t *wideBuffer = (wchar_t *)malloc(wideLength * sizeof(wchar_t));
if (!wideBuffer) {
wprintf(L"Memory allocation failed\n");
free(responseBuffer);
return NULL;
}
MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)responseBuffer, utf8Length,
wideBuffer, wideLength);
int gbkLength = WideCharToMultiByte(CP_ACP, 0, wideBuffer, wideLength, NULL,
0, NULL, NULL);
if (gbkLength == 0) {
wprintf(L"WideCharToMultiByte failed with error: %d\n", GetLastError());
free(responseBuffer);
free(wideBuffer);
return NULL;
}
char *gbkBuffer = (char *)malloc(gbkLength + 1); // +1 for null terminator
if (!gbkBuffer) {
wprintf(L"Memory allocation failed\n");
free(responseBuffer);
free(wideBuffer);
return NULL;
}
WideCharToMultiByte(CP_ACP, 0, wideBuffer, wideLength, gbkBuffer, gbkLength,
NULL, NULL);
// 释放内存
free(responseBuffer);
free(wideBuffer);
// 添加 null terminator
gbkBuffer[gbkLength] = '\0';
// 设置返回大小
*resultSize = gbkLength;
return gbkBuffer;
}
char *url_encode(const char *str) {
if (str == NULL) return NULL;
// 计算编码后的长度
int len = strlen(str);
char *encoded =
(char *)malloc(len * 3 + 1); // 最坏情况:每个字符都需要编码(变成%XX)
if (!encoded) return NULL;
int j = 0;
for (int i = 0; i < len; i++) {
unsigned char c = str[i];
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
encoded[j++] = c;
} else {
sprintf(&encoded[j], "%%%02X", c);
j += 3;
}
}
encoded[j] = '\0';
return encoded;
}