diff --git a/src/text/libs/dir_utils.c b/src/text/libs/dir_utils.c new file mode 100644 index 00000000..3b45d700 --- /dev/null +++ b/src/text/libs/dir_utils.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include "dir_utils.h" + +void combine(char* destination, const char* path1, const char* path2) { + if(path1 == NULL || path2 == NULL) { + strcpy(destination, ""); + } + else if(path2 == NULL || strlen(path2) == 0) { + strcpy(destination, path1); + } + else if(path1 == NULL || strlen(path1) == 0) { + strcpy(destination, path2); + } + else { + char directory_separator[] = "/"; +#ifdef WIN32 + directory_separator[0] = '\\'; +#endif + const char *last_char = path1; + while(*last_char != '\0') + last_char++; + int append_directory_separator = 0; + if(strcmp(last_char, directory_separator) != 0) { + append_directory_separator = 1; + } + strcpy(destination, path1); + if(append_directory_separator) + strcat(destination, directory_separator); + strcat(destination, path2); + } +} \ No newline at end of file diff --git a/src/text/libs/dir_utils.h b/src/text/libs/dir_utils.h new file mode 100644 index 00000000..db0d1be9 --- /dev/null +++ b/src/text/libs/dir_utils.h @@ -0,0 +1,6 @@ +#ifndef DIRUTILS +#define DIRUTILS + +extern void combine(char* destination, const char* path1, const char* path2); + +#endif \ No newline at end of file diff --git a/src/text/text-loader.c b/src/text/text-loader.c index f50eb813..b8c9f96c 100644 --- a/src/text/text-loader.c +++ b/src/text/text-loader.c @@ -5,13 +5,16 @@ #include #include "dialog_ids.h" #include "game/ingame_menu.h" +#include +#include +#include "libs/dir_utils.h" struct DialogEntry* * dialogPool; char* load_file(char const* path) { char* buffer = 0; long length = 0; - FILE * f = fopen (path, "rb"); + FILE * f = fopen (path, "rb"); if (f) { fseek (f, 0, SEEK_END); @@ -22,22 +25,38 @@ char* load_file(char const* path) { fread(buffer, sizeof(char), length, f); } fclose(f); + buffer[length] = '\0'; + return buffer; } - buffer[length] = '\0'; - - return buffer; + return NULL; } void preloadTexts(){ - char * jsonTxt = load_file("res/texts/es.json"); + + char * cwd = "/home/alex/Documents/Projects/Render96/Render96ex - FastBuild/build/us_pc"; + //getcwd(cwd, sizeof(cwd)); + #ifndef WIN32 + char * file = "/res/texts/es.json"; + #else + char * file = "\\res\\texts\\es.json"; + #endif + + char * language_file = malloc((strlen(cwd) + strlen(file)) * sizeof(char)); + strcpy(language_file, ""); + strcat(language_file, cwd); + strcat(language_file, file); + + printf("Loading File: %s\n", language_file); + + char * jsonTxt = load_file( language_file ); + if(jsonTxt == NULL) return; cJSON *json = cJSON_Parse(jsonTxt); if (json == NULL) { const char *error_ptr = cJSON_GetErrorPtr(); - if (error_ptr != NULL) - { + if (error_ptr != NULL) { fprintf(stderr, "Error before: %s\n", error_ptr); - } + } exit(1); } const cJSON *dialog = NULL; @@ -65,16 +84,17 @@ void preloadTexts(){ strcpy(dialogTxt, ""); int currLine = 0; cJSON_ArrayForEach(line, lines) { - char * str = line->valuestring; + char * str = line->valuestring; strcat(dialogTxt, str); if(currLine < lineAmount - 1) { strcat(dialogTxt, "\n"); currLine++; } } - entry->str = getTranslatedText(dialogTxt); - free(dialogTxt); + + entry->str = getTranslatedText(dialogTxt); dialogPool[id] = entry; + free(dialogTxt); } free(jsonTxt); }