📄 hog.c
字号:
static int HOG_isArchive(const char *filename, int forWriting){ void *fh; PHYSFS_uint32 fileCount; int retval = hog_open(filename, forWriting, &fh, &fileCount); if (fh != NULL) __PHYSFS_platformClose(fh); return(retval);} /* HOG_isArchive */static int hog_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two){ HOGentry *a = (HOGentry *) _a; return(strcmp(a[one].name, a[two].name));} /* hog_entry_cmp */static void hog_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two){ HOGentry tmp; HOGentry *first = &(((HOGentry *) _a)[one]); HOGentry *second = &(((HOGentry *) _a)[two]); memcpy(&tmp, first, sizeof (HOGentry)); memcpy(first, second, sizeof (HOGentry)); memcpy(second, &tmp, sizeof (HOGentry));} /* hog_entry_swap */static int hog_load_entries(const char *name, int forWriting, HOGinfo *info){ void *fh = NULL; PHYSFS_uint32 fileCount; HOGentry *entry; BAIL_IF_MACRO(!hog_open(name, forWriting, &fh, &fileCount), NULL, 0); info->entryCount = fileCount; info->entries = (HOGentry *) malloc(sizeof (HOGentry) * fileCount); if (info->entries == NULL) { __PHYSFS_platformClose(fh); BAIL_MACRO(ERR_OUT_OF_MEMORY, 0); } /* if */ for (entry = info->entries; fileCount > 0; fileCount--, entry++) { if (__PHYSFS_platformRead(fh, &entry->name, 13, 1) != 1) { __PHYSFS_platformClose(fh); return(0); } /* if */ if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1) { __PHYSFS_platformClose(fh); return(0); } /* if */ entry->size = PHYSFS_swapULE32(entry->size); entry->startPos = __PHYSFS_platformTell(fh); if (entry->startPos == -1) { __PHYSFS_platformClose(fh); return(0); } // Skip over entry if (!__PHYSFS_platformSeek(fh, entry->startPos + entry->size)) { __PHYSFS_platformClose(fh); return(0); } } /* for */ __PHYSFS_platformClose(fh); __PHYSFS_sort(info->entries, info->entryCount, hog_entry_cmp, hog_entry_swap); return(1);} /* hog_load_entries */static DirHandle *HOG_openArchive(const char *name, int forWriting){ HOGinfo *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 (HOGinfo)); if (info == NULL) { __PHYSFS_setError(ERR_OUT_OF_MEMORY); goto HOG_openArchive_failed; } /* if */ memset(info, '\0', sizeof (HOGinfo)); info->filename = (char *) malloc(strlen(name) + 1); if (info->filename == NULL) { __PHYSFS_setError(ERR_OUT_OF_MEMORY); goto HOG_openArchive_failed; } /* if */ if (!hog_load_entries(name, forWriting, info)) goto HOG_openArchive_failed; strcpy(info->filename, name); info->last_mod_time = modtime; retval->funcs = &__PHYSFS_DirFunctions_HOG; return(retval);HOG_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);} /* HOG_openArchive */static LinkedStringList *HOG_enumerateFiles(DirHandle *h, const char *dirname, int omitSymLinks){ HOGinfo *info = ((HOGinfo *) h->opaque); HOGentry *entry = info->entries; LinkedStringList *retval = NULL, *p = NULL; PHYSFS_uint32 max = info->entryCount; PHYSFS_uint32 i; /* no directories in HOG 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);} /* HOG_enumerateFiles */static HOGentry *hog_find_entry(HOGinfo *info, const char *name){ char *ptr = strchr(name, '.'); HOGentry *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 = __PHYSFS_platformStricmp(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);} /* hog_find_entry */static int HOG_exists(DirHandle *h, const char *name){ return(hog_find_entry(((HOGinfo *) h->opaque), name) != NULL);} /* HOG_exists */static int HOG_isDirectory(DirHandle *h, const char *name, int *fileExists){ *fileExists = HOG_exists(h, name); return(0); /* never directories in a groupfile. */} /* HOG_isDirectory */static int HOG_isSymLink(DirHandle *h, const char *name, int *fileExists){ *fileExists = HOG_exists(h, name); return(0); /* never symlinks in a groupfile. */} /* HOG_isSymLink */static PHYSFS_sint64 HOG_getLastModTime(DirHandle *h, const char *name, int *fileExists){ HOGinfo *info = ((HOGinfo *) h->opaque); PHYSFS_sint64 retval = -1; *fileExists = (hog_find_entry(info, name) != NULL); if (*fileExists) /* use time of HOG itself in the physical filesystem. */ retval = ((HOGinfo *) h->opaque)->last_mod_time; return(retval);} /* HOG_getLastModTime */static FileHandle *HOG_openRead(DirHandle *h, const char *fnm, int *fileExists){ HOGinfo *info = ((HOGinfo *) h->opaque); FileHandle *retval; HOGfileinfo *finfo; HOGentry *entry; entry = hog_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 = (HOGfileinfo *) malloc(sizeof (HOGfileinfo)); 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_HOG; retval->dirHandle = h; return(retval);} /* HOG_openRead */static FileHandle *HOG_openWrite(DirHandle *h, const char *name){ BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);} /* HOG_openWrite */static FileHandle *HOG_openAppend(DirHandle *h, const char *name){ BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);} /* HOG_openAppend */static int HOG_remove(DirHandle *h, const char *name){ BAIL_MACRO(ERR_NOT_SUPPORTED, 0);} /* HOG_remove */static int HOG_mkdir(DirHandle *h, const char *name){ BAIL_MACRO(ERR_NOT_SUPPORTED, 0);} /* HOG_mkdir */#endif /* defined PHYSFS_SUPPORTS_HOG *//* end of hog.c ... */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -