Exemplo n.º 1
0
Arquivo: nemu.c Projeto: wareya/gzrt
/* Add a game to the list */
static int
nc_add_game ( char * name )
{
    char ** keys;
    int amount, i;
    struct Game * g = malloc( sizeof(struct Game) );
    g->name = strdup( name );
    g->cheats = NULL;
    
    /* Append to list */
    games = g_list_append( games, g );
    
    /* Load codes */
    amount = atoi( g_key_file_get_value( config, name, "NumCheats", NULL ) );
    
    /* Load each one */
    for( i = 0; i < amount; i++ )
    {
        char buffer[64];
        struct Cheat * cheat = malloc( sizeof(struct Cheat) );
        int count, j;
        
        sprintf( buffer, "CheatName%uCount", i );
        count = atoi( g_key_file_get_value( config, name, buffer, NULL ) );
        cheat->count = count;
        
        /* Allocate mem for codes */
        cheat->codes = malloc( sizeof(struct GSCode) * count );
        
        /* Set the name */
        sprintf( buffer, "CheatName%u", i );
        cheat->name = g_key_file_get_value( config, name, buffer, NULL );
        
        /* Read 'em */
        for( j = 0; j < count; j++ )
        {
            char buffer[64];
            char * result;
            struct GSCode * cur = &cheat->codes[j];
            
            sprintf( buffer, "CheatName%uCode%u", i, j );
            
            if( !(result = g_key_file_get_value( config, name, buffer, NULL )) )
            {
                continue;
            }
            
            sscanf( result, "%08X %04X", &cur->address, &cur->value );
            free( result );
        }
        
        /* Append the cheat */
        g->cheats = g_list_append( g->cheats, cheat );
        
    cont:;
    }
    
    /* Return the ID */
    return g_list_length( games ) - 1;
}
Exemplo n.º 2
0
static gboolean
get_values (GKeyFile *kfile, const gchar *group,
	    gchar **email, gchar **name, size_t *tstamp)
{
	GError *err;

	err = NULL;
	*email = g_key_file_get_value (kfile, group, EMAIL_KEY, &err);
	if (!*email) {
		g_warning ("cannot get e-mail for %s: %s",
			   group, err->message ? err->message: "error");
		if (err)
			g_error_free (err);
		return FALSE;
	}

	*tstamp = (time_t)g_key_file_get_integer (kfile, group, TSTAMP_KEY, &err);
	if (err) {
		g_warning ("cannot get timestamp for %s: %s",
			   group, err->message ? err->message: "error");
		if (err)
			g_error_free (err);
		return FALSE;
	}

	/* name is not required */
	*name = g_key_file_get_value (kfile, group, NAME_KEY, NULL);

	return TRUE;
}
Exemplo n.º 3
0
static gboolean
get_values (GKeyFile *kfile, const gchar *group,
	    gchar **email, gchar **name, gboolean *personal, size_t *tstamp)
{
	GError *err;
	err = NULL;

	do {
		*email = g_key_file_get_value (kfile, group, EMAIL_KEY, &err);
		if (!*email)
			break;

		*tstamp = (time_t)g_key_file_get_integer (kfile, group,
							  TSTAMP_KEY, &err);
		if (err)
			break;
		*personal = g_key_file_get_boolean (kfile, group,
						    PERSONAL_KEY, NULL);
		*name = g_key_file_get_value (kfile, group, NAME_KEY, NULL);


		return TRUE;

	} while (0);

	g_warning ("error getting value for %s: %s",
		   group, err->message ? err->message: "error");
	g_clear_error (&err);

	return FALSE;
}
static void accountrc_decrypt(const gchar *filename)
{
	GKeyFile *kf = g_key_file_new();
	GError *error = NULL;

	if (! g_key_file_load_from_file(kf, filename, 0, &error)) {
		fprintf(stderr, "Failed to open file: %s\n", error->message);
		g_error_free(error);
	} else {
		gsize n_groups, i;
		gchar **groups = g_key_file_get_groups(kf, &n_groups);

		for (i = 0; i < n_groups; i++) {
			gchar *input = g_key_file_get_value(kf, groups[i], "password", NULL);
			gchar *address = g_key_file_get_value(kf, groups[i], "address", NULL);
			gchar *account = g_key_file_get_value(kf, groups[i], "account_name", NULL);
			gchar *output = input ? pass_decrypt(input) : NULL;

			printf("password for %s, %s is \"%s\"\n", account, address, output);
			g_free(input);
			g_free(output);
			g_free(address);
			g_free(account);
		}
		g_strfreev(groups);
	}
	g_key_file_free(kf);
}
Exemplo n.º 5
0
static gboolean
parse_config_file (const char *filename,
                   char **plugins,
                   char **dhcp_client,
                   char ***dns_plugins,
                   char **log_level,
                   char **log_domains,
                   GError **error)
{
	GKeyFile *config;

	config = g_key_file_new ();
	if (!config) {
		g_set_error (error, 0, 0,
		             "Not enough memory to load config file.");
		return FALSE;
	}

	g_key_file_set_list_separator (config, ',');
	if (!g_key_file_load_from_file (config, filename, G_KEY_FILE_NONE, error))
		return FALSE;

	*plugins = g_key_file_get_value (config, "main", "plugins", error);
	if (*error)
		return FALSE;

	*dhcp_client = g_key_file_get_value (config, "main", "dhcp", NULL);
	*dns_plugins = g_key_file_get_string_list (config, "main", "dns", NULL, NULL);

	*log_level = g_key_file_get_value (config, "logging", "level", NULL);
	*log_domains = g_key_file_get_value (config, "logging", "domains", NULL);

	g_key_file_free (config);
	return TRUE;
}
gboolean
_nm_keyfile_a_contains_all_in_b (GKeyFile *kf_a, GKeyFile *kf_b)
{
	gs_strfreev char **groups = NULL;
	guint i, j;

	if (kf_a == kf_b)
		return TRUE;
	if (!kf_a || !kf_b)
		return FALSE;

	groups = g_key_file_get_groups (kf_a, NULL);
	for (i = 0; groups && groups[i]; i++) {
		gs_strfreev char **keys = NULL;

		keys = g_key_file_get_keys (kf_a, groups[i], NULL, NULL);
		if (!keys)
			continue;

		for (j = 0; keys[j]; j++) {
			gs_free char *key_a = g_key_file_get_value (kf_a, groups[i], keys[j], NULL);
			gs_free char *key_b = g_key_file_get_value (kf_b, groups[i], keys[j], NULL);

			if (g_strcmp0 (key_a, key_b) != 0)
				return FALSE;
		}
	}
	return TRUE;
}
Exemplo n.º 7
0
static void
extract_backup_dirs (const gchar *filename,
                     gchar **data_dir,
                     gchar **config_dir)
{
	GKeyFile *key_file;
	GError *error = NULL;

	g_return_if_fail (filename != NULL);
	g_return_if_fail (data_dir != NULL);
	g_return_if_fail (config_dir != NULL);

	key_file = g_key_file_new ();
	g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &error);

	if (error) {
		g_warning ("Failed to read '%s': %s", filename, error->message);
		g_error_free (error);
	} else {
		gchar *tmp;

		tmp = g_key_file_get_value (key_file, "dirs", "data", NULL);
		if (tmp)
			*data_dir = g_shell_quote (tmp);
		g_free (tmp);

		tmp = g_key_file_get_value (key_file, "dirs", "config", NULL);
		if (tmp)
			*config_dir = g_shell_quote (tmp);
		g_free (tmp);
	}

	g_key_file_free (key_file);
}
Exemplo n.º 8
0
static void
extract_backup_data (const gchar *filename,
                     gchar **restored_version,
                     gchar **data_dir,
                     gchar **config_dir)
{
	GKeyFile *key_file;
	GError *error = NULL;

	g_return_if_fail (filename != NULL);
	g_return_if_fail (data_dir != NULL);
	g_return_if_fail (config_dir != NULL);

	key_file = g_key_file_new ();
	g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &error);

	if (error != NULL) {
		g_warning ("Failed to read '%s': %s", filename, error->message);
		g_error_free (error);

	/* This is the current format as of Evolution 3.6. */
	} else if (g_key_file_has_group (key_file, KEY_FILE_GROUP)) {
		gchar *tmp;

		tmp = g_key_file_get_value (
			key_file, KEY_FILE_GROUP, "Version", NULL);
		if (tmp != NULL)
			*restored_version = g_strstrip (g_strdup (tmp));
		g_free (tmp);

		tmp = g_key_file_get_value (
			key_file, KEY_FILE_GROUP, "UserDataDir", NULL);
		if (tmp != NULL)
			*data_dir = g_shell_quote (tmp);
		g_free (tmp);

		tmp = g_key_file_get_value (
			key_file, KEY_FILE_GROUP, "UserConfigDir", NULL);
		if (tmp != NULL)
			*config_dir = g_shell_quote (tmp);
		g_free (tmp);

	/* This is the legacy format with no version information. */
	} else if (g_key_file_has_group (key_file, "dirs")) {
		gchar *tmp;

		tmp = g_key_file_get_value (key_file, "dirs", "data", NULL);
		if (tmp)
			*data_dir = g_shell_quote (tmp);
		g_free (tmp);

		tmp = g_key_file_get_value (key_file, "dirs", "config", NULL);
		if (tmp)
			*config_dir = g_shell_quote (tmp);
		g_free (tmp);
	}

	g_key_file_free (key_file);
}
Exemplo n.º 9
0
static GKeyFile *
preset_open_and_parse_header (GstPreset * preset, const gchar * preset_path,
    guint64 * preset_version)
{
  GKeyFile *in;
  GError *error = NULL;
  gboolean res;
  const gchar *element_name;
  gchar *name;

  in = g_key_file_new ();

  res = g_key_file_load_from_file (in, preset_path,
      G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error);
  if (!res || error != NULL)
    goto load_error;

  /* element type name and preset name must match or we are dealing with a wrong
   * preset file */
  element_name = G_OBJECT_TYPE_NAME (preset);
  name =
      g_key_file_get_value (in, PRESET_HEADER, PRESET_HEADER_ELEMENT_NAME,
      NULL);

  if (!name || strcmp (name, element_name))
    goto wrong_name;

  g_free (name);

  /* get the version now so that the caller can check it */
  if (preset_version) {
    gchar *str =
        g_key_file_get_value (in, PRESET_HEADER, PRESET_HEADER_VERSION, NULL);
    *preset_version = preset_parse_version (str);
    g_free (str);
  }

  return in;

  /* ERRORS */
load_error:
  {
    GST_WARNING_OBJECT (preset, "Unable to read preset file %s: %s",
        preset_path, error->message);
    g_error_free (error);
    g_key_file_free (in);
    return NULL;
  }
wrong_name:
  {
    GST_WARNING_OBJECT (preset,
        "Wrong element name in preset file %s. Expected %s, got %s",
        preset_path, element_name, GST_STR_NULL (name));
    g_free (name);
    g_key_file_free (in);
    return NULL;
  }
}
static GKeyFile *
load_file_and_read_header (const gchar * path, gchar ** targetname,
    gchar ** categoryname, gchar ** description, GError ** error)
{
  GKeyFile *in;
  gboolean res;
  GError *key_error = NULL;

  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  in = g_key_file_new ();

  GST_DEBUG ("path:%s", path);

  res =
      g_key_file_load_from_file (in, path,
      G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &key_error);
  if (!res || key_error != NULL)
    goto load_error;

  key_error = NULL;
  *targetname =
      g_key_file_get_value (in, GST_ENCODING_TARGET_HEADER, "name", &key_error);
  if (!*targetname)
    goto empty_name;

  *categoryname =
      g_key_file_get_value (in, GST_ENCODING_TARGET_HEADER, "category", NULL);
  *description =
      g_key_file_get_value (in, GST_ENCODING_TARGET_HEADER, "description",
      NULL);

  return in;

load_error:
  {
    GST_WARNING ("Unable to read GstEncodingTarget file %s: %s",
        path, key_error->message);
    g_propagate_error (error, key_error);
    g_key_file_free (in);
    return NULL;
  }

empty_name:
  {
    GST_WARNING ("Wrong header in file %s: %s", path, key_error->message);
    g_propagate_error (error, key_error);
    g_key_file_free (in);
    return NULL;
  }
}
static gboolean
_nm_keyfile_equals_ordered (GKeyFile *kf_a, GKeyFile *kf_b)
{
	gs_strfreev char **groups = NULL;
	gs_strfreev char **groups_b = NULL;
	guint i, j;

	if (kf_a == kf_b)
		return TRUE;
	if (!kf_a || !kf_b)
		return FALSE;

	groups = g_key_file_get_groups (kf_a, NULL);
	groups_b = g_key_file_get_groups (kf_b, NULL);
	if (!groups && !groups_b)
		return TRUE;
	if (!groups || !groups_b)
		return FALSE;
	for (i = 0; groups[i] && groups_b[i] && !strcmp (groups[i], groups_b[i]); i++)
		;
	if (groups[i] || groups_b[i])
		return FALSE;

	for (i = 0; groups[i]; i++) {
		gs_strfreev char **keys = NULL;
		gs_strfreev char **keys_b = NULL;

		keys = g_key_file_get_keys (kf_a, groups[i], NULL, NULL);
		keys_b = g_key_file_get_keys (kf_b, groups[i], NULL, NULL);

		if ((!keys) != (!keys_b))
			return FALSE;
		if (!keys)
			continue;

		for (j = 0; keys[j] && keys_b[j] && !strcmp (keys[j], keys_b[j]); j++)
			;
		if (keys[j] || keys_b[j])
			return FALSE;

		for (j = 0; keys[j]; j++) {
			gs_free char *key_a = g_key_file_get_value (kf_a, groups[i], keys[j], NULL);
			gs_free char *key_b = g_key_file_get_value (kf_b, groups[i], keys[j], NULL);

			if (g_strcmp0 (key_a, key_b) != 0)
				return FALSE;
		}
	}
	return TRUE;
}
Exemplo n.º 12
0
/* This function reads the type_c contexts.  */
gboolean 
read_types (tclient_c *tclient)
{
    GKeyFile *keys = 0;
    gint i = 0;
    gchar *buf = 0;
    gint buf_size = 0;

    buf = (gchar *)load_remote_file(tclient, "types.conf");
    if (!buf)
    {
        start_error_dialog(tclient, __FUNCTION__, "Could not load the types file");
        return FALSE;
    }

    buf_size = strlen(buf);

    /* Parse the keys */
    keys = g_key_file_new();
    if(g_key_file_load_from_data(keys, buf, buf_size, G_KEY_FILE_NONE, NULL))
    {
        gchar **group = 0;
        gsize group_size = 0;

        group = g_key_file_get_groups(keys, &group_size);

        /* Go through the groups and create connections for them */
        for (i = 0; i < group_size; i++)
        {
            type_c *type;

            type = g_malloc0 (sizeof (type_c));

            type->alias = g_strdup(group[i]);
            type->program = g_key_file_get_value(keys, group[i], "program", NULL);
            type->arguments = g_key_file_get_value(keys, group[i], "arguments", NULL);
            type->description = g_key_file_get_value(keys, group[i], "description", NULL);

            tclient->connection_type = g_list_append(tclient->connection_type, type);
        }

        g_strfreev(group);
    }

    g_key_file_free(keys);

    return TRUE;
}
Exemplo n.º 13
0
/**
 * gva_nplayers_lookup:
 * @game: the name of a game
 * @max_alternating: return location for the maximum alternating players
 * @max_simultaneous: return location for the maximum simultaneous players
 * @error: return location for a #GError, or %NULL
 *
 * Returns the maximum number of alternating and/or simultaneous players for
 * @game.  If @game only allows alternating players, @max_simultaneous will
 * be zero.  If @game only allows simultaneous players, @max_alternating will
 * be zero.  If @game is listed in the file but the number of players is
 * unknown or cannot be parsed, both @max_alternating and @max_simultaneous
 * will be zero.  In all of these cases the function returns %TRUE.
 *
 * If an error occurs, the function returns %FALSE and sets @error, leaving
 * @max_alternating and @max_simultaneous unaltered.
 *
 * Returns: %TRUE on success, %FALSE if an error occurred
 **/
