135 lines
3.5 KiB
C
135 lines
3.5 KiB
C
#include "szs.h"
|
|
|
|
|
|
SzsFile* szsFileNew() {
|
|
// allocate the memory for the wrapper and the internal file
|
|
SzsFile* file = (SzsFile*)malloc(sizeof(SzsFile));
|
|
file->_internal = (szs_file_t*)malloc(sizeof(szs_file_t));
|
|
|
|
// initialize the internal structure
|
|
InitializeSZS(file->_internal);
|
|
|
|
return file;
|
|
}
|
|
|
|
void szsFileFree(SzsFile* file) {
|
|
// reset the SZS file
|
|
ResetSZS(file->_internal);
|
|
|
|
// free the memory
|
|
free(file->_internal);
|
|
free(file);
|
|
}
|
|
|
|
SzsFile* szsFileLoad(const char* path) {
|
|
// create a new empty SZS file
|
|
SzsFile* file = szsFileNew();
|
|
|
|
// load the file
|
|
if (LoadSZS(
|
|
file->_internal,
|
|
path,
|
|
true,
|
|
false,
|
|
false
|
|
) > ERR_WARNING) {
|
|
// if an error occurred, cancel the file allocation and return NULL
|
|
szsFileFree(file);
|
|
return NULL;
|
|
}
|
|
|
|
return file;
|
|
}
|
|
|
|
SzsFile* szsFileCreate(const char* path) {
|
|
// create a new empty SZS file
|
|
SzsFile* file = szsFileNew();
|
|
|
|
// create the file from a directory
|
|
if (CreateSZS(
|
|
file->_internal,
|
|
NULL,
|
|
path,
|
|
NULL,
|
|
NULL,
|
|
-1,
|
|
-1,
|
|
false
|
|
) > ERR_WARNING) {
|
|
// if an error occurred, cancel the file allocation and return NULL
|
|
szsFileFree(file);
|
|
return NULL;
|
|
}
|
|
|
|
return file;
|
|
}
|
|
|
|
void szsFileExtract(SzsFile* file, const char* path) {
|
|
// TODO(Faraphel): don't seem to work
|
|
|
|
// set the destination of the base archive
|
|
// TODO(Faraphel): the output directory name is the filename with a .d extension instead of .szs
|
|
// for customizability, the extraction should instead be implemented manually.
|
|
file->_internal->fname = strdup(path);
|
|
|
|
ExtractFilesSZS(
|
|
file->_internal,
|
|
-1,
|
|
false,
|
|
NULL,
|
|
path
|
|
);
|
|
}
|
|
|
|
SzsSubfile* szsSubfileNew(szs_subfile_t* internal_subfile) {
|
|
// allocate memory for our subfile wrapper
|
|
SzsSubfile* subfile = (SzsSubfile*)malloc(sizeof(SzsSubfile));
|
|
|
|
// save the base subfile
|
|
subfile->_internal = internal_subfile;
|
|
subfile->path_p = &subfile->_internal->path;
|
|
subfile->name_p = &subfile->_internal->name;
|
|
subfile->size_p = &subfile->_internal->size;
|
|
subfile->data_p = &subfile->_internal->data;
|
|
subfile->is_directory_p = (bool*)&subfile->_internal->is_dir;
|
|
|
|
return subfile;
|
|
}
|
|
|
|
void szsSubfileFree(SzsSubfile* subfile) {
|
|
free(subfile);
|
|
}
|
|
|
|
void szsFileDiscover(SzsFile* file, bool force) {
|
|
// if the subfiles are already loaded and force is disabled, directly exit
|
|
if (file->_internal->subfile.used != 0 && !force)
|
|
return;
|
|
|
|
// reset the list of subfiles and reload it
|
|
CollectFilesSZS(file->_internal, true, 0, -1, SORT_NONE);
|
|
}
|
|
|
|
SzsSubfileIterator* szsSubfileIterateNew(SzsFile* file) {
|
|
szsFileDiscover(file, false);
|
|
SzsSubfileIterator* iterator = malloc(sizeof(SzsSubfileIterator));
|
|
|
|
iterator->_internal_current = &file->_internal->subfile.list[0];
|
|
iterator->_internal_final = &file->_internal->subfile.list[file->_internal->subfile.size];
|
|
|
|
return iterator;
|
|
}
|
|
|
|
void szsSubfileIterateFree(SzsSubfileIterator* iterator) {
|
|
free(iterator);
|
|
}
|
|
|
|
SzsSubfile* szsSubfileIterateNext(SzsSubfileIterator* iterator) {
|
|
// if we went over the last file in the list, return NULL
|
|
if (iterator->_internal_current > iterator->_internal_final)
|
|
return NULL;
|
|
|
|
// get the next subfile
|
|
SzsSubfile* subfile = szsSubfileNew(iterator->_internal_current++);
|
|
|
|
return subfile;
|
|
}
|