Repository navigation
Expand file tree
/
Copy pathstdio_file.cpp
More file actions
332 lines (266 loc) · 7.12 KB
/
Copy pathstdio_file.cpp
File metadata and controls
332 lines (266 loc) · 7.12 KB
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
#include "stdio_file.h"
#include <errno.h>
#ifdef WIN32
#include <io.h>
//#include <afx.h>
#endif
#include <fcntl.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//__WILLOW_BEGIN_NAMESPACE
////////////////////////////////////////////////////////////////////////////
// StdioFile implementation
//##ModelId=3C8F7CC80028
StdioFile::StdioFile()
{
m_pStream = NULL;
}
//##ModelId=3C8F7CC80032
StdioFile::StdioFile(FILE* pOpenStream) : File(NULL)
{
m_pStream = pOpenStream;
BASE_ASSERT(!m_bCloseOnDelete);
}
//StdioFile::StdioFile(LPCSTR lpszFileName, UINT nOpenFlags)
//##ModelId=3C8F7CC8003C
StdioFile::StdioFile(LPCSTR lpszFileName, LPCSTR lpszOpenFlags)
{
BASE_ASSERT(lpszFileName != NULL);
Open(lpszFileName, lpszOpenFlags);
}
//##ModelId=3C8F7CC8008C
StdioFile::~StdioFile()
{
BASE_ASSERT_VALID(this);
if (m_pStream != NULL && m_bCloseOnDelete)
Close();
}
//int StdioFile::Open(LPCSTR lpszFileName, UINT nOpenFlags,
// FileException* pException)
//##ModelId=3C8F7CC800DC
int StdioFile::Open(LPCSTR lpszFileName, LPCSTR lpszOpenFlags)
{
BASE_ASSERT(lpszFileName != NULL);
m_pStream = NULL;
if (!File::Open(lpszFileName, lpszOpenFlags))
return FALSE;
BASE_ASSERT(m_bCloseOnDelete);
/*
char szMode[4]; // C-runtime open string
int nMode = 0;
// determine read/write mode depending on File mode
if (nOpenFlags & modeCreate)
{
if (nOpenFlags & modeNoTruncate)
szMode[nMode++] = 'a';
else
szMode[nMode++] = 'w';
}
else if (nOpenFlags & modeWrite)
szMode[nMode++] = 'a';
else
szMode[nMode++] = 'r';
// add '+' if necessary (when read/write modes mismatched)
if (szMode[0] == 'r' && (nOpenFlags & modeReadWrite) ||
szMode[0] != 'r' && !(nOpenFlags & modeWrite))
{
// current szMode mismatched, need to add '+' to fix
szMode[nMode++] = '+';
}
// will be inverted if not necessary
int nFlags = _O_RDONLY|_O_TEXT;
if (nOpenFlags & (modeWrite|modeReadWrite))
nFlags ^= _O_RDONLY;
if (nOpenFlags & typeBinary)
szMode[nMode++] = 'b', nFlags ^= _O_TEXT;
else
szMode[nMode++] = 't';
szMode[nMode++] = '\0';
*/
/*
// open a C-runtime low-level file handle
int nHandle = _open_osfhandle(m_hFile, nFlags);
// open a C-runtime stream from that handle
if (nHandle != -1)
m_pStream = _fdopen(nHandle, szMode);
*/
if (m_pStream == NULL)
{
File::Abort(); // close m_hFile
return FALSE;
}
return TRUE;
}
//##ModelId=3C8F7CC800F0
long StdioFile::Read(void* lpBuf, unsigned int nCount)
{
BASE_ASSERT_VALID(this);
BASE_ASSERT(m_pStream != NULL);
if (nCount == 0)
return 0; // avoid Win32 "null-read"
BASE_ASSERT(lpBuf!=NULL);
unsigned int nRead = 0;
if ((nRead = fread(lpBuf, sizeof(BYTE), nCount, m_pStream)) == 0 && !feof(m_pStream))
return(-1);
if (ferror(m_pStream))
{
clearerr(m_pStream);
return(-1);
}
return nRead;
}
//##ModelId=3C8F7CC80104
int StdioFile::Write(const void* lpBuf, unsigned int nCount)
{
BASE_ASSERT_VALID(this);
BASE_ASSERT(m_pStream != NULL);
BASE_ASSERT(lpBuf!=NULL);
if (fwrite(lpBuf, sizeof(BYTE), nCount, m_pStream) != nCount)
return(FALSE);
return(TRUE);
}
//##ModelId=3C8F7CC80050
int StdioFile::WriteString(LPCSTR lpsz)
{
BASE_ASSERT(lpsz != NULL);
BASE_ASSERT(m_pStream != NULL);
if (fputs(lpsz, m_pStream) == EOF)
return(FALSE);
return(TRUE);
}
int StdioFile::WriteF(const char *fmt,...)
{
va_list ap;
va_start(ap, fmt);
int iRet = vfprintf(m_pStream, fmt, ap);
va_end(ap);
return iRet >= 0 ;
}
//##ModelId=3C8F7CC80064
LPSTR StdioFile::ReadString(LPSTR lpsz, unsigned int nMax)
{
BASE_ASSERT(lpsz != NULL);
BASE_ASSERT(m_pStream != NULL);
LPSTR lpszResult = fgets(lpsz, nMax, m_pStream);
if (lpszResult == NULL && !feof(m_pStream))
{
clearerr(m_pStream);
return(NULL);
}
return lpszResult;
}
//##ModelId=3C8F7CC80079
int StdioFile::ReadString(string& rString)
{
BASE_ASSERT_VALID(this);
rString.clear(); // = &stEmptyNull; // empty string without deallocating
const int nMaxSize = 128;
char szBuf[nMaxSize+2] ;
memset(szBuf,0,nMaxSize+2) ;
//LPSTR lpsz = rString.GetBuffer(nMaxSize);
LPSTR lpszResult;
int nLen = 0;
for (;;)
{
lpszResult = fgets(szBuf, nMaxSize+1, m_pStream);
rString += szBuf ;
// handle error/eof case
if (lpszResult == NULL && !feof(m_pStream))
{
clearerr(m_pStream);
return(FALSE);
}
// if string is read completely or EOF
if (lpszResult == NULL ||
(nLen = strlen(szBuf)) < nMaxSize ||
szBuf[nLen-1] == '\n')
break;
//nLen = rString.GetLength();
//lpsz = rString.GetBuffer(nMaxSize + nLen) + nLen;
memset(szBuf,0,nMaxSize+2) ;
}
// remove '\n' from end of string if present
//lpsz = rString.GetBuffer(0);
//nLen = rString.GetLength();
//if (nLen != 0 && lpsz[nLen-1] == '\n')
// rString.GetBufferSetLength(nLen-1);
return lpszResult != NULL;
}
//##ModelId=3C8F7CC80119
long StdioFile::Seek(long lOff, unsigned int nFrom)
{
BASE_ASSERT_VALID(this);
BASE_ASSERT(nFrom == begin || nFrom == end || nFrom == current);
BASE_ASSERT(m_pStream != NULL);
if (fseek(m_pStream, lOff, nFrom) != 0)
return(-1);
long pos = ftell(m_pStream);
return pos;
}
//##ModelId=3C8F7CC800C8
long StdioFile::GetPosition() const
{
BASE_ASSERT_VALID(this);
BASE_ASSERT(m_pStream != NULL);
long pos = ftell(m_pStream);
if (pos == -1)
return(-1);
return pos;
}
//##ModelId=3C8F7CC80140
int StdioFile::Flush()
{
BASE_ASSERT_VALID(this);
if (m_pStream != NULL && fflush(m_pStream) != 0)
return(FALSE);
return(TRUE);
}
//##ModelId=3C8F7CC8014A
int StdioFile::Close()
{
BASE_ASSERT_VALID(this);
//BASE_ASSERT(m_pStream != NULL);
int nErr = 0;
if (m_pStream != NULL)
nErr = fclose(m_pStream);
m_bCloseOnDelete = FALSE;
m_pStream = NULL;
if (nErr != 0)
return(FALSE);
return(TRUE);
}
//##ModelId=3C8F7CC80136
void StdioFile::Abort()
{
BASE_ASSERT_VALID(this);
if (m_pStream != NULL && m_bCloseOnDelete)
fclose(m_pStream); // close but ignore errors
m_pStream = NULL;
m_bCloseOnDelete = FALSE;
}
//##ModelId=3C8F7CC80155
File* StdioFile::Duplicate() const
{
BASE_ASSERT_VALID(this);
BASE_ASSERT(m_pStream != NULL);
return NULL;
}
//##ModelId=3C8F7CC8015F
int StdioFile::LockRange(unsigned long /* dwPos */, unsigned long /* dwCount */)
{
BASE_ASSERT_VALID(this);
BASE_ASSERT(m_pStream != NULL);
return(FALSE);
}
//##ModelId=3C8F7CC80173
int StdioFile::UnlockRange(unsigned long /* dwPos */, unsigned long /* dwCount */)
{
BASE_ASSERT_VALID(this);
BASE_ASSERT(m_pStream != NULL);
return(FALSE);
}
////////////////////////////////////////////////////////////////////////////
//__WILLOW_END_NAMESPACE