X-Git-Url: http://git.jankratochvil.net/?p=gnokii.git;a=blobdiff_plain;f=common%2Fcfgreader.c;fp=common%2Fcfgreader.c;h=16365a27abf7ea5cb5c5491e88fa8cbb68e8618d;hp=1b18760b04ad0538e21935b00f6e756067784518;hb=1379b6d01c3c3bd082a8ab32433450d8a2f3419c;hpb=a363234939c6030f24d092e9d4d97ee40f50ce03 diff --git a/common/cfgreader.c b/common/cfgreader.c index 1b18760..16365a2 100644 --- a/common/cfgreader.c +++ b/common/cfgreader.c @@ -15,11 +15,8 @@ Modified from code by Tim Potter. $Log$ - Revision 1.1.1.1 2001/11/25 21:58:58 short - :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Sun Nov 25 22:56 CET 2001 - - Revision 1.16 2001/11/14 10:46:12 pkot - Small cleanup with __unices__ + Revision 1.1.1.2 2002/04/03 00:07:51 short + Found in "gnokii-working" directory, some November-patches version Revision 1.15 2001/06/10 11:24:57 machek Kill "slash star" inside comment. @@ -49,11 +46,16 @@ #include #include #include +#if __unices__ +# include +#endif #include #include #include "cfgreader.h" +struct CFG_Header *CFG_Info; + /* Read configuration information from a ".INI" style file */ struct CFG_Header *CFG_ReadFile(char *filename) { @@ -208,7 +210,7 @@ int CFG_WriteFile(struct CFG_Header *cfg, char *filename) * with key or NULL if no such key exists. */ -char *CFG_Get(struct CFG_Header *cfg, char *section, char *key) +char *CFG_Get(struct CFG_Header *cfg, const char *section, const char *key) { struct CFG_Header *h; struct CFG_Entry *e; @@ -233,6 +235,29 @@ char *CFG_Get(struct CFG_Header *cfg, char *section, char *key) return NULL; } +/* + * Return all the entries of the fiven section. + */ + +void CFG_GetForeach(struct CFG_Header *cfg, const char *section, CFG_GetForeach_func func) +{ + struct CFG_Header *h; + struct CFG_Entry *e; + + if ((cfg == NULL) || (section == NULL) || (func == NULL)) { + return; + } + + /* Search for section name */ + for (h = cfg; h != NULL; h = h->next) { + if (strcmp(section, h->section) == 0) { + /* Search for key within section */ + for (e = h->entries; e != NULL; e = e->next) + (*func)(section,e->key,e->value); + } + } +} + /* Set the value of a key in a config file. Return the new value if the section/key can be found, else return NULL. */ @@ -269,12 +294,17 @@ char *CFG_Set(struct CFG_Header *cfg, char *section, char *key, int readconfig(char **model, char **port, char **initlength, char **connection, char **bindir) { - struct CFG_Header *cfg_info; char *homedir; char rcfile[200]; char *DefaultConnection = "serial"; char *DefaultBindir = "/usr/local/sbin/"; + /* I know that it doesn't belong here but currently there is now generic + * application init function anywhere. + */ + setvbuf(stdout, NULL, _IONBF, 0); + setvbuf(stderr, NULL, _IONBF, 0); + #ifdef WIN32 homedir = getenv("HOMEDRIVE"); strncpy(rcfile, homedir ? homedir : "", 200); @@ -288,34 +318,34 @@ int readconfig(char **model, char **port, char **initlength, #endif /* Try opening .gnokirc from users home directory first */ - if ((cfg_info = CFG_ReadFile(rcfile)) == NULL) { + if ((CFG_Info = CFG_ReadFile(rcfile)) == NULL) { /* It failed so try for /etc/gnokiirc */ - if ((cfg_info = CFG_ReadFile("/etc/gnokiirc")) == NULL) { + if ((CFG_Info = CFG_ReadFile("/etc/gnokiirc")) == NULL) { /* That failed too so exit */ fprintf(stderr, _("Couldn't open %s or /etc/gnokiirc. Exiting now...\n"), rcfile); return -1; } } - (char *)*model = CFG_Get(cfg_info, "global", "model"); + (char *)*model = CFG_Get(CFG_Info, "global", "model"); if (!*model) { fprintf(stderr, _("Config error - no model specified. Exiting now...\n")); return -2; } - (char *)*port = CFG_Get(cfg_info, "global", "port"); + (char *)*port = CFG_Get(CFG_Info, "global", "port"); if (!*port) { fprintf(stderr, _("Config error - no port specified. Exiting now...\n")); return -3; } - (char *)*initlength = CFG_Get(cfg_info, "global", "initlength"); + (char *)*initlength = CFG_Get(CFG_Info, "global", "initlength"); if (!*initlength) (char *)*initlength = "default"; - (char *)*connection = CFG_Get(cfg_info, "global", "connection"); + (char *)*connection = CFG_Get(CFG_Info, "global", "connection"); if (!*connection) (char *)*connection = DefaultConnection; - (char *)*bindir = CFG_Get(cfg_info, "global", "bindir"); + (char *)*bindir = CFG_Get(CFG_Info, "global", "bindir"); if (!*bindir) (char *)*bindir = DefaultBindir; return 0;