gboolean
gva_nplayers_lookup (const gchar *game,
                     gint *max_alternating,
                     gint *max_simultaneous,
                     GError **error)
{
        gchar *nplayers;
        gchar *cp;

        nplayers = g_key_file_get_value (keyfile, "NPlayers", game, error);
        if (nplayers == NULL)
                return FALSE;

        if (max_alternating != NULL)
                *max_alternating = 0;

        if (max_simultaneous != NULL)
                *max_simultaneous = 0;

        cp = strchr (nplayers, '/');
        if (cp != NULL)
        {
                *cp++ = '\0';
                nplayers_parse (
                        g_strstrip (cp), max_alternating, max_simultaneous);
        }

        nplayers_parse (
                g_strstrip (nplayers), max_alternating, max_simultaneous);

        return TRUE;
}
Exemplo n.º 14
0
gboolean
mc_config_read_file (mc_config_t * mc_config, const gchar * ini_path)
{
    mc_config_t *tmp_config;
    gchar **groups, **curr_grp;
    gchar **keys, **curr_key;
    gchar *value;

    if (mc_config == NULL) {
        return FALSE;
    }

    tmp_config = mc_config_init (ini_path);
    if (tmp_config == NULL)
        return FALSE;

    groups = mc_config_get_groups (tmp_config, NULL);

    for (curr_grp = groups; *curr_grp != NULL; curr_grp++) {
        keys = mc_config_get_keys (tmp_config, *curr_grp, NULL);
        for (curr_key = keys; *curr_key != NULL; curr_key++) {
            value = g_key_file_get_value (tmp_config->handle, *curr_grp, *curr_key, NULL);
            if (value == NULL)
                continue;

            g_key_file_set_value (mc_config->handle, *curr_grp, *curr_key, value);
            g_free (value);
        }
        g_strfreev (keys);
    }
    g_strfreev (groups);
    mc_config_deinit (tmp_config);
    return TRUE;
}
static void add_properties(GHashTable *ht, GKeyFile *kf,
			   const char *group) {
  g_hash_table_insert(ht,
		      g_strdup(OPENSLIDE_PROPERTY_NAME_VENDOR),
		      g_strdup("hamamatsu"));

  char **keys = g_key_file_get_keys(kf, group, NULL, NULL);
  if (keys == NULL) {
    return;
  }

  for (char **key = keys; *key != NULL; key++) {
    char *value = g_key_file_get_value(kf, group, *key, NULL);
    if (value) {
      g_hash_table_insert(ht,
			  g_strdup_printf("hamamatsu.%s", *key),
			  g_strdup(value));
      g_free(value);
    }
  }

  g_strfreev(keys);

  // this allows openslide.objective-power to have a fractional component
  // but it's better than rounding
  _openslide_duplicate_double_prop(ht, "hamamatsu.SourceLens",
                                   OPENSLIDE_PROPERTY_NAME_OBJECTIVE_POWER);
  // TODO: can we calculate MPP from PhysicalWidth/PhysicalHeight?
}
Exemplo n.º 16
0
static void
moreinfo_handle_normal(GKeyFile * key_file, gchar * group, gchar ** keys)
{
    GtkTreeIter		 parent;
    GtkTreeStore	*store = GTK_TREE_STORE(shell->moreinfo->model);
    gint                 i;

    gtk_tree_store_append(store, &parent, NULL);
    gtk_tree_store_set(store, &parent, INFO_TREE_COL_NAME, group, -1);

    for (i = 0; keys[i]; i++) {
        gchar *key = keys[i];
        GtkTreeIter child;
        gchar *value;

        value = g_key_file_get_value(key_file, group, key, NULL);

        if (g_utf8_validate(key, -1, NULL) && g_utf8_validate(value, -1, NULL)) {
            strend(key, '#');

            gtk_tree_store_append(store, &child, &parent);
            gtk_tree_store_set(store, &child, INFO_TREE_COL_VALUE, value,
                               INFO_TREE_COL_NAME, key, -1);
        }

        g_free(value);
    }
}
Exemplo n.º 17
0
GtkWidget * yui_file_entry_new(GKeyFile * keyfile, const gchar * group, const gchar * key, gint flags, const gchar * label) {
	GtkWidget * entry;
	YuiFileEntry * yfe;
	gchar * entryText;

	entry = GTK_WIDGET(g_object_new(yui_file_entry_get_type(), "spacing", 10,
		"key-file", keyfile, "group", group, "key", key, NULL));
	yfe = YUI_FILE_ENTRY(entry);

	yfe->flags = flags;

	if (label) {
        	gtk_box_pack_start(GTK_BOX(yfe), gtk_label_new_with_mnemonic(label), FALSE, FALSE, 0);
	}

        yfe->entry = gtk_entry_new ();
        gtk_box_pack_start(GTK_BOX(yfe), yfe->entry, TRUE, TRUE, 0);

	if (flags & YUI_FILE_ENTRY_BROWSE) {
	        yfe->button = gtk_button_new_with_mnemonic ("Browse");
        	g_signal_connect(yfe->button, "clicked", G_CALLBACK(yui_file_entry_browse), yfe);
	        gtk_box_pack_start(GTK_BOX(yfe), yfe->button, FALSE, FALSE, 0);
	}

	entryText = g_key_file_get_value(yfe->keyfile, yfe->group, yfe->key, 0);
	if ( !entryText ) entryText = "";
        gtk_entry_set_text(GTK_ENTRY(yfe->entry), entryText );
        g_signal_connect(GTK_ENTRY(yfe->entry), "changed", G_CALLBACK(yui_file_entry_changed), yfe);

	return entry;
}
Exemplo n.º 18
0
static void
translate_keyfile_to_json( const char * old_file, const char * new_file )
{
    tr_benc    dict;
    GKeyFile * keyfile;
    gchar **   keys;
    gsize      i;
    gsize      length;

    static struct pref_entry {
        const char*   oldkey;
        const char*   newkey;
    } renamed[] = {
        { "default-download-directory", "download-dir"             },
        { "encrypted-connections-only", "encryption"               },
        { "listening-port",             "peer-port"                },
        { "nat-traversal-enabled",      "port-forwarding-enabled"  },
        { "open-dialog-folder",         "open-dialog-dir"          },
        { "watch-folder",               "watch-dir"                },
        { "watch-folder-enabled",       "watch-dir-enabled"        }
    };

    keyfile = g_key_file_new( );
    g_key_file_load_from_file( keyfile, old_file, 0, NULL );
    length = 0;
    keys = g_key_file_get_keys( keyfile, "general", &length, NULL );

    tr_bencInitDict( &dict, length );
    for( i = 0; i < length; ++i )
    {
        guint        j;
        const char * key = keys[i];
        gchar *      val = g_key_file_get_value( keyfile, "general", key,
                           NULL );

        for( j = 0; j < G_N_ELEMENTS( renamed ); ++j )
            if( !strcmp( renamed[j].oldkey, key ) )
                key = renamed[j].newkey;

        if( !strcmp( val, "true" ) || !strcmp( val, "false" ) )
            tr_bencDictAddInt( &dict, key, !strcmp( val, "true" ) );
        else
        {
            char * end;
            long   l;
            errno = 0;
            l = strtol( val, &end, 10 );
            if( !errno && end && !*end )
                tr_bencDictAddInt( &dict, key, l );
            else
                tr_bencDictAddStr( &dict, key, val );
        }

        g_free( val );
    }

    g_key_file_free( keyfile );
    tr_bencToFile( &dict, TR_FMT_JSON, new_file );
    tr_bencFree( &dict );
}
void
_nm_keyfile_copy (GKeyFile *dst, GKeyFile *src)
{
	gs_strfreev char **groups = NULL;
	guint g, k;

	groups = g_key_file_get_groups (src, NULL);
	for (g = 0; groups && groups[g]; g++) {
		const char *group = groups[g];
		gs_strfreev char **keys = NULL;

		keys = g_key_file_get_keys (src, group, NULL, NULL);
		if (!keys)
			continue;

		for (k = 0; keys[k]; k++) {
			const char *key = keys[k];
			gs_free char *value = NULL;

			value = g_key_file_get_value (src, group, key, NULL);
			if (value)
				g_key_file_set_value (dst, group, key, value);
			else
				g_key_file_remove_key (dst, group, key, NULL);
		}
	}
}
Exemplo n.º 20
0
/**
 * tp_g_key_file_get_uint64:
 * @key_file: a non-%NULL #GKeyFile
 * @group_name: a non-%NULL group name
 * @key: a non-%NULL key
 * @error: return location for a #GError
 *
 * Returns the value associated with @key under @group_name as an unsigned
 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
 * large positive results without truncation.
 *
 * Returns: the value associated with the key as an unsigned 64-bit integer,
 * or 0 if the key was not found or could not be parsed.
 *
 * Since: 0.7.31
 */
