return 0;
}
-// see enum field_types @database.h
-static char *conv_table[] = {
- "name",
- "email",
- "address",
- "address2",
- "city",
- "state",
- "zip",
- "country",
- "phone",
- "workphone",
- "fax",
- "mobile",
- "nick",
- "url",
- "notes",
- "anniversary",
- 0 /* ITEM_FIELDS */
-};
-
+// see also enum field_types @database.h
+extern abook_field standard_fields[];
static int
find_field_enum(char *s) {
- int i = 0;
- while (conv_table[i]) {
- if(!safe_strcmp(conv_table[i], s))
- return i;
- i++;
- }
- // failed
- return -1;
+ int i = -1;
+ while(standard_fields[++i].key)
+ if(!strcmp(standard_fields[i].key, s))
+ return i;
+ return -1;
}
/* Convert a string with named placeholders to
exit(EXIT_FAILURE);
}
+ char tmp[1] = { 0 };
char *p, *start, *field_name = NULL;
p = start = s;
while(*p) {
if(*p == '{') {
start = ++p;
+
+ if(! *start) goto cannotparse;
p = strchr(start, '}');
- if(! *p) {
- fprintf(stderr, _("parse_custom_format: invalid format\n"));
- exit(EXIT_FAILURE);
- }
+ if(! p) goto cannotparse;
strcat(fmt_string, "%s");
field_name = strndup(start, (size_t)(p-start));
*ft = find_field_enum(field_name);
}
ft++;
- p++;
- start = p;
- } else {
+ start = ++p;
+ }
+
+ else if(*p == '\\') {
+ ++p;
+ if(! *p) tmp[0] = '\\'; // last char is a '\' ?
+ else if(*p == 'n') *tmp = '\n';
+ else if(*p == 't') *tmp = '\t';
+ else if(*p == 'r') *tmp = '\r';
+ else if(*p == 'v') *tmp = '\v';
+ else if(*p == 'b') *tmp = '\b';
+ else if(*p == 'a') *tmp = '\a';
+ else *tmp = *p;
+ strncat(fmt_string, tmp, 1);
+ start = ++p;
+ }
+
+ // if no '\' following: quick mode using strchr/strncat
+ else if(! strchr(start, '\\')) {
p = strchr(start, '{');
- if(p && *p) {
+ if(p) { // copy until the next placeholder
strncat(fmt_string, start, (size_t)(p-start));
start = p;
}
- else {
+ else { // copy till the end
strncat( fmt_string,
start,
FORMAT_STRING_LEN - strlen(fmt_string) - 1 );
break;
}
}
+
+ // otherwise character by character
+ else {
+ strncat(fmt_string, p, 1);
+ start = ++p;
+ }
}
- *ft = 66;
+
+ *ft = ITEM_FIELDS;
+ return;
+
+ cannotparse:
+ fprintf(stderr, _("%s: invalid format, index %ld\n"), __FUNCTION__, (start - s));
+ free(fmt_string);
+ while(*ft) free(ft--);
+ exit(EXIT_FAILURE);
}
static int
// we first check that all fields exist before continuing
if(*fmt == '!') {
enum field_types *ftp = ft;
- while(*ft != 66) {
+ while(*ft != ITEM_FIELDS) {
if(! db_fget(item, *ft) )
return 1;
ft++;
fprintf(out, "%s", safe_str(db_fget(item, *ft)));
ft++;
fmt+=2;
- } else if (*ft == 66) {
+ } else if (*ft == ITEM_FIELDS) {
fprintf(out, "%s", fmt);
return 0;
} else {