DungeonCrawl
Loading...
Searching...
No Matches
local_handler.c File Reference

Implements localization for the game. More...

#include "local_handler.h"
#include "../logging/logger.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Data Structures

struct  observer_node

Macros

#define PATH_SEP   "/"
#define LOCAL_DIRECTORY   "resources/local"

Typedefs

typedef struct observer_node observer_node_t

Functions

int init_local_handler (const local_lang_t lang)
 Initialize the local language handler by setting up the language and opening the corresponding 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.
local_lang_t get_language (void)
 Retrieve the currently set language of the local handler.
void observe_local (void(*update_func)(void))
 Registers an observer function to be notified of updates from the local handler.
void shutdown_local_handler (void)
 Shut down the local language handler by releasing resources and closing the resource file.

Variables

observer_node_tobserver_list = NULL
FILE * local_file = NULL
local_lang_t current_lang

Detailed Description

Implements localization for the game.

Definition in file local_handler.c.

Macro Definition Documentation

◆ LOCAL_DIRECTORY

#define LOCAL_DIRECTORY   "resources/local"

Definition at line 18 of file local_handler.c.

◆ PATH_SEP

#define PATH_SEP   "/"

Definition at line 17 of file local_handler.c.

Typedef Documentation

◆ observer_node_t

Definition at line 21 of file local_handler.c.

Function Documentation

◆ get_language()

local_lang_t get_language ( void )

Retrieve the currently set language of the local handler.

This function returns the current language configured for the local handler. If the local handler is not initialized, it logs a warning message and defaults the language to English.

Returns
the current language as a value of local_lang_t, or LANGE_EN if the handler is not initialized

Definition at line 116 of file local_handler.c.

116 {
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}
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

◆ get_local_string()

char * get_local_string ( const char * key)

Get the localized string for the given key.

Parameters
keythe key for the localized string
Returns
the localized string, if the key is not found, returns the key itself
Note
The caller must free the returned string.

Definition at line 54 of file local_handler.c.

54 {
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}

◆ init_local_handler()

int init_local_handler ( local_lang_t lang)

Initialize the local language handler by setting up the language and opening the corresponding resource file.

This function initializes the handler to use the specified language. It opens the associated language file based on a predefined mapping and prepares the observer list for updates. If the handler is already initialized, a warning is logged and initialization is skipped.

Parameters
langthe language to be set for the local handler
Returns
0 on success, or 1 if the resource file cannot be opened

Definition at line 32 of file local_handler.c.

32 {
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}

◆ observe_local()

void observe_local ( void(* update_func )(void))

Registers an observer function to be notified of updates from the local handler.

This function adds a new observer to the observer list maintained by the local handler. The observer function will be invoked when relevant updates occur. The observer handler must be initialized before calling this function. If the observer function or handler is invalid, the registration is aborted, and an error is logged.

Parameters
update_functhe observer function to be registered for receiving updates

Definition at line 124 of file local_handler.c.

124 {
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}

◆ set_language()

int set_language ( local_lang_t lang)

Sets the current language for the local handler and updates all registered observers.

This function updates the current language setting which determines the active language file to be used. It attempts to close the current language file, opens the new associated language resource file, and subsequently notifies the observer list by invoking their respective update functions. The function ensures the validity of the input language and handles errors related to file operations or uninitialized states.

Parameters
langthe language to be set (e.g., LANGE_EN, LANGE_DE)
Returns
0 on success, 1 if the specified language file cannot be opened, or 2 if the handler is not initialized or the language is invalid

Definition at line 89 of file local_handler.c.

89 {
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}

◆ shutdown_local_handler()

void shutdown_local_handler ( void )

Shut down the local language handler by releasing resources and closing the resource file.

This function deallocates all memory used by the observer list tied to the local handler. It ensures that all dynamically allocated observer nodes are freed to prevent memory leaks. Additionally, it closes the language resource file if it was previously opened.

Proper usage of this function ensures a clean shutdown of the local language handler by releasing occupied resources and leaving no lingering operations.

Definition at line 140 of file local_handler.c.

140 {
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}

Variable Documentation

◆ current_lang

local_lang_t current_lang

Definition at line 30 of file local_handler.c.

◆ local_file

FILE* local_file = NULL

Definition at line 29 of file local_handler.c.

◆ observer_list

observer_node_t* observer_list = NULL

Definition at line 28 of file local_handler.c.