]> git.deb.at Git - pkg/abook.git/blobdiff - filter.c
- replace abook_malloc, abook_realloc and my_free with new xmalloc routines
[pkg/abook.git] / filter.c
index 5fdec626f45954e0d364e085c1a9337db0641feb..e11a60e18ee9bfb35a26ead350449e67677a21cb 100644 (file)
--- a/filter.c
+++ b/filter.c
@@ -22,6 +22,7 @@
 #include "list.h"
 #include "misc.h"
 #include "options.h"
+#include "xmalloc.h"
 #include <assert.h>
 
 extern int items;
@@ -40,6 +41,7 @@ static int      ldif_parse_file(FILE *handle);
 static int     mutt_parse_file(FILE *in);
 static int     pine_parse_file(FILE *in);
 static int     csv_parse_file(FILE *in);
+static int     allcsv_parse_file(FILE *in);
 
 /*
  * export filter prototypes
@@ -49,6 +51,7 @@ static int      ldif_export_database(FILE *out, struct db_enumerator e);
 static int     html_export_database(FILE *out, struct db_enumerator e);
 static int     pine_export_database(FILE *out, struct db_enumerator e);
 static int     csv_export_database(FILE *out, struct db_enumerator e);
+static int     allcsv_export_database(FILE *out, struct db_enumerator e);
 static int     palm_export_database(FILE *out, struct db_enumerator e);
 static int     gcrd_export_database(FILE *out, struct db_enumerator e);
 static int     mutt_alias_export(FILE *out, struct db_enumerator e);
@@ -66,6 +69,7 @@ struct abook_input_filter i_filters[] = {
        { "mutt", "mutt alias", mutt_parse_file },
        { "pine", "pine addressbook", pine_parse_file },
        { "csv", "comma separated values", csv_parse_file },
+       { "allcsv", "comma separated all values", allcsv_parse_file },
        { "\0", NULL, NULL }
 };
 
@@ -77,6 +81,7 @@ struct abook_output_filter e_filters[] = {
        { "pine", "pine addressbook", pine_export_database },
        { "gcrd", "GnomeCard (VCard) addressbook", gcrd_export_database },
        { "csv", "comma separated values", csv_export_database },
+       { "allcsv", "comma separated all values", allcsv_export_database },
        { "palmcsv", "Palm comma separated values", palm_export_database},
        { "elm", "elm alias", elm_alias_export },
        { "text", "plain text", text_export_database },
@@ -534,7 +539,7 @@ ldif_add_item(ldif_item ldif_item)
 
 bail_out:
        for(i=0; i < LDIF_ITEM_FIELDS; i++)
-               my_free(ldif_item[i]);
+               xfree(ldif_item[i]);
 
 }
 
@@ -554,7 +559,7 @@ ldif_convert(ldif_item item, char *type, char *value)
                                if( safe_strcmp("person", value))
                                        break;
                        if(item[i])
-                               my_free(item[i]);
+                               xfree(item[i]);
                        item[i] = strdup(value);
                }
        }
@@ -575,7 +580,7 @@ ldif_parse_file(FILE *handle)
                        continue;
 
                if( -1 == ( str_parse_line(line, &type, &value, &vlen)) ) {
-                       my_free(line);
+                       xfree(line);
                        continue; /* just skip the errors */
                }
 
@@ -583,7 +588,7 @@ ldif_parse_file(FILE *handle)
 
                ldif_convert(item, type, value);
 
-               my_free(line);
+               xfree(line);
        } while ( !feof(handle) );
 
        ldif_convert(item, "dn", "");