guint64
tp_g_key_file_get_uint64 (GKeyFile *key_file,
                          const gchar *group_name,
                          const gchar *key,
                          GError **error)
{
  gchar *s, *end;
  guint64 v;

  g_return_val_if_fail (key_file != NULL, -1);
  g_return_val_if_fail (group_name != NULL, -1);
  g_return_val_if_fail (key != NULL, -1);

  s = g_key_file_get_value (key_file, group_name, key, error);

  if (s == NULL)
    return 0;

  v = g_ascii_strtoull (s, &end, 10);

  if (*s == '\0' || *end != '\0')
    {
      g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
          "Key '%s' in group '%s' has value '%s' where uint64 was expected",
          key, group_name, s);
      return 0;
    }

  g_free (s);
  return v;
}
Exemplo n.º 21
0
static void add_keys(struct vpn_config_provider *config_provider,
			GKeyFile *keyfile, const char *group)
{
	char **avail_keys;
	gsize nb_avail_keys, i;

	avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
	if (!avail_keys)
		return;

	for (i = 0 ; i < nb_avail_keys; i++) {
		char *value = g_key_file_get_value(keyfile, group,
						avail_keys[i], NULL);
		if (!value) {
			connman_warn("Cannot find value for %s",
							avail_keys[i]);
			continue;
		}

		set_string(config_provider, avail_keys[i], value);
		g_free(value);
	}

	g_strfreev(avail_keys);
}
Exemplo n.º 22
0
/* Load a section from a keyfile to a hash-table
   (replace existing keys in the hash) */
