@@ -107,7 +107,7 @@ static Int s_totalOpen = 0;
107
107
// =================================================================
108
108
109
109
LocalFile::LocalFile ()
110
- #ifdef USE_BUFFERED_IO
110
+ #if USE_BUFFERED_IO
111
111
: m_file(NULL )
112
112
#else
113
113
: m_handle(-1 )
@@ -127,7 +127,7 @@ LocalFile::LocalFile()
127
127
128
128
LocalFile::~LocalFile ()
129
129
{
130
- #ifdef USE_BUFFERED_IO
130
+ #if USE_BUFFERED_IO
131
131
if (m_file)
132
132
{
133
133
fclose (m_file);
@@ -167,61 +167,54 @@ Bool LocalFile::open( const Char *filename, Int access )
167
167
}
168
168
169
169
/* here we translate WSYS file access to the std C equivalent */
170
- #ifdef USE_BUFFERED_IO
171
- char mode[32 ];
172
- char * m = mode;
173
-
174
- if (m_access & APPEND)
170
+ #if USE_BUFFERED_IO
171
+
172
+ // r open for reading (The file must exist)
173
+ // w open for writing (creates file if it doesn't exist). Deletes content and overwrites the file.
174
+ // a open for appending (creates file if it doesn't exist).
175
+ // r+ open for reading and writing (The file must exist).
176
+ // w+ open for reading and writing.
177
+ // If file exists deletes content and overwrites the file, otherwise creates an empty new file.
178
+ // a+ open for reading and writing (append if file exists).
179
+
180
+ const Bool write = (m_access & WRITE) != 0 ;
181
+ const Bool readwrite = (m_access & READWRITE) == READWRITE;
182
+ const Bool append = (m_access & APPEND) != 0 ;
183
+ const Bool create = (m_access & CREATE) != 0 ;
184
+ const Bool truncate = (m_access & TRUNCATE) != 0 ;
185
+ const Bool binary = (m_access & BINARY) != 0 ;
186
+
187
+ const Char *mode = NULL ;
188
+
189
+ // Mode string selection (mimics _open flag combinations)
190
+ // TEXT is implicit for fopen if 'b' is not present
191
+ // READ is implicit here if not READWRITE or WRITE
192
+ if (readwrite)
175
193
{
176
- DEBUG_CRASH ((" not yet supported by buffered mode" ));
177
- }
178
-
179
- if (m_access & TRUNCATE)
180
- {
181
- DEBUG_CRASH ((" not yet supported by buffered mode" ));
182
- }
183
-
184
- if ((m_access & READWRITE ) == READWRITE )
185
- {
186
- if (m_access & CREATE)
187
- {
188
- *m++ = ' w' ;
189
- *m++ = ' +' ;
190
- }
194
+ if (append)
195
+ mode = binary ? " a+b" : " a+" ;
196
+ else if (truncate || create)
197
+ mode = binary ? " w+b" : " w+" ;
191
198
else
192
- {
193
- *m++ = ' r' ;
194
- *m++ = ' +' ;
195
- }
199
+ mode = binary ? " r+b" : " r+" ;
196
200
}
197
- else if (m_access & WRITE)
198
- {
199
- *m++ = ' w' ;
200
- }
201
- else
202
- {
203
- *m++ = ' r' ;
204
- DEBUG_ASSERTCRASH (!(m_access & TRUNCATE), (" cannot truncate with read-only" ));
205
- }
206
-
207
- if (m_access & TEXT)
201
+ else if (write)
208
202
{
209
- *m++ = ' t' ;
203
+ if (append)
204
+ mode = binary ? " ab" : " a" ;
205
+ else
206
+ mode = binary ? " wb" : " w" ;
210
207
}
211
- if (m_access & BINARY)
208
+ else // implicitly read-only
212
209
{
213
- *m++ = ' b ' ;
210
+ mode = binary ? " rb " : " r " ;
214
211
}
215
212
216
- *m++ = 0 ;
217
-
218
213
m_file = fopen (filename, mode);
219
214
if (m_file == NULL )
220
215
{
221
216
goto error;
222
217
}
223
-
224
- // setvbuf(m_file, m_vbuf, _IOFBF, sizeof(BUF_SIZE));
225
218
226
219
#else
227
220
@@ -257,7 +250,7 @@ Bool LocalFile::open( const Char *filename, Int access )
257
250
flags |= _O_WRONLY;
258
251
flags |= _O_CREAT;
259
252
}
260
- else
253
+ else // implicitly read-only
261
254
{
262
255
flags |= _O_RDONLY;
263
256
}
@@ -318,15 +311,15 @@ Int LocalFile::read( void *buffer, Int bytes )
318
311
319
312
if (buffer == NULL )
320
313
{
321
- #ifdef USE_BUFFERED_IO
314
+ #if USE_BUFFERED_IO
322
315
fseek (m_file, bytes, SEEK_CUR);
323
316
#else
324
317
_lseek (m_handle, bytes, SEEK_CUR);
325
318
#endif
326
319
return bytes;
327
320
}
328
321
329
- #ifdef USE_BUFFERED_IO
322
+ #if USE_BUFFERED_IO
330
323
Int ret = fread (buffer, 1 , bytes, m_file);
331
324
#else
332
325
Int ret = _read ( m_handle, buffer, bytes );
@@ -347,7 +340,7 @@ Int LocalFile::write( const void *buffer, Int bytes )
347
340
return -1 ;
348
341
}
349
342
350
- #ifdef USE_BUFFERED_IO
343
+ #if USE_BUFFERED_IO
351
344
Int ret = fwrite (buffer, 1 , bytes, m_file);
352
345
#else
353
346
Int ret = _write ( m_handle, buffer, bytes );
@@ -366,21 +359,21 @@ Int LocalFile::seek( Int pos, seekMode mode)
366
359
switch ( mode )
367
360
{
368
361
case START:
362
+ DEBUG_ASSERTCRASH (pos >= 0 , (" LocalFile::seek - pos must be >= 0 when seeking from the beginning of the file" ));
369
363
lmode = SEEK_SET;
370
364
break ;
371
365
case CURRENT:
372
366
lmode = SEEK_CUR;
373
367
break ;
374
368
case END:
375
- DEBUG_ASSERTCRASH (pos <= 0 , (" LocalFile::seek - pos should be <= 0 for a seek starting at the end of the file" ));
376
369
lmode = SEEK_END;
377
370
break ;
378
371
default :
379
- // bad seek mode
372
+ DEBUG_CRASH (( " LocalFile::seek - bad seek mode" ));
380
373
return -1 ;
381
374
}
382
375
383
- #ifdef USE_BUFFERED_IO
376
+ #if USE_BUFFERED_IO
384
377
Int ret = fseek (m_file, pos, lmode);
385
378
if (ret == 0 )
386
379
return ftell (m_file);
@@ -406,7 +399,7 @@ Bool LocalFile::scanInt(Int &newInt)
406
399
407
400
// skip preceding non-numeric characters
408
401
do {
409
- #ifdef USE_BUFFERED_IO
402
+ #if USE_BUFFERED_IO
410
403
val = fread (&c, 1 , 1 , m_file);
411
404
#else
412
405
val = _read ( m_handle, &c, 1 );
@@ -419,7 +412,7 @@ Bool LocalFile::scanInt(Int &newInt)
419
412
420
413
do {
421
414
tempstr.concat (c);
422
- #ifdef USE_BUFFERED_IO
415
+ #if USE_BUFFERED_IO
423
416
val = fread (&c, 1 , 1 , m_file);
424
417
#else
425
418
val = _read ( m_handle, &c, 1 );
@@ -428,7 +421,7 @@ Bool LocalFile::scanInt(Int &newInt)
428
421
429
422
// put the last read char back, since we didn't use it.
430
423
if (val != 0 ) {
431
- #ifdef USE_BUFFERED_IO
424
+ #if USE_BUFFERED_IO
432
425
fseek (m_file, -1 , SEEK_CUR);
433
426
#else
434
427
_lseek (m_handle, -1 , SEEK_CUR);
@@ -454,7 +447,7 @@ Bool LocalFile::scanReal(Real &newReal)
454
447
455
448
// skip the preceding white space
456
449
do {
457
- #ifdef USE_BUFFERED_IO
450
+ #if USE_BUFFERED_IO
458
451
val = fread (&c, 1 , 1 , m_file);
459
452
#else
460
453
val = _read ( m_handle, &c, 1 );
@@ -470,15 +463,15 @@ Bool LocalFile::scanReal(Real &newReal)
470
463
if (c == ' .' ) {
471
464
sawDec = TRUE ;
472
465
}
473
- #ifdef USE_BUFFERED_IO
466
+ #if USE_BUFFERED_IO
474
467
val = fread (&c, 1 , 1 , m_file);
475
468
#else
476
469
val = _read (m_handle, &c, 1 );
477
470
#endif
478
471
} while ((val != 0 ) && (((c >= ' 0' ) && (c <= ' 9' )) || ((c == ' .' ) && !sawDec)));
479
472
480
473
if (val != 0 ) {
481
- #ifdef USE_BUFFERED_IO
474
+ #if USE_BUFFERED_IO
482
475
fseek (m_file, -1 , SEEK_CUR);
483
476
#else
484
477
_lseek (m_handle, -1 , SEEK_CUR);
@@ -503,7 +496,7 @@ Bool LocalFile::scanString(AsciiString &newString)
503
496
504
497
// skip the preceding whitespace
505
498
do {
506
- #ifdef USE_BUFFERED_IO
499
+ #if USE_BUFFERED_IO
507
500
val = fread (&c, 1 , 1 , m_file);
508
501
#else
509
502
val = _read (m_handle, &c, 1 );
@@ -516,15 +509,15 @@ Bool LocalFile::scanString(AsciiString &newString)
516
509
517
510
do {
518
511
newString.concat (c);
519
- #ifdef USE_BUFFERED_IO
512
+ #if USE_BUFFERED_IO
520
513
val = fread (&c, 1 , 1 , m_file);
521
514
#else
522
515
val = _read (m_handle, &c, 1 );
523
516
#endif
524
517
} while ((val != 0 ) && (!isspace (c)));
525
518
526
519
if (val != 0 ) {
527
- #ifdef USE_BUFFERED_IO
520
+ #if USE_BUFFERED_IO
528
521
fseek (m_file, -1 , SEEK_CUR);
529
522
#else
530
523
_lseek (m_handle, -1 , SEEK_CUR);
@@ -547,13 +540,13 @@ void LocalFile::nextLine(Char *buf, Int bufSize)
547
540
// seek to the next new-line.
548
541
do {
549
542
if ((buf == NULL ) || (i >= (bufSize-1 ))) {
550
- #ifdef USE_BUFFERED_IO
543
+ #if USE_BUFFERED_IO
551
544
val = fread (&c, 1 , 1 , m_file);
552
545
#else
553
546
val = _read (m_handle, &c, 1 );
554
547
#endif
555
548
} else {
556
- #ifdef USE_BUFFERED_IO
549
+ #if USE_BUFFERED_IO
557
550
val = fread (buf + i, 1 , 1 , m_file);
558
551
#else
559
552
val = _read (m_handle, buf + i, 1 );
0 commit comments