@@ -618,6 +623,7 @@ static int
 mutt_read_line(FILE *in, char **alias, char **rest)
 {
        char *line, *ptr, *tmp;
+       size_t alias_len;
 
        if( !(line = ptr = getaline(in)) )
                return 1; /* error / EOF */
@@ -640,13 +646,16 @@ mutt_read_line(FILE *in, char **alias, char **rest)
        while( ! ISSPACE(*ptr) )
                ptr++;
 
-       if( (*alias = (char *)malloc(ptr - tmp)) == NULL) {
+       /* includes also the trailing zero */
+       alias_len = (size_t)(ptr - tmp + 1);
+
+       if( (*alias = xmalloc(alias_len)) == NULL) {
                free(line);
                return 1;
        }
 
-       strncpy(*alias, tmp, ptr - tmp - 1);
-       *(*alias + (ptr - tmp)) = 0;
+       strncpy(*alias, tmp, alias_len - 1);
+       *(*alias + alias_len - 1) = 0;
 
        while(ISSPACE(*ptr))
                ptr++;
@@ -717,7 +726,7 @@ mutt_parse_email(list_item item)
                                free(item[EMAIL]);
                                item[EMAIL] = tmp;
                        } else {
-                               my_free(email);
+                               xfree(email);
                        }
                }
        }
@@ -929,7 +938,7 @@ pine_parse_buf(char *buf)
        int i, len, last;
        int pine_conv_table[]= {NICK, NAME, EMAIL, -1, NOTES};
 
-       memset(&item, 0, sizeof(item) );
+       memset(&item, 0, sizeof(item));
        
        for(i=0, last=0; !last ; i++) {
                if( ! (end = strchr(start, '\t')) )
@@ -967,7 +976,7 @@ pine_parse_file(FILE *in)
 
        while(!feof(in)) {
                for(i = 2;;i++) {
-                       buf = (char *) realloc(buf, i*LINESIZE);
+                       buf = xrealloc(buf, i*LINESIZE);
                        if(i == 2)
                                strcpy(buf, line);
                        fgets(line, LINESIZE, in);
@@ -981,14 +990,14 @@ pine_parse_file(FILE *in)
                        strcat(buf, ptr);
                }
                if( *buf == '#' ) {
-                       my_free(buf);
+                       xfree(buf);
                        continue;
                }
                pine_fixbuf(buf);
 
                pine_parse_buf(buf);
 
-               my_free(buf);
+               xfree(buf);
        }
 
        return 0;
@@ -1047,6 +1056,29 @@ static int csv_conv_table[] = {
        NICK
 };
 
+static int allcsv_conv_table[] = {
+       NAME,
+       EMAIL,
+       ADDRESS,
+       ADDRESS2,
+       CITY,
+       STATE,
+       ZIP,
+       COUNTRY,
+       PHONE,
+       WORKPHONE,
+       FAX,
+       MOBILEPHONE,
+       NICK,
+       URL,
+       NOTES,
+       CUSTOM1,
+       CUSTOM2,
+       CUSTOM3,
+       CUSTOM4,
+       CUSTOM5,
+};
+
 static void
 csv_convert_emails(char *s)
 {
@@ -1076,7 +1108,7 @@ csv_remove_quotes(char *s)
        len = strlen(trimmed);
        if(trimmed[len - 1] == '\"' && *trimmed == '\"') {
                if(len < 3) {
-                       my_free(copy);
+                       xfree(copy);
                        return NULL;
                }
                trimmed[len - 1] = 0;
@@ -1086,7 +1118,7 @@ csv_remove_quotes(char *s)
                return trimmed;
        }
 
-       my_free(copy);
+       xfree(copy);
        return strdup(s);
 }
 
@@ -1105,7 +1137,26 @@ csv_store_field(list_item item, char *s, int field)
                        && csv_conv_table[field] >= 0) {
                item[csv_conv_table[field]] = newstr;
        } else {
-               my_free(newstr);
+               xfree(newstr);
+       }
+}
+
+static void
+allcsv_store_field(list_item item, char *s, int field)
+{
+       char *newstr = NULL;
+
+       if(!s || !*s)
+               return;
+
+       if( !(newstr = csv_remove_quotes(s)) )
+               return;
+
+       if(field < (int)(sizeof(allcsv_conv_table) / sizeof(*allcsv_conv_table))
+                       && allcsv_conv_table[field] >= 0) {
+               item[allcsv_conv_table[field]] = newstr;
+       } else {
+               xfree(newstr);
        }
 }
 
@@ -1174,6 +1225,42 @@ csv_parse_line(char *line)
        add_item2database(item);
 }
 
