Git-Url: 
http://git.frugalware.org/gitweb/gitweb.cgi?p=yaxmenu.git;a=commitdiff;h=a7f0d8da1b45cbc763701d40802354b890115877

commit a7f0d8da1b45cbc763701d40802354b890115877
Author: James Buren <r...@frugalware.org>
Date:   Mon Dec 7 00:03:35 2009 -0600

config.c
* initial draft

diff --git a/src/config.c b/src/config.c
new file mode 100644
index 0000000..40bafc4
--- /dev/null
+++ b/src/config.c
@@ -0,0 +1,87 @@
+#include "globals.h"
+#include "flags.h"
+#include "utility.h"
+#include "config.h"
+
+static const char config_file_name[]     = "cfgrc";
+static const char config_parser_format[] = "%*[^=]=%n%*[^\n]\n%n";
+static const char config_key_theme[]     = "theme";
+static const char config_key_menu[]      = "menu";
+
+static inline void _config_set_string(char **dest,const char *val) {
+  assert(dest && val);
+
+  if(!*dest)
+    *dest = xstrdup(val);
+}
+
+static inline bool _config_parse_line(char *line) {
+  char *str, *key, *val;
+  size_t i, j;
+
+  assert(line);
+
+  str = strchr(line,G->comment_char);
+
+  if(str)
+    *str = '\0';
+
+  line += strspn(line,G->whitespace_chars);
+
+  if(!*line)
+    return true;
+
+  if(sscanf(line,config_parser_format,&i,&j))
+    return false;
+
+  *(line + i - 1) = '\0';
+
+  *(line + j - 1) = '\0';
+
+  key = xstrtrim(line);
+
+  val = xstrtrim(line + i);
+
+  if(!*key || !*val)
+    return false;
+
+  if(!strcasecmp(key,config_key_theme))
+    _config_set_string(&G->theme,val);
+  else if(!strcasecmp(key,config_key_menu))
+    _config_set_string(&G->menu,val);
+  else
+    return false;
+
+  return true;
+}
+
+bool config_parse(void) {
+  FILE *file;
+  char path[PATH_MAX], line[LINE_MAX], **str;
+  size_t num;
+
+  for( str = G->cfgdirs ; *str ; ++str ) {
+    xsnprintf(path,"%s/%s",*str,config_file_name);
+    file = fopen(path,"r");
+    if(file)
+      break;
+    else if(G->flags & FLAG_VERBOSE)
+      eprintf("%s: %s\n",strerror(errno),path);
+  }
+
+  if(!file) {
+    eprintf("Failed to open a configuration file for parsing.\n");
+    return false;
+  }
+
+  for( num = 1 ; fgets(line,array_entries(line),file) ; ++num )
+    if(!_config_parse_line(line)) {
+      eprintf("Parsing error at line %u in %s.\n",num,path);
+      fclose(file);
+      return false;
+    }
+
+  fclose(file);
+
+  return true;
+}
_______________________________________________
Frugalware-git mailing list
Frugalware-git@frugalware.org
http://frugalware.org/mailman/listinfo/frugalware-git

Reply via email to