src/fileio.cpp
branchcpp_gui
changeset 6298 c30fe89622df
parent 6285 187e3ef04cc9
child 6303 84c215fc8eb8
equal deleted inserted replaced
6297:4bf29d14edba 6298:c30fe89622df
    16 /*************************************************/
    16 /*************************************************/
    17 
    17 
    18 #define FIO_BUFFER_SIZE 512
    18 #define FIO_BUFFER_SIZE 512
    19 #define MAX_HANDLES 64
    19 #define MAX_HANDLES 64
    20 
    20 
    21 typedef struct {
    21 struct Fio {
    22 	byte *buffer, *buffer_end;          ///< position pointer in local buffer and last valid byte of buffer
    22 	byte *buffer, *buffer_end;          ///< position pointer in local buffer and last valid byte of buffer
    23 	uint32 pos;                         ///< current (system) position in file
    23 	uint32 pos;                         ///< current (system) position in file
    24 	FILE *cur_fh;                       ///< current file handle
    24 	FILE *cur_fh;                       ///< current file handle
    25 	FILE *handles[MAX_HANDLES];         ///< array of file handles we can have open
    25 	FILE *handles[MAX_HANDLES];         ///< array of file handles we can have open
    26 	byte buffer_start[FIO_BUFFER_SIZE]; ///< local buffer when read from file
    26 	byte buffer_start[FIO_BUFFER_SIZE]; ///< local buffer when read from file
    27 #if defined(LIMITED_FDS)
    27 #if defined(LIMITED_FDS)
    28 	uint open_handles;                  ///< current amount of open handles
    28 	uint open_handles;                  ///< current amount of open handles
    29 	const char *filename[MAX_HANDLES];  ///< array of filenames we (should) have open
    29 	const char *filename[MAX_HANDLES];  ///< array of filenames we (should) have open
    30 	uint usage_count[MAX_HANDLES];      ///< count how many times this file has been opened
    30 	uint usage_count[MAX_HANDLES];      ///< count how many times this file has been opened
    31 #endif /* LIMITED_FDS */
    31 #endif /* LIMITED_FDS */
    32 } Fio;
    32 };
    33 
    33 
    34 static Fio _fio;
    34 static Fio _fio;
    35 
    35 
    36 /* Get current position in file */
    36 /* Get current position in file */
    37 uint32 FioGetPos(void)
    37 uint32 FioGetPos()
    38 {
    38 {
    39 	return _fio.pos + (_fio.buffer - _fio.buffer_start) - FIO_BUFFER_SIZE;
    39 	return _fio.pos + (_fio.buffer - _fio.buffer_start) - FIO_BUFFER_SIZE;
    40 }
    40 }
    41 
    41 
    42 void FioSeekTo(uint32 pos, int mode)
    42 void FioSeekTo(uint32 pos, int mode)
    71 	assert(f != NULL);
    71 	assert(f != NULL);
    72 	_fio.cur_fh = f;
    72 	_fio.cur_fh = f;
    73 	FioSeekTo(GB(pos, 0, 24), SEEK_SET);
    73 	FioSeekTo(GB(pos, 0, 24), SEEK_SET);
    74 }
    74 }
    75 
    75 
    76 byte FioReadByte(void)
    76 byte FioReadByte()
    77 {
    77 {
    78 	if (_fio.buffer == _fio.buffer_end) {
    78 	if (_fio.buffer == _fio.buffer_end) {
    79 		_fio.pos += FIO_BUFFER_SIZE;
    79 		_fio.pos += FIO_BUFFER_SIZE;
    80 		fread(_fio.buffer = _fio.buffer_start, 1, FIO_BUFFER_SIZE, _fio.cur_fh);
    80 		fread(_fio.buffer = _fio.buffer_start, 1, FIO_BUFFER_SIZE, _fio.cur_fh);
    81 	}
    81 	}
    92 		FioReadByte();
    92 		FioReadByte();
    93 		n--;
    93 		n--;
    94 	}
    94 	}
    95 }
    95 }
    96 
    96 
    97 uint16 FioReadWord(void)
    97 uint16 FioReadWord()
    98 {
    98 {
    99 	byte b = FioReadByte();
    99 	byte b = FioReadByte();
   100 	return (FioReadByte() << 8) | b;
   100 	return (FioReadByte() << 8) | b;
   101 }
   101 }
   102 
   102 
   103 uint32 FioReadDword(void)
   103 uint32 FioReadDword()
   104 {
   104 {
   105 	uint b = FioReadWord();
   105 	uint b = FioReadWord();
   106 	return (FioReadWord() << 16) | b;
   106 	return (FioReadWord() << 16) | b;
   107 }
   107 }
   108 
   108 
   122 		_fio.open_handles--;
   122 		_fio.open_handles--;
   123 #endif /* LIMITED_FDS */
   123 #endif /* LIMITED_FDS */
   124 	}
   124 	}
   125 }
   125 }
   126 
   126 
   127 void FioCloseAll(void)
   127 void FioCloseAll()
   128 {
   128 {
   129 	int i;
   129 	int i;
   130 
   130 
   131 	for (i = 0; i != lengthof(_fio.handles); i++)
   131 	for (i = 0; i != lengthof(_fio.handles); i++)
   132 		FioCloseFile(i);
   132 		FioCloseFile(i);