* fix CSV quoting (fields with commas)

This commit is contained in:
djcb 2013-05-27 04:23:49 -07:00
parent f4d3eb2d7a
commit c50c0695e4
3 changed files with 45 additions and 5 deletions

View File

@ -456,7 +456,7 @@ mu_util_fputs_encoded (const char *str, FILE *stream)
if (!conv || err) {
/* conversion failed; this happens because is
* some cases GMime may gives us non-UTF-8
* string from e.g. wrongly encoded
* strings from e.g. wrongly encoded
* message-subjects; if so, we escape the
* string */
g_warning ("%s: g_locale_from_utf8 failed: %s",

View File

@ -1,4 +1,4 @@
.TH MU CFIND 1 "October 2012" "User Manuals"
.TH MU CFIND 1 "May 2013" "User Manuals"
.SH NAME
@ -43,7 +43,8 @@ list of contacts. Note, \fBmu cfind\fR uses does not directly use the
database, but uses a cache file with e-mail addresses, which is populated
during the indexing process.
The regular expressions are Perl-compatible (as per the PCRE-library).
The regular expressions are Perl-compatible (as per the PCRE-library used by
GRegex).
.SH OPTIONS
@ -60,9 +61,15 @@ sets the output format to the given value. The following are available:
| wl | wanderlust addressbook format |
| org-contact | org-mode org-contact format |
| bbdb | BBDB format |
| csv | comma-separated values |
| csv | comma-separated values (*) |
.fi
(*) CSV is not really standardized, but \fBmu cfind\fR follows some common
practices: any double-quote is replaced by a double-double quote (thus,
"hello" become ""hello"", and fields with commas are put in
double-quotes. Normally, this should only apply to name fields.
.TP
\fB\-\-personal\fR only show addresses seen in messages where one of 'my'
e-mail addresses was seen in one of the address fields; this is to exclude

View File

@ -231,6 +231,38 @@ each_contact_org_contact (const char *email, const char *name)
name, email);
}
static void
print_csv_field (const char *str)
{
char *s;
if (!str)
return;
s = mu_str_replace (str, "\"", "\"\"");
if (strchr (s, ','))
mu_util_print_encoded ("\"%s\"", s);
else
mu_util_print_encoded ("%s", s);
g_free (s);
}
static void
each_contact_csv (const char *email, const char *name)
{
print_csv_field (name);
mu_util_print_encoded (",");
print_csv_field (email);
mu_util_print_encoded ("\n");
}
static void
print_plain (const char *email, const char *name, gboolean color)
{
@ -259,6 +291,7 @@ struct _ECData {
typedef struct _ECData ECData;
static void
each_contact (const char *email, const char *name, gboolean personal,
time_t tstamp, ECData *ecdata)
@ -287,7 +320,7 @@ each_contact (const char *email, const char *name, gboolean personal,
each_contact_bbdb (email, name, tstamp);
break;
case MU_CONFIG_FORMAT_CSV:
mu_util_print_encoded ("%s,%s\n", name ? name : "", email);
each_contact_csv (email, name);
break;
default:
print_plain (email, name, ecdata->color);