GKeyFile *gnc_state_load (const QofSession *session) { GKeyFile *keyfile = NULL; GError *error = NULL; /* Drop possible previous state_file first */ if (state_file) { g_key_file_free (state_file); state_file = NULL; } gnc_state_set_base (session); if (state_file_name_pre_241) state_file = gnc_key_file_load_from_file (state_file_name_pre_241, TRUE, TRUE, NULL); else if (state_file_name) state_file = gnc_key_file_load_from_file (state_file_name, TRUE, TRUE, NULL); return gnc_state_get_current (); }
void gnc_exp_parser_real_init ( gboolean addPredefined ) { gchar *filename, **keys, **key, *str_value; GKeyFile *key_file; gnc_numeric value; if (parser_inited) gnc_exp_parser_shutdown (); variable_bindings = g_hash_table_new (g_str_hash, g_str_equal); /* This comes after the statics have been initialized. Not at the end! */ parser_inited = TRUE; if ( addPredefined ) { filename = gnc_exp_parser_filname(); key_file = gnc_key_file_load_from_file(filename, TRUE, FALSE, NULL); if (key_file) { keys = g_key_file_get_keys(key_file, GROUP_NAME, NULL, NULL); for (key = keys; key && *key; key++) { str_value = g_key_file_get_string(key_file, GROUP_NAME, *key, NULL); if (str_value && string_to_gnc_numeric(str_value, &value)) { gnc_exp_parser_set_value (*key, gnc_numeric_reduce (value)); } } g_strfreev(keys); g_key_file_free(key_file); } g_free(filename); } }
static void gnc_state_set_base (const QofSession *session) { gchar *basename, *original = NULL, *filename, *file_guid; gchar *sf_extension = NULL, *newstyle_filename = NULL; const gchar *uri; gchar guid_string[GUID_ENCODING_LENGTH+1]; QofBook *book; const GncGUID *guid; GKeyFile *key_file = NULL; gint i; /* Reset filenames possibly found in a previous run */ g_free (state_file_name); g_free (state_file_name_pre_241); state_file_name = NULL; state_file_name_pre_241 = NULL; uri = qof_session_get_url(session); ENTER("session %p (%s)", session, uri ? uri : "(null)"); if (!uri) { LEAVE("no uri, nothing to do"); return; } /* Get the book GncGUID */ book = qof_session_get_book(session); guid = qof_entity_get_guid(QOF_INSTANCE(book)); guid_to_string_buff(guid, guid_string); if (gnc_uri_is_file_uri (uri)) { /* The book_uri is a true file, use its basename. */ gchar *path = gnc_uri_get_path (uri); basename = g_path_get_basename (path); g_free (path); } else { /* The book_uri is composed of database connection parameters. */ gchar* protocol = NULL; gchar* host = NULL; gchar* dbname = NULL; gchar* username = NULL; gchar* password = NULL; gint portnum = 0; gnc_uri_get_components (uri, &protocol, &host, &portnum, &username, &password, &dbname); basename = g_strjoin ("_", protocol, host, username, dbname, NULL); g_free (protocol); g_free (host); g_free (username); g_free (password); g_free (dbname); } DEBUG ("Basename %s", basename); original = gnc_build_book_path (basename); g_free (basename); DEBUG ("Original %s", original); sf_extension = g_strdup (STATE_FILE_EXT); i = 1; while (1) { if (i == 1) filename = g_strconcat (original, sf_extension, NULL); else filename = g_strdup_printf ("%s_%d%s", original, i, sf_extension); DEBUG ("Trying %s", filename); key_file = gnc_key_file_load_from_file (filename, TRUE, FALSE, NULL); DEBUG ("Result %p", key_file); if (!key_file) { DEBUG ("No key file by that name"); if (g_strcmp0 (sf_extension, STATE_FILE_EXT) == 0) { DEBUG ("Trying old state file names for compatibility"); i = 1; g_free (sf_extension); sf_extension = g_strdup (""); /* Regardless of whether or not an old state file is found, * the currently tested name should be used for the future * state file. */ state_file_name = filename; continue; } /* No old style file found. We'll return with the new file name * we set earlier, and no existing key file. */ g_free (filename); break; } file_guid = g_key_file_get_string (key_file, STATE_FILE_TOP, STATE_FILE_BOOK_GUID, NULL); DEBUG ("File GncGUID is %s", file_guid ? file_guid : "<not found>"); if (g_strcmp0 (guid_string, file_guid) == 0) { DEBUG ("Matched !!!"); /* Save the found file for later use. Which name to save to * depends on whether it was an old or new style file name */ if (g_strcmp0 (sf_extension, STATE_FILE_EXT) == 0) state_file_name = filename; else state_file_name_pre_241 = filename; g_free (file_guid); break; } DEBUG ("Clean up this pass"); g_free (file_guid); g_key_file_free (key_file); g_free (filename); i++; } DEBUG("Clean up"); g_free(sf_extension); g_free(original); g_key_file_free (key_file); LEAVE (); }