2009-04-20 16:20:40 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2012-11-23 22:43:00 -08:00
|
|
|
#include "efreet_alloca.h"
|
2010-11-25 15:48:59 -08:00
|
|
|
|
2009-04-20 16:20:40 -07:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2010-02-08 11:27:58 -08:00
|
|
|
#include <Ecore_File.h>
|
|
|
|
|
2011-02-05 11:05:55 -08:00
|
|
|
/* define macros and variable for using the eina logging system */
|
2009-09-21 09:36:29 -07:00
|
|
|
#define EFREET_MODULE_LOG_DOM _efreet_ini_log_dom
|
|
|
|
static int _efreet_ini_log_dom = -1;
|
|
|
|
|
2011-02-05 11:05:55 -08:00
|
|
|
#include "Efreet.h"
|
|
|
|
#include "efreet_private.h"
|
|
|
|
|
2008-12-17 07:33:43 -08:00
|
|
|
static Eina_Hash *efreet_ini_parse(const char *file);
|
2010-08-04 05:33:48 -07:00
|
|
|
static const char *efreet_ini_unescape(const char *str) EINA_ARG_NONNULL(1);
|
2008-12-17 07:33:43 -08:00
|
|
|
static Eina_Bool
|
|
|
|
efreet_ini_section_save(const Eina_Hash *hash, const void *key, void *data, void *fdata);
|
|
|
|
static Eina_Bool
|
|
|
|
efreet_ini_value_save(const Eina_Hash *hash, const void *key, void *data, void *fdata);
|
2007-02-03 05:05:30 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
* @return Returns > 0 on success or 0 on failure
|
|
|
|
* @brief Initialize the Ini parser subsystem
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
efreet_ini_init(void)
|
|
|
|
{
|
2010-10-07 15:22:33 -07:00
|
|
|
_efreet_ini_log_dom = eina_log_domain_register
|
2011-04-12 15:42:01 -07:00
|
|
|
("efreet_ini", EFREET_DEFAULT_LOG_COLOR);
|
2009-10-09 23:19:13 -07:00
|
|
|
if (_efreet_ini_log_dom < 0)
|
|
|
|
{
|
2011-04-12 15:42:01 -07:00
|
|
|
EINA_LOG_ERR("Efreet: Could not create a log domain for efreet_ini");
|
2010-02-04 01:12:35 -08:00
|
|
|
return 0;
|
2009-10-09 23:19:13 -07:00
|
|
|
}
|
|
|
|
return 1;
|
2007-02-03 05:05:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
* @returns the number of initializations left for this system
|
|
|
|
* @brief Attempts to shut down the subsystem if nothing else is using it
|
|
|
|
*/
|
2009-10-09 23:19:13 -07:00
|
|
|
void
|
2007-02-03 05:05:30 -08:00
|
|
|
efreet_ini_shutdown(void)
|
|
|
|
{
|
2009-09-21 09:36:29 -07:00
|
|
|
eina_log_domain_unregister(_efreet_ini_log_dom);
|
2011-02-05 11:06:55 -08:00
|
|
|
_efreet_ini_log_dom = -1;
|
2007-02-03 05:05:30 -08:00
|
|
|
}
|
|
|
|
|
2007-11-04 01:32:35 -08:00
|
|
|
EAPI Efreet_Ini *
|
2007-02-03 05:05:30 -08:00
|
|
|
efreet_ini_new(const char *file)
|
|
|
|
{
|
|
|
|
Efreet_Ini *ini;
|
|
|
|
|
|
|
|
ini = NEW(Efreet_Ini, 1);
|
|
|
|
if (!ini) return NULL;
|
|
|
|
|
2008-05-08 07:22:21 -07:00
|
|
|
/* This can validly be NULL at the moment as _parse() will return NULL
|
|
|
|
* if the input file doesn't exist. Should we change _parse() to create
|
|
|
|
* the hash and only return NULL on failed parse? */
|
2007-02-03 05:05:30 -08:00
|
|
|
ini->data = efreet_ini_parse(file);
|
|
|
|
|
|
|
|
return ini;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
* @param file The file to parse
|
2008-12-17 07:33:43 -08:00
|
|
|
* @return Returns an Eina_Hash with the contents of @a file, or NULL if the
|
2008-05-08 07:22:21 -07:00
|
|
|
* file fails to parse or if the file doesn't exist
|
2008-12-17 07:33:43 -08:00
|
|
|
* @brief Parses the ini file @a file into an Eina_Hash
|
2007-02-03 05:05:30 -08:00
|
|
|
*/
|
2008-12-17 07:33:43 -08:00
|
|
|
static Eina_Hash *
|
2007-02-03 05:05:30 -08:00
|
|
|
efreet_ini_parse(const char *file)
|
|
|
|
{
|
2012-10-19 16:33:05 -07:00
|
|
|
Eina_Hash *data = NULL, *section = NULL;
|
|
|
|
Eina_Iterator *it = NULL;
|
2012-10-18 04:19:28 -07:00
|
|
|
Eina_File_Line *line;
|
2012-10-19 16:33:05 -07:00
|
|
|
Eina_File *f;
|
2009-06-19 23:50:01 -07:00
|
|
|
|
2012-10-18 04:19:28 -07:00
|
|
|
f = eina_file_open(file, EINA_FALSE);
|
2012-10-19 16:33:05 -07:00
|
|
|
if (!f)
|
|
|
|
return NULL;
|
2007-09-06 09:05:35 -07:00
|
|
|
|
2012-10-18 04:19:28 -07:00
|
|
|
data = eina_hash_string_small_new(EINA_FREE_CB(eina_hash_free));
|
|
|
|
if (!data) goto error;
|
2008-05-06 08:56:40 -07:00
|
|
|
|
2011-07-04 02:35:39 -07:00
|
|
|
/* let's make mmap safe and just get 0 pages for IO erro */
|
|
|
|
eina_mmap_safety_enabled_set(EINA_TRUE);
|
2012-05-23 13:10:27 -07:00
|
|
|
|
2012-10-18 04:19:28 -07:00
|
|
|
it = eina_file_map_lines(f);
|
|
|
|
if (!it) goto error;
|
|
|
|
EINA_ITERATOR_FOREACH(it, line)
|
2008-05-06 08:56:40 -07:00
|
|
|
{
|
2012-10-18 04:19:28 -07:00
|
|
|
const char *eq;
|
|
|
|
unsigned int start = 0;
|
|
|
|
|
|
|
|
/* skip empty lines */
|
|
|
|
if (line->length == 0) continue;
|
|
|
|
/* skip white space at start of line */
|
|
|
|
while ((start < line->length) && (isspace((unsigned char)line->start[start])))
|
|
|
|
start++;
|
|
|
|
/* skip empty lines */
|
|
|
|
if (start == line->length) continue;
|
|
|
|
/* skip comments */
|
|
|
|
if (line->start[start] == '#') continue;
|
2007-02-03 05:05:30 -08:00
|
|
|
|
|
|
|
/* new section */
|
2012-10-18 04:19:28 -07:00
|
|
|
if (line->start[start] == '[')
|
2007-02-03 05:05:30 -08:00
|
|
|
{
|
2012-10-18 04:19:28 -07:00
|
|
|
const char *head_start;
|
|
|
|
const char *head_end;
|
2008-05-06 08:56:40 -07:00
|
|
|
|
2012-10-18 04:19:28 -07:00
|
|
|
head_start = &(line->start[start]) + 1;
|
|
|
|
head_end = memchr(line->start, ']', line->length);
|
2007-02-03 05:05:30 -08:00
|
|
|
|
2012-10-18 04:19:28 -07:00
|
|
|
if (head_end)
|
2007-02-03 05:05:30 -08:00
|
|
|
{
|
2012-10-18 04:19:28 -07:00
|
|
|
char *header;
|
|
|
|
size_t len;
|
2008-05-06 08:56:40 -07:00
|
|
|
|
2012-10-18 04:19:28 -07:00
|
|
|
len = head_end - head_start + 1;
|
|
|
|
header = alloca(len);
|
2008-05-06 21:14:45 -07:00
|
|
|
|
2012-10-18 04:19:28 -07:00
|
|
|
memcpy(header, head_start, len - 1);
|
|
|
|
header[len - 1] = '\0';
|
2008-05-06 08:56:40 -07:00
|
|
|
|
2010-04-01 12:41:03 -07:00
|
|
|
section = eina_hash_string_small_new(EINA_FREE_CB(eina_stringshare_del));
|
2007-02-03 05:05:30 -08:00
|
|
|
|
2010-04-01 12:41:03 -07:00
|
|
|
eina_hash_del_by_key(data, header);
|
2008-12-17 07:33:43 -08:00
|
|
|
eina_hash_add(data, header, section);
|
2007-02-03 05:05:30 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* invalid file - skip line? or refuse to parse file? */
|
|
|
|
/* just printf for now till we figure out what to do */
|
2011-03-18 12:47:57 -07:00
|
|
|
// ERR("Invalid file (%s) (missing ] on group name)", file);
|
2007-02-03 05:05:30 -08:00
|
|
|
}
|
2012-10-18 04:19:28 -07:00
|
|
|
continue;
|
2007-02-03 05:05:30 -08:00
|
|
|
}
|
|
|
|
|
2010-08-21 06:52:25 -07:00
|
|
|
if (!section)
|
2007-02-03 05:05:30 -08:00
|
|
|
{
|
2011-04-12 15:42:15 -07:00
|
|
|
INF("Invalid file (%s) (missing section)", file);
|
|
|
|
goto error;
|
2008-05-06 08:56:40 -07:00
|
|
|
}
|
|
|
|
|
2012-10-18 04:19:28 -07:00
|
|
|
eq = memchr(line->start, '=', line->length);
|
2008-05-06 21:14:45 -07:00
|
|
|
|
2012-10-18 04:19:28 -07:00
|
|
|
if (eq)
|
2008-05-06 08:56:40 -07:00
|
|
|
{
|
2012-10-18 04:19:28 -07:00
|
|
|
const char *key_start, *key_end;
|
|
|
|
const char *value_start, *value_end;
|
2010-08-04 05:33:58 -07:00
|
|
|
char *key, *value;
|
2012-10-18 04:19:28 -07:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
key_start = &(line->start[start]);
|
|
|
|
key_end = eq - 1;
|
2007-02-03 05:05:30 -08:00
|
|
|
|
2008-05-06 08:56:40 -07:00
|
|
|
/* trim whitespace from end of key */
|
2012-10-18 04:19:28 -07:00
|
|
|
while ((isspace((unsigned char)*key_end)) && (key_end > key_start))
|
|
|
|
key_end--;
|
|
|
|
key_end++;
|
2008-05-06 21:14:45 -07:00
|
|
|
|
2012-10-18 04:19:28 -07:00
|
|
|
/* make sure we have a key */
|
|
|
|
if (key_start == key_end) continue;
|
2007-02-03 05:05:30 -08:00
|
|
|
|
2012-10-18 04:19:28 -07:00
|
|
|
value_start = eq + 1;
|
|
|
|
value_end = line->end;
|
2008-05-06 08:56:40 -07:00
|
|
|
|
2012-10-18 04:19:28 -07:00
|
|
|
/* line->end points to char after '\n' or '\r' */
|
|
|
|
value_end--;
|
|
|
|
/* trim whitespace from end of value */
|
|
|
|
while ((isspace((unsigned char)*value_end)) && (value_end > value_start))
|
|
|
|
value_end--;
|
|
|
|
value_end++;
|
2008-05-06 08:56:40 -07:00
|
|
|
|
2012-10-18 04:19:28 -07:00
|
|
|
/* trim whitespace from start of value */
|
|
|
|
while ((isspace((unsigned char)*value_start)) && (value_start < value_end))
|
|
|
|
value_start++;
|
|
|
|
|
|
|
|
len = key_end - key_start + 1;
|
|
|
|
key = alloca(len);
|
2008-05-06 21:14:45 -07:00
|
|
|
|
2012-10-18 04:19:28 -07:00
|
|
|
memcpy(key, key_start, len - 1);
|
|
|
|
key[len - 1] = '\0';
|
2008-05-06 21:14:45 -07:00
|
|
|
|
2012-10-18 04:19:28 -07:00
|
|
|
/* empty value allowed */
|
|
|
|
if (value_end == value_start)
|
|
|
|
{
|
|
|
|
eina_hash_del_by_key(section, key);
|
|
|
|
eina_hash_add(section, key, "");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = value_end - value_start + 1;
|
|
|
|
value = alloca(len);
|
|
|
|
memcpy(value, value_start, len - 1);
|
|
|
|
value[len - 1] = '\0';
|
2008-05-06 08:56:40 -07:00
|
|
|
|
2012-10-18 04:19:28 -07:00
|
|
|
eina_hash_del_by_key(section, key);
|
|
|
|
eina_hash_add(section, key, efreet_ini_unescape(value));
|
|
|
|
}
|
2007-02-03 05:05:30 -08:00
|
|
|
}
|
2011-04-12 15:42:15 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* invalid file... */
|
|
|
|
INF("Invalid file (%s) (missing = from key=value pair)", file);
|
|
|
|
goto error;
|
|
|
|
}
|
2007-02-03 05:05:30 -08:00
|
|
|
}
|
2012-10-18 04:19:28 -07:00
|
|
|
eina_iterator_free(it);
|
|
|
|
eina_file_close(f);
|
2007-02-03 05:05:30 -08:00
|
|
|
|
2010-03-01 10:16:32 -08:00
|
|
|
#if 0
|
|
|
|
if (!eina_hash_population(data))
|
|
|
|
{
|
|
|
|
eina_hash_free(data);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2007-02-03 05:05:30 -08:00
|
|
|
return data;
|
2011-04-12 15:42:15 -07:00
|
|
|
error:
|
|
|
|
if (data) eina_hash_free(data);
|
2012-10-18 04:19:28 -07:00
|
|
|
if (it) eina_iterator_free(it);
|
2012-10-19 16:33:05 -07:00
|
|
|
|
|
|
|
eina_file_close(f);
|
2011-04-12 15:42:15 -07:00
|
|
|
return NULL;
|
2007-02-03 05:05:30 -08:00
|
|
|
}
|
|
|
|
|
2007-11-04 01:32:35 -08:00
|
|
|
EAPI void
|
2007-02-03 05:05:30 -08:00
|
|
|
efreet_ini_free(Efreet_Ini *ini)
|
|
|
|
{
|
|
|
|
if (!ini) return;
|
|
|
|
|
|
|
|
IF_FREE_HASH(ini->data);
|
|
|
|
FREE(ini);
|
|
|
|
}
|
|
|
|
|
2007-11-04 01:32:35 -08:00
|
|
|
EAPI int
|
2007-02-03 05:05:30 -08:00
|
|
|
efreet_ini_save(Efreet_Ini *ini, const char *file)
|
|
|
|
{
|
2010-02-08 11:27:58 -08:00
|
|
|
char *dir;
|
2007-02-03 05:05:30 -08:00
|
|
|
FILE *f;
|
2012-05-23 13:10:27 -07:00
|
|
|
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(ini, 0);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(ini->data, 0);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(file, 0);
|
2007-02-03 05:05:30 -08:00
|
|
|
|
2010-02-08 11:27:58 -08:00
|
|
|
dir = ecore_file_dir_get(file);
|
|
|
|
if (!ecore_file_mkpath(dir))
|
|
|
|
{
|
|
|
|
free(dir);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
free(dir);
|
2007-09-06 09:05:35 -07:00
|
|
|
f = fopen(file, "wb");
|
2007-02-03 05:05:30 -08:00
|
|
|
if (!f) return 0;
|
2008-12-17 07:33:43 -08:00
|
|
|
eina_hash_foreach(ini->data, efreet_ini_section_save, f);
|
2007-02-03 05:05:30 -08:00
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-11-04 01:32:35 -08:00
|
|
|
EAPI int
|
2007-02-03 05:05:30 -08:00
|
|
|
efreet_ini_section_set(Efreet_Ini *ini, const char *section)
|
|
|
|
{
|
2012-05-23 13:10:27 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(ini, 0);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(ini->data, 0);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(section, 0);
|
2007-02-03 05:05:30 -08:00
|
|
|
|
2008-12-17 07:33:43 -08:00
|
|
|
ini->section = eina_hash_find(ini->data, section);
|
2007-02-03 05:05:30 -08:00
|
|
|
return (ini->section ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2007-11-04 01:32:35 -08:00
|
|
|
EAPI void
|
2007-02-03 05:05:30 -08:00
|
|
|
efreet_ini_section_add(Efreet_Ini *ini, const char *section)
|
|
|
|
{
|
2008-12-17 07:33:43 -08:00
|
|
|
Eina_Hash *hash;
|
2007-02-03 05:05:30 -08:00
|
|
|
|
2012-05-23 13:10:27 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN(ini);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN(section);
|
2007-02-03 05:05:30 -08:00
|
|
|
|
|
|
|
if (!ini->data)
|
2010-02-04 01:12:35 -08:00
|
|
|
ini->data = eina_hash_string_small_new(EINA_FREE_CB(eina_hash_free));
|
2008-12-17 07:33:43 -08:00
|
|
|
if (eina_hash_find(ini->data, section)) return;
|
2007-02-03 05:05:30 -08:00
|
|
|
|
2010-04-01 12:41:03 -07:00
|
|
|
hash = eina_hash_string_small_new(EINA_FREE_CB(eina_stringshare_del));
|
2008-12-17 07:33:43 -08:00
|
|
|
eina_hash_add(ini->data, section, hash);
|
2007-02-03 05:05:30 -08:00
|
|
|
}
|
|
|
|
|
2007-11-04 01:32:35 -08:00
|
|
|
EAPI const char *
|
2007-02-03 05:05:30 -08:00
|
|
|
efreet_ini_string_get(Efreet_Ini *ini, const char *key)
|
|
|
|
{
|
2012-05-23 13:10:27 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(ini, NULL);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(ini->section, NULL);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(key, NULL);
|
2007-02-03 05:05:30 -08:00
|
|
|
|
2008-12-17 07:33:43 -08:00
|
|
|
return eina_hash_find(ini->section, key);
|
2007-02-03 05:05:30 -08:00
|
|
|
}
|
|
|
|
|
2007-11-04 01:32:35 -08:00
|
|
|
EAPI void
|
2007-02-03 05:05:30 -08:00
|
|
|
efreet_ini_string_set(Efreet_Ini *ini, const char *key, const char *value)
|
|
|
|
{
|
2012-05-23 13:10:27 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN(ini);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN(ini->section);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN(key);
|
2007-02-03 05:05:30 -08:00
|
|
|
|
2010-04-01 12:41:03 -07:00
|
|
|
eina_hash_del_by_key(ini->section, key);
|
|
|
|
eina_hash_add(ini->section, key, eina_stringshare_add(value));
|
2007-02-03 05:05:30 -08:00
|
|
|
}
|
|
|
|
|
2007-11-04 01:32:35 -08:00
|
|
|
EAPI int
|
2007-02-03 05:05:30 -08:00
|
|
|
efreet_ini_int_get(Efreet_Ini *ini, const char *key)
|
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
|
2012-05-23 13:10:27 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(ini, -1);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(ini->section, -1);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(key, -1);
|
2007-02-03 05:05:30 -08:00
|
|
|
|
|
|
|
str = efreet_ini_string_get(ini, key);
|
|
|
|
if (str) return atoi(str);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-11-04 01:32:35 -08:00
|
|
|
EAPI void
|
2007-02-03 05:05:30 -08:00
|
|
|
efreet_ini_int_set(Efreet_Ini *ini, const char *key, int value)
|
|
|
|
{
|
|
|
|
char str[12];
|
|
|
|
|
2012-05-23 13:10:27 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN(ini);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN(ini->section);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN(key);
|
2007-02-03 05:05:30 -08:00
|
|
|
|
|
|
|
snprintf(str, 12, "%d", value);
|
|
|
|
efreet_ini_string_set(ini, key, str);
|
|
|
|
}
|
|
|
|
|
2007-11-04 01:32:35 -08:00
|
|
|
EAPI double
|
2007-02-03 05:05:30 -08:00
|
|
|
efreet_ini_double_get(Efreet_Ini *ini, const char *key)
|
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
|
2012-05-23 13:10:27 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(ini, -1);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(ini->section, -1);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(key, -1);
|
2007-02-03 05:05:30 -08:00
|
|
|
|
|
|
|
str = efreet_ini_string_get(ini, key);
|
|
|
|
if (str) return atof(str);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-11-04 01:32:35 -08:00
|
|
|
EAPI void
|
2007-02-03 05:05:30 -08:00
|
|
|
efreet_ini_double_set(Efreet_Ini *ini, const char *key, double value)
|
|
|
|
{
|
|
|
|
char str[512];
|
|
|
|
size_t len;
|
|
|
|
|
2012-05-23 13:10:27 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN(ini);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN(ini->section);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN(key);
|
2007-02-03 05:05:30 -08:00
|
|
|
|
|
|
|
snprintf(str, 512, "%.6f", value);
|
|
|
|
len = strlen(str) - 1;
|
|
|
|
/* Strip trailing zero's */
|
2007-04-04 05:00:40 -07:00
|
|
|
while (str[len] == '0' && str[len - 1] != '.') str[len--] = '\0';
|
2007-02-03 05:05:30 -08:00
|
|
|
efreet_ini_string_set(ini, key, str);
|
|
|
|
}
|
|
|
|
|
2007-11-04 01:32:35 -08:00
|
|
|
EAPI unsigned int
|
2007-02-03 05:05:30 -08:00
|
|
|
efreet_ini_boolean_get(Efreet_Ini *ini, const char *key)
|
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
|
2012-05-23 13:10:27 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(ini, 0);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(ini->section, 0);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(key, 0);
|
2007-02-03 05:05:30 -08:00
|
|
|
|
|
|
|
str = efreet_ini_string_get(ini, key);
|
|
|
|
if (str && !strcmp("true", str)) return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-11-04 01:32:35 -08:00
|
|
|
EAPI void
|
2007-02-03 05:05:30 -08:00
|
|
|
efreet_ini_boolean_set(Efreet_Ini *ini, const char *key, unsigned int value)
|
|
|
|
{
|
2012-05-23 13:10:27 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN(ini);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN(ini->section);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN(key);
|
2007-02-03 05:05:30 -08:00
|
|
|
|
|
|
|
if (value) efreet_ini_string_set(ini, key, "true");
|
|
|
|
else efreet_ini_string_set(ini, key, "false");
|
|
|
|
}
|
|
|
|
|
2007-11-04 01:32:35 -08:00
|
|
|
EAPI const char *
|
2007-02-03 05:05:30 -08:00
|
|
|
efreet_ini_localestring_get(Efreet_Ini *ini, const char *key)
|
|
|
|
{
|
|
|
|
const char *lang, *country, *modifier;
|
|
|
|
const char *val = NULL;
|
|
|
|
char *buf;
|
|
|
|
int maxlen = 5; /* _, @, [, ] and \0 */
|
|
|
|
int found = 0;
|
|
|
|
|
2012-05-23 13:10:27 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(ini, NULL);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(ini->section, NULL);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(key, NULL);
|
2007-02-03 05:05:30 -08:00
|
|
|
|
|
|
|
lang = efreet_lang_get();
|
|
|
|
country = efreet_lang_country_get();
|
|
|
|
modifier = efreet_lang_modifier_get();
|
|
|
|
|
|
|
|
maxlen += strlen(key);
|
|
|
|
if (lang) maxlen += strlen(lang);
|
|
|
|
if (country) maxlen += strlen(country);
|
|
|
|
if (modifier) maxlen += strlen(modifier);
|
|
|
|
|
2010-08-03 12:31:24 -07:00
|
|
|
buf = alloca(maxlen);
|
2007-02-03 05:05:30 -08:00
|
|
|
|
|
|
|
if (lang && modifier && country)
|
|
|
|
{
|
|
|
|
snprintf(buf, maxlen, "%s[%s_%s@%s]", key, lang, country, modifier);
|
|
|
|
val = efreet_ini_string_get(ini, buf);
|
2008-07-22 06:30:25 -07:00
|
|
|
if (val && (*val != '\0')) found = 1;
|
2007-02-03 05:05:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!found && lang && country)
|
|
|
|
{
|
|
|
|
snprintf(buf, maxlen, "%s[%s_%s]", key, lang, country);
|
|
|
|
val = efreet_ini_string_get(ini, buf);
|
2008-07-22 06:30:25 -07:00
|
|
|
if (val && (*val != '\0')) found = 1;
|
2007-02-03 05:05:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!found && lang && modifier)
|
|
|
|
{
|
|
|
|
snprintf(buf, maxlen, "%s[%s@%s]", key, lang, modifier);
|
|
|
|
val = efreet_ini_string_get(ini, buf);
|
2008-07-22 06:30:25 -07:00
|
|
|
if (val && (*val != '\0')) found = 1;
|
2007-02-03 05:05:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!found && lang)
|
|
|
|
{
|
|
|
|
snprintf(buf, maxlen, "%s[%s]", key, lang);
|
|
|
|
val = efreet_ini_string_get(ini, buf);
|
2008-07-22 06:30:25 -07:00
|
|
|
if (val && (*val != '\0')) found = 1;
|
2007-02-03 05:05:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
val = efreet_ini_string_get(ini, key);
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2007-11-04 01:32:35 -08:00
|
|
|
EAPI void
|
2007-02-03 05:05:30 -08:00
|
|
|
efreet_ini_localestring_set(Efreet_Ini *ini, const char *key, const char *value)
|
|
|
|
{
|
|
|
|
const char *lang, *country, *modifier;
|
|
|
|
char *buf;
|
|
|
|
int maxlen = 5; /* _, @, [, ] and \0 */
|
|
|
|
|
2012-05-23 13:10:27 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN(ini);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN(ini->section);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN(key);
|
2007-02-03 05:05:30 -08:00
|
|
|
|
|
|
|
lang = efreet_lang_get();
|
|
|
|
country = efreet_lang_country_get();
|
|
|
|
modifier = efreet_lang_modifier_get();
|
|
|
|
|
|
|
|
maxlen += strlen(key);
|
|
|
|
if (lang) maxlen += strlen(lang);
|
|
|
|
if (country) maxlen += strlen(country);
|
|
|
|
if (modifier) maxlen += strlen(modifier);
|
|
|
|
|
2010-07-25 13:25:40 -07:00
|
|
|
buf = alloca(maxlen);
|
2007-02-03 05:05:30 -08:00
|
|
|
|
|
|
|
if (lang && modifier && country)
|
|
|
|
snprintf(buf, maxlen, "%s[%s_%s@%s]", key, lang, country, modifier);
|
|
|
|
else if (lang && country)
|
|
|
|
snprintf(buf, maxlen, "%s[%s_%s]", key, lang, country);
|
|
|
|
else if (lang && modifier)
|
|
|
|
snprintf(buf, maxlen, "%s[%s@%s]", key, lang, modifier);
|
|
|
|
else if (lang)
|
|
|
|
snprintf(buf, maxlen, "%s[%s]", key, lang);
|
|
|
|
else
|
2010-07-25 13:38:28 -07:00
|
|
|
return;
|
2007-02-03 05:05:30 -08:00
|
|
|
|
|
|
|
efreet_ini_string_set(ini, buf, value);
|
|
|
|
}
|
|
|
|
|
2009-10-24 02:23:51 -07:00
|
|
|
EAPI void
|
|
|
|
efreet_ini_key_unset(Efreet_Ini *ini, const char *key)
|
|
|
|
{
|
2012-05-23 13:10:27 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN(ini);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN(ini->section);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN(key);
|
2009-10-24 02:23:51 -07:00
|
|
|
|
2010-04-01 12:41:03 -07:00
|
|
|
eina_hash_del_by_key(ini->section, key);
|
2009-10-24 02:23:51 -07:00
|
|
|
}
|
|
|
|
|
2007-02-03 05:05:30 -08:00
|
|
|
/**
|
|
|
|
* @param str The string to unescape
|
|
|
|
* @return An allocated unescaped string
|
|
|
|
* @brief Unescapes backslash escapes in a string
|
|
|
|
*/
|
2010-04-01 12:41:03 -07:00
|
|
|
static const char *
|
2007-02-03 05:05:30 -08:00
|
|
|
efreet_ini_unescape(const char *str)
|
|
|
|
{
|
|
|
|
char *buf, *dest;
|
|
|
|
const char *p;
|
|
|
|
|
2010-04-01 12:41:03 -07:00
|
|
|
if (!strchr(str, '\\')) return eina_stringshare_add(str);
|
|
|
|
buf = alloca(strlen(str) + 1);
|
2007-02-03 05:05:30 -08:00
|
|
|
|
|
|
|
p = str;
|
|
|
|
dest = buf;
|
2010-04-01 12:41:03 -07:00
|
|
|
while (*p)
|
2007-02-03 05:05:30 -08:00
|
|
|
{
|
2008-05-03 02:25:43 -07:00
|
|
|
if ((*p == '\\') && (p[1] != '\0'))
|
2007-02-03 05:05:30 -08:00
|
|
|
{
|
|
|
|
p++;
|
|
|
|
switch (*p)
|
|
|
|
{
|
|
|
|
case 's':
|
|
|
|
*(dest++) = ' ';
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
*(dest++) = '\n';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
*(dest++) = '\t';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
*(dest++) = '\r';
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
*(dest++) = '\\';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
(*dest++) = '\\';
|
|
|
|
(*dest++) = *p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*(dest++) = *p;
|
|
|
|
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*(dest) = '\0';
|
2010-04-01 12:41:03 -07:00
|
|
|
return eina_stringshare_add(buf);
|
2007-02-03 05:05:30 -08:00
|
|
|
}
|
|
|
|
|
2008-12-17 07:33:43 -08:00
|
|
|
static Eina_Bool
|
2012-12-29 15:04:40 -08:00
|
|
|
efreet_ini_section_save(const Eina_Hash *hash EINA_UNUSED, const void *key, void *value, void *fdata)
|
2007-02-03 05:05:30 -08:00
|
|
|
{
|
2008-12-17 07:33:43 -08:00
|
|
|
FILE *f = fdata;
|
|
|
|
|
|
|
|
fprintf(f, "[%s]\n", (char *)key);
|
|
|
|
eina_hash_foreach(value, efreet_ini_value_save, f);
|
|
|
|
return EINA_TRUE;
|
2007-02-03 05:05:30 -08:00
|
|
|
}
|
|
|
|
|
2008-12-17 07:33:43 -08:00
|
|
|
static Eina_Bool
|
2012-12-29 15:04:40 -08:00
|
|
|
efreet_ini_value_save(const Eina_Hash *hash EINA_UNUSED, const void *key, void *value, void *fdata)
|
2007-02-03 05:05:30 -08:00
|
|
|
{
|
2008-12-17 07:33:43 -08:00
|
|
|
FILE *f = fdata;
|
|
|
|
|
|
|
|
fprintf(f, "%s=%s\n", (char *)key, (char *)value);
|
|
|
|
return EINA_TRUE;
|
2007-02-03 05:05:30 -08:00
|
|
|
}
|