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/util.c | |
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/util.c')
-rw-r--r-- | src/common/util.c | 50 |
1 files changed, 50 insertions, 0 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; +} + |