static void
section_to_hash (GKeyFile *file, const gchar *section, GHashTable *hash)
{
    GError *error;
    gchar **keys;
    gint i;

    error = NULL;
    keys = g_key_file_get_keys (file, section, NULL, &error);
    if (error) {
        g_printf ("Error: Couldn't load configuration section %s!\n", section);
    }

    for (i = 0; keys[i] != NULL; i++) {
        gchar *uncompressed_value;

        uncompressed_value = g_key_file_get_value (file, section, keys[i],
                             NULL);
        g_hash_table_insert (hash,
                             g_strdup (keys[i]),
                             g_strcompress (uncompressed_value));
        g_free (uncompressed_value);
    }
    g_strfreev (keys);
}
Exemplo n.º 23
0
static void
pka_config_dump (void)
{
	gchar **groups;
	gchar **keys;
	gchar *key;
	gchar *value;
	gint i;
	gint j;

	g_return_if_fail(config != NULL);

	ENTRY;
	DEBUG(Config, "--- HOST DEFAULTS %s", config_filename);
	groups = g_key_file_get_groups(config, NULL);
	for (i = 0; groups[i]; i++) {
		keys = g_key_file_get_keys(config, groups[i], NULL, NULL);
		for (j = 0; keys[j]; j++) {
			key = g_strdup_printf("%s.%s", groups[i], keys[j]);
			value = g_key_file_get_value(config, groups[i], keys[j], NULL);
			DEBUG(Config, "  %32s = \"%s\"", key, value);
			g_free(key);
			g_free(value);
		}
		g_strfreev(keys);
	}
	g_strfreev(groups);
	EXIT;
}
Exemplo n.º 24
0
/** Reads the HostID from a previously generated configuration file.
 *
 * @note It is the responsibility of the calling function to free the returned host_id
 *
 * @return The string containing the HostID or NULL
 */