+static void
+allcsv_parse_line(char *line)
+{
+       char *p, *start;
+       int field;
+       bool in_quote = FALSE;
+       list_item item;
+
+       memset(item, 0, sizeof(item));
+
+       for(p = start = line, field = 0; *p; p++) {
+               if(in_quote) {
+                       if(csv_is_valid_quote_end(p))
+                               in_quote = FALSE;
+               } else {
+                       if ( (((p - start) / sizeof (char)) < 2 ) &&
+                               csv_is_valid_quote_start(p) )
+                               in_quote = TRUE;
+               }
+
+               if( *p == ',' && !in_quote) {
+                       *p = 0;
+                       allcsv_store_field(item, start, field);
+                       field++;
+                       start = p + 1;
+               }
+       }
+       /*
+        * store last field
+        */
+       allcsv_store_field(item, start, field);
+
+       csv_convert_emails(item[EMAIL]);
+       add_item2database(item);
+}
+
 
 static int
 csv_parse_file(FILE *in)
@@ -1186,7 +1273,24 @@ csv_parse_file(FILE *in)
                if(line && *line && *line != CSV_COMMENT_CHAR)
                        csv_parse_line(line);
 
-               my_free(line);
+               xfree(line);
+       }
+
+       return 0;
+}
+
+static int
+allcsv_parse_file(FILE *in)
+{
+       char *line = NULL;
+
+       while(!feof(in)) {
+               line = getaline(in);
+
+               if(line && *line && *line != CSV_COMMENT_CHAR)
+                       allcsv_parse_line(line);
+
+               xfree(line);
        }
 
        return 0;
@@ -1254,6 +1358,64 @@ csv_export_database(FILE *out, struct db_enumerator e)
        return 0;
 }
 
+static int
+allcsv_export_database(FILE *out, struct db_enumerator e)
+{
+       /*
+        * TODO: Should get these atomatically from abook_fileds
+        *  - JH
+        */
+       int allcsv_export_fields[] = {
+               NAME,
+               EMAIL,
+               ADDRESS,
+               ADDRESS2,
+               CITY,
+               STATE,
+               ZIP,
+               COUNTRY,
+               PHONE,
+               WORKPHONE,
+               FAX,
+               MOBILEPHONE,
+               NICK,
+               URL,
+               NOTES,
+               CUSTOM1,
+               CUSTOM2,
+               CUSTOM3,
+               CUSTOM4,
+               CUSTOM5,
+               CSV_LAST
+       };
+
+       fprintf(out, "#");
+       fprintf(out, "\"NAME\",");
+       fprintf(out, "\"EMAIL\",");
+       fprintf(out, "\"ADDRESS\",");
+       fprintf(out, "\"ADDRESS2\",");
+       fprintf(out, "\"CITY\",");
+       fprintf(out, "\"STATE\",");
+       fprintf(out, "\"ZIP\",");
+       fprintf(out, "\"COUNTRY\",");
+       fprintf(out, "\"PHONE\",");
+       fprintf(out, "\"WORKPHONE\",");
+       fprintf(out, "\"FAX\",");
+       fprintf(out, "\"MOBILEPHONE\",");
+       fprintf(out, "\"NICK\",");
+       fprintf(out, "\"URL\",");
+       fprintf(out, "\"NOTES\",");
+       fprintf(out, "\"CUSTOM1\",");
+       fprintf(out, "\"CUSTOM2\",");
+       fprintf(out, "\"CUSTOM3\",");
+       fprintf(out, "\"CUSTOM4\",");
+       fprintf(out, "\"CUSTOM5\"\n");
+
+       csv_export_common(out, e, allcsv_export_fields, NULL);
+       
+       return 0;
+}
+
 /*
  * palm csv
  */
@@ -1444,7 +1606,7 @@ mutt_alias_export(FILE *out, struct db_enumerator e)
                                alias,
                                database[e.item][NAME],
                                email);
-               my_free(alias);
+               xfree(alias);
        }
 
        return 0;
@@ -1611,7 +1773,7 @@ elm_alias_export(FILE *out, struct db_enumerator e)
                                alias,
                                database[e.item][NAME],
                                email);
-               my_free(alias);
+               xfree(alias);
        }
 
        return 0;