From: Raphaƫl Droz Date: Tue, 30 Aug 2011 14:20:39 +0000 (+0200) Subject: custom output format (2/4): item-based and the other database-based output functions X-Git-Tag: upstream/0.6.1~2^2~51 X-Git-Url: https://git.deb.at/w?a=commitdiff_plain;h=c6c64f87def0694eb80ecc10fde881a203e82fcf;p=pkg%2Fabook.git custom output format (2/4): item-based and the other database-based output functions * custom_export_database() takes care of building a field_types pointer from the format string by calling parse_custom_format(). * custom_export_item() output each entry according to the format string itself + the associated *field_types. The default value for custom_format is: "{nick} ({name}): {mobile}" --- diff --git a/abook.c b/abook.c index e9f8841..9f95655 100644 --- a/abook.c +++ b/abook.c @@ -50,6 +50,10 @@ static void add_email(int); char *datafile = NULL; static char *rcfile = NULL; +// custom formatting +char custom_format[FORMAT_STRING_LEN] = "{nick} ({name}): {mobile}"; +char *parsed_custom_format = NULL; +enum field_types *custom_format_fields = 0; struct abook_output_item_filter selected_item_filter; bool alternative_datafile = FALSE; diff --git a/filter.c b/filter.c index 1fb95ed..a58868c 100644 --- a/filter.c +++ b/filter.c @@ -66,6 +66,7 @@ static int text_export_database(FILE *out, struct db_enumerator e); static int spruce_export_database(FILE *out, struct db_enumerator e); static int wl_export_database(FILE *out, struct db_enumerator e); static int bsdcal_export_database(FILE *out, struct db_enumerator e); +static int custom_export_database(FILE *out, struct db_enumerator e); /* * end of function declarations @@ -2415,6 +2416,93 @@ parse_custom_format(char *s, char *fmt_string, enum field_types *ft) *ft = 66; } +static int +custom_export_item(FILE *out, int item, char *s, enum field_types *ft); + + +// used to store the format string from --outformatstr when "custom" format is used +// default value overriden in export_file() +extern char *parsed_custom_format; +extern enum field_types *custom_format_fields; + +/* wrapper for custom_export_item: + 1) avoid messing with extern pointer + 2) adds \n + 3) follow the prototype needed for an abook_output_item_filter entry */ +void +custom_print_item(FILE *out, int item) +{ + + if(custom_export_item(out, item, parsed_custom_format, custom_format_fields) == 0) + fprintf(out, "\n"); +} + +static int +custom_export_item(FILE *out, int item, char *fmt, enum field_types *ft) +{ + char *p, *q = 0; + + // if the first character is '!': + // we first check that all fields exist before continuing + if(*fmt == '!') { + enum field_types *ftp = ft; + while(*ft != 66) { + if(! db_fget(item, *ft) ) + return 1; + ft++; + } + ft = ftp; + fmt++; + } + + while (*fmt) { + if(!strncmp(fmt, "%s", 2)) { + fprintf(out, "%s", safe_str(db_fget(item, *ft))); + ft++; + fmt+=2; + } else if (*ft == 66) { + fprintf(out, "%s", fmt); + return 0; + } else { + p = strchr(fmt, '%'); + if(*p) { + q = strndup(fmt, (size_t)(p-fmt)); + fprintf(out, "%s", q); + free(q); + fmt = p; + } + else { + fprintf(out, "%s", fmt); + return 0; + } + } + } + + return 0; +} + +// used to store the format string from --outformatstr when "custom" format is used +// default value overriden from abook.c +extern char custom_format[FORMAT_STRING_LEN]; + +static int +custom_export_database(FILE *out, struct db_enumerator e) +{ + char *format_string = + (char *)malloc(FORMAT_STRING_LEN * sizeof(char*)); + + enum field_types *ft = + (enum field_types *)malloc(FORMAT_STRING_MAX_FIELDS * sizeof(enum field_types *)); + + parse_custom_format(custom_format, format_string, ft); + + db_enumerate_items(e) { + if(custom_export_item(out, e.item, format_string, ft) == 0) + fprintf(out, "\n"); + } + return 0; +} + /* * end of BSD calendar export filter */ diff --git a/filter.h b/filter.h index 796df5f..548bf7a 100644 --- a/filter.h +++ b/filter.h @@ -5,6 +5,7 @@ #define FILTNAME_LEN 8 #define FORMAT_STRING_LEN 128 +#define FORMAT_STRING_MAX_FIELDS 16 struct abook_output_filter { @@ -39,6 +40,7 @@ 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); +void custom_print_item(FILE *out, int item); int fexport(char filtname[FILTNAME_LEN], FILE *handle, int enum_mode);