void vfs_run_callbacks() { for (int i = 0; i < vfs_global->file_count; i++) { for (int j = 0, j_size = stb_arr_len(vfs_global->file_table[i].read_callbacks); j < j_size; j++) { read_callback_t cbck = vfs_global->file_table[i].read_callbacks[j].fn; cbck(vfs_global->file_table[i].simplename, vfs_global->file_table[i].size, vfs_global->file_table[i].data, vfs_global->file_table[i].read_callbacks[j].userdata); } } }
static void vfs_reload(struct vfs_file *f, int force) { time_t lastChange = stb_ftimestamp(f->name); if (f->lastChange != lastChange || force) { f->file = stb_fopen(f->name, "rb"); if (f->file == 0) { return; } fseek(f->file, 0, SEEK_SET); free(f->data); f->lastChange = lastChange; // Hacky solution to make sure the OS is finished with the fseek call // How can this be solved better? f->size = 0; while (f->size == 0) { f->size = stb_filelen(f->file); } f->data = malloc(f->size); fread(f->data, 1, f->size, f->file); stb_fclose(f->file, 0); for (int j = 0, j_size = stb_arr_len(f->read_callbacks); j < j_size; j++) { read_callback_t cbck = f->read_callbacks[j].fn; cbck(f->simplename, f->size, f->data, f->read_callbacks[j].userdata); } } }
int main(int argc, char **argv) { int w,h; //test_ycbcr(); #if 0 // test hdr asserts for (h=0; h < 100; h += 2) for (w=0; w < 200; ++w) hdr_data[h][w][0] = (float) rand(), hdr_data[h][w][1] = (float) rand(), hdr_data[h][w][2] = (float) rand(); stbi_write_hdr("output/test.hdr", 200,200,3,hdr_data[0][0]); #endif if (argc > 1) { int i, n; for (i=1; i < argc; ++i) { int res; int w2,h2,n2; unsigned char *data; printf("%s\n", argv[i]); res = stbi_info(argv[1], &w2, &h2, &n2); data = stbi_load(argv[i], &w, &h, &n, 4); if (data) free(data); else printf("Failed &n\n"); data = stbi_load(argv[i], &w, &h, 0, 1); if (data) free(data); else printf("Failed 1\n"); data = stbi_load(argv[i], &w, &h, 0, 2); if (data) free(data); else printf("Failed 2\n"); data = stbi_load(argv[i], &w, &h, 0, 3); if (data) free(data); else printf("Failed 3\n"); data = stbi_load(argv[i], &w, &h, &n, 4); assert(data); assert(w == w2 && h == h2 && n == n2); assert(res); if (data) { char fname[512]; stb_splitpath(fname, argv[i], STB_FILE); stbi_write_png(stb_sprintf("output/%s.png", fname), w, h, 4, data, w*4); stbi_write_bmp(stb_sprintf("output/%s.bmp", fname), w, h, 4, data); stbi_write_tga(stb_sprintf("output/%s.tga", fname), w, h, 4, data); stbi_write_png_to_func(dummy_write,0, w, h, 4, data, w*4); stbi_write_bmp_to_func(dummy_write,0, w, h, 4, data); stbi_write_tga_to_func(dummy_write,0, w, h, 4, data); free(data); } else printf("FAILED 4\n"); } } else { int i, nope=0; #ifdef PNGSUITE_PRIMARY char **files = stb_readdir_files("pngsuite/primary"); #else char **files = stb_readdir_files("images"); #endif for (i=0; i < stb_arr_len(files); ++i) { int n; char **failed = NULL; unsigned char *data; printf("."); //printf("%s\n", files[i]); data = stbi_load(files[i], &w, &h, &n, 0); if (data) free(data); else stb_arr_push(failed, "&n"); data = stbi_load(files[i], &w, &h, 0, 1); if (data) free(data); else stb_arr_push(failed, "1"); data = stbi_load(files[i], &w, &h, 0, 2); if (data) free(data); else stb_arr_push(failed, "2"); data = stbi_load(files[i], &w, &h, 0, 3); if (data) free(data); else stb_arr_push(failed, "3"); data = stbi_load(files[i], &w, &h, 0, 4); if (data) ; else stb_arr_push(failed, "4"); if (data) { char fname[512]; #ifdef PNGSUITE_PRIMARY int w2,h2; unsigned char *data2; stb_splitpath(fname, files[i], STB_FILE_EXT); data2 = stbi_load(stb_sprintf("pngsuite/primary_check/%s", fname), &w2, &h2, 0, 4); if (!data2) printf("FAILED: couldn't load 'pngsuite/primary_check/%s\n", fname); else { if (w != w2 || h != w2 || 0 != memcmp(data, data2, w*h*4)) { int x,y,c; if (w == w2 && h == h2) for (y=0; y < h; ++y) for (x=0; x < w; ++x) for (c=0; c < 4; ++c) assert(data[y*w*4+x*4+c] == data2[y*w*4+x*4+c]); printf("FAILED: %s loaded but didn't match PRIMARY_check 32-bit version\n", files[i]); } free(data2); } #else stb_splitpath(fname, files[i], STB_FILE); stbi_write_png(stb_sprintf("output/%s.png", fname), w, h, 4, data, w*4); #endif free(data); } if (failed) { int j; printf("FAILED: "); for (j=0; j < stb_arr_len(failed); ++j) printf("%s ", failed[j]); printf(" -- %s\n", files[i]); } } printf("Tested %d files.\n", i); } return 0; }
void vfs_mount(const char* dir) { if (dir == NULL) { vfs_error("Mount directory is NULL\n"); return; } char path[VFS_MOUNT_PATH_MAX]; strcpy(path, dir); size_t pathlen = strlen(path); if (pathlen == 0) { vfs_error("Mount directory is of length 0\n"); return; } if (path[pathlen - 1] == '\\' || path[pathlen - 1] == '/') { path[pathlen - 1] = '\0'; } char** filenames = stb_readdir_recursive(path, NULL); if (filenames == NULL) { vfs_error("Could not read directory: %s\n", path); return; } int num_new_files = stb_arr_len(filenames); for (int i = 0; i < num_new_files; i++) { struct vfs_file new_file; strcpy(new_file.name, filenames[i]); strcpy(new_file.simplename, filenames[i] + strlen(path) + 1); int replaced = 0; for (int j = 0; j < vfs_global->file_count; j++) { if (strcmp(vfs_global->file_table[j].simplename, new_file.simplename) == 0) { stb_fclose(vfs_global->file_table[j].file, 0); free(vfs_global->file_table[j].data); strcpy(vfs_global->file_table[j].name, new_file.name); vfs_global->file_table[j].file = stb_fopen(vfs_global->file_table[j].name, "rb"); vfs_global->file_table[j].lastChange = stb_ftimestamp(vfs_global->file_table[j].name); vfs_global->file_table[j].size = stb_filelen(vfs_global->file_table[j].file); vfs_global->file_table[j].data = malloc(vfs_global->file_table[j].size); fread(vfs_global->file_table[j].data, 1, vfs_global->file_table[j].size, vfs_global->file_table[j].file); stb_fclose(vfs_global->file_table[i].file, 0); replaced = 1; break; } } if (!replaced) { new_file.read_callbacks = 0; new_file.file = stb_fopen(new_file.name, "rb"); new_file.lastChange = stb_ftimestamp(new_file.name); new_file.size = stb_filelen(new_file.file); new_file.data = malloc(new_file.size); fread(new_file.data, 1, new_file.size, new_file.file); stb_fclose(new_file.file, 0); vfs_global->file_table[vfs_global->file_count] = new_file; vfs_global->file_count++; } } }