|
12 | 12 |
|
13 | 13 | #define DEBUG 0 |
14 | 14 |
|
15 | | -char *DEFAULT_MG_DIRECTORY_PATH = "/sdcard/MG"; |
| 15 | +char* DEFAULT_MG_DIRECTORY_PATH = "/sdcard/MG"; |
16 | 16 |
|
17 | | -char *mg_directory_path; |
18 | | -char *config_file_path; |
19 | | -char *log_file_path; |
20 | | -char *glsl_cache_file_path; |
| 17 | +char* mg_directory_path; |
| 18 | +char* config_file_path; |
| 19 | +char* log_file_path; |
| 20 | +char* glsl_cache_file_path; |
21 | 21 |
|
22 | | -static cJSON *config_json = NULL; |
| 22 | +static cJSON* config_json = NULL; |
23 | 23 |
|
24 | 24 | int initialized = 0; |
25 | 25 |
|
26 | | -char *concatenate(char *str1, char *str2) { |
27 | | - std::string str = std::string(str1) + str2; |
28 | | - char *result = new char[str.size() + 1]; |
29 | | - strcpy(result, str.c_str()); |
30 | | - return result; |
| 26 | +char* concatenate(char* str1, char* str2) { |
| 27 | + std::string str = std::string(str1) + str2; |
| 28 | + char* result = new char[str.size() + 1]; |
| 29 | + strcpy(result, str.c_str()); |
| 30 | + return result; |
31 | 31 | } |
32 | 32 |
|
33 | 33 | int check_path() { |
34 | | - char *var = getenv("MG_DIR_PATH"); |
35 | | - mg_directory_path = var ? var : DEFAULT_MG_DIRECTORY_PATH; |
36 | | - config_file_path = concatenate(mg_directory_path, "/config.json"); |
37 | | - log_file_path = concatenate(mg_directory_path, "/latest.log"); |
38 | | - glsl_cache_file_path = concatenate(mg_directory_path, "/glsl_cache.tmp"); |
39 | | - |
40 | | - if (mkdir(mg_directory_path, 0755) != 0 && errno != EEXIST) { |
41 | | - LOG_E("Error creating MG directory.\n") |
42 | | - return 0; |
43 | | - } |
44 | | - return 1; |
| 34 | + char* var = getenv("MG_DIR_PATH"); |
| 35 | + mg_directory_path = var ? var : DEFAULT_MG_DIRECTORY_PATH; |
| 36 | + config_file_path = concatenate(mg_directory_path, "/config.json"); |
| 37 | + log_file_path = concatenate(mg_directory_path, "/latest.log"); |
| 38 | + glsl_cache_file_path = concatenate(mg_directory_path, "/glsl_cache.tmp"); |
| 39 | + |
| 40 | + if (mkdir(mg_directory_path, 0755) != 0 && errno != EEXIST) { |
| 41 | + LOG_E("Error creating MG directory.\n") |
| 42 | + return 0; |
| 43 | + } |
| 44 | + return 1; |
45 | 45 | } |
46 | 46 |
|
47 | 47 | int config_refresh() { |
48 | | - LOG_D("MG_DIRECTORY_PATH=%s", mg_directory_path) |
49 | | - LOG_D("CONFIG_FILE_PATH=%s", config_file_path) |
50 | | - LOG_D("LOG_FILE_PATH=%s", log_file_path) |
51 | | - LOG_D("GLSL_CACHE_FILE_PATH=%s", glsl_cache_file_path) |
52 | | - |
53 | | - FILE *file = fopen(config_file_path, "r"); |
54 | | - if (file == NULL) { |
55 | | - LOG_E("Unable to open config file %s", config_file_path); |
56 | | - return 0; |
57 | | - } |
58 | | - |
59 | | - fseek(file, 0, SEEK_END); |
60 | | - long file_size = ftell(file); |
61 | | - fseek(file, 0, SEEK_SET); |
62 | | - |
63 | | - char *file_content = (char *)malloc(file_size + 1); |
64 | | - if (file_content == NULL) { |
65 | | - LOG_E("Unable to allocate memory for file content"); |
| 48 | + LOG_D("MG_DIRECTORY_PATH=%s", mg_directory_path) |
| 49 | + LOG_D("CONFIG_FILE_PATH=%s", config_file_path) |
| 50 | + LOG_D("LOG_FILE_PATH=%s", log_file_path) |
| 51 | + LOG_D("GLSL_CACHE_FILE_PATH=%s", glsl_cache_file_path) |
| 52 | + |
| 53 | + FILE* file = fopen(config_file_path, "r"); |
| 54 | + if (file == NULL) { |
| 55 | + LOG_E("Unable to open config file %s", config_file_path); |
| 56 | + return 0; |
| 57 | + } |
| 58 | + |
| 59 | + fseek(file, 0, SEEK_END); |
| 60 | + long file_size = ftell(file); |
| 61 | + fseek(file, 0, SEEK_SET); |
| 62 | + |
| 63 | + char* file_content = (char*)malloc(file_size + 1); |
| 64 | + if (file_content == NULL) { |
| 65 | + LOG_E("Unable to allocate memory for file content"); |
| 66 | + fclose(file); |
| 67 | + return 0; |
| 68 | + } |
| 69 | + |
| 70 | + fread(file_content, 1, file_size, file); |
66 | 71 | fclose(file); |
67 | | - return 0; |
68 | | - } |
| 72 | + file_content[file_size] = '\0'; |
69 | 73 |
|
70 | | - fread(file_content, 1, file_size, file); |
71 | | - fclose(file); |
72 | | - file_content[file_size] = '\0'; |
| 74 | + config_json = cJSON_Parse(file_content); |
| 75 | + free(file_content); |
73 | 76 |
|
74 | | - config_json = cJSON_Parse(file_content); |
75 | | - free(file_content); |
| 77 | + if (config_json == NULL) { |
| 78 | + LOG_E("Error parsing config JSON: %s\n", cJSON_GetErrorPtr()); |
| 79 | + return 0; |
| 80 | + } |
76 | 81 |
|
77 | | - if (config_json == NULL) { |
78 | | - LOG_E("Error parsing config JSON: %s\n", cJSON_GetErrorPtr()); |
79 | | - return 0; |
80 | | - } |
81 | | - |
82 | | - initialized = 1; |
83 | | - return 1; |
| 82 | + initialized = 1; |
| 83 | + return 1; |
84 | 84 | } |
85 | 85 |
|
86 | | -int config_get_int(char *name) { |
87 | | - if (config_json == NULL) { |
88 | | - return -1; |
89 | | - } |
| 86 | +int config_get_int(char* name) { |
| 87 | + if (config_json == NULL) { |
| 88 | + return -1; |
| 89 | + } |
90 | 90 |
|
91 | | - cJSON *item = cJSON_GetObjectItem(config_json, name); |
92 | | - if (item == NULL || !cJSON_IsNumber(item)) { |
93 | | - LOG_D("Config item '%s' not found or not an integer.\n", name); |
94 | | - return -1; |
95 | | - } |
| 91 | + cJSON* item = cJSON_GetObjectItem(config_json, name); |
| 92 | + if (item == NULL || !cJSON_IsNumber(item)) { |
| 93 | + LOG_D("Config item '%s' not found or not an integer.\n", name); |
| 94 | + return -1; |
| 95 | + } |
96 | 96 |
|
97 | | - return item->valueint; |
| 97 | + return item->valueint; |
98 | 98 | } |
99 | 99 |
|
100 | | -char *config_get_string(char *name) { |
101 | | - if (config_json == NULL) { |
102 | | - return NULL; |
103 | | - } |
| 100 | +char* config_get_string(char* name) { |
| 101 | + if (config_json == NULL) { |
| 102 | + return NULL; |
| 103 | + } |
104 | 104 |
|
105 | | - cJSON *item = cJSON_GetObjectItem(config_json, name); |
106 | | - if (item == NULL || !cJSON_IsString(item)) { |
107 | | - LOG_D("Config item '%s' not found or not a string.\n", name); |
108 | | - return ""; |
109 | | - } |
| 105 | + cJSON* item = cJSON_GetObjectItem(config_json, name); |
| 106 | + if (item == NULL || !cJSON_IsString(item)) { |
| 107 | + LOG_D("Config item '%s' not found or not a string.\n", name); |
| 108 | + return ""; |
| 109 | + } |
110 | 110 |
|
111 | | - return item->valuestring; |
| 111 | + return item->valuestring; |
112 | 112 | } |
113 | 113 |
|
114 | 114 | void config_cleanup() { |
115 | | - if (config_json != NULL) { |
116 | | - cJSON_Delete(config_json); |
117 | | - config_json = NULL; |
118 | | - } |
| 115 | + if (config_json != NULL) { |
| 116 | + cJSON_Delete(config_json); |
| 117 | + config_json = NULL; |
| 118 | + } |
119 | 119 | } |
0 commit comments