forked from LIJI32/SameBoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
50 lines (43 loc) · 999 Bytes
/
utils.c
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
#include <SDL.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "utils.h"
static const char *resource_folder(void)
{
static const char *ret = NULL;
if (!ret) {
ret = SDL_GetBasePath();
if (!ret) {
ret = "./";
}
}
return ret;
}
char *resource_path(const char *filename)
{
static char path[1024];
snprintf(path, sizeof(path), "%s%s", resource_folder(), filename);
#ifdef DATA_DIR
if (access(path, F_OK) == 0) {
return path;
}
snprintf(path, sizeof(path), "%s%s", DATA_DIR, filename);
#endif
return path;
}
void replace_extension(const char *src, size_t length, char *dest, const char *ext)
{
memcpy(dest, src, length);
dest[length] = 0;
/* Remove extension */
for (size_t i = length; i--;) {
if (dest[i] == '/') break;
if (dest[i] == '.') {
dest[i] = 0;
break;
}
}
/* Add new extension */
strcat(dest, ext);
}