The beginnings of a config file manager. Mostly works,

but I haven't tested it enough yet.

The idea is to run
Epplet_load_config_file("~/.whaterver");
assuming that the config file looks e.g. this:

a_number 	123
some_word 	hello
my_string 	hello world

and then query for the keys in the first column using

char * s;

s = Epplet_query_config_file("my_string");
if (s)
  {
    /* s is now "hello world". */
  }


Hope that's a good idea.

Cheers,
-- Christian.


SVN revision: 838
This commit is contained in:
Christian Kreibich 1999-10-18 15:50:58 +00:00
parent 0a414ad2c2
commit e5d23bbf3c
2 changed files with 120 additions and 0 deletions

View File

@ -75,6 +75,24 @@ struct _etimer
#define ESYNC ECommsSend("nop");free(ECommsWaitForMessage());
/* The structures for the config file management ... */
typedef struct _dictentry
{
char *key;
char *value;
}
DictEntry;
typedef struct _dictionary
{
DictEntry *entries;
int num_entries;
}
Dictionary;
static Dictionary *edict = NULL;
static void CommsFindCommsWindow(void);
static void ECommsSetup(Display *d);
static void CommsHandleDestroy(Window win);
@ -254,6 +272,26 @@ Epplet_Init(char *name,
sigaction(SIGCHLD, &sa, (struct sigaction *)0);
}
void
Epplet_cleanup(void)
{
int i;
if (edict)
{
for (i = 0; i < edict->num_entries; i++)
{
if (edict->entries[i].key)
free(edict->entries[i].key);
if (edict->entries[i].value)
free(edict->entries[i].value);
}
free(edict->entries);
free(edict);
edict = NULL;
}
}
void
Epplet_show(void)
{
@ -3573,6 +3611,79 @@ Epplet_dialog_ok(char *text)
free(s);
}
int
Epplet_load_config_file(char *filename)
{
char s[1024], s2[1024], s3[1024];
FILE *f;
if (filename)
{
if (strlen(filename) > 1)
{
if (filename[0] == '~' && filename[1] == '/')
{
filename += 2;
snprintf(s, 1024, "%s/%s", getenv("HOME"), filename);
}
else
{
snprintf(s, 1024, "%s", filename);
}
}
f = fopen(s, "r");
if (f)
{
if (!edict)
{
edict = (Dictionary*)malloc(sizeof(Dictionary));
}
if (edict)
{
edict->entries = NULL;
edict->num_entries = 0;
while ((fscanf(f, "%s %[^\n]\n", (char*)&s2, (char*)&s3) != EOF))
{
edict->num_entries++;
edict->entries = realloc(edict->entries, sizeof(DictEntry) * edict->num_entries);
edict->entries[edict->num_entries-1].key = NULL;
edict->entries[edict->num_entries-1].value = NULL;
edict->entries[edict->num_entries-1].key = strdup(s2);
edict->entries[edict->num_entries-1].value = strdup(s3);
}
fclose(f);
return 1;
}
}
return 0;
}
else
return 0;
}
char *
Epplet_query_config_file(char *key)
{
int i;
if (edict)
{
for (i = 0; i < edict->num_entries; i++)
{
if (edict->entries[i].key)
{
if (!strcmp(key, edict->entries[i].key))
/* we've found the key */
return edict->entries[i].value;
}
}
}
return NULL;
}
int
Epplet_get_hslider_clicked(Epplet_gadget gadget)
{

View File

@ -50,6 +50,9 @@ typedef struct _rgb_buf
/* apps more horizontal than vertical shodul set this to 0. */
void Epplet_Init(char *name, char *version, char *info, int w, int h,
int argc, char **argv, char vertical);
/* you should call this to give the epplet a chance to clean itself up */
/* before exiting */
void Epplet_cleanup(void);
/* actualy display the app */
void Epplet_show(void);
/* ask E to remember stuff about it - you don't need to do this at startup */
@ -316,6 +319,12 @@ void Epplet_dialog_ok(char *text);
int Epplet_get_hslider_clicked(Epplet_gadget gadget);
int Epplet_get_vslider_clicked(Epplet_gadget gadget);
/****************************************************************************/
/* Config file handling stuff */
/****************************************************************************/
int Epplet_load_config_file(char *filename);
char *Epplet_query_config_file(char *key);
/****************************************************************************/
/* Convenience macros to make using the above calls easier */
/****************************************************************************/