DungeonCrawl
Loading...
Searching...
No Matches
local_handler.c
Go to the documentation of this file.
1
5#include "local_handler.h"
6
7#include "../logging/logger.h"
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12
13#ifdef _WIN32
14 #define PATH_SEP "\\"
15 #define LOCAL_DIRECTORY "resources\\local"
16#else
17 #define PATH_SEP "/"
18 #define LOCAL_DIRECTORY "resources/local"
19#endif//_WIN32
20
21typedef struct observer_node observer_node_t;
22
23typedef struct observer_node {
24 void (*update_func)(void);
25 observer_node_t* next;
26} observer_node_t;
27
28observer_node_t* observer_list = NULL;
29FILE* local_file = NULL;
30local_lang_t current_lang;
31
32int init_local_handler(const local_lang_t lang) {
33 if (local_file != NULL) {
34 log_msg(WARNING, "Local", "Local handler is already initialized.");
35 return 0;
36 }
37
38 current_lang = lang;
39
40 char rel_path[128];
41 snprintf(rel_path, sizeof(rel_path), "%s" PATH_SEP "%s", LOCAL_DIRECTORY, local_file_mapping[lang].file_name);
42
43 local_file = fopen(rel_path, "r");
44 RETURN_WHEN_NULL(local_file, 1, "Local", "Failed to open local file.");
45
46 observer_list = malloc(sizeof(observer_node_t));
47 RETURN_WHEN_NULL(observer_list, 1, "Local", "Failed to allocate memory for observer list.");
48
49 observer_list->update_func = NULL;
50 observer_list->next = NULL;
51 return 0;
52}
53
54char* get_local_string(const char* key) {
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);
58
59 rewind(local_file);
60
61 while (fgets(line, sizeof(line), local_file)) {
62 //if the line is empty or a comment, skip it
63 if (line[0] == '\n' || line[0] == '#') continue;
64
65 if (line[key_len] == '=') {
66 if (strncmp(line, key, key_len) == 0) {
67 // get the starting position
68 char* start = strchr(line, '"');
69 if (!start) continue;
70
71 // get the end position
72 char* end = strchr(start + 1, '"');
73 if (!end) continue;
74
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.");
78
79 snprintf(result, len + 1, "%s", start + 1);
80 return result;
81 }
82 }
83 }
84
85 return strdup(key);
86}
87
88
89int set_language(const local_lang_t lang) {
90 RETURN_WHEN_NULL(local_file, 2, "Local", "Local handler is not initialized.");
91
92 if (lang >= MAX_LANG) {
93 log_msg(WARNING, "Local", "Invalid language: %d.", lang);
94 return 2;
95 }
96 current_lang = lang;
97 fclose(local_file);
98
99 char rel_path[128];
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.");
103
104 // go through the observer list
105 const observer_node_t* current = observer_list;
106 while (current != NULL) {
107 if (current->update_func != NULL) {
108 current->update_func();//call the observer function
109 }
110 current = current->next;
111 }
112
113 return 0;
114}
115
116local_lang_t get_language(void) {
117 if (local_file == NULL) {
118 log_msg(WARNING, "Local", "Local handler is not initialized.");
119 return LANGE_EN;// default to English if not initialized
120 }
121 return current_lang;
122}
123
124void observe_local(void (*update_func)(void)) {
125 RETURN_WHEN_NULL(local_file, , "Local", "Local handler is not initialized.");
126 RETURN_WHEN_NULL(update_func, , "Local", "Invalid observer function.");
127
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.");
130
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;
136 }
137 current->next = new_node;
138}
139
141 // free the observer list
142 observer_node_t* current = observer_list;
143 while (current != NULL) {
144 observer_node_t* next = current->next;
145 free(current);
146 current = next;
147 }
148
149 if (local_file != NULL) fclose(local_file);
150}
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.
Definition logger.c:246
Header file for logging functionality of the game.