]> git.deb.at Git - pkg/abook.git/commitdiff
custom output format (1/4): created a parser for {placeholders}
authorRaphaël Droz <raphael.droz+floss@gmail.com>
Tue, 30 Aug 2011 13:57:52 +0000 (15:57 +0200)
committerRaphaël Droz <raphael.droz+floss@gmail.com>
Wed, 24 Oct 2012 16:23:52 +0000 (18:23 +0200)
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)

database.h
filter.c
filter.h

index 4d33ff478179f0886f2b09571881c6119752ce42..23ee2943f9e3c5fbd7140c9f12ec12a1da65f186 100644 (file)
@@ -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,
index 2735331d1d5a110d09d79cdf61bf020606158795..1fb95ed4d649c5003e1ccb2825f778165e8da1a5 100644 (file)
--- 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
  */
index c31c76fa7d8105bc5d47e1d42cfe33d0515a1d4b..796df5f2026d832383bf2d8529910a2b4ec7f9ea 100644 (file)
--- 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);