[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] Add code to read configuration lines from a string as well ...



Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/tmp/cvs-serv20541/src/common

Modified Files:
	util.c util.h 
Log Message:
Add code to read configuration lines from a string as well as a file

Index: util.c
===================================================================
RCS file: /home/or/cvsroot/src/common/util.c,v
retrieving revision 1.167
retrieving revision 1.168
diff -u -d -r1.167 -r1.168
--- util.c	4 Nov 2004 04:01:19 -0000	1.167
+++ util.c	4 Nov 2004 22:29:45 -0000	1.168
@@ -875,7 +875,7 @@
 int
 parse_line_from_file(char *line, size_t maxlen, FILE *f,
                      char **key_out, char **value_out) {
-  char *s, *key, *end, *value;
+  char *s;
 
 try_next_line:
   if(!fgets(line, maxlen, f)) {
@@ -883,6 +883,17 @@
       return 0;
     return -1; /* real error */
   }
+  line[maxlen-1] = '\0';
+
+  s = parse_line_from_str(line, key_out, value_out);
+  if (!s)
+    return -1;
+  if (!*key_out)
+    goto try_next_line;
+
+  return 1;
+
+#if 0
 
   if((s = strchr(line,'#'))) /* strip comments */
     *s = 0; /* stop the line there */
@@ -920,6 +931,73 @@
   log_fn(LOG_DEBUG,"got keyword '%s', value '%s'", key, value);
   *key_out = key, *value_out = value;
   return 1;
+#endif
+}
+
+
+/** DOCDOC.
+ *
+ * Return next line or end of string on success, NULL on failure.
+ */
+char *
+parse_line_from_str(char *line, char **key_out, char **value_out)
+{
+  char *key, *val, *cp;
+
+  tor_assert(key_out);
+  tor_assert(value_out);
+
+  *key_out = *value_out = key = val = NULL;
+  /* Skip until the first keyword. */
+  while (1) {
+    while (isspace(*line))
+      ++line;
+    if (*line == '#') {
+      while (*line && *line != '\n')
+        ++line;
+    } else {
+      break;
+    }
+  }
+
+  if (!*line) { /* End of string? */
+    *key_out = *value_out = NULL;
+    return line;
+  }
+
+  /* Skip until the next space. */
+  key = line;
+  while (*line && !isspace(*line) && *line != '#')
+    ++line;
+
+  /* Skip until the value */
+  while (*line == ' ' || *line == '\t')
+    *line++ = '\0';
+  val = line;
+
+  /* Find the end of the line. */
+  while (*line && *line != '\n' && *line != '#')
+    ++line;
+  if (*line == '\n')
+    cp = line++;
+  else {
+    cp = line-1;
+  }
+  while (cp>=val && isspace(*cp))
+    *cp-- = '\0';
+
+  if (*line == '#') {
+    do {
+      *line++ = '\0';
+    } while (*line && *line != '\n');
+    if (*line == '\n')
+      ++line;
+  }
+
+  *key_out = key;
+  *value_out = val;
+
+  return line;
 }
 
 /** Expand any homedir prefix on 'filename'; return a newly allocated

Index: util.h
===================================================================
RCS file: /home/or/cvsroot/src/common/util.h,v
retrieving revision 1.113
retrieving revision 1.114
diff -u -d -r1.113 -r1.114
--- util.h	3 Nov 2004 19:02:48 -0000	1.113
+++ util.h	4 Nov 2004 22:29:45 -0000	1.114
@@ -92,6 +92,7 @@
                         int bin);
 char *read_file_to_str(const char *filename, int bin);
 int parse_line_from_file(char *line, size_t maxlen, FILE *f, char **key_out, char **value_out);
+char *parse_line_from_str(char *line, char **key_out, char **value_out);
 char *expand_filename(const char *filename);
 
 /* Net helpers */