Mercurial > repos > blastem
comparison util.c @ 955:229c23b3ab73
Switch to storing SRAM/EEPROM and save states in a per-game directory rather than next to the ROM (for SRAM/EEPROM) or in the current working directory (for save states)
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 15 Apr 2016 18:29:39 -0700 |
parents | 252dfd29831d |
children | 1618d3676a35 |
comparison
equal
deleted
inserted
replaced
954:cbc5b39e5518 | 955:229c23b3ab73 |
---|---|
6 #include <stdarg.h> | 6 #include <stdarg.h> |
7 | 7 |
8 #include <sys/types.h> | 8 #include <sys/types.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <unistd.h> | 10 #include <unistd.h> |
11 #include <errno.h> | |
11 | 12 |
12 #ifdef __ANDROID__ | 13 #ifdef __ANDROID__ |
13 #include <android/log.h> | 14 #include <android/log.h> |
14 #define info_puts(msg) __android_log_write(ANDROID_LOG_INFO, "BlastEm", msg) | 15 #define info_puts(msg) __android_log_write(ANDROID_LOG_INFO, "BlastEm", msg) |
15 #define warning_puts(msg) __android_log_write(ANDROID_LOG_WARN, "BlastEm", msg) | 16 #define warning_puts(msg) __android_log_write(ANDROID_LOG_WARN, "BlastEm", msg) |
89 if (!*text) { | 90 if (!*text) { |
90 return text; | 91 return text; |
91 } | 92 } |
92 *text = 0; | 93 *text = 0; |
93 return text+1; | 94 return text+1; |
95 } | |
96 | |
97 char * basename_no_extension(char *path) | |
98 { | |
99 char *lastdot = NULL; | |
100 char *lastslash = NULL; | |
101 char *cur; | |
102 for (cur = path; *cur; cur++) | |
103 { | |
104 if (*cur == '.') { | |
105 lastdot = cur; | |
106 } else if (*cur == '/') { | |
107 lastslash = cur + 1; | |
108 } | |
109 } | |
110 if (!lastdot) { | |
111 lastdot = cur; | |
112 } | |
113 if (!lastslash) { | |
114 lastslash = path; | |
115 } | |
116 char *barename = malloc(lastdot-lastslash+1); | |
117 memcpy(barename, lastslash, lastdot-lastslash); | |
118 barename[lastdot-lastslash] = 0; | |
119 | |
120 return barename; | |
94 } | 121 } |
95 | 122 |
96 uint32_t nearest_pow2(uint32_t val) | 123 uint32_t nearest_pow2(uint32_t val) |
97 { | 124 { |
98 uint32_t ret = 1; | 125 uint32_t ret = 1; |
337 free(list[i].name); | 364 free(list[i].name); |
338 } | 365 } |
339 free(list); | 366 free(list); |
340 } | 367 } |
341 | 368 |
369 int ensure_dir_exists(char *path) | |
370 { | |
371 struct stat st; | |
372 if (stat(path, &st)) { | |
373 if (errno == ENOENT) { | |
374 char *parent = strdup(path); | |
375 char *sep = strrchr(parent, '/'); | |
376 if (sep && sep != parent) { | |
377 *sep = 0; | |
378 if (!ensure_dir_exists(parent)) { | |
379 free(parent); | |
380 return 0; | |
381 } | |
382 free(parent); | |
383 } | |
384 return mkdir(path, 0777) == 0; | |
385 } else { | |
386 char buf[80]; | |
387 strerror_r(errno, buf, sizeof(buf)); | |
388 warning("stat failed with error: %s", buf); | |
389 return 0; | |
390 } | |
391 } | |
392 return S_ISDIR(st.st_mode); | |
393 } | |
394 | |
342 #endif | 395 #endif |
343 | 396 |
344 #ifdef __ANDROID__ | 397 #ifdef __ANDROID__ |
345 | 398 |
346 #include <SDL.h> | 399 #include <SDL.h> |
371 SDL_RWclose(rw); | 424 SDL_RWclose(rw); |
372 return ret; | 425 return ret; |
373 } | 426 } |
374 | 427 |
375 char const *get_config_dir() | 428 char const *get_config_dir() |
429 { | |
430 return SDL_AndroidGetInternalStoragePath(); | |
431 } | |
432 | |
433 char const *get_save_dir() | |
376 { | 434 { |
377 return SDL_AndroidGetInternalStoragePath(); | 435 return SDL_AndroidGetInternalStoragePath(); |
378 } | 436 } |
379 | 437 |
380 #else | 438 #else |
428 } | 486 } |
429 } | 487 } |
430 return confdir; | 488 return confdir; |
431 } | 489 } |
432 | 490 |
433 #endif | 491 char const *get_save_dir() |
492 { | |
493 static char* savedir; | |
494 if (!savedir) { | |
495 char *homedir = get_home_dir(); | |
496 if (homedir) { | |
497 savedir = alloc_concat(homedir, "/.local/share/blastem"); | |
498 } | |
499 } | |
500 return savedir; | |
501 } | |
502 | |
503 #endif |