15 #define LOCAL_DIRECTORY "resources\\local"
18 #define LOCAL_DIRECTORY "resources/local"
24 void (*update_func)(void);
25 observer_node_t* next;
28observer_node_t* observer_list = NULL;
29FILE* local_file = NULL;
30local_lang_t current_lang;
33 if (local_file != NULL) {
34 log_msg(WARNING,
"Local",
"Local handler is already initialized.");
41 snprintf(rel_path,
sizeof(rel_path),
"%s" PATH_SEP
"%s", LOCAL_DIRECTORY, local_file_mapping[lang].file_name);
43 local_file = fopen(rel_path,
"r");
44 RETURN_WHEN_NULL(local_file, 1,
"Local",
"Failed to open local file.");
46 observer_list = malloc(
sizeof(observer_node_t));
47 RETURN_WHEN_NULL(observer_list, 1,
"Local",
"Failed to allocate memory for observer list.");
49 observer_list->update_func = NULL;
50 observer_list->next = NULL;
55 RETURN_WHEN_NULL(local_file, NULL,
"Local",
"Local handler is not initialized.");
56 static char line[512];
57 const size_t key_len = strlen(key);
61 while (fgets(line,
sizeof(line), local_file)) {
63 if (line[0] ==
'\n' || line[0] ==
'#')
continue;
65 if (line[key_len] ==
'=') {
66 if (strncmp(line, key, key_len) == 0) {
68 char* start = strchr(line,
'"');
72 char* end = strchr(start + 1,
'"');
75 const size_t len = end - (start + 1);
76 char* result = (
char*) malloc(len + 1);
77 RETURN_WHEN_NULL(result, NULL,
"Local",
"Failed to allocate memory for local string.");
79 snprintf(result, len + 1,
"%s", start + 1);
90 RETURN_WHEN_NULL(local_file, 2,
"Local",
"Local handler is not initialized.");
92 if (lang >= MAX_LANG) {
93 log_msg(WARNING,
"Local",
"Invalid language: %d.", lang);
100 snprintf(rel_path,
sizeof(rel_path),
"%s" PATH_SEP
"%s", LOCAL_DIRECTORY, local_file_mapping[lang].file_name);
101 local_file = fopen(rel_path,
"r");
102 RETURN_WHEN_NULL(local_file, 1,
"Local",
"Failed to open local file.");
105 const observer_node_t* current = observer_list;
106 while (current != NULL) {
107 if (current->update_func != NULL) {
108 current->update_func();
110 current = current->next;
117 if (local_file == NULL) {
118 log_msg(WARNING,
"Local",
"Local handler is not initialized.");
125 RETURN_WHEN_NULL(local_file, ,
"Local",
"Local handler is not initialized.");
126 RETURN_WHEN_NULL(update_func, ,
"Local",
"Invalid observer function.");
128 observer_node_t* new_node = malloc(
sizeof(observer_node_t));
129 RETURN_WHEN_NULL(new_node, ,
"Local",
"Failed to allocate memory for observer node.");
131 new_node->update_func = update_func;
132 new_node->next = NULL;
133 observer_node_t* current = observer_list;
134 while (current->next != NULL) {
135 current = current->next;
137 current->next = new_node;
142 observer_node_t* current = observer_list;
143 while (current != NULL) {
144 observer_node_t* next = current->next;
149 if (local_file != NULL) fclose(local_file);
local_lang_t get_language(void)
Retrieve the currently set language of the local handler.
void shutdown_local_handler(void)
Shut down the local language handler by releasing resources and closing the resource file.
char * get_local_string(const char *key)
Get the localized string for the given key.
int set_language(const local_lang_t lang)
Sets the current language for the local handler and updates all registered observers.
int init_local_handler(const local_lang_t lang)
Initialize the local language handler by setting up the language and opening the corresponding resour...
void observe_local(void(*update_func)(void))
Registers an observer function to be notified of updates from the local handler.
Exposes public functions for the localization handler.
void log_msg(const log_level_t level, const char *module, const char *format,...)
Logs a formatted message with a specified log level and module.
Header file for logging functionality of the game.