void userpref_get_host_id(char **host_id)
{
	gchar *config_file;
	GKeyFile *key_file;
	gchar *loc_host_id;

	config_file =
		g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL);

	/* now parse file to get the HostID */
	key_file = g_key_file_new();
	if (g_key_file_load_from_file(key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL)) {
		loc_host_id = g_key_file_get_value(key_file, "Global", "HostID", NULL);
		if (loc_host_id)
			*host_id = strdup((char *) loc_host_id);
		g_free(loc_host_id);
	}
	g_key_file_free(key_file);
	g_free(config_file);

	if (!*host_id) {
		/* no config, generate host_id */
		*host_id = userpref_generate_host_id();
		userpref_set_host_id(*host_id);
	}

	log_debug_msg("%s: Using %s as HostID\n", __func__, *host_id);
}
Exemplo n.º 25
0
/* the caller must free @value after usage */
static gboolean
gst_preset_default_get_meta (GstPreset * preset, const gchar * name,
    const gchar * tag, gchar ** value)
{
  GKeyFile *presets;
  gchar *key;

  /* get the presets from the type */
  if (!(presets = preset_get_keyfile (preset)))
    goto no_presets;

  key = g_strdup_printf ("_meta/%s", tag);
  *value = g_key_file_get_value (presets, name, key, NULL);
  g_free (key);

  return TRUE;

  /* ERRORS */
no_presets:
  {
    GST_WARNING_OBJECT (preset, "no presets");
    *value = NULL;
    return FALSE;
  }
}
Exemplo n.º 26
0
int main(int argc, char **argv)
{
  GKeyFile *key_file = g_key_file_new();
  GError *err = NULL;

  // load file
  if (!g_key_file_load_from_file(
          key_file,
          "test.ini",
          G_KEY_FILE_KEEP_COMMENTS,
          &err)) {
    printf("%s\n", err->message);
  }

  // read groups
  gchar **groups = g_key_file_get_groups(key_file, NULL);
  printf("%s\n", *groups);
  // get a value
  gchar *name = g_key_file_get_value(key_file,
                                     groups[0],
                                     "user",
                                     &err);
  printf("name: %s\n", name);

  // free memory
  g_strfreev(groups);
  
  return EXIT_SUCCESS;
}
Exemplo n.º 27
0
/**
 * fm_folder_config_get_uint64
 * @fc: a configuration descriptor
 * @key: a key to search
 * @val: (out): location to save the value
 *
 * Returns the value associated with @key as an unsigned integer.
 *
 * Returns: %TRUE if key was found and value is an unsigned integer.
 *
 * Since: 1.2.0
 */
