src/fileio.cpp
changeset 8374 7a1b6c89cb89
parent 8273 1d95cbda404b
child 9084 55b2d2cb8b90
equal deleted inserted replaced
8373:7431d91527f2 8374:7a1b6c89cb89
    31 	FILE *cur_fh;                          ///< current file handle
    31 	FILE *cur_fh;                          ///< current file handle
    32 	const char *filename;                  ///< current filename
    32 	const char *filename;                  ///< current filename
    33 	FILE *handles[MAX_FILE_SLOTS];         ///< array of file handles we can have open
    33 	FILE *handles[MAX_FILE_SLOTS];         ///< array of file handles we can have open
    34 	byte buffer_start[FIO_BUFFER_SIZE];    ///< local buffer when read from file
    34 	byte buffer_start[FIO_BUFFER_SIZE];    ///< local buffer when read from file
    35 	const char *filenames[MAX_FILE_SLOTS]; ///< array of filenames we (should) have open
    35 	const char *filenames[MAX_FILE_SLOTS]; ///< array of filenames we (should) have open
       
    36 	char *shortnames[MAX_FILE_SLOTS];///< array of short names for spriteloader's use
    36 #if defined(LIMITED_FDS)
    37 #if defined(LIMITED_FDS)
    37 	uint open_handles;                     ///< current amount of open handles
    38 	uint open_handles;                     ///< current amount of open handles
    38 	uint usage_count[MAX_FILE_SLOTS];      ///< count how many times this file has been opened
    39 	uint usage_count[MAX_FILE_SLOTS];      ///< count how many times this file has been opened
    39 #endif /* LIMITED_FDS */
    40 #endif /* LIMITED_FDS */
    40 };
    41 };
    45 uint32 FioGetPos()
    46 uint32 FioGetPos()
    46 {
    47 {
    47 	return _fio.pos + (_fio.buffer - _fio.buffer_start) - FIO_BUFFER_SIZE;
    48 	return _fio.pos + (_fio.buffer - _fio.buffer_start) - FIO_BUFFER_SIZE;
    48 }
    49 }
    49 
    50 
    50 const char *FioGetFilename()
    51 const char *FioGetFilename(uint8 slot)
    51 {
    52 {
    52 	return _fio.filename;
    53 	return _fio.shortnames[slot];
    53 }
    54 }
    54 
    55 
    55 void FioSeekTo(uint32 pos, int mode)
    56 void FioSeekTo(uint32 pos, int mode)
    56 {
    57 {
    57 	if (mode == SEEK_CUR) pos += FioGetPos();
    58 	if (mode == SEEK_CUR) pos += FioGetPos();
   129 
   130 
   130 static inline void FioCloseFile(int slot)
   131 static inline void FioCloseFile(int slot)
   131 {
   132 {
   132 	if (_fio.handles[slot] != NULL) {
   133 	if (_fio.handles[slot] != NULL) {
   133 		fclose(_fio.handles[slot]);
   134 		fclose(_fio.handles[slot]);
       
   135 
       
   136 		free(_fio.shortnames[slot]);
       
   137 		_fio.shortnames[slot] = NULL;
       
   138 
   134 		_fio.handles[slot] = NULL;
   139 		_fio.handles[slot] = NULL;
   135 #if defined(LIMITED_FDS)
   140 #if defined(LIMITED_FDS)
   136 		_fio.open_handles--;
   141 		_fio.open_handles--;
   137 #endif /* LIMITED_FDS */
   142 #endif /* LIMITED_FDS */
   138 	}
   143 	}
   182 	uint32 pos = ftell(f);
   187 	uint32 pos = ftell(f);
   183 
   188 
   184 	FioCloseFile(slot); // if file was opened before, close it
   189 	FioCloseFile(slot); // if file was opened before, close it
   185 	_fio.handles[slot] = f;
   190 	_fio.handles[slot] = f;
   186 	_fio.filenames[slot] = filename;
   191 	_fio.filenames[slot] = filename;
       
   192 
       
   193 	/* Store the filename without path and extension */
       
   194 	const char *t = strrchr(filename, PATHSEPCHAR);
       
   195 	_fio.shortnames[slot] = strdup(t == NULL ? filename : t);
       
   196 	char *t2 = strrchr(_fio.shortnames[slot], '.');
       
   197 	if (t2 != NULL) *t2 = '\0';
       
   198 	strtolower(_fio.shortnames[slot]);
       
   199 
   187 #if defined(LIMITED_FDS)
   200 #if defined(LIMITED_FDS)
   188 	_fio.usage_count[slot] = 0;
   201 	_fio.usage_count[slot] = 0;
   189 	_fio.open_handles++;
   202 	_fio.open_handles++;
   190 #endif /* LIMITED_FDS */
   203 #endif /* LIMITED_FDS */
   191 	FioSeekToFile(slot, pos);
   204 	FioSeekToFile(slot, pos);