Skip to content

Commit 11b811f

Browse files
committed
[Perf] (...): Improve performance.
1 parent b835018 commit 11b811f

7 files changed

Lines changed: 1666 additions & 1627 deletions

File tree

src/main/cpp/config/config.cpp

Lines changed: 78 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -12,108 +12,108 @@
1212

1313
#define DEBUG 0
1414

15-
char *DEFAULT_MG_DIRECTORY_PATH = "/sdcard/MG";
15+
char* DEFAULT_MG_DIRECTORY_PATH = "/sdcard/MG";
1616

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;
2121

22-
static cJSON *config_json = NULL;
22+
static cJSON* config_json = NULL;
2323

2424
int initialized = 0;
2525

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;
3131
}
3232

3333
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;
4545
}
4646

4747
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);
6671
fclose(file);
67-
return 0;
68-
}
72+
file_content[file_size] = '\0';
6973

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);
7376

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+
}
7681

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;
8484
}
8585

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+
}
9090

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+
}
9696

97-
return item->valueint;
97+
return item->valueint;
9898
}
9999

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+
}
104104

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+
}
110110

111-
return item->valuestring;
111+
return item->valuestring;
112112
}
113113

114114
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+
}
119119
}

0 commit comments

Comments
 (0)