6
// Load a binary file to a dynamic array.
8
bool LoadFileToArray(std::vector<BYTE>& Result, const TCHAR* Filename, NFileManager& FileManager )
10
NSerializer* Reader = FileManager.CreateFileReader( Filename );
14
if(Reader->GetFileSize() < 0)
20
Result.resize(Reader->GetFileSize());
21
Reader->Serialize(&Result[0], Result.size());
22
bool Success = Reader->Close();
28
Load a text file the a ANSICHAR array. The Array is null terminated. Useful for loading shader files.
30
bool LoadTextFileToAnsiArray( std::vector<ANSICHAR>& Result, const TCHAR* Filename, NFileManager& FileManager )
33
NSerializer* Reader = FileManager.CreateFileReader( Filename );
37
int Size = Reader->GetFileSize();
45
std::vector<BYTE> ByteArray;
47
ByteArray.resize(Size);
48
Reader->Serialize(&ByteArray[0], Result.size() );
49
bool Success = Reader->Close();
52
if( Size>=2 && !(Size&1) && Memcmp(&Result[0], &INL_UTF16_LE[1], INL_UTF16_LE[0])==0) // (BYTE)ByteArray[0]==0xff && (BYTE)ByteArray[1]==0xfe )
54
// UTF16 - Little Endian
55
int numElement = Size/sizeof(UNICHAR) + 1; // +1 for null char
56
Result.resize(numElement);
57
for( int i=0; i<numElement-1; i++ )
58
Result[i] = ConvertUnicodeCharToTCHAR( (WORD)(ANSIUCHAR)ByteArray[i*2+2] + (WORD)(ANSIUCHAR)ByteArray[i*2+3]*256 );
59
Result[numElement] = 0;
61
else if( Size>=2 && !(Size&1) && Memcmp(&Result[0], &INL_UTF16_LE[1], INL_UTF16_LE[0])==0)
63
// UTF16 - Big Endian.
64
int numElement = Size/sizeof(TCHAR) + 1; // +1 for null char
65
Result.resize(numElement);
66
for( int i=0; i<numElement-1; i++ )
67
Result[i] = ConvertUnicodeCharToTCHAR( (WORD)(ANSIUCHAR)ByteArray[i*2+3] + (WORD)(ANSIUCHAR)ByteArray[i*2+2]*256 );
68
Result[numElement] = 0;
74
Result.resize(Size + 1); // +1 for null char
75
for(int i = 0; i < Size; i++)
76
Result[i] = ByteArray[i];
84
Load a text file in an ANSICHAR array. The Array is null terminated.
86
bool LoadTextFileToUnicodeArray( std::vector<UNICHAR>& Result, const TCHAR* Filename, NFileManager& FileManager )
89
NSerializer* Reader = FileManager.CreateFileReader( Filename );
93
int Size = Reader->GetFileSize();
101
std::vector<BYTE> ByteArray;
103
ByteArray.resize(Size);
104
Reader->Serialize( &ByteArray[0], Result.size() );
105
bool Success = Reader->Close();
108
if( Size>=2 && !(Size&1) && Memcmp(&Result[0], &INL_UTF16_LE[1], INL_UTF16_LE[0])==0) // (BYTE)ByteArray[0]==0xff && (BYTE)ByteArray[1]==0xfe )
110
// UTF16 - Little Endian
111
int numElement = Size + 1; // +1 for null char
112
Result.resize(numElement);
113
for( int i=0; i<numElement-1; i++ )
114
Result[i] = ( (WORD)(ANSIUCHAR)ByteArray[i*2+2] + (WORD)(ANSIUCHAR)ByteArray[i*2+3]*256 );
115
Result[numElement] = 0;
117
else if( Size>=2 && !(Size&1) && Memcmp(&Result[0], &INL_UTF16_LE[1], INL_UTF16_LE[0])==0)
119
// UTF16 - Big Endian.
120
int numElement = Size + 1; // +1 for null char
121
Result.resize(numElement);
122
for( int i=0; i<numElement-1; i++ )
123
Result[i] = ConvertUnicodeCharToTCHAR( (WORD)(ANSIUCHAR)ByteArray[i*2+3] + (WORD)(ANSIUCHAR)ByteArray[i*2+2]*256 );
124
Result[numElement] = 0;
128
// There is no way to detect that a file really contains ascii. Or is there?
129
// Make sure this file is really ascii.
131
However as an additional check to
132
the heuristic of looking for unprintable characters, another trick is to
133
check if the newline string is consistent. It should always be either "\n"
134
(for UNIX-like systems), "\r" (for Mac-like systems) or "\r\n" (for
135
Windows-like systems). If the file starts switching around between these, it
136
probably isn't a valid ASCII file on any of the above three platforms.
140
for( int i=0; i<Size; i++ )
142
if(Result[i] == 0 && (i!=Size-1))
147
if((Result[i] < 0x20 || Result[i]>=0xFF) && ((Result[i]!=0x0A/*New Line, Line feed*/) && (Result[i]!=0x0D/*Carriage return*/)))
152
// http://www.websiterepairguy.com/articles/os/crlf.html
154
The carriage return is often referred to by the capital letters CR.
155
On a Macintosh, every line has a CR at the end.
157
Under Linux (a variant of Unix), the end of a line is indicated by
158
a line feed. Every line ends with a line feed or LF.
160
Calling the end of a line an LF versus a CR is not just semantics.
161
These are 2 very real characters with 2 very real and very separate
162
numeric representations on a computer. A CR is a 13 in the ASCII table
163
of characters and an LF is a 10 in the ASCII table of characters.
165
Contributing to the confusion is that fact that Microsoft Windows does
166
things yet another way. Under Microsoft Windows, lines end with a combination
167
of 2 characters -- a CR followed by a LF. Symbolically, this is represented
168
as CRLF or carriage return, line feed.
170
// Todo. Check if the file mixes a combination of \n (Linux) \r (Mac) and \r\n (Windows).
171
// If it does, the file is not ASCII.
177
Result.resize(Size+1);
178
for( int i=0; i<Size; i++ )
179
Result[i] = ConvertAnsiCharToUnicodeChar(ByteArray[i]);
188
Load a text file to an NString. The file maybe ANSI or Unicode.
189
The resulting string is TCHAR.
191
bool LoadFileToString( NString& Result, const TCHAR* Filename, NFileManager& FileManager )
193
NSerializer* Reader = FileManager.CreateFileReader(Filename);
196
nuxDebugMsg(TEXT("[LoadFileToString] Cannot read from file: %s"), Filename);
199
t_u32 Size = Reader->GetFileSize();
200
std::vector<ANSICHAR> ByteArray(Size+2);
201
Reader->Serialize(&ByteArray[0], Size);
202
bool Success = Reader->Close();
206
std::vector<TCHAR> ResultArray;
208
// Detect Unicode Byte Order Mark
210
// FF FE UTF-16, little endian
211
// FE FF UTF-16, big endian
212
// FF FE 00 00 UTF-32, little endian
213
// 00 00 FE FF UTF-32, big-endian
214
// Note: Microsoft uses UTF-16, little endian byte order.
216
// Little Endian UTF-16: size should be >=2, even, and the first two bytes should be 0xFF followed by 0xFE
217
if((Size>=2) && !(Size&1) && ((BYTE)ByteArray[0] == 0xff) && ((BYTE)ByteArray[1] == 0xfe))
219
// UTF16 - Little Endian
220
int numElement = Size/sizeof(TCHAR);
222
ResultArray.resize(numElement);
223
for( int i=0; i<numElement-1; i++ )
224
ResultArray[i] = ConvertUnicodeCharToTCHAR( (WORD)(ANSIUCHAR)ByteArray[i*2+2] + (WORD)(ANSIUCHAR)ByteArray[i*2+3]*256 );
225
ResultArray[numElement] = 0;
227
else if((Size >= 2) && !(Size & 1) && ((BYTE)ByteArray[0] == 0xfe) && ((BYTE)ByteArray[1] == 0xff))
229
// UTF16 - Big Endian.
230
int numElement = Size/sizeof(TCHAR);
232
ResultArray.resize(numElement);
233
for(int i = 0; i < numElement - 1; i++)
234
ResultArray[i] = ConvertUnicodeCharToTCHAR( (WORD)(ANSIUCHAR)ByteArray[i*2+3] + (WORD)(ANSIUCHAR)ByteArray[i*2+2]*256 );
235
ResultArray[numElement] = 0;
241
ResultArray.resize(Size + 1);
242
for(t_u32 i = 0; i < Size; i++)
243
ResultArray[i] = ConvertAnsiCharToTCHAR(ByteArray[i]);
244
ResultArray[Size] = 0;
246
Result = &ResultArray[0];
251
Save BYTE array to file.
253
bool SaveArrayToFile( const std::vector<BYTE>& Array, const TCHAR* Filename, NFileManager& FileManager )
255
NSerializer* Ar = FileManager.CreateFileWriter( Filename );
258
Ar->Serialize( const_cast<BYTE*>(&Array[0]), Array.size() );
264
Save string to File. Attempt to write it as ASCII if possible. If not write as UTF16-BE.
266
bool SaveStringToFile( const NString& String, const TCHAR* Filename, NFileManager& FileManager )
268
if( !String.Length())
270
NSerializer* Ar = FileManager.CreateFileWriter( Filename );
273
bool SaveAsUnicode = false, Success = true;
275
for( int i = 0; i < String.Length(); i++ )
277
// Test if the UNICODE 0xABCD is the same as the ASCII 0x00CB.
278
if( (*String)[i] != (TCHAR)(ANSIUCHAR)ConvertTCHARToAnsiChar((*String)[i]))
280
//The string need to be written in ASCII. We write the string as UTF16-BigEndian
281
Ar->Serialize(INL_CONST_CAST(BYTE*, &UTF16_BE[1]), UTF16_BE[0] /*size*/);
282
SaveAsUnicode = true;
287
if( SaveAsUnicode || (sizeof(TCHAR) == 1) )
289
t_u32 s = (t_u32)String.Length()*sizeof(TCHAR);
290
Ar->Serialize( INL_CONST_CAST(TCHAR*, String.GetTCharPtr()), (t_u32)s);
294
t_u32 s = (t_u32)String.Length();
295
std::vector<ANSICHAR> AnsiBuffer((t_u32)s);
296
// Cast all character down from UTF16 to ANSI
297
for(t_u32 i = 0; i < (t_u32) String.Length(); i++ )
298
AnsiBuffer[i] = ConvertTCHARToAnsiChar((t_u32)String[i]);
300
s = (t_u32)String.Length();
301
Ar->Serialize( INL_CONST_CAST(ANSICHAR*, &AnsiBuffer[0]), s);
305
GFileManager.Delete( Filename );