comparison util.c @ 1541:f8ef74e7c800

Merged nuklear_ui into default
author Michael Pavone <pavone@retrodev.com>
date Sun, 25 Mar 2018 12:01:49 -0700
parents e94cff9cb625
children 5efeca06d942
comparison
equal deleted inserted replaced
1533:78b7fc03c7c6 1541:f8ef74e7c800
202 size--; 202 size--;
203 } 203 }
204 *(output++) = 0; 204 *(output++) = 0;
205 } 205 }
206 206
207 char *utf16be_to_utf8(uint8_t *buf, uint32_t max_size)
208 {
209 uint8_t *cur = buf;
210 uint32_t converted_size = 0;
211 for (uint32_t i = 0; i < max_size; i++, cur+=2)
212 {
213 uint16_t code = *cur << 16 | cur[1];
214 if (!code) {
215 break;
216 }
217 if (code < 0x80) {
218 converted_size++;
219 } else if (code < 0x800) {
220 converted_size += 2;
221 } else {
222 //TODO: Deal with surrogate pairs
223 converted_size += 3;
224 }
225 }
226 char *out = malloc(converted_size + 1);
227 char *cur_out = out;
228 cur = buf;
229 for (uint32_t i = 0; i < max_size; i++, cur+=2)
230 {
231 uint16_t code = *cur << 16 | cur[1];
232 if (!code) {
233 break;
234 }
235 if (code < 0x80) {
236 *(cur_out++) = code;
237 } else if (code < 0x800) {
238 *(cur_out++) = 0xC0 | code >> 6;
239 *(cur_out++) = 0x80 | (code & 0x3F);
240 } else {
241 //TODO: Deal with surrogate pairs
242 *(cur_out++) = 0xF0 | code >> 12;
243 *(cur_out++) = 0x80 | (code >> 6 & 0x3F);
244 *(cur_out++) = 0x80 | (code & 0x3F);
245 }
246 }
247 *cur_out = 0;
248 return out;
249 }
250
207 char is_path_sep(char c) 251 char is_path_sep(char c)
208 { 252 {
209 #ifdef _WIN32 253 #ifdef _WIN32
210 if (c == '\\') { 254 if (c == '\\') {
211 return 1; 255 return 1;
248 barename[lastdot-lastslash] = 0; 292 barename[lastdot-lastslash] = 0;
249 293
250 return barename; 294 return barename;
251 } 295 }
252 296
253 char *path_extension(char *path) 297 char *path_extension(char const *path)
254 { 298 {
255 char *lastdot = NULL; 299 char const *lastdot = NULL;
256 char *lastslash = NULL; 300 char const *lastslash = NULL;
257 char *cur; 301 char const *cur;
258 for (cur = path; *cur; cur++) 302 for (cur = path; *cur; cur++)
259 { 303 {
260 if (*cur == '.') { 304 if (*cur == '.') {
261 lastdot = cur; 305 lastdot = cur;
262 } else if (is_path_sep(*cur)) { 306 } else if (is_path_sep(*cur)) {
266 if (!lastdot || (lastslash && lastslash > lastdot)) { 310 if (!lastdot || (lastslash && lastslash > lastdot)) {
267 //no extension 311 //no extension
268 return NULL; 312 return NULL;
269 } 313 }
270 return strdup(lastdot+1); 314 return strdup(lastdot+1);
315 }
316
317 uint8_t path_matches_extensions(char *path, char **ext_list, uint32_t num_exts)
318 {
319 char *ext = path_extension(path);
320 if (!ext) {
321 return 0;
322 }
323 uint32_t extidx;
324 for (extidx = 0; extidx < num_exts; extidx++)
325 {
326 if (!strcasecmp(ext, ext_list[extidx])) {
327 return 1;
328 }
329 }
330 return 0;
271 } 331 }
272 332
273 char * path_dirname(char *path) 333 char * path_dirname(char *path)
274 { 334 {
275 char *lastslash = NULL; 335 char *lastslash = NULL;
487 //adjust for difference between Windows and Unix Epoch 547 //adjust for difference between Windows and Unix Epoch
488 wintime -= 11644473600LL; 548 wintime -= 11644473600LL;
489 return (time_t)wintime; 549 return (time_t)wintime;
490 } 550 }
491 551
492 int ensure_dir_exists(char *path) 552 int ensure_dir_exists(const char *path)
493 { 553 {
494 if (CreateDirectory(path, NULL)) { 554 if (CreateDirectory(path, NULL)) {
495 return 1; 555 return 1;
496 } 556 }
497 if (GetLastError() == ERROR_ALREADY_EXISTS) { 557 if (GetLastError() == ERROR_ALREADY_EXISTS) {
641 //Android's Bionic doesn't support the new style so we'll use the old one instead 701 //Android's Bionic doesn't support the new style so we'll use the old one instead
642 return st.st_mtime; 702 return st.st_mtime;
643 #endif 703 #endif
644 } 704 }
645 705
646 int ensure_dir_exists(char *path) 706 int ensure_dir_exists(const char *path)
647 { 707 {
648 struct stat st; 708 struct stat st;
649 if (stat(path, &st)) { 709 if (stat(path, &st)) {
650 if (errno == ENOENT) { 710 if (errno == ENOENT) {
651 char *parent = strdup(path); 711 char *parent = strdup(path);
678 free(list[i].name); 738 free(list[i].name);
679 } 739 }
680 free(list); 740 free(list);
681 } 741 }
682 742
743 static int sort_dir_alpha(const void *a, const void *b)
744 {
745 const dir_entry *da, *db;
746 da = a;
747 db = b;
748 if (da->is_dir != db->is_dir) {
749 return db->is_dir - da->is_dir;
750 }
751 return strcasecmp(((dir_entry *)a)->name, ((dir_entry *)b)->name);
752 }
753
754 void sort_dir_list(dir_entry *list, size_t num_entries)
755 {
756 qsort(list, num_entries, sizeof(dir_entry), sort_dir_alpha);
757 }
758
683 #ifdef __ANDROID__ 759 #ifdef __ANDROID__
684 760
685 #include <SDL.h> 761 #include <SDL.h>
686 char *read_bundled_file(char *name, uint32_t *sizeret) 762 char *read_bundled_file(char *name, uint32_t *sizeret)
687 { 763 {