X-Git-Url: https://git.deb.at/?a=blobdiff_plain;f=filter.c;h=4b5330e4b383eadd8e59e25b645f0a458cb1696a;hb=4b8f9231090ada43f7e16987ec46ac7f45a914ec;hp=03ad954a73cf3ab8c0d44460ce54339ae3d1e060;hpb=baf1c7616dd0a5fa1bafde2045ac2af22d95122a;p=pkg%2Fabook.git diff --git a/filter.c b/filter.c index 03ad954..4b5330e 100644 --- a/filter.c +++ b/filter.c @@ -19,6 +19,7 @@ #include "abook.h" #include "database.h" #include "edit.h" +#include "gettext.h" #include "list.h" #include "misc.h" #include "options.h" @@ -42,6 +43,7 @@ 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); +static int palmcsv_parse_file(FILE *in); /* * export filter prototypes @@ -64,28 +66,29 @@ static int spruce_export_database(FILE *out, struct db_enumerator e); */ struct abook_input_filter i_filters[] = { - { "abook", "abook native format", parse_database }, - { "ldif", "ldif / Netscape addressbook", ldif_parse_file }, - { "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 }, + { "abook", N_("abook native format"), parse_database }, + { "ldif", N_("ldif / Netscape addressbook"), ldif_parse_file }, + { "mutt", N_("mutt alias"), mutt_parse_file }, + { "pine", N_("pine addressbook"), pine_parse_file }, + { "csv", N_("comma separated values"), csv_parse_file }, + { "allcsv", N_("comma separated all values"), allcsv_parse_file }, + { "palmcsv", N_("Palm comma separated values"), palmcsv_parse_file }, { "\0", NULL, NULL } }; struct abook_output_filter e_filters[] = { - { "abook", "abook native format", write_database }, - { "ldif", "ldif / Netscape addressbook (.4ld)", ldif_export_database }, - { "mutt", "mutt alias", mutt_alias_export }, - { "html", "html document", html_export_database }, - { "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 }, - { "spruce", "Spruce address book", spruce_export_database }, + { "abook", N_("abook native format"), write_database }, + { "ldif", N_("ldif / Netscape addressbook (.4ld)"), ldif_export_database }, + { "mutt", N_("mutt alias"), mutt_alias_export }, + { "html", N_("html document"), html_export_database }, + { "pine", N_("pine addressbook"), pine_export_database }, + { "gcrd", N_("GnomeCard (VCard) addressbook"), gcrd_export_database }, + { "csv", N_("comma separated values"), csv_export_database }, + { "allcsv", N_("comma separated all values"), allcsv_export_database }, + { "palmcsv", N_("Palm comma separated values"), palm_export_database}, + { "elm", N_("elm alias"), elm_alias_export }, + { "text", N_("plain text"), text_export_database }, + { "spruce", N_("Spruce address book"), spruce_export_database }, { "\0", NULL, NULL } }; @@ -98,17 +101,17 @@ print_filters() { int i; - puts("input:"); + puts(_("input:")); for(i=0; *i_filters[i].filtname ; i++) printf("\t%s\t%s\n", i_filters[i].filtname, - i_filters[i].desc); + gettext(i_filters[i].desc)); putchar('\n'); - puts("output:"); + puts(_("output:")); for(i=0; *e_filters[i].filtname ; i++) printf("\t%s\t%s\n", e_filters[i].filtname, - e_filters[i].desc); + gettext(e_filters[i].desc)); putchar('\n'); } @@ -145,13 +148,13 @@ get_real_name() pwent = getpwnam(username); - if((tmp = strdup(pwent->pw_gecos)) == NULL) - return strdup(username); + if((tmp = xstrdup(pwent->pw_gecos)) == NULL) + return xstrdup(username); rtn = sscanf(pwent->pw_gecos, "%[^,]", tmp); if (rtn == EOF || rtn == 0) { free(tmp); - return strdup(username); + return xstrdup(username); } else return tmp; } @@ -170,17 +173,17 @@ import_screen() clear(); refresh_statusline(); - headerline("import database"); + headerline(_("import database")); - mvaddstr(3, 1, "please select a filter"); + mvaddstr(3, 1, _("please select a filter")); for(i=0; *i_filters[i].filtname ; i++) mvprintw(5 + i, 6, "%c -\t%s\t%s\n", 'a' + i, i_filters[i].filtname, - i_filters[i].desc); + gettext(i_filters[i].desc)); - mvprintw(6 + i, 6, "x -\tcancel"); + mvprintw(6 + i, 6, _("x -\tcancel")); } int @@ -201,16 +204,16 @@ import_database() mvaddstr(5+filter, 2, "->"); - filename = ask_filename("Filename: "); - if( !filename ) { + filename = ask_filename(_("Filename: ")); + if(!filename) { refresh_screen(); return 2; } - if( i_read_file(filename, i_filters[filter].func ) ) - statusline_msg("Error occured while opening the file"); - else if( tmp == items ) - statusline_msg("Hmm.., file seems not to be a valid file"); + if(i_read_file(filename, i_filters[filter].func )) + statusline_msg(_("Error occured while opening the file")); + else if(tmp == items) + statusline_msg(_("Hmm.., file seems not to be a valid file")); refresh_screen(); free(filename); @@ -226,7 +229,7 @@ i_read_file(char *filename, int (*func) (FILE *in)) FILE *in; int ret = 0; - if( ( in = abook_fopen( filename, "r" ) ) == NULL ) + if( (in = abook_fopen( filename, "r" )) == NULL ) return 1; ret = (*func) (in); @@ -244,28 +247,28 @@ import_file(char filtname[FILTNAME_LEN], char *filename) int ret = 0; for(i=0;; i++) { - if( ! strncasecmp(i_filters[i].filtname, filtname, + if(! strncasecmp(i_filters[i].filtname, filtname, FILTNAME_LEN) ) break; - if( ! *i_filters[i].filtname ) { + if(! *i_filters[i].filtname) { i = -1; break; } } - if( i<0 ) + if(i < 0) return -1; - if( !strcmp(filename, "-") ) { + if(!strcmp(filename, "-")) { struct stat s; - if( (fstat(fileno(stdin), &s)) == -1 || S_ISDIR(s.st_mode)) + 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 ) + if(tmp == items) ret = 1; return ret; @@ -287,17 +290,17 @@ export_screen() refresh_statusline(); - headerline("export database"); + headerline(_("export database")); - mvaddstr(3, 1, "please select a filter"); + mvaddstr(3, 1, _("please select a filter")); - for(i=0; *e_filters[i].filtname ; i++) + for(i = 0; *e_filters[i].filtname ; i++) mvprintw(5 + i, 6, "%c -\t%s\t%s\n", 'a' + i, e_filters[i].filtname, - e_filters[i].desc); + gettext(e_filters[i].desc)); - mvprintw(6 + i, 6, "x -\tcancel"); + mvprintw(6 + i, 6, _("x -\tcancel")); } int @@ -318,8 +321,9 @@ export_database() mvaddstr(5+filter, 2, "->"); - if( selected_items() ) { - statusline_addstr("Export All/Selected/Cancel (A/s/c)"); + if(selected_items()) { + /* TODO gettext: handle translated keypresses? */ + statusline_addstr(_("Export All/Selected/Cancel (A/s/c)")); switch( tolower(getch()) ) { case 's': enum_mode = ENUM_SELECTED; @@ -331,14 +335,14 @@ export_database() clear_statusline(); } - filename = ask_filename("Filename: "); - if( !filename ) { + filename = ask_filename(_("Filename: ")); + if(!filename) { refresh_screen(); return 2; } - if( e_write_file(filename, e_filters[filter].func, enum_mode ) ) - statusline_msg("Error occured while exporting"); + if( e_write_file(filename, e_filters[filter].func, enum_mode)) + statusline_msg(_("Error occured while exporting")); refresh_screen(); free(filename); @@ -354,10 +358,10 @@ e_write_file(char *filename, int (*func) (FILE *in, struct db_enumerator e), int ret = 0; struct db_enumerator enumerator = init_db_enumerator(mode); - if( (out = fopen(filename, "a")) == NULL ) + if((out = fopen(filename, "a")) == NULL) return 1; - if( ftell(out) ) + if(ftell(out)) return 1; ret = (*func) (out, enumerator); @@ -374,10 +378,10 @@ fexport(char filtname[FILTNAME_LEN], FILE *handle, int enum_mode) struct db_enumerator e = init_db_enumerator(enum_mode); for(i=0;; i++) { - if( ! strncasecmp(e_filters[i].filtname, filtname, - FILTNAME_LEN) ) + if(!strncasecmp(e_filters[i].filtname, filtname, + FILTNAME_LEN)) break; - if( ! *e_filters[i].filtname ) { + if(!*e_filters[i].filtname) { i = -1; break; } @@ -397,19 +401,19 @@ export_file(char filtname[FILTNAME_LEN], char *filename) struct db_enumerator e = init_db_enumerator(mode); for(i=0;; i++) { - if( ! strncasecmp(e_filters[i].filtname, filtname, - FILTNAME_LEN) ) + if(!strncasecmp(e_filters[i].filtname, filtname, + FILTNAME_LEN)) break; - if( ! *e_filters[i].filtname ) { + if(!*e_filters[i].filtname) { i = -1; break; } } - if( i<0 ) + if(i < 0) return -1; - if( !strcmp(filename, "-") ) + if(!strcmp(filename, "-")) ret = (e_filters[i].func) (stdout, e); else ret = e_write_file(filename, e_filters[i].func, mode); @@ -486,7 +490,7 @@ ldif_read_line(FILE *in) pos = ftell(in); line = getaline(in); - if( feof(in) || !line ) + if(feof(in) || !line) break; if(i == 1) { @@ -526,13 +530,13 @@ ldif_add_item(ldif_item li) memset(abook_item, 0, sizeof(abook_item)); - if(!li[LDIF_ITEM_FIELDS -1] ) + if(!li[LDIF_ITEM_FIELDS -1]) goto bail_out; for(i=0; i < LDIF_ITEM_FIELDS; i++) { - if(ldif_conv_table[i] >= 0 && li[i] && *li[i] ) - abook_item[ldif_conv_table[i]] = strdup(li[i]); + if(ldif_conv_table[i] >= 0 && li[i] && *li[i]) + abook_item[ldif_conv_table[i]] = xstrdup(li[i]); } add_item2database(abook_item); @@ -548,19 +552,19 @@ ldif_convert(ldif_item item, char *type, char *value) { int i; - if( !strcmp(type, "dn") ) { + if(!strcmp(type, "dn")) { ldif_add_item(item); return; } for(i=0; i < LDIF_ITEM_FIELDS; i++) { - if( !safe_strcmp(ldif_field_names[i], type) && *value ) { - if( i == LDIF_ITEM_FIELDS -1) /* this is a dirty hack */ - if( safe_strcmp("person", value)) + if(!safe_strcmp(ldif_field_names[i], type) && *value) { + if(i == LDIF_ITEM_FIELDS - 1) /* this is a dirty hack */ + if(safe_strcmp("person", value)) break; if(item[i]) xfree(item[i]); - item[i] = strdup(value); + item[i] = xstrdup(value); } } } @@ -576,10 +580,10 @@ ldif_parse_file(FILE *handle) memset(item, 0, sizeof(item)); do { - if( ! (line = ldif_read_line(handle)) ) + if( !(line = ldif_read_line(handle)) ) continue; - if( -1 == ( str_parse_line(line, &type, &value, &vlen)) ) { + if(-1 == (str_parse_line(line, &type, &value, &vlen))) { xfree(line); continue; /* just skip the errors */ } @@ -601,7 +605,7 @@ ldif_fix_string(char *str) { int i, j; - for( i = 0, j = 0; j < (int)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] ); @@ -631,7 +635,7 @@ mutt_read_line(FILE *in, char **alias, char **rest) while( ISSPACE(*ptr) ) ptr++; - if( strncmp("alias", ptr, 5) ) { + if(strncmp("alias", ptr, 5)) { free(line); return 1; } @@ -656,7 +660,7 @@ mutt_read_line(FILE *in, char **alias, char **rest) while(ISSPACE(*ptr)) ptr++; - *rest = strdup(ptr); + *rest = xstrdup(ptr); free(line); return 0; @@ -737,11 +741,11 @@ mutt_parse_file(FILE *in) for(;;) { memset(item, 0, sizeof(item)); - if( !mutt_read_line(in, &item[NICK], + if(!mutt_read_line(in, &item[NICK], &item[NAME]) ) mutt_parse_email(item); - if( feof(in) ) { + if(feof(in)) { free_list_item(item); break; } @@ -789,7 +793,7 @@ ldif_export_database(FILE *out, struct db_enumerator e) ldif_fput_type_and_value(out, "dn", tmp); free(tmp); - for(j=0; j < LDIF_ITEM_FIELDS; j++) { + for(j = 0; j < LDIF_ITEM_FIELDS; j++) { if(ldif_conv_table[j] >= 0) { if(ldif_conv_table[j] == EMAIL) ldif_fput_type_and_value(out, @@ -826,7 +830,7 @@ html_export_database(FILE *out, struct db_enumerator e) { char tmp[MAX_EMAILSTR_LEN]; - if( items < 1 ) + if(items < 1) return 2; extra_column = (extra_column > 2 && extra_column < ITEM_FIELDS) ? @@ -907,7 +911,7 @@ pine_convert_emails(char *s) int i; char *tmp; - if( s == NULL || *s != '(' ) + if(s == NULL || *s != '(') return; for(i=0; s[i]; i++ ) @@ -916,8 +920,8 @@ pine_convert_emails(char *s) if( ( tmp = strchr(s,')')) ) *tmp=0; - for(i=1; ( tmp = strchr(s, ',') ) != NULL ; i++, s=tmp+1 ) - if( i > MAX_EMAILS - 1 ) { + for(i = 1; ( tmp = strchr(s, ',') ) != NULL ; i++, s = tmp + 1) + if(i > MAX_EMAILS - 1) { *tmp = 0; break; } @@ -937,7 +941,7 @@ pine_parse_buf(char *buf) memset(&item, 0, sizeof(item)); for(i=0, last=0; !last ; i++) { - if( ! (end = strchr(start, '\t')) ) + if( !(end = strchr(start, '\t')) ) last=1; len = last ? strlen(start) : (int) (end-start); @@ -948,7 +952,7 @@ pine_parse_buf(char *buf) strncpy(tmp, start, len); tmp[len] = 0; if(*tmp) - item[pine_conv_table[i]] = strdup(tmp); + item[pine_conv_table[i]] = xstrdup(tmp); } start = end + 1; } @@ -977,15 +981,15 @@ pine_parse_file(FILE *in) strcpy(buf, line); fgets(line, LINESIZE, in); ptr=(char *)&line; - if(*ptr != ' ' || feof(in) ) + if(*ptr != ' ' || feof(in)) break; else - while( *ptr == ' ') + while(*ptr == ' ') ptr++; strcat(buf, ptr); } - if( *buf == '#' ) { + if(*buf == '#') { xfree(buf); continue; } @@ -1043,6 +1047,8 @@ pine_export_database(FILE *out, struct db_enumerator e) */ #define CSV_COMMENT_CHAR '#' +#define CSV_DUPLICATE_SEPARATOR " " +#define CSV_TABLE_SIZE(t) (sizeof (t) / sizeof *(t)) static int csv_conv_table[] = { NAME, @@ -1075,17 +1081,39 @@ static int allcsv_conv_table[] = { CUSTOM5, }; +static int palmcsv_conv_table[] = { + NAME, /* Last name */ + NAME, /* First name */ + NOTES, /* Title */ + NICK, /* Company */ + WORKPHONE, + PHONE, + FAX, + MOBILEPHONE, + EMAIL, + ADDRESS, + CITY, + STATE, + ZIP, + COUNTRY, + CUSTOM1, + CUSTOM2, + CUSTOM3, + CUSTOM4, + CUSTOM5, +}; + static void csv_convert_emails(char *s) { int i; char *tmp; - if( s == NULL ) + if(s == NULL) return; - for(i=1; ( tmp = strchr(s, ',') ) != NULL ; i++, s = tmp + 1 ) - if( i > MAX_EMAILS - 1 ) { + for(i = 1; ( tmp = strchr(s, ',') ) != NULL ; i++, s = tmp + 1) + if(i > MAX_EMAILS - 1) { *tmp = 0; break; } @@ -1098,7 +1126,7 @@ csv_remove_quotes(char *s) char *copy, *trimmed; int len; - copy = trimmed = strdup(s); + copy = trimmed = xstrdup(s); strtrim(trimmed); len = strlen(trimmed); @@ -1109,36 +1137,26 @@ csv_remove_quotes(char *s) } trimmed[len - 1] = 0; trimmed++; - trimmed = strdup(trimmed); + trimmed = xstrdup(trimmed); free(copy); return trimmed; } xfree(copy); - return strdup(s); + return xstrdup(s); } -static void -csv_store_field(list_item item, char *s, int field) +static int +csv_field_to_item(int *table_base, size_t table_size, int field) { - char *newstr = NULL; - - if(!s || !*s) - return; + if(field < table_size) + return table_base[field]; - if( !(newstr = csv_remove_quotes(s)) ) - return; - - if(field < (int)(sizeof(csv_conv_table) / sizeof(*csv_conv_table)) - && csv_conv_table[field] >= 0) { - item[csv_conv_table[field]] = newstr; - } else { - xfree(newstr); - } + return -1; } static void -allcsv_store_field(list_item item, char *s, int field) +csv_store_item(list_item item, int i, char *s) { char *newstr = NULL; @@ -1148,9 +1166,17 @@ allcsv_store_field(list_item item, char *s, int field) 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; + if(i >= 0) { + if (item[i] != NULL) { + char *oldstr = item[i]; + + item[i] = strconcat(newstr, CSV_DUPLICATE_SEPARATOR, + oldstr, NULL); + xfree(newstr); + xfree(oldstr); + } else { + item[i] = newstr; + } } else { xfree(newstr); } @@ -1186,7 +1212,7 @@ csv_is_valid_quote_start(char *p) } static void -csv_parse_line(char *line) +csv_parse_line(char *line, int *table_base, size_t table_size) { char *p, *start; int field; @@ -1205,9 +1231,13 @@ csv_parse_line(char *line) in_quote = TRUE; } - if( *p == ',' && !in_quote) { + if(*p == ',' && !in_quote) { *p = 0; - csv_store_field(item, start, field); + csv_field_to_item(table_base, table_size, field); + csv_store_item(item, + csv_field_to_item(table_base, + table_size, field), + start); field++; start = p + 1; } @@ -1215,51 +1245,15 @@ csv_parse_line(char *line) /* * store last field */ - csv_store_field(item, start, field); + csv_store_item(item, csv_field_to_item(table_base, table_size, field), + start); csv_convert_emails(item[EMAIL]); 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) +csv_parse_file_common(FILE *in, int *conv_table, size_t table_size) { char *line = NULL; @@ -1267,7 +1261,7 @@ csv_parse_file(FILE *in) line = getaline(in); if(line && *line && *line != CSV_COMMENT_CHAR) - csv_parse_line(line); + csv_parse_line(line, conv_table, table_size); xfree(line); } @@ -1276,20 +1270,24 @@ csv_parse_file(FILE *in) } static int -allcsv_parse_file(FILE *in) +csv_parse_file(FILE *in) { - char *line = NULL; - - while(!feof(in)) { - line = getaline(in); - - if(line && *line && *line != CSV_COMMENT_CHAR) - allcsv_parse_line(line); + return csv_parse_file_common(in, csv_conv_table, + CSV_TABLE_SIZE(csv_conv_table)); +} - xfree(line); - } +static int +allcsv_parse_file(FILE *in) +{ + return csv_parse_file_common(in, allcsv_conv_table, + CSV_TABLE_SIZE(allcsv_conv_table)); +} - return 0; +static int +palmcsv_parse_file(FILE *in) +{ + return csv_parse_file_common(in, palmcsv_conv_table, + CSV_TABLE_SIZE(palmcsv_conv_table)); } /* @@ -1576,9 +1574,9 @@ mutt_alias_genalias(int i) char *tmp, *pos; if(database[i][NICK]) - return strdup(database[i][NICK]); + return xstrdup(database[i][NICK]); - tmp = strdup(database[i][NAME]); + tmp = xstrdup(database[i][NAME]); if( ( pos = strchr(tmp, ' ') ) ) *pos = 0;