-
Notifications
You must be signed in to change notification settings - Fork 5
/
LBPUnicodeTextFile.cpp
243 lines (198 loc) · 4.19 KB
/
LBPUnicodeTextFile.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
#include "stdafx.h"
#include "LBPUnicodeTextFile.h"
#include "LBPString.h"
#include "LBP_UTF.h"
CLBPUnicodeTextFile::CLBPUnicodeTextFile(bool bIsUTF8) : CFile()
{
m_bIsUTF8 = bIsUTF8;
}
CLBPUnicodeTextFile::CLBPUnicodeTextFile(LPCTSTR lpszFileName, UINT nOpenFlags, bool bIsUTF8) : CFile(lpszFileName, nOpenFlags)
{
m_bIsUTF8 = bIsUTF8;
WriteOrReadHeader(nOpenFlags);
}
BOOL CLBPUnicodeTextFile::Open(LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError)
{
BOOL bRet = FALSE;
int iAttemptsCounter = 0;
srand((unsigned)time(NULL));
while (!bRet && (iAttemptsCounter < 100))
{
//#if _MFC_VER >= 0x0800
// bRet = Open2(lpszFileName, nOpenFlags, pError);
//#else
bRet = CFile::Open(lpszFileName, nOpenFlags, pError);
//#endif
if (!bRet)
{
iAttemptsCounter++;
// Calculate random sleep time (approx 100ms).
double dRandom = rand();
dRandom /= RAND_MAX;
dRandom -= 0.5;
dRandom *= 10;
Sleep((ULONG)(100+dRandom));
}
}
if (bRet)
{
bRet = WriteOrReadHeader(nOpenFlags);
}
return bRet;
}
bool CLBPUnicodeTextFile::WriteOrReadHeader(UINT iFlags)
{
// Skip or write the unicode flags
if (iFlags & modeCreate)
{
UCHAR pBuf[3];
int iBOMSize = 2;
if (m_bIsUTF8)
{
pBuf[0] = (UCHAR)0xEF;
pBuf[1] = (UCHAR)0xBB;
pBuf[2] = (UCHAR)0xBF;
iBOMSize = 3;
}
else
{
pBuf[0] = (UCHAR)0xFF;
pBuf[1] = (UCHAR)0xFE;
}
Write(pBuf, iBOMSize);
return true;
}
return SkipOverBOM(m_bIsUTF8);
}
bool CLBPUnicodeTextFile::SkipOverBOM(bool& bIsUTF8)
{
UCHAR pBuf[3];
if (2 != Read(pBuf, 2))
return false;
if (pBuf[0] == (UCHAR)0xFF)
{
if (pBuf[1] == (UCHAR)0xFE)
{
bIsUTF8 = false;
return true;
}
}
// This might be UTF8
if (pBuf[0] == (UCHAR)0xEF)
{
if (pBuf[1] == (UCHAR)0xBB)
{
if (1 == Read(pBuf + 2, 1))
{
if (pBuf[2] == (UCHAR)0xBF)
{
bIsUTF8 = true;
return true;
}
}
}
}
return false;
}
bool CLBPUnicodeTextFile::ReadEverything(CLBPString& sEverything)
{
const DWORD dwSize = (DWORD)(GetLength() - GetPosition());
const DWORD dwSizeIncTerm = dwSize + 2; // Terminator for UTF8 or UTF16.
BYTE* pBuffer = new BYTE[dwSizeIncTerm];
::ZeroMemory(pBuffer, dwSizeIncTerm);
Read(pBuffer, dwSize);
CLBP_UTF utf;
if (m_bIsUTF8)
{
utf.SetUTF8((const LBP_UTF8_CHAR*)pBuffer);
}
else
{
utf.SetUTF16((const LBP_UTF16_CHAR*)pBuffer);
}
sEverything = utf.String();
delete [] pBuffer;
return !sEverything.IsEmpty();
}
// 15th June 2018 John Croudy, https://mcneel.myjetbrains.com/youtrack/issue/RH-46786
// Length is in Chars. Multiply by size of the char for writing to the file.
bool CLBPUnicodeTextFile::WriteUTF8Line(const CLBPString& sLine)
{
CLBPString sLine2 = sLine;
sLine2 += L"\r\n";
CLBP_UTF utf;
utf.Set(sLine2);
const auto lengthInChars = LBP_UTF_LengthInChars<LBP_UTF8_CHAR>(utf.UTF8());
Write(utf.UTF8(), (UINT)(lengthInChars * sizeof(LBP_UTF8_CHAR)));
return true;
}
bool CLBPUnicodeTextFile::WriteUTF16Line(const CLBPString& sLine)
{
CLBPString sLine2 = sLine;
sLine2 += L"\r\n";
CLBP_UTF utf;
utf.Set(sLine2);
const auto lengthInChars = LBP_UTF_LengthInChars<LBP_UTF16_CHAR>(utf.UTF16());
Write(utf.UTF16(), (UINT)(lengthInChars * sizeof(LBP_UTF16_CHAR)));
return true;
}
bool CLBPUnicodeTextFile::WriteLine(const CLBPString& sLine)
{
if (m_bIsUTF8)
{
return WriteUTF8Line(sLine);
}
else
{
return WriteUTF16Line(sLine);
}
}
#ifdef _MFC42
void CLBPUnicodeTextFile::SetLength(DWORD dwNewLen)
#else
void CLBPUnicodeTextFile::SetLength(ULONGLONG dwNewLen)
#endif
{
const USHORT uBOMLength = m_bIsUTF8 ? 3 : 2;
if (dwNewLen < uBOMLength)
{
// This will destroy the BOM. Clear the file and re-write the BOM.
CFile::SetLength(0);
WriteOrReadHeader(CFile::modeCreate);
}
else
{
CFile::SetLength(dwNewLen);
}
}
CLBPFile::CLBPFile() : CFile()
{
}
CLBPFile::CLBPFile(LPCTSTR lpszFileName, UINT nOpenFlags) : CFile(lpszFileName, nOpenFlags)
{
}
CLBPFile::~CLBPFile()
{
}
char CLBPFile::putc(char c)
{
Write(&c, sizeof(char));
return c;
}
short CLBPFile::putshort(short s)
{
Write(&s, sizeof(short));
return s;
}
char CLBPFile::getc()
{
char c;
Read(&c, sizeof(char));
return c;
}
short CLBPFile::getshort()
{
short s;
Read(&s, sizeof(short));
return s;
}