diff --git a/c/src/include/utils.h b/c/src/include/utils.h index 80af7286..c987d833 100644 --- a/c/src/include/utils.h +++ b/c/src/include/utils.h @@ -1,10 +1,34 @@ #pragma once #include #include +#ifdef _WIN32 +#include +#include +#define PATH_SEPARATOR "\\" +#else #include - +#include +#define PATH_SEPARATOR "/" +#endif char* get_parent_directory(char* path, const unsigned int levels) { +#ifdef _WIN32 + static char drive[_MAX_DRIVE]; + static char dir[_MAX_DIR]; + _splitpath(path, drive, dir, NULL, NULL); + for (unsigned int i = 0; i < levels; ++i) { + size_t len = strlen(dir); + if (len > 1 && (dir[len - 1] == '\\' || dir[len - 1] == '/')) { + dir[len - 1] = '\0'; + } + char* last_slash = strrchr(dir, '\\'); + if (!last_slash) last_slash = strrchr(dir, '/'); + if (last_slash) *last_slash = '\0'; + } + static char parent_dir[_MAX_PATH]; + snprintf(parent_dir, sizeof(parent_dir), "%s%s", drive, dir); + return parent_dir; +#else char* dir = dirname(path); for (unsigned int i = 0; i < levels; ++i) { dir = dirname(dir); @@ -12,27 +36,37 @@ char* get_parent_directory(char* path, const unsigned int levels) { break; } } - return dir; +#endif } - char *get_data_file(const char *name) { const char* start = __FILE__; - char* absolute_path = realpath(start, NULL); +#ifdef _WIN32 + char absolute_path[MAX_PATH]; + if (!_fullpath(absolute_path, start, MAX_PATH)) { + perror("_fullpath"); + return NULL; + } +#else + char* absolute_path = realpath(start, NULL); if (!absolute_path) { perror("realpath"); + return NULL; } +#endif char* parents_dir = get_parent_directory(absolute_path, 3); const size_t p_len = strlen(parents_dir), name_len = strlen(name); char *file_path = (char *)malloc(p_len + name_len + 8); memcpy(file_path, parents_dir, p_len); - memcpy(file_path + p_len, "/_data/", 7); + memcpy(file_path + p_len, PATH_SEPARATOR "_data" PATH_SEPARATOR, 7); memcpy(file_path + p_len + 7, name, name_len); file_path[p_len + name_len + 7] = 0; FILE* file = fopen(file_path, "r"); +#ifndef _WIN32 free(absolute_path); +#endif // free(parents_dir); free(file_path); if (!file) {