GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — sm64tools/utils.c at master · queueRAM/sm64tools Expand file tree/ Copy pathutils.cMore file actions 276 lines (241 loc) · 5.26 KB / Copy pathutils.cFile metadata and controls276 lines (241 loc) · 5.26 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 #include <dirent.h> #include <fcntl.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #if defined(_MSC_VER) || defined(__MINGW32__) #include <io.h> #include <sys/utime.h> #else #include <unistd.h> #include <utime.h> #endif #include "utils.h" // global verbosity setting int g_verbosity = 0; int read_s16_be(unsigned char *buf) { unsigned tmp = read_u16_be(buf); int ret; if (tmp > 0x7FFF) { ret = -((int)0x10000 - (int)tmp); } else { ret = (int)tmp; } return ret; } float read_f32_be(unsigned char *buf) { union {uint32_t i; float f;} ret; ret.i = read_u32_be(buf); return ret.f; } int is_power2(unsigned int val) { while (((val & 1) == 0) && (val > 1)) { val >>= 1; } return (val == 1); } void fprint_hex(FILE *fp, const unsigned char *buf, int length) { int i; for (i = 0; i < length; i++) { fprint_byte(fp, buf[i]); fputc(' ', fp); } } void fprint_hex_source(FILE *fp, const unsigned char *buf, int length) { int i; for (i = 0; i < length; i++) { if (i > 0) fputs(", ", fp); fputs("0x", fp); fprint_byte(fp, buf[i]); } } void print_hex(const unsigned char *buf, int length) { fprint_hex(stdout, buf, length); } void swap_bytes(unsigned char *data, long length) { long i; unsigned char tmp; for (i = 0; i < length; i += 2) { tmp = data[i]; data[i] = data[i+1]; data[i+1] = tmp; } } void reverse_endian(unsigned char *data, long length) { long i; unsigned char tmp; for (i = 0; i < length; i += 4) { tmp = data[i]; data[i] = data[i+3]; data[i+3] = tmp; tmp = data[i+1]; data[i+1] = data[i+2]; data[i+2] = tmp; } } long filesize(const char *filename) { struct stat st; if (stat(filename, &st) == 0) { return st.st_size; } return -1; } void touch_file(const char *filename) { int fd; //fd = open(filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666); fd = open(filename, O_WRONLY|O_CREAT, 0666); if (fd >= 0) { utime(filename, NULL); close(fd); } } long read_file(const char *file_name, unsigned char **data) { FILE *in; unsigned char *in_buf = NULL; long file_size; long bytes_read; in = fopen(file_name, "rb"); if (in == NULL) { return -1; } // allocate buffer to read from offset to end of file fseek(in, 0, SEEK_END); file_size = ftell(in); // sanity check if (file_size > 256*MB) { return -2; } in_buf = malloc(file_size); fseek(in, 0, SEEK_SET); // read bytes bytes_read = fread(in_buf, 1, file_size, in); if (bytes_read != file_size) { return -3; } fclose(in); *data = in_buf; return bytes_read; } long write_file(const char *file_name, unsigned char *data, long length) { FILE *out; long bytes_written; // open output file out = fopen(file_name, "wb"); if (out == NULL) { perror(file_name); return -1; } bytes_written = fwrite(data, 1, length, out); fclose(out); return bytes_written; } void generate_filename(const char *in_name, char *out_name, char *extension) { char tmp_name[FILENAME_MAX]; int len; int i; strcpy(tmp_name, in_name); len = strlen(tmp_name); for (i = len - 1; i > 0; i--) { if (tmp_name[i] == '.') { break; } } if (i <= 0) { i = len; } tmp_name[i] = '\0'; sprintf(out_name, "%s.%s", tmp_name, extension); } char *basename(const char *name) { const char *base = name; while (*name) { if (*name++ == '/') { base = name; } } return (char *)base; } void make_dir(const char *dir_name) { struct stat st = {0}; if (stat(dir_name, &st) == -1) { mkdir(dir_name, 0755); } } long copy_file(const char *src_name, const char *dst_name) { unsigned char *buf; long bytes_written; long bytes_read; bytes_read = read_file(src_name, &buf); if (bytes_read > 0) { bytes_written = write_file(dst_name, buf, bytes_read); if (bytes_written != bytes_read) { bytes_read = -1; } free(buf); } return bytes_read; } void dir_list_ext(const char *dir, const char *extension, dir_list *list) { char *pool; char *pool_ptr; struct dirent *entry; DIR *dfd; int idx; dfd = opendir(dir); if (dfd == NULL) { ERROR("Can't open '%s'\n", dir); exit(1); } pool = malloc(FILENAME_MAX * MAX_DIR_FILES); pool_ptr = pool; idx = 0; while ((entry = readdir(dfd)) != NULL && idx < MAX_DIR_FILES) { if (!extension || str_ends_with(entry->d_name, extension)) { sprintf(pool_ptr, "%s/%s", dir, entry->d_name); list->files[idx] = pool_ptr; pool_ptr += strlen(pool_ptr) + 1; idx++; } } list->count = idx; closedir(dfd); } void dir_list_free(dir_list *list) { // assume first entry in array is allocated if (list->files[0]) { free(list->files[0]); list->files[0] = NULL; } } int str_ends_with(const char *str, const char *suffix) { if (!str || !suffix) { return 0; } size_t len_str = strlen(str); size_t len_suffix = strlen(suffix); if (len_suffix > len_str) { return 0; } return (0 == strncmp(str + len_str - len_suffix, suffix, len_suffix)); } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |