From: Raphaƫl Droz Date: Tue, 30 Aug 2011 13:57:52 +0000 (+0200) Subject: custom output format (1/4): created a parser for {placeholders} X-Git-Tag: upstream/0.6.1~2^2~52 X-Git-Url: https://git.deb.at/w?a=commitdiff_plain;h=0968f14408d8b41570bea076d44d5edbbc3477ed;p=pkg%2Fabook.git custom output format (1/4): created a parser for {placeholders} This creates a field_types pointer from a string containing placeholders where fields' names are allowed. field_types *ft is terminated by the magic value 66 and is initialized by parse_custom_format() (see the part 2/3) --- diff --git a/database.h b/database.h index 4d33ff4..23ee294 100644 --- a/database.h +++ b/database.h @@ -6,7 +6,7 @@ #define MAX_EMAILSTR_LEN (MAX_LIST_ITEMS * (MAX_EMAIL_LEN + 1) + 1) #define MAX_FIELD_LEN 81 -enum { +enum field_types { NAME = 0, /* important */ EMAIL, ADDRESS, diff --git a/filter.c b/filter.c index 2735331..1fb95ed 100644 --- a/filter.c +++ b/filter.c @@ -2332,6 +2332,89 @@ bsdcal_export_database(FILE *out, struct db_enumerator e) 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 */ +}; + +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; +} + +/* Convert a string with named placeholders to + a *printf() compatible string. + Stores the abook field values into ft. */ +void +parse_custom_format(char *s, char *fmt_string, enum field_types *ft) +{ + if(! fmt_string || ! ft) { + fprintf(stderr, _("parse_custom_format: fmt_string or ft not allocated\n")); + exit(EXIT_FAILURE); + } + + char *p, *start, *field_name = NULL; + p = start = s; + + while(*p) { + if(*p == '{') { + start = ++p; + p = strchr(start, '}'); + if(! *p) { + fprintf(stderr, _("parse_custom_format: invalid format\n")); + exit(EXIT_FAILURE); + } + strcat(fmt_string, "%s"); + field_name = strndup(start, (size_t)(p-start)); + *ft = find_field_enum(field_name); + if(*ft == -1) { + fprintf(stderr, _("parse_custom_format: invalid placeholder: {%s}\n"), field_name); + exit(EXIT_FAILURE); + } + + ft++; + p++; + start = p; + } else { + p = strchr(start, '{'); + if(p && *p) { + strncat(fmt_string, start, (size_t)(p-start)); + start = p; + } + else { + strncat( fmt_string, + start, + FORMAT_STRING_LEN - strlen(fmt_string) - 1 ); + break; + } + } + } + *ft = 66; +} + /* * end of BSD calendar export filter */ diff --git a/filter.h b/filter.h index c31c76f..796df5f 100644 --- a/filter.h +++ b/filter.h @@ -4,6 +4,7 @@ #include "database.h" #define FILTNAME_LEN 8 +#define FORMAT_STRING_LEN 128 struct abook_output_filter { @@ -37,6 +38,8 @@ struct abook_output_item_filter void e_write_item(FILE *out, int item, void (*func) (FILE *in, int item)); void muttq_print_item(FILE *file, int item); +void parse_custom_format(char *s, char *fmt_string, enum field_types *ft); + int fexport(char filtname[FILTNAME_LEN], FILE *handle, int enum_mode);