Skip to content

Commit

Permalink
try to make file io more portable
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 4, 2024
1 parent b77ea65 commit 33a8b89
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions c/src/include/utils.h
Original file line number Diff line number Diff line change
@@ -1,38 +1,72 @@
#pragma once
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <direct.h>
#include <windows.h>
#define PATH_SEPARATOR "\\"
#else
#include <libgen.h>

#include <unistd.h>
#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);
if (strcmp(dir, "/") == 0 || strcmp(dir, ".") == 0) {
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) {
Expand Down

0 comments on commit 33a8b89

Please sign in to comment.