X-Git-Url: https://git.deb.at/w?a=blobdiff_plain;f=filter.c;h=a22d69edf3bd94c82585b6415d5783449033c44a;hb=8f1adc6cc353b1b7d0460ccec6023aefdeb1829b;hp=848aab94050d31625baae85119077ed15fb656d6;hpb=41a02e0be1194fa46951a7c376888e9693631d49;p=pkg%2Fabook.git diff --git a/filter.c b/filter.c index 848aab9..a22d69e 100644 --- a/filter.c +++ b/filter.c @@ -7,6 +7,8 @@ * Copyright (C) Jaakko Heinonen */ +#define _GNU_SOURCE + #include #include #include @@ -1337,7 +1339,7 @@ palmcsv_parse_file(FILE *in) */ static char *vcard_fields[] = { - "FN", /* NAME */ + "FN", /* FORMATTED NAME */ "EMAIL", /* EMAIL */ "ADR", /* ADDRESS */ "ADR", /* ADDRESS2 - not used */ @@ -1352,6 +1354,7 @@ static char *vcard_fields[] = { "NICKNAME", /* NICK */ "URL", /* URL */ "NOTE", /* NOTES */ + "N", /* NAME: special case/mapping in vcard_parse_line() */ NULL /* not implemented: ANNIVERSARY, ITEM_FIELDS */ }; @@ -1481,35 +1484,50 @@ vcard_parse_address(list_item item, char *line) xfree(value); } +static void +vcard_parse_name(list_item item, char *line) +{ + // store the "N" field into "NAME" *if* no "FN:" + // value has already been stored here + if(item[0]) return; + + int i = -1; + item[0] = vcard_get_line_element(line, VCARD_VALUE); + // "N:" can be multivalued => replace ';' separators by ' ' + while(item[0][++i]) if(item[0][i] == ';') item[0][i] = ' '; + + // http://www.daniweb.com/software-development/c/code/216919 + char *original = item[0], *p = original; + int trimmed = 0; + do { + if (*original != ' ' || trimmed) { + trimmed = 1; *p++ = *original; + } + } while(*original++); +} + static void vcard_parse_phone(list_item item, char *line) { - int index = 8; char *type = vcard_get_line_element(line, VCARD_KEY_ATTRIBUTE); char *value = vcard_get_line_element(line, VCARD_VALUE); /* set the standard number */ - if (!type) { - item[index] = value; - } + if (!type) item_fput(item, PHONE, value); /* * see rfc2426 section 3.3.1 * Note: we probably support both vCard 2 and 3 */ else { - if (strcasestr(type, "home") != NULL) { - item[index] = xstrdup(value); - } - if (strcasestr(type, "work") != NULL) { - item[index+1] = xstrdup(value); - } - if (strcasestr(type, "fax") != NULL) { - item[index+2] = xstrdup(value); - } - if (strcasestr(type, "cell") != NULL) { - item[index+3] = xstrdup(value); - } + if (strcasestr(type, "home") != NULL) + item_fput(item, PHONE, xstrdup(value)); + else if (strcasestr(type, "work") != NULL) + item_fput(item, WORKPHONE, xstrdup(value)); + else if (strcasestr(type, "fax") != NULL) + item_fput(item, FAX, xstrdup(value)); + else if (strcasestr(type, "cell") != NULL) + item_fput(item, MOBILEPHONE, xstrdup(value)); xfree(type); xfree(value); @@ -1532,6 +1550,8 @@ vcard_parse_line(list_item item, char *line) vcard_parse_address(item, line); else if(0 == strcmp(key, "TEL")) vcard_parse_phone(item, line); + else if(0 == strcmp(key, "N")) + vcard_parse_name(item, line); else item[i] = vcard_get_line_element(line, VCARD_VALUE); return;