mirror of
https://github.com/ondra-novak/gates_of_skeldal.git
synced 2025-07-20 13:15:16 -04:00
66 lines
1.9 KiB
C
66 lines
1.9 KiB
C
#include <platform/platform.h>
|
|
#include <libs/event.h>
|
|
#include "globals.h"
|
|
#include "lang.h"
|
|
#include <libs/strlite.h>
|
|
#include <libs/memman.h>
|
|
|
|
|
|
static char *lang_folder = NULL;
|
|
|
|
static void free_lang_folder(void) {
|
|
free(lang_folder);
|
|
}
|
|
|
|
const char *lang_get_folder(void) {
|
|
return lang_folder;
|
|
}
|
|
|
|
void lang_set_folder(const char *path) {
|
|
if (lang_folder == NULL) atexit(free_lang_folder);
|
|
lang_folder = strdup(path);
|
|
}
|
|
void lang_patch_stringtable(TSTR_LIST *lst, const char *object_name, const char *prefix) {
|
|
if (lang_folder == NULL) return;
|
|
const char *fname = set_file_extension(object_name, ".csv");
|
|
fname = concat2(prefix, fname);
|
|
const char *path = build_pathname(2, lang_folder, fname);
|
|
TSTRINGTABLE *st = stringtable_load(path);
|
|
if (!st) return;
|
|
for (int i = 0, cnt = str_count(*lst); i<cnt; ++i) {
|
|
const char *newstr = stringtable_find(st, i, NULL);
|
|
if (newstr) str_replace(lst, i, newstr);
|
|
}
|
|
stringtable_free(st);
|
|
|
|
}
|
|
TSTRINGTABLE *lang_load(const char *object_name) {
|
|
if (lang_folder == NULL) return NULL;
|
|
const char *fname = set_file_extension(object_name, ".csv");
|
|
const char *path = build_pathname(2, lang_folder, fname);
|
|
TSTRINGTABLE *st = stringtable_load(path);
|
|
return st;
|
|
}
|
|
|
|
const char *lang_replace_path_if_exists(const char *file) {
|
|
if (lang_folder == NULL) return NULL;
|
|
const char *path = build_pathname(2, lang_folder, file);
|
|
if (check_file_exists(path)) return path;
|
|
return NULL;
|
|
}
|
|
|
|
|
|
char *lang_load_string(const char *filename) {
|
|
if (lang_folder == NULL) return NULL;
|
|
const char *path = build_pathname(2, lang_folder, filename);
|
|
FILE *f = fopen(path, "r");
|
|
if (f == NULL) return NULL;
|
|
fseek(f, 0, SEEK_END);
|
|
long sz = ftell(f);
|
|
char *trg = getmem(sz+1);
|
|
fseek(f, 0, SEEK_SET);
|
|
fread(trg, 1 , sz, f);
|
|
fclose(f);
|
|
trg[sz] = 0;
|
|
return trg;
|
|
}
|