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
#include "cfgreader.h"
+struct CFG_Header *CFG_Info;
+
/* Read configuration information from a ".INI" style file */
struct CFG_Header *CFG_ReadFile(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;
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. */
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);
#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;