diff options
author | Roger Dingledine <arma@torproject.org> | 2003-09-29 07:50:08 +0000 |
---|---|---|
committer | Roger Dingledine <arma@torproject.org> | 2003-09-29 07:50:08 +0000 |
commit | 467d278b8b00740e876ae5ba9e0798ad702b5a67 (patch) | |
tree | b19da3d974a3c3d4290ea748f40283cb75c6651f /src/common | |
parent | 5d31f7155793b80285ba61ec8688dbc6c117ed52 (diff) | |
download | tor-467d278b8b00740e876ae5ba9e0798ad702b5a67.tar tor-467d278b8b00740e876ae5ba9e0798ad702b5a67.tar.gz |
more cleanup and rearranging
still not finished integrating new dirserv stuff
svn:r507
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/util.c | 50 | ||||
-rw-r--r-- | src/common/util.h | 10 |
2 files changed, 52 insertions, 8 deletions
diff --git a/src/common/util.c b/src/common/util.c index e290ab047..7b14039b6 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -430,3 +430,53 @@ char *read_file_to_str(const char *filename) { 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; +} + diff --git a/src/common/util.h b/src/common/util.h index 65f2760c8..0f7ac3b4d 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -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 @@ file_status_t file_status(const char *filename); 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. |