src/fileio.cpp
branchnoai
changeset 10455 22c441f5adf9
parent 9724 b39bc69bb2f2
child 10513 33cb70ff2f5d
equal deleted inserted replaced
10412:ef44f62cb8b9 10455:22c441f5adf9
    43 static Fio _fio;
    43 static Fio _fio;
    44 
    44 
    45 /* Get current position in file */
    45 /* Get current position in file */
    46 uint32 FioGetPos()
    46 uint32 FioGetPos()
    47 {
    47 {
    48 	return _fio.pos + (_fio.buffer - _fio.buffer_start) - FIO_BUFFER_SIZE;
    48 	return _fio.pos + (_fio.buffer - _fio.buffer_end);
    49 }
    49 }
    50 
    50 
    51 const char *FioGetFilename(uint8 slot)
    51 const char *FioGetFilename(uint8 slot)
    52 {
    52 {
    53 	return _fio.shortnames[slot];
    53 	return _fio.shortnames[slot];
    89 }
    89 }
    90 
    90 
    91 byte FioReadByte()
    91 byte FioReadByte()
    92 {
    92 {
    93 	if (_fio.buffer == _fio.buffer_end) {
    93 	if (_fio.buffer == _fio.buffer_end) {
    94 		_fio.pos += FIO_BUFFER_SIZE;
    94 		_fio.buffer = _fio.buffer_start;
    95 		fread(_fio.buffer = _fio.buffer_start, 1, FIO_BUFFER_SIZE, _fio.cur_fh);
    95 		size_t size = fread(_fio.buffer, 1, FIO_BUFFER_SIZE, _fio.cur_fh);
       
    96 		_fio.pos += size;
       
    97 		_fio.buffer_end = _fio.buffer_start + size;
       
    98 
       
    99 		if (size == 0) return 0;
    96 	}
   100 	}
    97 	return *_fio.buffer++;
   101 	return *_fio.buffer++;
    98 }
   102 }
    99 
   103 
   100 void FioSkipBytes(int n)
   104 void FioSkipBytes(int n)
   122 }
   126 }
   123 
   127 
   124 void FioReadBlock(void *ptr, uint size)
   128 void FioReadBlock(void *ptr, uint size)
   125 {
   129 {
   126 	FioSeekTo(FioGetPos(), SEEK_SET);
   130 	FioSeekTo(FioGetPos(), SEEK_SET);
   127 	_fio.pos += size;
   131 	_fio.pos += fread(ptr, 1, size, _fio.cur_fh);
   128 	fread(ptr, 1, size, _fio.cur_fh);
       
   129 }
   132 }
   130 
   133 
   131 static inline void FioCloseFile(int slot)
   134 static inline void FioCloseFile(int slot)
   132 {
   135 {
   133 	if (_fio.handles[slot] != NULL) {
   136 	if (_fio.handles[slot] != NULL) {
   313 #endif
   316 #endif
   314 
   317 
   315 	f = fopen(buf, mode);
   318 	f = fopen(buf, mode);
   316 #if !defined(WIN32)
   319 #if !defined(WIN32)
   317 	if (f == NULL) {
   320 	if (f == NULL) {
   318 		strtolower(buf + strlen(_searchpaths[sp]) - 1);
   321 		strtolower(buf + ((subdir == NO_DIRECTORY) ? 0 : strlen(_searchpaths[sp]) - 1));
   319 		f = fopen(buf, mode);
   322 		f = fopen(buf, mode);
   320 	}
   323 	}
   321 #endif
   324 #endif
   322 	if (f != NULL && filesize != NULL) {
   325 	if (f != NULL && filesize != NULL) {
   323 		/* Find the size of the file */
   326 		/* Find the size of the file */
   428 	/* Check if absolute or relative path */
   431 	/* Check if absolute or relative path */
   429 	const char *s = strchr(dest, PATHSEPCHAR);
   432 	const char *s = strchr(dest, PATHSEPCHAR);
   430 
   433 
   431 	/* Add absolute path */
   434 	/* Add absolute path */
   432 	if (s == NULL || dest != s) {
   435 	if (s == NULL || dest != s) {
   433 		getcwd(dest, MAX_PATH);
   436 		if (getcwd(dest, MAX_PATH) == NULL) *dest = '\0';
   434 		AppendPathSeparator(dest, MAX_PATH);
   437 		AppendPathSeparator(dest, MAX_PATH);
   435 		ttd_strlcat(dest, dir, MAX_PATH);
   438 		ttd_strlcat(dest, dir, MAX_PATH);
   436 	}
   439 	}
   437 	AppendPathSeparator(dest, MAX_PATH);
   440 	AppendPathSeparator(dest, MAX_PATH);
   438 
   441 
   482 	/* Make a char of 512 empty bytes */
   485 	/* Make a char of 512 empty bytes */
   483 	char empty[512];
   486 	char empty[512];
   484 	memset(&empty[0], 0, sizeof(empty));
   487 	memset(&empty[0], 0, sizeof(empty));
   485 
   488 
   486 	while (!feof(f)) {
   489 	while (!feof(f)) {
   487 		fread(&th, 1, 512, f);
   490 		pos += fread(&th, 1, 512, f);
   488 		pos += 512;
       
   489 
   491 
   490 		/* Check if we have the new tar-format (ustar) or the old one (a lot of zeros after 'link' field) */
   492 		/* Check if we have the new tar-format (ustar) or the old one (a lot of zeros after 'link' field) */
   491 		if (strncmp(th.magic, "ustar", 5) != 0 && memcmp(&th.magic, &empty[0], 512 - offsetof(TarHeader, magic)) != 0) {
   493 		if (strncmp(th.magic, "ustar", 5) != 0 && memcmp(&th.magic, &empty[0], 512 - offsetof(TarHeader, magic)) != 0) {
   492 			/* If we have only zeros in the block, it can be an end-of-file indicator */
   494 			/* If we have only zeros in the block, it can be an end-of-file indicator */
   493 			if (memcmp(&th, &empty[0], 512) == 0) continue;
   495 			if (memcmp(&th, &empty[0], 512) == 0) continue;
   627 	if (app_bundle != NULL) app_bundle[0] = '\0';
   629 	if (app_bundle != NULL) app_bundle[0] = '\0';
   628 #endif /* WITH_COCOA */
   630 #endif /* WITH_COCOA */
   629 	char *s = strrchr(exe, PATHSEPCHAR);
   631 	char *s = strrchr(exe, PATHSEPCHAR);
   630 	if (s != NULL) {
   632 	if (s != NULL) {
   631 		*s = '\0';
   633 		*s = '\0';
   632 		chdir(exe);
   634 		if (chdir(exe) != 0) DEBUG(misc, 0, "Directory with the binary does not exist?");
   633 		*s = PATHSEPCHAR;
   635 		*s = PATHSEPCHAR;
   634 	}
   636 	}
   635 #ifdef WITH_COCOA
   637 #ifdef WITH_COCOA
   636 	if (app_bundle != NULL) app_bundle[0] = '.';
   638 	if (app_bundle != NULL) app_bundle[0] = '.';
   637 #endif /* WITH_COCOA */
   639 #endif /* WITH_COCOA */
   669 #endif
   671 #endif
   670 
   672 
   671 #if defined(__MORPHOS__) || defined(__AMIGA__)
   673 #if defined(__MORPHOS__) || defined(__AMIGA__)
   672 	_searchpaths[SP_WORKING_DIR] = NULL;
   674 	_searchpaths[SP_WORKING_DIR] = NULL;
   673 #else
   675 #else
   674 	getcwd(tmp, MAX_PATH);
   676 	if (getcwd(tmp, MAX_PATH) == NULL) *tmp = '\0';
   675 	AppendPathSeparator(tmp, MAX_PATH);
   677 	AppendPathSeparator(tmp, MAX_PATH);
   676 	_searchpaths[SP_WORKING_DIR] = strdup(tmp);
   678 	_searchpaths[SP_WORKING_DIR] = strdup(tmp);
   677 #endif
   679 #endif
   678 
   680 
   679 	/* Change the working directory to that one of the executable */
   681 	/* Change the working directory to that one of the executable */
   680 	ChangeWorkingDirectory((char*)exe);
   682 	ChangeWorkingDirectory((char*)exe);
   681 	getcwd(tmp, MAX_PATH);
   683 	if (getcwd(tmp, MAX_PATH) == NULL) *tmp = '\0';
   682 	AppendPathSeparator(tmp, MAX_PATH);
   684 	AppendPathSeparator(tmp, MAX_PATH);
   683 	_searchpaths[SP_BINARY_DIR] = strdup(tmp);
   685 	_searchpaths[SP_BINARY_DIR] = strdup(tmp);
   684 
   686 
   685 #if defined(__MORPHOS__) || defined(__AMIGA__)
   687 #if defined(__MORPHOS__) || defined(__AMIGA__)
   686 	_searchpaths[SP_INSTALLATION_DIR] = NULL;
   688 	_searchpaths[SP_INSTALLATION_DIR] = NULL;