]> git.deb.at Git - pkg/abook.git/blobdiff - filter.c
merged palmcsv export filter
[pkg/abook.git] / filter.c
index e143ae4b0fabc8f864b7b83b1b8f85339f04a6ca..d699a5c6774abf6150f0f47c8a70d3d2040ddbae 100644 (file)
--- a/filter.c
+++ b/filter.c
@@ -12,6 +12,7 @@
 #include <string.h>
 #include <ctype.h>
 #include <pwd.h>
+#include <sys/stat.h>
 #include <sys/types.h>
 #include "abook_curses.h"
 #include "filter.h"
@@ -48,6 +49,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     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);
 static int     elm_alias_export(FILE *out, struct db_enumerator e);
@@ -75,6 +77,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 },
+       { "palmcsv", "Palm comma separated values", palm_export_database},
        { "elm", "elm alias", elm_alias_export },
        { "text", "plain text", text_export_database },
        { "spruce", "Spruce address book", spruce_export_database },
@@ -193,7 +196,7 @@ import_database()
        
        mvaddstr(5+filter, 2, "->");
        
-       filename = ask_filename("Filename: ", 1);
+       filename = ask_filename("Filename: ");
        if( !filename ) {
                refresh_screen();
                return 2;
@@ -201,8 +204,7 @@ import_database()
                
        if(  i_read_file(filename, i_filters[filter].func ) )
                statusline_msg("Error occured while opening the file");
-       else
-       if( tmp == items )
+       else if( tmp == items )
                statusline_msg("Hmm.., file seems not to be a valid file");
        
        refresh_screen();
@@ -219,7 +221,7 @@ i_read_file(char *filename, int (*func) (FILE *in))
        FILE *in;
        int ret = 0;
 
-       if( ( in = fopen( filename, "r" ) ) == NULL )
+       if( ( in = abook_fopen( filename, "r" ) ) == NULL )
                return 1;
 
        ret = (*func) (in);
@@ -230,14 +232,15 @@ i_read_file(char *filename, int (*func) (FILE *in))
 }
 
 int
