* ... - new cfgreader.c/CFG_GetForeach()
authorshort <>
Sun, 25 Nov 2001 23:10:58 +0000 (23:10 +0000)
committershort <>
Sun, 25 Nov 2001 23:10:58 +0000 (23:10 +0000)
some "const" keywords written - they are missing almost everywhere in Gnokii!
"CFG_info" is now global public symbol
 * Global symbols became IMO obsolete: {model,port,initlength,connection,bindir}
stdout/stderr are de-buffered (cfgreading is upon startup)
 * I know that it doesn't belong here but currently there is now generic
   application init function anywhere.
 * stdout/stderr de-buffering is already donw at various weird places all
   through the project

common/cfgreader.c
include/cfgreader.h

index 1b18760..df2458b 100644 (file)
   Modified from code by Tim Potter.
 
   $Log$
+  Revision 1.1.1.1.8.1  2001/11/25 23:10:58  short
+      * ... - new cfgreader.c/CFG_GetForeach()
+  some "const" keywords written - they are missing almost everywhere in Gnokii!
+  "CFG_info" is now global public symbol
+   * Global symbols became IMO obsolete: {model,port,initlength,connection,bindir}
+  stdout/stderr are de-buffered (cfgreading is upon startup)
+   * I know that it doesn't belong here but currently there is now generic
+     application init function anywhere.
+   * stdout/stderr de-buffering is already donw at various weird places all
+     through the project
+
   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
 
@@ -54,6 +65,8 @@
 
 #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 +221,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 +246,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 +305,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 +329,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;
index 252031e..fb3644d 100644 (file)
   Header file for config file reader.
 
   $Log$
+  Revision 1.1.1.1.8.1  2001/11/25 23:10:58  short
+      * ... - new cfgreader.c/CFG_GetForeach()
+  some "const" keywords written - they are missing almost everywhere in Gnokii!
+  "CFG_info" is now global public symbol
+   * Global symbols became IMO obsolete: {model,port,initlength,connection,bindir}
+  stdout/stderr are de-buffered (cfgreading is upon startup)
+   * I know that it doesn't belong here but currently there is now generic
+     application init function anywhere.
+   * stdout/stderr de-buffering is already donw at various weird places all
+     through the project
+
   Revision 1.1.1.1  2001/11/25 21:59:19  short
   :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Sun Nov 25 22:56 CET 2001
 
@@ -43,10 +54,16 @@ struct CFG_Header {
         char *section;
 };
 
+/* Global variables */
+
+extern struct CFG_Header *CFG_Info;
+
 /* Function prototypes */
 
 struct CFG_Header *CFG_ReadFile(char *filename);
-char              *CFG_Get(struct CFG_Header *cfg, char *section, char *key);
+char              *CFG_Get(struct CFG_Header *cfg, const char *section, const char *key);
+typedef void (*CFG_GetForeach_func)(const char *section,const char *key,const char *value);
+void               CFG_GetForeach(struct CFG_Header *cfg, const char *section, CFG_GetForeach_func func);
 char              *CFG_Set(struct CFG_Header *cfg, char *section, char *key, 
                            char *value);
 int                CFG_WriteFile(struct CFG_Header *cfg, char *filename);