summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: b3c965c)
raw | patch | inline | side by side (parent: b3c965c)
author | Jonas Fonseca <fonseca@diku.dk> | |
Sun, 28 May 2006 03:23:52 +0000 (05:23 +0200) | ||
committer | Jonas Fonseca <fonseca@antimatter.localdomain> | |
Sun, 28 May 2006 03:23:52 +0000 (05:23 +0200) |
This concludes the fundation for using read_properties to read options from
a config file.
a config file.
tig.c | patch | blob | history |
index 3a534fad991a854df671dba3ffca5e0a8848632a..c06a0e10f30e94577a14366573ace1a046583466 100644 (file)
--- a/tig.c
+++ b/tig.c
static void die(const char *err, ...);
static void report(const char *msg, ...);
-static int read_properties(FILE *pipe, int separator, int (*read)(char *, int, char *, int));
+static int read_properties(FILE *pipe, const char *separators, int (*read)(char *, int, char *, int));
static void set_nonblocking_input(bool loading);
static size_t utf8_length(const char *string, size_t max_width, int *coloffset, int *trimmed);
#define string_copy(dst, src) \
string_ncopy(dst, src, sizeof(dst))
+static char *
+chomp_string(char *name)
+{
+ int namelen;
+
+ while (isspace(*name))
+ name++;
+
+ namelen = strlen(name) - 1;
+ while (namelen > 0 && isspace(name[namelen]))
+ name[namelen--] = 0;
+
+ return name;
+}
+
/* Shell quoting
*
const char *cmd_env = getenv("TIG_LS_REMOTE");
const char *cmd = cmd_env && *cmd_env ? cmd_env : TIG_LS_REMOTE;
- return read_properties(popen(cmd, "r"), '\t', read_ref);
+ return read_properties(popen(cmd, "r"), "\t", read_ref);
}
static int
}
static int
-read_properties(FILE *pipe, int separator,
+read_properties(FILE *pipe, const char *separators,
int (*read_property)(char *, int, char *, int))
{
char buffer[BUFSIZ];
return ERR;
while (state == OK && (name = fgets(buffer, sizeof(buffer), pipe))) {
- char *value = strchr(name, separator);
- int namelen;
- int valuelen;
+ char *value;
+ size_t namelen;
+ size_t valuelen;
- if (value) {
- namelen = value - name;
- *value++ = 0;
+ name = chomp_string(name);
+ namelen = strcspn(name, separators);
+
+ if (name[namelen]) {
+ name[namelen] = 0;
+ value = chomp_string(name + namelen + 1);
valuelen = strlen(value);
- if (valuelen > 0) {
- valuelen--;
- value[valuelen] = 0;
- }
} else {
- namelen = strlen(name);
value = "";
valuelen = 0;
}