/* * This function verifies if a given file (by number * or name) is in the opened mpq archive. On success * it returns 0, otherwise LIBMPQ_EFILE_NOT_FOUND. */ int libmpq_file_check(mpq_archive *mpq_a, void *file, int type) { int found = 0; int i; char tempfile[PATH_MAX]; switch (type) { case LIBMPQ_FILE_TYPE_INT: /* check if we are in the range of available files. */ if (*(int *)file > libmpq_archive_info(mpq_a, LIBMPQ_MPQ_NUMFILES) || *(int *)file < 1) { return LIBMPQ_EFILE_NOT_FOUND; } else { return LIBMPQ_TOOLS_SUCCESS; } case LIBMPQ_FILE_TYPE_CHAR: for (i = 0; mpq_a->mpq_l->mpq_files[i]; i++) { sprintf(tempfile, (char *)mpq_a->mpq_l->mpq_files[i], i); if (strncmp(tempfile, (char *)file, strlen((char *)file)) == 0) { /* if file found break */ found = 1; break; } } /* if a file was found return 0 */ if (found == 1) { return LIBMPQ_TOOLS_SUCCESS; } else { return LIBMPQ_EFILE_NOT_FOUND; } default: return LIBMPQ_TOOLS_SUCCESS; } }
/* * This function searches the listfile for the filename. * On success it returns the filename, otherwiese a name * like file000001.xxx and if number is out of range it * returns NULL. */ char *libmpq_file_name(mpq_archive *mpq_a, const int number) { static char tempfile[PATH_MAX]; /* check if we are in the range of available files. */ if (number > libmpq_archive_info(mpq_a, LIBMPQ_MPQ_NUMFILES) || number < 1) { return NULL; } /* this is safe because we built a fallback filelist, if something was wrong. */ sprintf(tempfile, (char *)mpq_a->mpq_l->mpq_files[number - 1], number); return tempfile; }
/* * This function verifies if a given file (by number * or name) is in the opened mpq archive. On success * it returns 0, otherwise LIBMPQ_EFILE_NOT_FOUND. */ int libmpq_file_check(mpq_archive *mpq_a, void *file, int type) { switch (type) { case LIBMPQ_FILE_TYPE_INT: /* check if we are in the range of available files. */ if (*(int *)file > libmpq_archive_info(mpq_a, LIBMPQ_MPQ_NUMFILES) || *(int *)file < 1) { return LIBMPQ_EFILE_NOT_FOUND; } else { return LIBMPQ_TOOLS_SUCCESS; } case LIBMPQ_FILE_TYPE_CHAR: /* if a file was found return 0 */ if (libmpq_file_number(mpq_a, (char *)file) != LIBMPQ_EFILE_NOT_FOUND) { return LIBMPQ_TOOLS_SUCCESS; } else { return LIBMPQ_EFILE_NOT_FOUND; } default: return LIBMPQ_TOOLS_SUCCESS; } }
int libmpq_listfile_open(mpq_archive *mpq_a, char file[PATH_MAX]) { FILE *fp; //char **filelist; int i = 0; //int fl_count; //int fl_size; int fl_count_fb; int fl_size_fb; int result = LIBMPQ_TOOLS_SUCCESS; struct stat statbuf; /* get file status */ if (stat(file, &statbuf) < 0) { result = LIBMPQ_CONF_EFILE_NOT_FOUND; } /* check if file is a filename or directory */ /*if (S_ISDIR(statbuf.st_mode)) { // allocate memory for the file list filelist = (char **)malloc(LIBMPQ_CONF_FL_INCREMENT * sizeof(char *)); fl_count = 0; fl_size = LIBMPQ_CONF_FL_INCREMENT; // check if it is a valid listfile if (libmpq_detect_listfile_rec(file, &filelist, &fl_count, &fl_size)) { filelist == NULL; } filelist[fl_count] = NULL; // return if no listfile was found if (filelist == NULL) { result = LIBMPQ_CONF_EFILE_NOT_FOUND; } for (i = 0; filelist[i]; i++) { if ((fp = fopen(filelist[i], "r")) != NULL ) { result = libmpq_read_listfile(mpq_a, fp); fclose(fp); } } // freeing the listfile struct libmpq_free_listfile(filelist); }*/ /* if file is a regular file use it */ //if (S_ISREG(statbuf.st_mode)) { /* if specific listfile was forced. */ if ((fp = fopen(file, "r")) != NULL ) { result = libmpq_read_listfile(mpq_a, fp); fclose(fp); } else { result = LIBMPQ_CONF_EFILE_OPEN; } //} /* if error occured we need to create a fallback filelist. */ if (mpq_a->mpq_l->mpq_files == NULL) { /* allocate memory for the file list */ mpq_a->mpq_l->mpq_files = (unsigned char **)malloc(LIBMPQ_CONF_FL_INCREMENT * sizeof(char *)); fl_count_fb = 0; fl_size_fb = LIBMPQ_CONF_FL_INCREMENT; for (i = 0; i < libmpq_archive_info(mpq_a, LIBMPQ_MPQ_NUMFILES); i++) { /* set the next filelist entry to a copy of the file */ mpq_a->mpq_l->mpq_files[fl_count_fb++] = (unsigned char *)_strdup("file%06lu.xxx"); /* increase the array size */ if (fl_count_fb == fl_size_fb) { mpq_a->mpq_l->mpq_files = (unsigned char **)realloc(mpq_a->mpq_l->mpq_files, (fl_size_fb + LIBMPQ_CONF_FL_INCREMENT) * sizeof(char *)); fl_size_fb += LIBMPQ_CONF_FL_INCREMENT; } } mpq_a->mpq_l->mpq_files[fl_count_fb] = NULL; /* if no error occurs and no listfile was assigned, we think there was no matching listfile. */ if (result == 0) { result = LIBMPQ_CONF_EFILE_NOT_FOUND; } } return result; }