Changed config parsing method from just text to json, with json-c library.

This commit is contained in:
TheDarkBug 2021-04-19 00:01:29 +02:00
parent c802f4041f
commit 9752cfa15c

View file

@ -91,7 +91,6 @@ char user[32], host[256], shell[64], kernel[256], version_name[64], cpu_model[25
// functions definitions, to use them in main() // functions definitions, to use them in main()
int pkgman(); int pkgman();
void parse_json();
void parse_config(); void parse_config();
void get_info(); void get_info();
void list(); void list();
@ -146,7 +145,7 @@ int main(int argc, char *argv[])
break; break;
} }
} }
parse_json(); parse_config();
if ((argc == 1 && ascii_image_flag == 0) || (argc > 1 && ascii_image_flag == 0)) if ((argc == 1 && ascii_image_flag == 0) || (argc > 1 && ascii_image_flag == 0))
print_ascii(); print_ascii();
else if (ascii_image_flag) else if (ascii_image_flag)
@ -155,22 +154,22 @@ int main(int argc, char *argv[])
print_info(); print_info();
} }
void parse_json() void parse_config()
{ // using json-c library to parse the config { // using json-c library to parse the config
// allocating all necessary variables // allocating all necessary variables
char buffer[1024]; char buffer[1024];
char *homedir = getenv("HOME"); char *homedir = getenv("HOME");
config_directory = "./test.json";
struct json_object *parsed_json, *distributon, *image, *ascii, *user, *os, *kernel, *cpu, *gpu, *ram, *resolution, *shell, *pkgs, *uptime, *colors; struct json_object *parsed_json, *distributon, *image, *ascii, *user, *os, *kernel, *cpu, *gpu, *ram, *resolution, *shell, *pkgs, *uptime, *colors;
// opening and reading the config file // opening and reading the config file
FILE *config; FILE *config;
if (config_directory == NULL) if (config_directory == NULL)
config = fopen(strcat(homedir, "/.config/uwufetch/config"), "r"); config = fopen(strcat(homedir, "/.config/uwufetch/config.json"), "r");
else else
config = fopen(config_directory, "r"); config = fopen(config_directory, "r");
if (config == NULL) if (config == NULL)
return; return;
fread(buffer, 1024, 1, config); fread(buffer, 1024, 1, config);
fclose(config); fclose(config);
@ -191,11 +190,12 @@ void parse_json()
json_object_object_get_ex(parsed_json, "uptime", &uptime); json_object_object_get_ex(parsed_json, "uptime", &uptime);
json_object_object_get_ex(parsed_json, "colors", &colors); json_object_object_get_ex(parsed_json, "colors", &colors);
sprintf(version_name, "%s", json_object_get_string(distributon)); if (json_object_get_string(distributon) != NULL)
sprintf(version_name, "%s", json_object_get_string(distributon));
if (sprintf(image_name, "%s", json_object_get_string(image)) == 0) if (sprintf(image_name, "%s", json_object_get_string(image)) == 0)
ascii_image_flag = 1; ascii_image_flag = 1;
else // reset image_name var to avoid errors else // reset image_name var to avoid errors
sprintf(image_name, ""); sprintf(image_name, "%i", 0x0);
ascii_image_flag = !json_object_get_boolean(ascii); ascii_image_flag = !json_object_get_boolean(ascii);
show_user_info = json_object_get_boolean(user); show_user_info = json_object_get_boolean(user);
show_os = json_object_get_boolean(os); show_os = json_object_get_boolean(os);