result_t compress_directory(struct pak_file* pak, struct paki_args* args, const char* subdir) { result_t r; char directory[DH_PATH_MAX]; char filepath[DH_PATH_MAX]; char fullfilepath[DH_PATH_MAX]; strcpy(directory, args->path); if (subdir[0] != 0) { path_join(directory, directory, subdir, NULL); } WIN32_FIND_DATA fdata; char filter[DH_PATH_MAX]; path_join(filter, directory, "*", NULL); HANDLE find_hdl = FindFirstFile(filter, &fdata); if (find_hdl == INVALID_HANDLE_VALUE) { printf(TERM_BOLDRED "Creating pak failed: directory '%s' does not exist.\n" TERM_RESET, directory); return RET_FAIL; } /* read directory recuresively, and compress files into pak */ BOOL fr = TRUE; while (fr) { if (!str_isequal(fdata.cFileName, ".") && !str_isequal(fdata.cFileName, "..")) { filepath[0] = 0; if (subdir[0] != 0) { strcpy(filepath, subdir); } if (!BIT_CHECK(fdata.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY)) { /* put the file into the archive */ path_join(filepath, filepath, fdata.cFileName, NULL); path_join(fullfilepath, directory, fdata.cFileName, NULL); r = archive_put(pak, args, fullfilepath, filepath); if (IS_OK(r) && BIT_CHECK(args->usage, PAKI_USAGE_VERBOSE)) { puts(filepath); } else if (IS_FAIL(r)) { err_sendtolog(FALSE); args->err_cnt ++; } args->file_cnt ++; } else { /* it's a directory, recurse */ path_join(filepath, filepath, fdata.cFileName, NULL); compress_directory(pak, args, filepath); } } fr = FindNextFile(find_hdl, &fdata); } FindClose(find_hdl); return RET_OK; }
result_t compress_directory(struct pak_file* pak, struct paki_args* args, const char* subdir) { result_t r; char directory[DH_PATH_MAX]; char filepath[DH_PATH_MAX]; char fullfilepath[DH_PATH_MAX]; strcpy(directory, args->path); if (subdir[0] != 0) { path_join(directory, path_norm(directory, directory), subdir, NULL); } DIR* dir = opendir(directory); if (dir == NULL) { printf(TERM_BOLDRED "Creating pak failed: directory '%s' does not exist.\n" TERM_RESET, directory); return RET_FAIL; } /* read directory recuresively, and compress files into pak */ struct dirent* ent = readdir(dir); while (ent != NULL) { if (!str_isequal(ent->d_name, ".") && !str_isequal(ent->d_name, "..")) { filepath[0] = 0; if (subdir[0] != 0) strcpy(filepath, subdir); if (ent->d_type != DT_DIR) { // put the file into the archive path_join(filepath, filepath, ent->d_name, NULL); path_join(fullfilepath, directory, ent->d_name, NULL); r = archive_put(pak, args, fullfilepath, filepath); if (IS_OK(r) && BIT_CHECK(args->usage, PAKI_USAGE_VERBOSE)) { puts(filepath); } else if (IS_FAIL(r)) { err_sendtolog(FALSE); args->err_cnt ++; } args->file_cnt ++; } else { // it's a directory, recurse path_join(filepath, filepath, ent->d_name, NULL); compress_directory(pak, args, filepath); } } ent = readdir(dir); } closedir(dir); return RET_OK; }
reshandle_t rs_add_resource(const char* filepath, void* ptr, reshandle_t override_hdl, pfn_unload_res unload_func) { reshandle_t res_hdl = override_hdl; /* if rs_resource handle is overrided, release previous rs_resource and put new one into * current rs_resource slot */ if (res_hdl != INVALID_HANDLE) { struct rs_resource* rss = (struct rs_resource*)g_rs.ress.buffer; struct rs_resource* rs = &rss[GET_INDEX(res_hdl)]; if (rs->hdl != INVALID_HANDLE) rs->unload_func(rs->ptr); rs->ptr = ptr; rs->hdl = res_hdl; rs->unload_func = unload_func; ASSERT(str_isequal(rs->filepath, filepath)); } else { /* just add it to rs_resource database */ res_hdl = rs_add_todb(filepath, ptr, unload_func); if (res_hdl == INVALID_HANDLE) return INVALID_HANDLE; hashtable_chained_add(&g_rs.dict, hash_str(filepath), GET_INDEX(res_hdl)); } return res_hdl; }
bool test_fs_path() { printf("testing paths... "); if(!str_isequal(fs_basename("./file"), "file")) return printf("failed\n"), false; if(!str_isequal(fs_basename("file"), "file")) return printf("failed\n"), false; if(!str_isequal(fs_basename("/usr/local/bin"), "bin")) return printf("failed\n"), false; printf("okay\n"); return true; }
bool test_fs_manip() { char *str; printf("testing simple read/write... "); fs_writestr("testfile", "str-TEST"); str = fs_readstr("testfile"); if(!str_isequal(str, "str-TEST")) return printf("failed\n"), false; mem_free(str); printf("okay\n"); printf("testing file copy... "); fs_copy("copyfile", "testfile"); str = fs_readstr("copyfile"); if(!str_isequal(str, "str-TEST")) return printf("failed\n"), false; mem_free(str); printf("okay\n"); printf("testing file deletion... "); fs_rmfile("testfile"); fs_rmfile("copyfile"); if(fs_exists("testfile") || fs_exists("copyfile")) return printf("failed\n"), false; printf("okay\n"); return true; }
bool test_integer() { char *endptr, buf[64]; struct integer_t *val, *tmp; unsigned int rem; printf("testing integer... "); val = integer_zero(); integer_delete(val); val = integer_zero(); integer_add_uint(&val, 58); if((val->len != 1) || (val->arr[0] != 58)) return printf("failed\n"), false; integer_add_uint(&val, UINT_MAX); if((val->len != 2) || (val->arr[0] != 57) || (val->arr[1] != 1)) return printf("failed\n"), false; integer_delete(val); val = integer_new(14); integer_mul_ushort(&val, 11); if((val->len != 1) || (val->arr[0] != 154)) return printf("failed\n"), false; integer_mul_ushort(&val, 9261); if((val->len != 1) || (val->arr[0] != 1426194)) return printf("failed\n"), false; integer_delete(val); val = integer_new(4194314); rem = integer_div_ushort(&val, 20460); if((rem != 14) || (val->len != 1) || (val->arr[0] != 205)) return printf("failed\n"), false; rem = integer_div_ushort(&val, 3); if((rem != 1) || (val->len != 1) || (val->arr[0] != 68)) return printf("failed\n"), false; integer_delete(val); val = integer_parse("123", &endptr); if((val->len != 1) || val->neg || (val->arr[0] != 123)) return printf("failed\n"), false; integer_delete(val); val = integer_parse("-0x123", &endptr); if((val->len != 1) || !val->neg || (val->arr[0] != 0x123)) return printf("failed\n"), false; integer_delete(val); val = integer_new(78910); str_printf(buf, "%C", integer_chunk(val)); integer_delete(val); if(!str_isequal(buf, "78910")) return printf("failed\n"), false; val = integer_parse("4052555153018976267", NULL); tmp = integer_parse("4656612873077392578125", NULL); integer_add(&val, tmp); str_printf(buf, "%C", integer_chunk(val)); integer_delete(tmp); integer_delete(val); if(!str_isequal(buf, "4660665428230411554392")) return printf("failed\n"), false; val = integer_parse("13", NULL); tmp = integer_parse("28", NULL); integer_add(&val, tmp); str_printf(buf, "%C", integer_chunk(val)); integer_delete(tmp); integer_delete(val); if(!str_isequal(buf, "41")) return printf("failed\n"), false; val = integer_parse("28", NULL); tmp = integer_parse("13", NULL); integer_sub(&val, tmp); str_printf(buf, "%C", integer_chunk(val)); integer_delete(tmp); integer_delete(val); if(!str_isequal(buf, "15")) return printf("failed\n"), false; val = integer_parse("4656612873077392578125", NULL); tmp = integer_parse("4052555153018976267", NULL); integer_sub(&val, tmp); str_printf(buf, "%C", integer_chunk(val)); integer_delete(tmp); integer_delete(val); if(!str_isequal(buf, "4652560317924373601858")) return printf("failed\n"), false; val = integer_parse("483306625", NULL); tmp = integer_parse("-3916345896", NULL); integer_add(&val, tmp); str_printf(buf, "%C", integer_chunk(val)); integer_delete(tmp); integer_delete(val); if(!str_isequal(buf, "-3433039271")) return printf("failed\n"), false; val = integer_parse("483306625", NULL); tmp = integer_parse("-13582478396", NULL); integer_add(&val, tmp); str_printf(buf, "%C", integer_chunk(val)); integer_delete(tmp); integer_delete(val); if(!str_isequal(buf, "-13099171771")) return printf("failed\n"), false; val = integer_new(78910); if(!integer_uint32chk(val)) return printf("failed\n"), false; else if(integer_uint16chk(val)) return printf("failed\n"), false; else if(integer_uint8chk(val)) return printf("failed\n"), false; else if(integer_uint32(val) != 78910) return printf("failed\n"), false; else if(integer_uint16(val) != 13374) return printf("failed\n"), false; else if(integer_uint8(val) != 62) return printf("failed\n"), false; integer_delete(val); val = integer_new(-281); if(!integer_uint16chk(val)) return printf("failed\n"), false; else if(!integer_uint8chk(val)) return printf("failed\n"), false; else if(integer_uint32(val) != 4294967015) return printf("failed\n"), false; else if(integer_uint16(val) != 65255) return printf("failed\n"), false; else if(integer_uint8(val) != 231) return printf("failed %u\n", integer_uint8(val)), false; integer_delete(val); printf("okay\n"); return true; }