Mercurial > repos > blastem
comparison util.c @ 1696:956c1cce05e2 mame_interp
Merge from default
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Thu, 24 Jan 2019 19:15:59 -0800 |
parents | ba3fb7a3be6b |
children | eda8df5bc74c |
comparison
equal
deleted
inserted
replaced
1648:b7ecd0d6a77b | 1696:956c1cce05e2 |
---|---|
535 return path; | 535 return path; |
536 } | 536 } |
537 | 537 |
538 dir_entry *get_dir_list(char *path, size_t *numret) | 538 dir_entry *get_dir_list(char *path, size_t *numret) |
539 { | 539 { |
540 HANDLE dir; | 540 dir_entry *ret; |
541 WIN32_FIND_DATA file; | 541 if (path[0] == PATH_SEP[0] && !path[1]) { |
542 char *pattern = alloc_concat(path, "/*.*"); | 542 int drives = GetLogicalDrives(); |
543 dir = FindFirstFile(pattern, &file); | 543 size_t count = 0; |
544 free(pattern); | 544 for (int i = 0; i < 26; i++) |
545 if (dir == INVALID_HANDLE_VALUE) { | 545 { |
546 if (drives & (1 << i)) { | |
547 count++; | |
548 } | |
549 } | |
550 ret = calloc(count, sizeof(dir_entry)); | |
551 dir_entry *cur = ret; | |
552 for (int i = 0; i < 26; i++) | |
553 { | |
554 if (drives & (1 << i)) { | |
555 cur->name = malloc(4); | |
556 cur->name[0] = 'A' + i; | |
557 cur->name[1] = ':'; | |
558 cur->name[2] = PATH_SEP[0]; | |
559 cur->name[3] = 0; | |
560 cur->is_dir = 1; | |
561 cur++; | |
562 } | |
563 } | |
546 if (numret) { | 564 if (numret) { |
547 *numret = 0; | 565 *numret = count; |
548 } | 566 } |
549 return NULL; | 567 } else { |
550 } | 568 HANDLE dir; |
551 | 569 WIN32_FIND_DATA file; |
552 size_t storage = 64; | 570 char *pattern = alloc_concat(path, "/*.*"); |
553 dir_entry *ret = malloc(sizeof(dir_entry) * storage); | 571 dir = FindFirstFile(pattern, &file); |
554 size_t pos = 0; | 572 free(pattern); |
555 | 573 if (dir == INVALID_HANDLE_VALUE) { |
556 do { | 574 if (numret) { |
557 if (pos == storage) { | 575 *numret = 0; |
558 storage = storage * 2; | 576 } |
559 ret = realloc(ret, sizeof(dir_entry) * storage); | 577 return NULL; |
560 } | 578 } |
561 ret[pos].name = strdup(file.cFileName); | 579 |
562 ret[pos++].is_dir = (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; | 580 size_t storage = 64; |
563 } while (FindNextFile(dir, &file)); | 581 ret = malloc(sizeof(dir_entry) * storage); |
564 | 582 size_t pos = 0; |
565 FindClose(dir); | 583 |
566 if (numret) { | 584 if (path[1] == ':' && (!path[2] || (path[2] == PATH_SEP[0] && !path[3]))) { |
567 *numret = pos; | 585 //we are in the root of a drive, add a virtual .. entry |
586 //for navigating to the virtual root directory | |
587 ret[pos].name = strdup(".."); | |
588 ret[pos++].is_dir = 1; | |
589 } | |
590 | |
591 do { | |
592 if (pos == storage) { | |
593 storage = storage * 2; | |
594 ret = realloc(ret, sizeof(dir_entry) * storage); | |
595 } | |
596 ret[pos].name = strdup(file.cFileName); | |
597 ret[pos++].is_dir = (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; | |
598 } while (FindNextFile(dir, &file)); | |
599 | |
600 FindClose(dir); | |
601 if (numret) { | |
602 *numret = pos; | |
603 } | |
568 } | 604 } |
569 return ret; | 605 return ret; |
570 } | 606 } |
571 | 607 |
572 time_t get_modification_time(char *path) | 608 time_t get_modification_time(char *path) |
837 | 873 |
838 #else | 874 #else |
839 | 875 |
840 char *read_bundled_file(char *name, uint32_t *sizeret) | 876 char *read_bundled_file(char *name, uint32_t *sizeret) |
841 { | 877 { |
842 char *exe_dir = get_exe_dir(); | 878 #ifdef DATA_PATH |
843 if (!exe_dir) { | 879 char *data_dir = DATA_PATH; |
880 #else | |
881 char *data_dir = get_exe_dir(); | |
882 if (!data_dir) { | |
844 if (sizeret) { | 883 if (sizeret) { |
845 *sizeret = -1; | 884 *sizeret = -1; |
846 } | 885 } |
847 return NULL; | 886 return NULL; |
848 } | 887 } |
849 char const *pieces[] = {exe_dir, PATH_SEP, name}; | 888 #endif |
889 char const *pieces[] = {data_dir, PATH_SEP, name}; | |
850 char *path = alloc_concat_m(3, pieces); | 890 char *path = alloc_concat_m(3, pieces); |
851 FILE *f = fopen(path, "rb"); | 891 FILE *f = fopen(path, "rb"); |
852 free(path); | 892 free(path); |
853 if (!f) { | 893 if (!f) { |
854 if (sizeret) { | 894 if (sizeret) { |
889 return NULL; | 929 return NULL; |
890 } | 930 } |
891 | 931 |
892 char const *get_config_dir() | 932 char const *get_config_dir() |
893 { | 933 { |
894 return get_userdata_dir(); | 934 static char* confdir; |
935 if (!confdir) { | |
936 char const *base = get_userdata_dir(); | |
937 if (base) { | |
938 confdir = alloc_concat(base, PATH_SEP "blastem"); | |
939 } | |
940 } | |
941 return confdir; | |
895 } | 942 } |
896 #define CONFIG_PREFIX "" | 943 #define CONFIG_PREFIX "" |
897 #define SAVE_PREFIX "" | 944 #define SAVE_PREFIX "" |
898 | 945 |
899 #else | 946 #else |