From 6a23259f5dc12467155399612f0bc1dcffa35753 Mon Sep 17 00:00:00 2001 From: Taylor Richards Date: Sun, 4 Jul 2004 11:27:29 +0000 Subject: [PATCH] cleanup CFILE code a little, warning fixes, remove redundant dir checks, amd64 support --- src/cfile/cfile.cpp | 25 ++-- src/cfile/cfilesystem.cpp | 254 +++++--------------------------------- 2 files changed, 39 insertions(+), 240 deletions(-) diff --git a/src/cfile/cfile.cpp b/src/cfile/cfile.cpp index 1e438ea..de984ec 100644 --- a/src/cfile/cfile.cpp +++ b/src/cfile/cfile.cpp @@ -15,6 +15,9 @@ * Utilities for operating on files * * $Log$ + * Revision 1.11 2004/07/04 11:27:29 taylor + * cleanup CFILE code a little, warning fixes, remove redundant dir checks, amd64 support + * * Revision 1.10 2004/06/11 00:28:39 tigital * byte-swapping changes for bigendian systems * @@ -353,21 +356,13 @@ int cfile_in_root_dir(char *exe_path) strncpy(path_copy, exe_path, 2047); // count how many slashes there are in the path -#ifdef PLAT_UNIX - tok = strtok(path_copy, "/"); -#else - tok = strtok(path_copy, "\\"); -#endif + tok = strtok(path_copy, DIR_SEPARATOR_STR); if(tok == NULL){ return 1; } do { token_count++; -#ifdef PLAT_UNIX - tok = strtok(NULL, "/"); -#else - tok = strtok(NULL, "\\"); -#endif + tok = strtok(NULL, DIR_SEPARATOR_STR); } while(tok != NULL); // root directory if we have <= 1 slash @@ -408,11 +403,7 @@ int cfile_init(char *exe_dir, char *cdrom_dir) #endif while (i--) { -#ifdef PLAT_UNIX - if (buf[i] == '/'){ -#else - if (buf[i] == '\\'){ -#endif + if (buf[i] == DIR_SEPARATOR_CHAR){ break; } } @@ -834,7 +825,11 @@ CFILE *cfopen(char *file_path, char *mode, int type, int dir_type, bool localize if ( strchr(mode,'w') ) { // For write-only files, require a full path or a path type +#ifdef PLAT_UNIX + if ( strpbrk(file_path, "/") ) { +#else if ( strpbrk(file_path,"/\\:") ) { +#endif // Full path given? strcpy(longname, file_path ); } else { diff --git a/src/cfile/cfilesystem.cpp b/src/cfile/cfilesystem.cpp index 82e51ff..97b320b 100644 --- a/src/cfile/cfilesystem.cpp +++ b/src/cfile/cfilesystem.cpp @@ -19,6 +19,9 @@ * all those locations, inherently enforcing precedence orders. * * $Log$ + * Revision 1.11 2004/07/04 11:27:29 taylor + * cleanup CFILE code a little, warning fixes, remove redundant dir checks, amd64 support + * * Revision 1.10 2004/06/11 00:29:22 tigital * byte-swapping changes for bigendian systems * @@ -502,13 +505,8 @@ void cf_build_root_list(char *cdrom_dir) } // do we already have a slash? as in the case of a root directory install -#ifdef PLAT_UNIX - if(strlen(root->path) && (root->path[strlen(root->path)-1] != '/')){ - strcat(root->path, "/"); // put trailing backslash on for easier path construction -#else - if(strlen(root->path) && (root->path[strlen(root->path)-1] != '\\')){ - strcat(root->path, "\\"); // put trailing backslash on for easier path construction -#endif + if(strlen(root->path) && (root->path[strlen(root->path)-1] != DIR_SEPARATOR_CHAR)){ + strcat(root->path, DIR_SEPARATOR_STR); // put trailing backslash on for easier path construction } root->roottype = CF_ROOTTYPE_PATH; @@ -560,16 +558,23 @@ void cf_search_root_path(int root_index) for (i=CF_TYPE_ROOT; ipath ); if(strlen(Pathtypes[i].path)){ strcat( search_path, Pathtypes[i].path ); - strcat( search_path, "/" ); - } + strcat( search_path, DIR_SEPARATOR_STR ); + } + +#ifdef PLAT_UNIX + DIR *dirp; + struct dirent *dir; + + // only get pilots from the primary root as there could be nasty + // permission issues otherwise + if ( (root_index > 0) && ((Pathtypes[i].index == CF_TYPE_SINGLE_PLAYERS) + || (Pathtypes[i].index == CF_TYPE_MULTI_PLAYERS)) ) { + break; + } dirp = opendir (search_path); if ( dirp ) { @@ -614,13 +619,6 @@ void cf_search_root_path(int root_index) closedir(dirp); } #else - strcpy( search_path, root->path ); - - if(strlen(Pathtypes[i].path)){ - strcat( search_path, Pathtypes[i].path ); - strcat( search_path, "\\" ); - } - strcat( search_path, "*.*" ); int find_handle; @@ -673,7 +671,7 @@ typedef struct VP_FILE { int offset; int size; char filename[32]; - time_t write_time; + fs_time_t write_time; } VP_FILE; void cf_search_root_pack(int root_index) @@ -722,21 +720,13 @@ void cf_search_root_pack(int root_index) if ( !stricmp( find.filename, ".." )) { int l = strlen(search_path); char *p = &search_path[l-1]; -#ifdef PLAT_UNIX - while( (p > search_path) && (*p != '/') ) { -#else - while( (p > search_path) && (*p != '\\') ) { -#endif + while( (p > search_path) && (*p != DIR_SEPARATOR_CHAR) ) { p--; } *p = 0; } else { if ( strlen(search_path) ) { -#ifdef PLAT_UNIX - strcat( search_path, "/" ); -#else - strcat( search_path, "\\" ); -#endif + strcat( search_path, DIR_SEPARATOR_STR ); } strcat( search_path, find.filename ); } @@ -878,7 +868,11 @@ int cf_find_file_location( char *filespec, int pathtype, char *pack_filename, in // of the file // NOTE: full path should also include localization, if so desired +#ifdef PLAT_UNIX + if ( strpbrk(filespec, "/") ) { +#else if ( strpbrk(filespec,"/\\:") ) { // do we have a full path already? +#endif FILE *fp = fopen(filespec, "rb" ); if (fp) { if ( size ) *size = filelength(fileno(fp)); @@ -924,26 +918,6 @@ int cf_find_file_location( char *filespec, int pathtype, char *pack_filename, in } } -#ifdef PLAT_UNIX - // search the secondary directory (game directory) as well since the user dir is default - for (i=0; ipath ); if ( f->pack_offset < 1 ) { strcat( pack_filename, Pathtypes[f->pathtype_index].path ); -#ifdef PLAT_UNIX - strcat( pack_filename, "/" ); -#else - strcat( pack_filename, "\\" ); -#endif + strcat( pack_filename, DIR_SEPARATOR_STR ); strcat( pack_filename, f->name_ext ); } } @@ -995,11 +965,7 @@ int cf_find_file_location( char *filespec, int pathtype, char *pack_filename, in if(strlen(Pathtypes[f->pathtype_index].path)){ strcat( pack_filename, Pathtypes[f->pathtype_index].path ); -#ifdef PLAT_UNIX - strcat( pack_filename, "/" ); -#else - strcat( pack_filename, "\\" ); -#endif + strcat( pack_filename, DIR_SEPARATOR_STR ); } strcat( pack_filename, f->name_ext ); @@ -1129,58 +1095,6 @@ int cf_get_file_list( int max, char **list, int pathtype, char *filter, int sort closedir(dirp); } - - // grab secondary (game) directory as well but be sure to skip the pilot - // directories as that would be a bad thing to load read-only pilots - if (pathtype != (CF_TYPE_PLAYERS || CF_TYPE_SINGLE_PLAYERS || CF_TYPE_MULTI_PLAYERS)) { - cf_create_secondary_path_string( filespec, pathtype, NULL ); - - DIR *dirp; - struct dirent *dir; - - dirp = opendir (filespec); - if ( dirp ) { - while ((dir = readdir (dirp)) != NULL) - { - if (num_files >= max) - break; - - if (fnmatch(filter, dir->d_name, 0) != 0) - continue; - - char fn[MAX_PATH]; - snprintf(fn, MAX_PATH-1, "%s/%s", filespec, dir->d_name); - fn[MAX_PATH-1] = 0; - - struct stat buf; - if (stat(fn, &buf) == -1) { - continue; - } - - if (!S_ISREG(buf.st_mode)) { - continue; - } - - if ( !Get_file_list_filter || (*Get_file_list_filter)(dir->d_name) ) { - ptr = strrchr(dir->d_name, '.'); - if (ptr) - l = ptr - dir->d_name; - else - l = strlen(dir->d_name); - - list[num_files] = (char *)malloc(l + 1); - strncpy(list[num_files], dir->d_name, l); - list[num_files][l] = 0; - if (info) - info[num_files].write_time = buf.st_mtime; - - num_files++; - } - } - - closedir(dirp); - } - } #else cf_create_default_path_string( filespec, pathtype, filter ); @@ -1214,7 +1128,6 @@ int cf_get_file_list( int max, char **list, int pathtype, char *filter, int sort } #endif - // Search all the packfiles and CD. if ( !Skip_packfile_search ) { for (i=0; i= max) - break; - - if (fnmatch(filter, dir->d_name, 0) != 0) - continue; - - char fn[MAX_PATH]; - snprintf(fn, MAX_PATH-1, "%s/%s", filespec, dir->d_name); - fn[MAX_PATH-1] = 0; - - struct stat buf; - if (stat(fn, &buf) == -1) { - continue; - } - - if (!S_ISREG(buf.st_mode)) { - continue; - } - - if ( !Get_file_list_filter || (*Get_file_list_filter)(dir->d_name) ) { - - strncpy(arr[num_files], dir->d_name, MAX_FILENAME_LEN - 1 ); - char *ptr = strrchr(arr[num_files], '.'); - if ( ptr ) { - *ptr = 0; - } - - if (info) { - info[num_files].write_time = buf.st_mtime; - } - - num_files++; - } - } - closedir(dirp); - } - } #else cf_create_default_path_string( filespec, pathtype, filter ); @@ -1520,7 +1383,7 @@ int cf_get_file_list_preallocated( int max, char arr[][MAX_FILENAME_LEN], char * void cf_create_default_path_string( char *path, int pathtype, char *filename, bool localize ) { #ifdef PLAT_UNIX - if ( filename && strpbrk(filename,"/") ) { + if ( filename && strpbrk(filename, "/") ) { #else if ( filename && strpbrk(filename,"/\\:") ) { #endif @@ -1542,65 +1405,7 @@ void cf_create_default_path_string( char *path, int pathtype, char *filename, bo // Don't add slash for root directory if (Pathtypes[pathtype].path[0] != '\0') { -#ifdef PLAT_UNIX - strcat(path, "/"); -#else - strcat(path, "\\"); -#endif - } - - // add filename - if (filename) { - strcat(path, filename); - - // localize filename - if (localize) { - // create copy of path - char temp_path[MAX_PATH_LEN]; - strcpy(temp_path, path); - - // localize the path - lcl_add_dir_to_path_with_filename(path); - - // verify localized path - FILE *fp = fopen(path, "rb"); - if (fp) { - fclose(fp); - } else { - strcpy(path, temp_path); - } - } - } - } -} - -#ifdef PLAT_UNIX -// this is the same as cf_create_default_path_string above but as it only shows -// files in the users directory this function will find files in the game -// installation directory -void cf_create_secondary_path_string( char *path, int pathtype, char *filename, bool localize ) -{ - if ( filename && strpbrk(filename,"/") ) { - - // Already has full path - strcpy( path, filename ); - - } else { - cf_root *root = cf_get_root(1); - - if (!root) { - strcpy(path, filename); - return; - } - - Assert(CF_TYPE_SPECIFIED(pathtype)); - - strcpy(path, root->path); - strcat(path, Pathtypes[pathtype].path); - - // Don't add slash for root directory - if (Pathtypes[pathtype].path[0] != '\0') { - strcat(path, "/"); + strcat(path, DIR_SEPARATOR_STR); } // add filename @@ -1627,4 +1432,3 @@ void cf_create_secondary_path_string( char *path, int pathtype, char *filename, } } } -#endif -- 2.39.2