-import(char filtname[FILTNAME_LEN], char *filename)
+import_file(char filtname[FILTNAME_LEN], char *filename)
 {
        int i;
        int tmp = items;
        int ret = 0;
 
        for(i=0;; i++) {
-               if( ! strncmp(i_filters[i].filtname, filtname, FILTNAME_LEN) )
+               if( ! strncasecmp(i_filters[i].filtname, filtname,
+                                       FILTNAME_LEN) )
                        break;
                if( ! *i_filters[i].filtname ) {
                        i = -1;
@@ -248,9 +251,13 @@ import(char filtname[FILTNAME_LEN], char *filename)
        if( i<0 )
                return -1;
 
-       if( !strcmp(filename, "-") )
-               ret = (*i_filters[i].func) (stdin);
-       else
+       if( !strcmp(filename, "-") ) {
+               struct stat s;
+               if( (fstat(fileno(stdin), &s)) == -1 || S_ISDIR(s.st_mode))
+                       ret = 1;
+               else
+                       ret = (*i_filters[i].func) (stdin);
+       } else
                ret =  i_read_file(filename, i_filters[i].func);
        
        if( tmp == items )
@@ -299,7 +306,7 @@ export_database()
        
        filter = getch() - 'a';
        if(filter == 'x' - 'a' ||
-               filter >= number_of_output_filters(e_filters) || filter < 0) {
+               filter >= number_of_output_filters() || filter < 0) {
                refresh_screen();
                return 1;
        }
@@ -319,7 +326,7 @@ export_database()
                clear_statusline();
        }
        
-       filename = ask_filename("Filename: ", 0);
+       filename = ask_filename("Filename: ");
        if( !filename ) {
                refresh_screen();
                return 2;
@@ -362,7 +369,8 @@ fexport(char filtname[FILTNAME_LEN], FILE *handle, int enum_mode)
        struct db_enumerator e = init_db_enumerator(enum_mode);
 
        for(i=0;; i++) {
-               if( ! strncmp(e_filters[i].filtname, filtname, FILTNAME_LEN) )
+               if( ! strncasecmp(e_filters[i].filtname, filtname,
+                                       FILTNAME_LEN) )
                        break;
                if( ! *e_filters[i].filtname ) {
                        i = -1;
@@ -376,7 +384,7 @@ fexport(char filtname[FILTNAME_LEN], FILE *handle, int enum_mode)
        
 
 int
-export(char filtname[FILTNAME_LEN], char *filename)
+export_file(char filtname[FILTNAME_LEN], char *filename)
 {
        const int mode = ENUM_ALL;
        int i;
@@ -384,7 +392,8 @@ export(char filtname[FILTNAME_LEN], char *filename)
        struct db_enumerator e = init_db_enumerator(mode);
        
        for(i=0;; i++) {
-               if( ! strncmp(e_filters[i].filtname, filtname, FILTNAME_LEN) )
+               if( ! strncasecmp(e_filters[i].filtname, filtname,
+                                       FILTNAME_LEN) )
                        break;
                if( ! *e_filters[i].filtname ) {
                        i = -1;
@@ -496,7 +505,7 @@ ldif_read_line(FILE *in)
                free(line);
        }
 
-       if( *buf == '#' ) {
+       if(buf && *buf == '#' ) {
                free(buf);
                return NULL;
        }
@@ -587,7 +596,7 @@ ldif_fix_string(char *str)
 {
        int i, j;
 
-       for( i = 0, j = 0; j < strlen(str); i++, j++)
+       for( i = 0, j = 0; j < (int)strlen(str); i++, j++)
                str[i] = ( str[j] == (char)0xc3 ?
                                (char) str[++j] + (char) 0x40 :
                                str[j] );
@@ -776,11 +785,12 @@ ldif_export_database(FILE *out, struct db_enumerator e)
 static void            html_export_write_head(FILE *out, int extra_column);
 static void            html_export_write_tail(FILE *out);
 
+extern int extra_column;
+
 static int
 html_export_database(FILE *out, struct db_enumerator e)
 {
        char tmp[MAX_EMAILSTR_LEN];
-       int extra_column = options_get_int("extra_column");
 
        if( items < 1 )
                return 2;
@@ -797,7 +807,7 @@ html_export_database(FILE *out, struct db_enumerator e)
                            tmp,
                            database[e.item][NAME] );
                else
-                   fprintf(out, "<tr>\n<td>%s>\n",
+                   fprintf(out, "<tr>\n<td>%s\n",
                            database[e.item][NAME] );
 
                fprintf(out, "<td>%s\n<td>%s\n",
@@ -817,14 +827,13 @@ html_export_write_head(FILE *out, int extra_column)
 {
        char *realname = get_real_name();
 
-       fprintf(out, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n");
-       fprintf(out, "<html>\n<head>\n  <title>%s's addressbook</title>",
-                       realname );
+       fprintf(out, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n");
+       fprintf(out, "<html>\n<head>\n <title>%s's addressbook</title>", realname );
        fprintf(out, "\n</head>\n<body>\n");
        fprintf(out, "\n<h2>%s's addressbook</h2>\n", realname );
        fprintf(out, "<br><br>\n\n");
 
-       fprintf(out, "<center><table border>\n");
+       fprintf(out, "<table border=\"1\" align=\"center\">\n");
        fprintf(out, "\n<tr><th>Name<th>E-mail address(es)<th>%s</tr>\n\n",
                        abook_fields[extra_column].name);
 
@@ -834,7 +843,7 @@ html_export_write_head(FILE *out, int extra_column)
 static void
 html_export_write_tail(FILE *out)
 {
-       fprintf(out, "\n</table></center>\n");
+       fprintf(out, "\n</table>\n");
        fprintf(out, "\n</body>\n</html>\n");
 }
        
@@ -852,7 +861,7 @@ pine_fixbuf(char *buf)
 {
        int i,j;
 
-       for(i=0,j=0; j < strlen(buf); i++, j++)
+       for(i=0,j=0; j < (int)strlen(buf); i++, j++)
                buf[i] = buf[j] == '\n' ? buf[++j] : buf[j];
 }
 
@@ -898,7 +907,7 @@ pine_parse_buf(char *buf)
                len = last ? strlen(start) : (int) (end-start);
                len = min(len, 400-1);
        
-               if(i < sizeof(pine_conv_table) / sizeof(*pine_conv_table)
+               if(i < (int)(sizeof(pine_conv_table) / sizeof(*pine_conv_table))
                                && pine_conv_table[i] >= 0) {
                        strncpy(tmp, start, len);
                        tmp[len] = 0;
@@ -1060,9 +1069,11 @@ csv_store_field(list_item item, char *s, int field)
        if( !(newstr = csv_remove_quotes(s)) )
                return;
 
-       if(field < (sizeof(csv_conv_table) / sizeof(*csv_conv_table))
+       if(field < (int)(sizeof(csv_conv_table) / sizeof(*csv_conv_table))
                        && csv_conv_table[field] >= 0) {
                item[csv_conv_table[field]] = newstr;
+       } else {
+               my_free(newstr);
        }
 }
 
@@ -1100,7 +1111,7 @@ csv_parse_line(char *line)
 {
        char *p, *start;
        int field;
-       int in_quote = FALSE;
+       bool in_quote = FALSE;
        list_item item;
 
        memset(item, 0, sizeof(item));
@@ -1195,6 +1206,102 @@ csv_export_database(FILE *out, struct db_enumerator e)
  */
 
 
+/*
+ * Palm csv addressbook export filter
+ */
+
+/* A few contants, not the best way to init them but better dan
+ * some number that will break things later on...              */
+#define        PALM_CSV_UNDEFINED       (LAST_FIELD+1)
+#define        PALM_CSV_END             (LAST_FIELD+2)
+#define        PALM_CSV_CAT             (LAST_FIELD+3)
+
+static void 
+palm_split_and_write_name(FILE *out, char *name) {
+       char *p;
+
+       assert(name);
+
+       if ( (p = strchr(name, ' ')) ) {
+               /*
+                * last name first
+                */
+               fprintf(out, "\"%s\",\"" , p + 1);
+               fwrite((void *)name, p - name, sizeof(char), out);
+               fputc('\"', out);
+       } else {
+               fprintf(out, "\"%s\"", safe_str(name));
+       }
+}
+
+static int
+palm_export_database(FILE *out, struct db_enumerator e)
+{
+       int j;
+       int palm_export_fields[] = {
+               NAME,                   /* LASTNAME, FIRSTNAME  */
+               PALM_CSV_UNDEFINED,     /* TITLE,               */
+               PALM_CSV_UNDEFINED,     /* COMPAGNY,            */
+               WORKPHONE,              /* WORK PHONE           */
+               PHONE,                  /* HOME PHONE           */
+               FAX,                    /* FAX                  */
+               MOBILEPHONE,            /* OTHER                */
+               EMAIL,                  /* EMAIL                */
+               ADDRESS,                /* ADDRESS              */ 
+               CITY,                   /* CITY                 */
+               STATE,                  /* STATE                */
+               ZIP,                    /* ZIP                  */
+               COUNTRY,                /* COUNTRY              */
+               NICK,                   /* DEFINED 1            */
+               URL,                    /* DEFINED 2            */
+               PALM_CSV_UNDEFINED,     /* DEFINED 3            */
+               PALM_CSV_UNDEFINED,     /* DEFINED 4            */
+               NOTES,                  /* NOTE                 */
+               PALM_CSV_END,           /* "0"                  */
+               PALM_CSV_CAT,           /* CATEGORY             */
+               -1
+       };
+
+       db_enumerate_items(e) {
+               for(j = 0; palm_export_fields[j] >= 0; j++) {
+
+               switch( palm_export_fields[j] ) {
+
+                       case PALM_CSV_UNDEFINED:
+                               fprintf(out, "\"\"");
+                               break;
+                       case PALM_CSV_END:
+                               fprintf(out, "\"0\"");
+                               break;
+                       case PALM_CSV_CAT:
+                               fprintf(out, "\"abook\"");
+                               break;
+                       case NAME:
+                               palm_split_and_write_name(out,
+                                               safe_str(database[e.item]
+                                               [palm_export_fields[j]])) ;
+                               break;
+
+                       default:
+                               fprintf(out, "\"%s\"" ,
+                                       safe_str(database[e.item]
+                                               [palm_export_fields[j]]));
+                       }
+
+                       if(palm_export_fields[j+1] >= 0)
+                               fputc(',', out);
+               }
+               fputc('\n', out);
+       }
+
+       return 0;
+}
+
+/*
+ * end of palm csv export filter
+ */
+
+
 /*
  * GnomeCard (VCard) addressbook export filter
  */
@@ -1390,7 +1497,7 @@ text_export_database(FILE * out, struct db_enumerator e)
        char emails[MAX_EMAILS][MAX_EMAIL_LEN];
        int j;
        char *realname = get_real_name();
-       char *style = options_get_str("address_style");
+       char *style = opt_get_str(STR_ADDRESS_STYLE);
 
        fprintf(out,
                "-----------------------------------------\n%s's address book\n"