All files and not just content file can now be compressed

This commit is contained in:
Timo Strunk 2014-09-12 19:06:36 +02:00
parent 565ca9fee8
commit 1883b03497
3 changed files with 55 additions and 52 deletions

49
file.c
View File

@ -133,50 +133,7 @@ static ssize_t read_content_file(const char *path, void **buf)
{
uint8_t *ret_buf = NULL;
ssize_t ret = -1;
/* Here we check, whether the file, we are about to read is
* inside an archive, or not.
*
* We determine, whether a file is inside a compressed archive,
* by checking for the # inside the URL.
*
* For example: fullpath: /home/user/game.7z/mygame.rom
* carchive_path: /home/user/game.7z
* */
#ifdef HAVE_COMPRESSION
if (path_contains_compressed_file(path))
{
//We split carchive path and relative path:
char archive_path[PATH_MAX];
strlcpy(archive_path,path,sizeof(archive_path));
char* archive_found = strchr(archive_path,'#');
//We assure that there is something after the '#' symbol
if (strlen(archive_found) <= 1)
{
/*
* This error condition happens for example, when
* path = /path/to/file.7z, or
* path = /path/to/file.7z#
*/
RARCH_ERR("Could not extract image path and carchive path from "
"path: %s.\n", path);
return -1;
}
rarch_assert(archive_found != NULL);
*archive_found = '\0';
archive_found+=1;
printf("relative_path: %s, archive_path: %s\n",archive_found,archive_path);
ret = read_compressed_file(archive_path,
archive_found,
(void**)&ret_buf);
}
else
#endif
ret = read_file(path, (void**) &ret_buf);
ret = read_file(path, (void**) &ret_buf);
if (ret <= 0)
return ret;
@ -431,8 +388,8 @@ static bool load_content(const struct retro_subsystem_info *special,
if (need_fullpath && path_contains_compressed_file(path))
{
RARCH_ERR("Compressed files are only supported for drivers,"
" where need_fullpath is set to false.\n");
goto end;
" where need_fullpath is set to false. Exiting.\n");
rarch_assert(false);
}
}
}

View File

@ -82,30 +82,54 @@ bool write_file(const char *path, const void *data, size_t size)
/* Generic compressed file loader. */
#ifdef HAVE_COMPRESSION
long read_compressed_file(const char * archive_path, const char *relative_path, void **buf)
long read_compressed_file(const char * path, void **buf)
{
//We split carchive path and relative path:
char archive_path[PATH_MAX];
strlcpy(archive_path,path,sizeof(archive_path));
char* archive_found = strchr(archive_path,'#');
rarch_assert(archive_found != NULL);
//We assure that there is something after the '#' symbol
if (strlen(archive_found) <= 1)
{
/*
* This error condition happens for example, when
* path = /path/to/file.7z, or
* path = /path/to/file.7z#
*/
RARCH_ERR("Could not extract image path and carchive path from "
"path: %s.\n", path);
return -1;
}
//We split the string in two, by putting a \0, where the hash was:
*archive_found = '\0';
archive_found+=1;
const char* file_ext = path_get_extension(archive_path);
#ifdef HAVE_7ZIP
if (strcasecmp(file_ext,"7z") == 0)
{
return read_7zip_file(archive_path,relative_path,buf);
return read_7zip_file(archive_path,archive_found,buf);
}
#endif
#ifdef HAVE_ZLIB
if (strcasecmp(file_ext,"zip") == 0)
{
return read_zip_file(archive_path,relative_path,buf);
return read_zip_file(archive_path,archive_found,buf);
}
#endif
return -1;
}
#endif
/* Generic file loader. */
long read_file(const char *path, void **buf)
static long read_generic_file(const char *path, void **buf)
{
long rc = 0, len = 0;
void *rom_buf = NULL;
FILE *file = fopen(path, "rb");
if (!file)
@ -138,6 +162,28 @@ error:
free(rom_buf);
*buf = NULL;
return -1;
}
/* Generic file loader. */
long read_file(const char *path, void **buf)
{
/* Here we check, whether the file, we are about to read is
* inside an archive, or not.
*
* We determine, whether a file is inside a compressed archive,
* by checking for the # inside the URL.
*
* For example: fullpath: /home/user/game.7z/mygame.rom
* carchive_path: /home/user/game.7z
* */
#ifdef HAVE_COMPRESSION
if (path_contains_compressed_file(path))
{
return read_compressed_file(path,buf);
}
#endif
return read_generic_file(path,buf);
}
/* Reads file content as one string. */

View File

@ -40,7 +40,7 @@ enum
#ifdef HAVE_COMPRESSION
long read_compressed_file(const char * archive_path, const char *relative_path, void **buf);
long read_compressed_file(const char * path, void **buf);
#endif
long read_file(const char *path, void **buf);