[Add] Pack features woo!! (list files).

This commit is contained in:
Allanis 2013-02-05 20:42:06 +00:00
parent f220145d7d
commit 1fa45f1ff2
2 changed files with 41 additions and 0 deletions

View File

@ -297,3 +297,43 @@ int pack_close(Packfile* file) {
return (i) ? -1 : 0;
}
// Load the filenames int the packfile to filenames.
// filenames should be freed after use
// On error if filenames is (char**)-1.
#define READ(f,b,n) if(read(f,b,n)!=n) { ERR("Too few bytes read. Expected more."); return; }
void pack_listfiles(const char* packfile, char** filenames, uint32_t* nfiles) {
int fd, j;
uint32_t i;
char* buf = malloc(sizeof(magic));
*nfiles = 0;
filenames = malloc(sizeof(char*));
filenames = (char**)-1;
fd = open(packfile, O_RDONLY);
if(fd == -1) {
ERR("opening %s: %s", packfile, strerror(errno));
return;
}
READ(fd, buf, sizeof(magic)); // Make sure it is a packfile.
if(memcpy(buf, &magic, sizeof(magic))) {
ERR("File %s is not a valid packfile", packfile);
return;
}
free(buf);
READ(fd, &nfiles, 4);
filenames = realloc(filenames,(*nfiles+1)*sizeof(char*));
for(i = 0; i < *nfiles; i++) {
// Start searching files.
j = 0;
filenames[i] = malloc(MAX_FILENAME * sizeof(char));
READ(fd, &filenames[i][j], 1); // Get the name.
while(filenames[i][j++] != '\0')
READ(fd, &filenames[i][j], 1);
}
filenames[i] == NULL;
close(fd);
}
#undef READ

View File

@ -19,4 +19,5 @@ int pack_close(Packfile* file);
// Fancy stuff.
void* pack_readfile(const char* packfile, const char* filename, uint32_t* filesize);
void pack_listfiles(const char* packfile, char** filenames, uint32_t* nfiles);