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

[or-cvs] more cleanup and rearranging



Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/home2/arma/work/onion/cvs/src/common

Modified Files:
	util.c util.h 
Log Message:
more cleanup and rearranging
still not finished integrating new dirserv stuff


Index: util.c
===================================================================
RCS file: /home/or/cvsroot/src/common/util.c,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -d -r1.21 -r1.22
--- util.c	28 Sep 2003 06:47:29 -0000	1.21
+++ util.c	29 Sep 2003 07:50:05 -0000	1.22
@@ -430,3 +430,53 @@
   return string;
 }
 
+/* read lines from f (no more than maxlen-1 bytes each) until we
+ * get one with a well-formed "key value".
+ * point *key to the first word in line, point *value to the second.
+ * Put a \0 at the end of key, remove everything at the end of value
+ * that is whitespace or comment.
+ * Return 1 if success, 0 if no more lines, -1 if error.
+ */
+int parse_line_from_file(char *line, int maxlen, FILE *f, char **key_out, char **value_out) {
+  char *s, *key, *end, *value;
+
+try_next_line:
+  if(!fgets(line, maxlen, f)) {
+    if(feof(f))
+      return 0;
+    return -1; /* real error */
+  }
+
+  if((s = strchr(line,'#'))) /* strip comments */
+    *s = 0; /* stop the line there */
+
+  /* remove end whitespace */
+  s = strchr(line, 0); /* now we're at the null */
+  do {
+    *s = 0;
+    s--;
+  } while (isspace(*s));
+
+  key = line;
+  while(isspace(*key))
+    key++;
+  if(*key == 0)
+    goto try_next_line; /* this line has nothing on it */
+  end = key;
+  while(*end && !isspace(*end))
+    end++;
+  value = end;
+  while(*value && isspace(*value))
+    value++;
+
+  if(!*end || !*value) { /* only a key on this line. no value. */
+    log_fn(LOG_WARNING,"Line has keyword '%s' but no value. Skipping.",s);
+    goto try_next_line;
+  }
+  *end = 0; /* null it out */
+
+  log_fn(LOG_DEBUG,"got keyword '%s', value '%s'", key, value);
+  *key_out = key, *value_out = value;
+  return 1;
+}
+

Index: util.h
===================================================================
RCS file: /home/or/cvsroot/src/common/util.h,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -d -r1.13 -r1.14
--- util.h	28 Sep 2003 06:47:29 -0000	1.13
+++ util.h	29 Sep 2003 07:50:05 -0000	1.14
@@ -5,21 +5,14 @@
 #ifndef __UTIL_H
 #define __UTIL_H
 
-#include "orconfig.h"
+#include "../or/or.h"
 
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
-#ifdef HAVE_TIME_H
-#include <time.h>
-#endif
 #if _MSC_VER > 1300
 #include <winsock2.h>
 #include <ws2tcpip.h>
 #elif defined(_MSC_VER)
 #include <winsock.h>
 #endif
-#include <errno.h>
 #ifndef HAVE_GETTIMEOFDAY
 #ifdef HAVE_FTIME
 #define USING_FAKE_TIMEVAL
@@ -68,6 +61,7 @@
 int check_private_dir(const char *dirname, int create);
 int write_str_to_file(const char *fname, const char *str);
 char *read_file_to_str(const char *filename);
+int parse_line_from_file(char *line, int maxlen, FILE *f, char **key_out, char **value_out);
 
 /* Minimalist interface to run a void function in the background.  On
    unix calls fork, on win32 calls beginthread.  Returns -1 on failure.