📄 grp.c
字号:
} /* grp_entry_cmp */static void grp_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two){ GRPentry tmp; GRPentry *first = &(((GRPentry *) _a)[one]); GRPentry *second = &(((GRPentry *) _a)[two]); memcpy(&tmp, first, sizeof (GRPentry)); memcpy(first, second, sizeof (GRPentry)); memcpy(second, &tmp, sizeof (GRPentry));} /* grp_entry_swap */static int grp_load_entries(const char *name, int forWriting, GRPinfo *info){ void *fh = NULL; PHYSFS_uint32 fileCount; PHYSFS_uint32 location = 16; /* sizeof sig. */ GRPentry *entry; char *ptr; BAIL_IF_MACRO(!grp_open(name, forWriting, &fh, &fileCount), NULL, 0); info->entryCount = fileCount; info->entries = (GRPentry *) malloc(sizeof (GRPentry) * fileCount); if (info->entries == NULL) { __PHYSFS_platformClose(fh); BAIL_MACRO(ERR_OUT_OF_MEMORY, 0); } /* if */ location += (16 * fileCount); for (entry = info->entries; fileCount > 0; fileCount--, entry++) { if (__PHYSFS_platformRead(fh, &entry->name, 12, 1) != 1) { __PHYSFS_platformClose(fh); return(0); } /* if */ entry->name[12] = '\0'; /* name isn't null-terminated in file. */ if ((ptr = strchr(entry->name, ' ')) != NULL) *ptr = '\0'; /* trim extra spaces. */ if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1) { __PHYSFS_platformClose(fh); return(0); } /* if */ entry->size = PHYSFS_swapULE32(entry->size); entry->startPos = location; location += entry->size; } /* for */ __PHYSFS_platformClose(fh); __PHYSFS_sort(info->entries, info->entryCount, grp_entry_cmp, grp_entry_swap); return(1);} /* grp_load_entries */static DirHandle *GRP_openArchive(const char *name, int forWriting){ GRPinfo *info; DirHandle *retval = malloc(sizeof (DirHandle)); PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name); BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL); info = retval->opaque = malloc(sizeof (GRPinfo)); if (info == NULL) { __PHYSFS_setError(ERR_OUT_OF_MEMORY); goto GRP_openArchive_failed; } /* if */ memset(info, '\0', sizeof (GRPinfo)); info->filename = (char *) malloc(strlen(name) + 1); if (info->filename == NULL) { __PHYSFS_setError(ERR_OUT_OF_MEMORY); goto GRP_openArchive_failed; } /* if */ if (!grp_load_entries(name, forWriting, info)) goto GRP_openArchive_failed; strcpy(info->filename, name); info->last_mod_time = modtime; retval->funcs = &__PHYSFS_DirFunctions_GRP; return(retval);GRP_openArchive_failed: if (retval != NULL) { if (retval->opaque != NULL) { if (info->filename != NULL) free(info->filename); if (info->entries != NULL) free(info->entries); free(info); } /* if */ free(retval); } /* if */ return(NULL);} /* GRP_openArchive */static LinkedStringList *GRP_enumerateFiles(DirHandle *h, const char *dirname, int omitSymLinks){ GRPinfo *info = ((GRPinfo *) h->opaque); GRPentry *entry = info->entries; LinkedStringList *retval = NULL, *p = NULL; PHYSFS_uint32 max = info->entryCount; PHYSFS_uint32 i; /* no directories in GRP files. */ BAIL_IF_MACRO(*dirname != '\0', ERR_NOT_A_DIR, NULL); for (i = 0; i < max; i++, entry++) retval = __PHYSFS_addToLinkedStringList(retval, &p, entry->name, -1); return(retval);} /* GRP_enumerateFiles */static GRPentry *grp_find_entry(GRPinfo *info, const char *name){ char *ptr = strchr(name, '.'); GRPentry *a = info->entries; PHYSFS_sint32 lo = 0; PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1); PHYSFS_sint32 middle; int rc; /* * Rule out filenames to avoid unneeded processing...no dirs, * big filenames, or extensions > 3 chars. */ BAIL_IF_MACRO((ptr) && (strlen(ptr) > 4), ERR_NO_SUCH_FILE, NULL); BAIL_IF_MACRO(strlen(name) > 12, ERR_NO_SUCH_FILE, NULL); BAIL_IF_MACRO(strchr(name, '/') != NULL, ERR_NO_SUCH_FILE, NULL); while (lo <= hi) { middle = lo + ((hi - lo) / 2); rc = strcmp(name, a[middle].name); if (rc == 0) /* found it! */ return(&a[middle]); else if (rc > 0) lo = middle + 1; else hi = middle - 1; } /* while */ BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);} /* grp_find_entry */static int GRP_exists(DirHandle *h, const char *name){ return(grp_find_entry(((GRPinfo *) h->opaque), name) != NULL);} /* GRP_exists */static int GRP_isDirectory(DirHandle *h, const char *name, int *fileExists){ *fileExists = GRP_exists(h, name); return(0); /* never directories in a groupfile. */} /* GRP_isDirectory */static int GRP_isSymLink(DirHandle *h, const char *name, int *fileExists){ *fileExists = GRP_exists(h, name); return(0); /* never symlinks in a groupfile. */} /* GRP_isSymLink */static PHYSFS_sint64 GRP_getLastModTime(DirHandle *h, const char *name, int *fileExists){ GRPinfo *info = ((GRPinfo *) h->opaque); PHYSFS_sint64 retval = -1; *fileExists = (grp_find_entry(info, name) != NULL); if (*fileExists) /* use time of GRP itself in the physical filesystem. */ retval = ((GRPinfo *) h->opaque)->last_mod_time; return(retval);} /* GRP_getLastModTime */static FileHandle *GRP_openRead(DirHandle *h, const char *fnm, int *fileExists){ GRPinfo *info = ((GRPinfo *) h->opaque); FileHandle *retval; GRPfileinfo *finfo; GRPentry *entry; entry = grp_find_entry(info, fnm); *fileExists = (entry != NULL); BAIL_IF_MACRO(entry == NULL, NULL, NULL); retval = (FileHandle *) malloc(sizeof (FileHandle)); BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL); finfo = (GRPfileinfo *) malloc(sizeof (GRPfileinfo)); if (finfo == NULL) { free(retval); BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL); } /* if */ finfo->handle = __PHYSFS_platformOpenRead(info->filename); if ( (finfo->handle == NULL) || (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) ) { free(finfo); free(retval); return(NULL); } /* if */ finfo->curPos = 0; finfo->entry = entry; retval->opaque = (void *) finfo; retval->funcs = &__PHYSFS_FileFunctions_GRP; retval->dirHandle = h; return(retval);} /* GRP_openRead */static FileHandle *GRP_openWrite(DirHandle *h, const char *name){ BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);} /* GRP_openWrite */static FileHandle *GRP_openAppend(DirHandle *h, const char *name){ BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);} /* GRP_openAppend */static int GRP_remove(DirHandle *h, const char *name){ BAIL_MACRO(ERR_NOT_SUPPORTED, 0);} /* GRP_remove */static int GRP_mkdir(DirHandle *h, const char *name){ BAIL_MACRO(ERR_NOT_SUPPORTED, 0);} /* GRP_mkdir */#endif /* defined PHYSFS_SUPPORTS_GRP *//* end of grp.c ... */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -