Polygon Crucher SDK - Documentation
Documentation
Loading...
Searching...
No Matches
IoFile.h
Go to the documentation of this file.
1//! @file IoFile.h
2//! @brief CXStringT is a template class for handling unicode, utf8 or ansi strings
3//!
4////////////////////////////////////////////////////////////////////////////////////////
5#ifndef IOFILE_H
6#define IOFILE_H
7
8#include "StdioFileEx.h"
9#include "FileInfo.h"
10
11BEGIN_MOOTOOLS_NAMESPACE
12
13//! @enum IOFILE_FLAGS
14//! Specify the way an IoFile is opened
15typedef enum IOFILE_FLAGS
16{
17 IOFILE_NONE = 0x00,
18 IOFILE_SILENT_MODE = 0x01,
19 IOFILE_MEMORY_FILE = 0x02,
20 IOFILE_ALLOW_EXCEPTIONS = 0x04,
21 IOFILE_TEXT_MODE = 0x08, //!< When reading, this will also automatically process file as unicode file if a BOM is present. When writing, the file is output as non unicode file
22 IOFILE_UNICODE_TEXT_MODE = 0x10, //!< When reading this acts as IOFILE_TEXT_MODE. When writing must be specified to write in Unicode mode
23 IOFILE_UTF8_TEXT_MODE = 0x20, //!< Combine with IOFILE_TEXT_MODE to force UTF8. Windows only.
24
25 // Internal flags
26 IOFILE_IS_READING = 0x1000, // Flag automatically set after OpenFile call (load = true or false)
28
29DLL_TOOLSFUNCTION void ReverseBuffer(void *, int);
30
31/*
32 * Macros
33 */
34#define END_OF_FILE CXFile::end
35#define BEGIN_OF_FILE CXFile::begin
36#define CURRENT_POS_OF_FILE CXFile::current
37
38class CXWaitCursor;
39//! @class IoFile
40//! @brief IoFile is a class for reading binary or ascii file using internally a CStdioFileEx.
41//! @details IoFile can also write data to a memory block using internally a CCustomMemFile.
42//!
43//! IoFile can be used to as a file finder using internally a CXFileFind\n
44class DLL_TOOLSFUNCTION IoFile
45{
46 private:
47 unsigned int flags;
48
49 CStdioFileEx *pFile; // Could be ever been open in text or binary mode
50 CCustomMemFile *pMemFile;
51 CXFile *pCurrentFile;
52 CXString filename;
53
54 // File Finder
55 CXFileFind *fileFinder;
56 bool fileFindContinue;
57 CXString extension;
58
59 // UI
60 CXWaitCursor *wait;
61
62 public:
63 IoFile();
64 IoFile(unsigned int ioFileFlags);
65 IoFile(bool silentMode, unsigned int ioFileFlags);
66 void Init(unsigned int ioFileFlags);
67 virtual ~IoFile();
68 void DeleteFiles();
69 bool GetErrorMessage(CXString& error, CXException* e = NULL);
70 void ReportError(CXException *e = NULL);
71 FILE *GetFileStream(void);
72 CXFile *GetCFile(void) { return pCurrentFile; }
73
74 CStdioFileEx *GetTextFile(void)
75 {
76 return pFile;
77 }
78
79 CXString GetFileName(void) { return filename; }
80 CXString GetFilePath();
81 CXString GetName();
82
83 bool OpenFile(const CXString& name, bool load, CXFileException* pException = NULL);
84 void CloseFile(bool abort = false);
85 void CloseAndDeleteFile(bool abort = false);
86
87 bool FindFileOpen(const CXString& name, const CXString& extension = CXString(), FILENAME_TYPE qualifyType = FILENAME_UNKNOWN);
88 bool FindFileNext(CXString&, bool& matchExtension, bool noPath = false);
89 void FindFileClose();
90 CXFileFind *GetFileFinder() {return fileFinder; }
91
92 inline bool IsWriting() const
93 {
94 return !(flags & IOFILE_IS_READING);
95 }
96
97 inline bool IsBinaryMode() const
98 {
99 return !(flags & IOFILE_TEXT_MODE);
100 }
101
102 unsigned int FileBufferRead(void *ptr, unsigned int sz) // Read atmost sz byte. return 0 means eof
103 {
104 sz = pCurrentFile->Read(ptr, sz);
105
106 if ((flags & IOFILE_ALLOW_EXCEPTIONS) && sz == 0)
107 XThrowExceptionWithArgs(CXFileException, CXFileException::endOfFile, filename);
108
109 return sz;
110 }
111
112 bool FileRead(void *ptr, unsigned int sz) // Read exactly sz byte or return false
113 {
114 if (pCurrentFile->Read(ptr, sz) == sz)
115 return true;
116
117 if (flags & IOFILE_ALLOW_EXCEPTIONS)
118 XThrowExceptionWithArgs(CXFileException, CXFileException::endOfFile, filename);
119
120 return false;
121 }
122
123 void WriteFloat(float value)
124 {
125 pCurrentFile->Write(&value, sizeof(float));
126 }
127
128 void WriteInt(int value)
129 {
130 pCurrentFile->Write(&value, sizeof(int));
131 }
132
133 //void WriteLong(long value)
134 //{
135 // pCurrentFile->Write(&value, sizeof(long));
136 //}
137
138 //void WriteULong(unsigned long value)
139 //{
140 // pCurrentFile->Write(&value, sizeof(unsigned long));
141 //}
142
143 void WriteUInt(unsigned int value)
144 {
145 pCurrentFile->Write(&value, sizeof(unsigned int));
146 }
147
148 void WriteShort(short value)
149 {
150 pCurrentFile->Write(&value, sizeof(short));
151 }
152
153 void WriteUShort(unsigned short value)
154 {
155 pCurrentFile->Write(&value, sizeof(unsigned short));
156 }
157
158 void WriteChar(char value)
159 {
160 pCurrentFile->Write(&value, sizeof(char));
161 }
162
163 void WriteUChar(unsigned char value)
164 {
165 pCurrentFile->Write(&value, sizeof(unsigned char));
166 }
167
168 void FileWrite(const void *ptr, unsigned int sz)
169 {
170 pCurrentFile->Write(ptr, sz);
171 }
172
173 void FileReverseWrite(void *ptr, unsigned int sz)
174 {
175 unsigned char *buf = xAllocateArray(unsigned char, sz);
176
177 memcpy(buf, ptr, sz);
178 ReverseBuffer(buf, sz);
179
180 pCurrentFile->Write(buf, sz);
181
182 xDeallocateArray(buf);
183 }
184
185 void FileReverseWrite(void *ptr, unsigned int elementNbr, unsigned int elementSize)
186 {
187 unsigned char *buf = xAllocateArray(unsigned char, elementNbr*elementSize);
188
190
191 for (unsigned int i = 0; i<elementNbr; i++)
192 ReverseBuffer((void *)(buf+i*elementSize), elementSize);
193
194 pCurrentFile->Write(buf, elementNbr*elementSize);
195
196 xDeallocateArray(buf);
197 }
198
199 void Flush()
200 {
201 // Flush is needed only for writing operation
202 // Flushing on an opened file erase the buffer: this causes data be missed while reading...
203 if (IsWriting())
204 pCurrentFile->Flush();
205 }
206
207 bool FileSeek(longint offset, CXFile::SeekPosition type)
208 {
209 Flush();
210 pCurrentFile->Seek(offset, type);
211
212 // return an exception if bad...
213 return 1;
214 }
215
216 fileuint FileGetPosition()
217 {
218 Flush();
219 return pCurrentFile->GetPosition();
220 }
221
222 fileuint FileGetLength()
223 {
224 Flush();
225 return pCurrentFile->GetLength();
226 }
227
228 bool FileReverseRead(void *ptr, unsigned int sz)
229 {
230 if (pCurrentFile->Read(ptr, sz) != sz)
231 {
232 if (flags & IOFILE_ALLOW_EXCEPTIONS)
233 XThrowExceptionWithArgs(CXFileException, CXFileException::endOfFile, filename);
234
235 return false;
236 }
237
238 ReverseBuffer(ptr, sz);
239
240 return true;
241 }
242
243 bool FileReverseRead(void *ptr, unsigned int elementNbr, unsigned int elementSize)
244 {
245 if (pCurrentFile->Read(ptr, elementNbr*elementSize) != elementNbr*elementSize)
246 {
247 if (flags & IOFILE_ALLOW_EXCEPTIONS)
248 XThrowExceptionWithArgs(CXFileException, CXFileException::endOfFile, filename);
249
250 return false;
251 }
252
253 for (unsigned int i = 0; i<elementNbr; i++)
254 ReverseBuffer((void *)((unsigned char *)ptr+i*elementSize), elementSize);
255
256 return true;
257 }
258
259 bool ReadString(CXString& rString)
260 {
261 XASSERT(!(flags & IOFILE_MEMORY_FILE));
262 if (pFile->ReadString(rString))
263 return true;
264
265 if (flags & IOFILE_ALLOW_EXCEPTIONS)
266 XThrowExceptionWithArgs(CXFileException, CXFileException::endOfFile, filename);
267
268 return false;
269 }
270
271 void WriteString(LPCTSTR wString)
272 {
273 XASSERT(!(flags & IOFILE_MEMORY_FILE));
274 pFile->WriteString(wString);
275 }
276};
277
278END_MOOTOOLS_NAMESPACE
279
280#endif /* IOFILE_H */
Contains many convenient functions for handling files, paths and give a way to add some custom format...
FILENAME_TYPE
Definition FileInfo.h:254
IOFILE_FLAGS
Definition IoFile.h:16
@ IOFILE_TEXT_MODE
When reading, this will also automatically process file as unicode file if a BOM is present....
Definition IoFile.h:21
@ IOFILE_UNICODE_TEXT_MODE
When reading this acts as IOFILE_TEXT_MODE. When writing must be specified to write in Unicode mode.
Definition IoFile.h:22
@ IOFILE_UTF8_TEXT_MODE
Combine with IOFILE_TEXT_MODE to force UTF8. Windows only.
Definition IoFile.h:23
CStdioFileEx is an advanced class for reading / writing text file.
CXTString< TCHAR > CXString
CXString depend on the target OS. Could be CXStringW (Windows) or CXStringA (Linux / Macos)
Definition XString.h:118
The class defines an x, y, z 3D point which can use int, float or double.
Definition 3DPoint.h:27
Definition CustomMemFile.h:15
CStdioFileEx reads or write unicode or ansi / utf8 string and convert them to CXString using string c...
Definition StdioFileEx.h:41
virtual bool ReadString(CXString &rString)
Read a CXString whatever if the file is encoded in unicode or utf8.
virtual void WriteString(LPCTSTR lpsz)
Write a CXString. The string is converted accordingly is the file is opened using modeWriteUnicode.
Definition XException.h:27
Definition XFileUtils.h:17
CXFile is the base class for CXStdioFile, CXStdioFileEx, CXMemFile.
Definition XFile.h:42
IoFile is a class for reading binary or ascii file using internally a CStdioFileEx.
Definition IoFile.h:45