-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleApplication6.cpp
460 lines (366 loc) · 9.6 KB
/
ConsoleApplication6.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// ConsoleApplication6.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "ConsoleApplication6.h"
#include "stdint.h"
#include "Strsafe.h"
#include "OleAuto.h"
#include <atlstr.h>
///Unicode and ANSI names StringCbVPrintfW (Unicode) and StringCbVPrintfA (ANSI)
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
**/
void print(_In_ const char* fmt, _In_ ...)
{
char log_buffer[2048];
va_list args;
va_start(args,fmt);
HRESULT hRes = StringCbVPrintfA(log_buffer, sizeof(log_buffer), fmt, args);
if (S_OK != hRes)
{
fprintf(
stderr,
"%s, StringCbVPrintfA() failed. res = 0x%08x",
__FUNCTION__,
hRes
);
return;
}
OutputDebugStringA(log_buffer);
fprintf(stdout, "%s \n", log_buffer);
}
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
**/
CStringW UTF8toUTF16(const CStringA& utf8)
{
CStringW utf16;
int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
if (len>1)
{
wchar_t *ptr = utf16.GetBuffer(len-1);
if (ptr) MultiByteToWideChar(CP_UTF8, 0, utf8, -1, ptr, len);
utf16.ReleaseBuffer();
}
return utf16;
}
bool AsciiToUnicode(const char * szAscii, wchar_t * szUnicode)
{
int len, i;
if((szUnicode == NULL) || (szAscii == NULL))
return false;
len = strlen(szAscii);
for(i=0;i<len+1;i++)
*szUnicode++ = static_cast<wchar_t>(*szAscii++);
return true;
}
bool UnicodeToAscii(const wchar_t * szUnicode, char * szAscii)
{
int len, i;
if((szUnicode == NULL) || (szAscii == NULL))
return false;
len = wcslen(szUnicode);
for(i=0;i<len+1;i++)
*szAscii++ = static_cast<char>(*szUnicode++);
return true;
}
int UnicodeToUtf8(TCHAR* pUnicode, char** pUtf8)
{
int len = ::WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)pUnicode, -1, NULL, 0, NULL, NULL);
*pUtf8 = new char[len];
::WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)pUnicode, -1, *pUtf8, len, NULL, NULL);
return len;
}
char* ANSIToUTF8(const char * pszCode)
{
int nLength, nLength2;
BSTR bstrCode;
char* pszUTFCode = NULL;
nLength = MultiByteToWideChar(CP_ACP, 0, pszCode, lstrlen((LPCWSTR)pszCode), NULL, NULL);
bstrCode = SysAllocStringLen(NULL, nLength);
MultiByteToWideChar(CP_ACP, 0, pszCode, lstrlen((LPCWSTR)pszCode), bstrCode, nLength);
nLength2 = WideCharToMultiByte(CP_UTF8, 0, bstrCode, -1, pszUTFCode, 0, NULL, NULL);
pszUTFCode = (char*)malloc(nLength2+1);
WideCharToMultiByte(CP_UTF8, 0, bstrCode, -1, pszUTFCode, nLength2, NULL, NULL);
return pszUTFCode;
}
char* UTF8ToANSI(const char *pszCode)
{
BSTR bstrWide;
char* pszAnsi;
int nLength;
nLength = MultiByteToWideChar(CP_UTF8, 0, pszCode, lstrlen((LPCWSTR)pszCode) + 1, NULL, NULL);
bstrWide = SysAllocStringLen(NULL, nLength);
MultiByteToWideChar(CP_UTF8, 0, pszCode, lstrlen((LPCWSTR)pszCode) + 1, bstrWide, nLength);
nLength = WideCharToMultiByte(CP_ACP, 0, bstrWide, -1, NULL, 0, NULL, NULL);
pszAnsi = new char[nLength];
WideCharToMultiByte(CP_ACP, 0, bstrWide, -1, pszAnsi, nLength, NULL, NULL);
SysFreeString(bstrWide);
return pszAnsi;
}
int is_file_existsW(TCHAR * file)
{
WIN32_FIND_DATA FindFileData;
HANDLE handle = FindFirstFile(file, &FindFileData) ;
int found = handle != INVALID_HANDLE_VALUE;
if(found)
{
//FindClose(&handle); this will crash
FindClose(handle);
}
return found;
}
bool create_bob_txt()
{
// current directory 를 구한다.
wchar_t *buf=NULL;
uint32_t buflen = 0;
buflen = GetCurrentDirectoryW(buflen, buf);
if (0 == buflen)
{
print("err, GetCurrentDirectoryW() failed. gle = 0x%08x", GetLastError());
return false;
}
buf = (PWSTR) malloc(sizeof(WCHAR) * buflen);
if (0 == GetCurrentDirectoryW(buflen, buf))
{
print("err, GetCurrentDirectoryW() failed. gle = 0x%08x", GetLastError());
free(buf);
return false;
}
// current dir \\ bob.txt 파일명 생성
wchar_t file_name[260];
if (!SUCCEEDED(StringCbPrintfW(
file_name,
sizeof(file_name),
L"%ws\\bob.txt",
buf)))
{
print("err, can not create file name");
free(buf);
return false;
}
if (true == is_file_existsW(file_name))
{
::DeleteFileW(file_name);
}
// 파일 생성
HANDLE file_handle = CreateFileW(
file_name,
GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(file_handle == INVALID_HANDLE_VALUE)
{
print("err, CreateFile(path=%ws), gle=0x%08x", file_name, GetLastError());
return false;
}
// 파일에 데이터 쓰기
DWORD bytes_written = 0;
wchar_t string_buf[1024];
if (!SUCCEEDED(StringCbPrintfW(
string_buf,
sizeof(string_buf),
L"동해물과 백두산이 마르고 닳도록 하느님이 보우하사 우리나라만세")))
{
print("err, can not create data to write.");
CloseHandle(file_handle);
return false;
}
char *pUtf8 =NULL;
int len = UnicodeToUtf8(string_buf,&pUtf8);
if (!WriteFile(file_handle, pUtf8, len, &bytes_written, NULL))
{
print("err, WriteFile() failed. gle = 0x%08x", GetLastError());
CloseHandle(file_handle);
return false;
}
// 영어로 쓰기
if (!SUCCEEDED(StringCbPrintfW(
string_buf,
sizeof(string_buf),
L"All work and no play makes jack a dull boy.")))
{
print("err, can not create data to write.");
CloseHandle(file_handle);
return false;
}
free(pUtf8);
*pUtf8 =NULL;
len = UnicodeToUtf8(string_buf,&pUtf8);
if (!WriteFile(file_handle, string_buf, wcslen(string_buf), &bytes_written, NULL))
{
print("err, WriteFile() failed. gle = 0x%08x", GetLastError());
CloseHandle(file_handle);
return false;
}
char string_bufa[1024];
if (!SUCCEEDED(StringCbPrintfA(
string_bufa,
sizeof(string_bufa),
"동해물과 백두산이 마르고 닳도록 하느님이 보우하사 우리나라만세")))
{
print("err, can not create data to write.");
CloseHandle(file_handle);
return false;
}
free(pUtf8);
pUtf8 =NULL;
pUtf8 =ANSIToUTF8(string_bufa);
len = strlen(pUtf8);
if (!WriteFile(file_handle, pUtf8, len, &bytes_written, NULL))
{
print("err, WriteFile() failed. gle = 0x%08x", GetLastError());
CloseHandle(file_handle);
return false;
}
// 영어로 쓰기
if (!SUCCEEDED(StringCbPrintfA(
string_bufa,
sizeof(string_bufa),
"All work and no play makes jack a dull boy.")))
{
print("err, can not create data to write.");
CloseHandle(file_handle);
return false;
}
free(pUtf8);
pUtf8 =NULL;
pUtf8 =ANSIToUTF8(string_bufa);
len = strlen(pUtf8);
if (!WriteFile(file_handle, string_bufa, strlen(string_bufa), &bytes_written, NULL))
{
print("err, WriteFile() failed. gle = 0x%08x", GetLastError());
CloseHandle(file_handle);
return false;
}
free(pUtf8);
// 파일 닫기
CloseHandle(file_handle);
wchar_t copy_file_name[260];
if (!SUCCEEDED(StringCbPrintfW(
copy_file_name,
sizeof(copy_file_name),
L"%ws\\bob2.txt",
buf)))
{
print("err, can not copy file name");
free(buf);
return false;
}
free(buf); buf = NULL;
/* BOB.txt -> BOB2.txt 파일 복사 */
if(!CopyFileW(file_name,copy_file_name,false)){
print("CopyFileW fail");
return false;
}
/* ReadFile()을 통한 읽기 후 출력*/
HANDLE read_file_handle = CreateFileW(
copy_file_name,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
0,
NULL);
if(read_file_handle == INVALID_HANDLE_VALUE)
{
print("err, CreateFile(path=%ws), gle=0x%08x", read_file_handle, GetLastError());
return false;
}
char read_buf[512];
char buf_noNull[512];
char* ansi_buf;
DWORD result=0;
ReadFile(read_file_handle,read_buf,sizeof(char)*512,&result,NULL);
// read_buf의 글자 사이사이에 있는 NULL값 제거 ( UTF8ToANSI 에서 strlen을 사용하기 때문)
int j=0;
for(int i=0;i<256;i++){
if(read_buf[i] != '\0'){
buf_noNull[j++] = read_buf[i];
}
}
buf_noNull[j]='\0';
// 출력
ansi_buf=UTF8ToANSI(buf_noNull);
printf("%s\n",ansi_buf);
CloseHandle(read_file_handle);
/* MMIO()을 통한 읽기 후 출력*/
read_file_handle = CreateFileW(
copy_file_name,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
0,
NULL);
if(read_file_handle == INVALID_HANDLE_VALUE)
{
print("err, CreateFile(path=%ws), gle=0x%08x", read_file_handle, GetLastError());
return false;
}
HANDLE file_map = CreateFileMapping(
read_file_handle,
NULL,
PAGE_READONLY,
0,
0,
NULL
);
if (NULL == file_map)
{
print("err, CreateFileMapping(%ws) failed, gle = %u", file_name, GetLastError());
CloseHandle(read_file_handle);
return false;
}
PCHAR file_view = (PCHAR) MapViewOfFile(
file_map,
FILE_MAP_READ,
0,
0,
0
);
if(file_view == NULL)
{
print("err, MapViewOfFile(%ws) failed, gle = %u", file_name, GetLastError());
CloseHandle(file_map);
CloseHandle(read_file_handle);
return false;
}
// read_buf의 글자 사이사이에 있는 NULL값 제거 ( UTF8ToANSI 에서 strlen을 사용하기 때문)
j=0;
for(int i=0;i<256;i++){
if(read_buf[i] != '\0'){
buf_noNull[j++] = file_view[i];
}
}
buf_noNull[j]='\0';
// 출력
ansi_buf=UTF8ToANSI(buf_noNull);
printf("%s\n",ansi_buf);
UnmapViewOfFile(file_view);
CloseHandle(file_map);
CloseHandle(read_file_handle);
/* BOB.txt 파일 삭제*/
::DeleteFileW(file_name);
return true;
}
int main(int argc, char **argv) {
create_bob_txt();
return 0;
}