| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <glib.h> | ||
| 2 | #include <stddef.h> | ||
| 3 | #include "examples/ini_parser.h" | ||
| 4 | |||
| 5 | ✗ | static gchar* config_path() { | |
| 6 | gchar* config_path = | ||
| 7 | ✗ | g_build_filename(g_get_user_config_dir(), ".gittorconfig", NULL); | |
| 8 | ✗ | return config_path; | |
| 9 | } | ||
| 10 | |||
| 11 | ✗ | extern int ini_parse() { | |
| 12 | ✗ | GError* error = NULL; | |
| 13 | ✗ | GKeyFile* keyfile = g_key_file_new(); | |
| 14 | ✗ | gchar* path = config_path(); | |
| 15 | |||
| 16 | // Load existing INI file (create if missing) | ||
| 17 | ✗ | GKeyFileFlags flags = | |
| 18 | G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS; // NOLINT | ||
| 19 | ✗ | g_key_file_load_from_file(keyfile, path, flags, &error); | |
| 20 | ✗ | if (error) { | |
| 21 | ✗ | g_printerr("Error loading file: %s\n", error->message); | |
| 22 | ✗ | g_clear_error(&error); | |
| 23 | |||
| 24 | // Default values | ||
| 25 | ✗ | g_key_file_set_string(keyfile, "user", "name", "Isaac Denning"); | |
| 26 | ✗ | g_key_file_set_string(keyfile, "user", "email", "isaac@example.com"); | |
| 27 | ✗ | g_key_file_set_string(keyfile, "user", "tag", "default"); | |
| 28 | } | ||
| 29 | |||
| 30 | // Read values | ||
| 31 | ✗ | gchar* name = g_key_file_get_string(keyfile, "user", "name", NULL); | |
| 32 | ✗ | gchar* email = g_key_file_get_string(keyfile, "user", "email", NULL); | |
| 33 | ✗ | gchar* tag = g_key_file_get_string(keyfile, "user", "tag", NULL); | |
| 34 | |||
| 35 | ✗ | g_print("Name: %s\n", name ? name : "(none)"); | |
| 36 | ✗ | g_print("Email: %s\n", email ? email : "(none)"); | |
| 37 | ✗ | g_print("Tag: %s\n", tag ? tag : "(none)"); | |
| 38 | |||
| 39 | // Modify values | ||
| 40 | ✗ | g_key_file_set_string(keyfile, "user", "tag", "modify"); | |
| 41 | |||
| 42 | // Export data to string | ||
| 43 | gsize length; | ||
| 44 | ✗ | gchar* data = g_key_file_to_data(keyfile, &length, &error); | |
| 45 | ✗ | if (error) { | |
| 46 | ✗ | g_printerr("Error exporting configuration: %s\n", error->message); | |
| 47 | ✗ | g_clear_error(&error); | |
| 48 | } | ||
| 49 | |||
| 50 | // Save data to disk | ||
| 51 | ✗ | g_file_set_contents(path, data, (gssize)length, &error); | |
| 52 | ✗ | if (error) { | |
| 53 | ✗ | g_printerr("Error saving configuration: %s\n", error->message); | |
| 54 | ✗ | g_clear_error(&error); | |
| 55 | } | ||
| 56 | |||
| 57 | // Clean up | ||
| 58 | ✗ | g_free(path); | |
| 59 | ✗ | g_free(data); | |
| 60 | ✗ | g_free(name); | |
| 61 | ✗ | g_free(email); | |
| 62 | ✗ | g_key_file_unref(keyfile); | |
| 63 | |||
| 64 | ✗ | return 0; | |
| 65 | } | ||
| 66 |