gboolean fm_folder_config_get_uint64(FmFolderConfig *fc, const char *key,
                                     guint64 *val)
{
    GError *error = NULL;
#if GLIB_CHECK_VERSION(2, 26, 0)
    guint64 ret = g_key_file_get_uint64(fc->kf, fc->group, key, &error);
#else
    gchar *s, *end;
    guint64 ret;

    s = g_key_file_get_value(fc->kf, fc->group, key, &error);
#endif
    if (error)
    {
        g_error_free(error);
        return FALSE;
    }
#if !GLIB_CHECK_VERSION(2, 26, 0)
    ret = g_ascii_strtoull(s, &end, 10);
    if (*s == '\0' || *end != '\0')
    {
        g_free(s);
        return FALSE;
    }
    g_free(s);
#endif
    *val = ret;
    return TRUE;
}
Exemplo n.º 28
0
/**
 * @brief Reads key/value pairs (strings) from a GKeyFile into a GHashtable.
 *
 * Will free the GKeyFile.
 *
 * @param gkeyfile GKeyFile to use, will be freed.
 *
 * @return A GHashTable, mirroring the file or NULL in case of an error.
 */
static GHashTable *
hash_table_from_gkeyfile (GKeyFile * gkeyfile)
{
  gchar **keys;
  gchar **keys_it;
  gsize length;
  GHashTable *returntable = NULL;

  if (!gkeyfile)
    return NULL;

  returntable = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);

  keys = g_key_file_get_keys (gkeyfile, GROUP_NONE, &length, NULL);
  keys_it = keys;

  // Add each key / value pair from file
  while (keys_it != NULL && (*keys_it) != NULL)
    {
      char *value =
        g_key_file_get_value (gkeyfile, GROUP_NONE, (*keys_it), NULL);
      g_hash_table_insert (returntable, estrdup (*keys_it), value);
      ++keys_it;
    }

  if (keys != NULL)
    g_strfreev (keys);

  g_key_file_free (gkeyfile);

  return returntable;
}
Exemplo n.º 29
0
void moloch_config_load_includes(char **includes)
{
    int       i, g, k;

    for (i = 0; includes[i]; i++) {
        GKeyFile *keyFile = g_key_file_new();
        GError *error = 0;
        gboolean status = g_key_file_load_from_file(keyFile, includes[i], G_KEY_FILE_NONE, &error);
        if (!status || error) {
            printf("Couldn't load config includes file (%s) %s\n", includes[i], (error?error->message:""));
            exit(1);
        }

        gchar **groups = g_key_file_get_groups (keyFile, NULL);
        for (g = 0; groups[g]; g++) {
            gchar **keys = g_key_file_get_keys (keyFile, groups[g], NULL, NULL);
            for (k = 0; keys[k]; k++) {
                char *value = g_key_file_get_value(keyFile, groups[g], keys[k], NULL);
                if (value && !error) {
                    g_key_file_set_value(molochKeyFile, groups[g], keys[k], value);
                    g_free(value);
                }
            }
            g_strfreev(keys);
        }
        g_strfreev(groups);
        g_key_file_free(keyFile);
    }
}
Exemplo n.º 30
0
void write_item_ex_info( FILE* of, const char* desktop_file )
{
    gsize len;
    GKeyFile* kf = g_key_file_new();
    if( g_key_file_load_from_file( kf, desktop_file, 0, NULL ) )
    {
        char** keys = g_key_file_get_keys( kf, "Desktop Entry", &len, NULL );
        char** key;
        char* val;
        char** vals;
        gsize n_vals;

        for( key = keys; *key; ++key )
        {
            if( g_str_has_prefix( *key, "X-" ) )
            {
                char* val = g_key_file_get_value( kf, "Desktop Entry", *key, NULL );
                fprintf( of, "%s=%s\n", *key, val );
                g_free( val );
            }
        }
        g_strfreev( keys );
    }
    g_key_file_free( kf );
}