gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
gbdk-support/png2hicolorgb/src/path_ops.c
1 // This is free and unencumbered software released into the public domain.
2 // For more information, please refer to <https://unlicense.org>
3 // bbbbbr 2020
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <strings.h>
8 #include <stdbool.h>
9
10 #include "common.h"
11 #include "path_ops.h"
12
13 const char kExtensionSeparator = '.';
14 const char kPathSeparator =
15
16 #ifdef _WIN32
17 #ifndef _WIN32
18 #define __WIN32__
19 #endif
20 #endif
21
22 #ifdef __WIN32__
23 '\\';
24 #else
25 '/';
26 #endif
27
28 const char kPathSeparator_unix = '/';
29
30
31 void filename_replace_extension(char * filename, char * new_ext, size_t maxlen) {
32
33 // Use a temp work string in case out and in filename are the same pointer
34 char temp[MAX_PATH];
35 char ext_sep[2] = {'\0'}; // default to empty string
36
37 // Add leading . to path if needed
38 if (new_ext[0] != kExtensionSeparator) {
39 ext_sep[0] = kExtensionSeparator;
40 ext_sep[1] = '\0';
41 }
42
43 // Strip extension from filename, append new extension
44 filename_remove_extension(filename);
45 snprintf(temp, maxlen, "%s%s%s", filename, ext_sep, new_ext);
46 snprintf(filename, maxlen, "%s", temp);
47 }
48
49
50 void filename_replace_path(char * filename, char * new_path, size_t maxlen) {
51
52 // Use a temp work string in case out and in filename are the same pointer
53 char temp[MAX_PATH];
54 char path_sep[2] = {'\0'}; // default to empty string
55
56 // Add trailing slash to path if needed (Windows needs both for when running under linix like env)
57 #ifdef __WIN32__
58 if (((new_path[(strlen(new_path)-1)] != kPathSeparator)) &&
59 ((new_path[(strlen(new_path)-1)] != kPathSeparator_unix)))
60 #else
61 if ((new_path[(strlen(new_path)-1)] != kPathSeparator))
62 #endif
63 {
64 path_sep[0] = kPathSeparator;
65 path_sep[1] = '\0';
66 }
67
68 // Strip path from path+filename, pre-pend new path
69 snprintf(temp, maxlen, "%s%s%s", new_path, path_sep, get_filename_from_path(filename));
70 snprintf(filename, maxlen, "%s", temp);
71 }
72
73
74 const char * get_filename_from_path(const char * path)
75 {
76 size_t i;
77
78 // Returns string starting at last occurrance of path separator char
79 for(i = strlen(path) - 1; i; i--) {
80
81 // Add trailing slash to path if needed (Windows needs both for when running under linix like env)
82 #ifdef __WIN32__
83 if ((path[i] == kPathSeparator) || (path[i] == kPathSeparator_unix))
84 #else
85 if (path[i] == kPathSeparator)
86 #endif
87 {
88 return &path[i+1];
89 }
90 }
91 return path;
92 }
93
94
95 void filename_remove_extension(char * path)
96 {
97 char * last_ext;
98 char * last_slash;
99
100 // Find the last path separator if present
101 // Starting from here ensures that no path ".." characters
102 // get mistaken as extension delimiters.
103 last_slash = strrchr (path, kExtensionSeparator);
104 if (!last_slash)
105 last_slash = path;
106
107 // Then check to see if there is an extension (starting with the last occurance of '.')
108 // (tries to remove *all* trailing extensions backward until the last slash)
109 last_ext = strrchr (last_slash, kExtensionSeparator);
110 while (last_ext) {
111 if (last_ext != NULL) {
112 // If an extension is found then overwrite it with a string terminator
113 *last_ext = '\0';
114 }
115 last_ext = strrchr (last_slash, kExtensionSeparator);
116 }
117 }
118
119
120 bool get_path_without_filename(const char * path, char * path_only, uint32_t str_max)
121 {
122 size_t i;
123
124 if (strlen(path) + 1 > str_max)
125 return false;
126
127 for(i = strlen(path) - 1; i; i--) {
128
129 // Add trailing slash to path if needed (Windows needs both for when running under linix like env)
130 #ifdef __WIN32__
131 if ((path[i] == kPathSeparator) || (path[i] == kPathSeparator_unix))
132 #else
133 if (path[i] == kPathSeparator)
134 #endif
135 {
136 memcpy(path_only, path, i+1 );
137 path_only[i+1] = '\0';
138 return true;
139 }
140 }
141
142 // memcpy(path_only, path, strlen(path));
143 // No separater found, so no path
144 path_only[0] = '\0';
145 return true;
146 }
147
148
149 // Case insensitive
150 bool matches_extension(char * filename, char * extension) {
151
152 if (strlen(filename) >= strlen(extension)) {
153 char * str_ext = filename + (strlen(filename) - strlen(extension));
154
155 return (strncasecmp(str_ext, extension, strlen(extension)) == 0);
156 }
157 else
158 return false;
159 }
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.