-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile.cpp
67 lines (54 loc) · 1.82 KB
/
file.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
#include "file.h"
#include <cstring>
#include <cwchar>
#pragma warning(disable: 6262) // stack bytes
file::file (const wchar_t * filename, UINT cp)
: h (CreateFile (filename, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL))
, cp (cp) {
if (this->h != INVALID_HANDLE_VALUE) {
switch (cp) {
case CP_UTF8:
case CP_UTF16:
this->write (L"\xFEFF", 1);
}
}
}
file::~file () {
if (this->h != INVALID_HANDLE_VALUE) {
CloseHandle (this->h);
}
}
bool file::created () const {
return this->h != INVALID_HANDLE_VALUE;
}
bool file::write (const wchar_t * string) {
if (this->h == INVALID_HANDLE_VALUE)
return false;
DWORD written;
if (this->cp != CP_UTF16) {
char buffer [65536];
if (auto length = WideCharToMultiByte (this->cp, 0, string, -1, buffer, sizeof buffer, NULL, NULL)) {
return WriteFile (this->h, buffer, length - 1, &written, NULL);
} else
return false;
} else
return WriteFile (this->h, string, (DWORD) (sizeof (wchar_t) * std::wcslen (string)), &written, NULL);
}
bool file::write (const wchar_t * string, std::size_t size) {
if (this->h == INVALID_HANDLE_VALUE)
return false;
DWORD written;
if (this->cp != CP_UTF16) {
static char buffer [65536];
if (auto length = WideCharToMultiByte (this->cp, 0, string, (DWORD) size, buffer, sizeof buffer, NULL, NULL)) {
return WriteFile (this->h, buffer, length, &written, NULL);
} else
return false;
} else
return WriteFile (this->h, string, (DWORD) (sizeof (wchar_t) * size), &written, NULL);
}
bool file::writeln (const wchar_t * string) {
return this->write (string)
&& this->write (L"\r\n", 2);
}