From 30c0ab964ff62ef95b1a0ec9c04ebbbb0cd51c43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrik=20Thunstr=C3=B6m?= Date: Sun, 25 Feb 2024 00:07:45 +0100 Subject: [PATCH] CSV Export: Last column no longer exported when hidden. Prior to fix the last column no matter if hidden or not was always included when exporting packet dissections to CSV. Fixes #19666. --- epan/print.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/epan/print.c b/epan/print.c index 40ea286d9e..1c0a8c5f80 100644 --- a/epan/print.c +++ b/epan/print.c @@ -1635,13 +1635,17 @@ static gchar *csv_massage_str(const gchar *source, const gchar *exceptions) return csv_str; } -static void csv_write_str(const char *str, char sep, FILE *fh) +static void csv_write_str(const char *str, char sep, FILE *fh, gboolean print_separator) { gchar *csv_str; /* Do not escape the UTF-8 right arrow character */ csv_str = csv_massage_str(str, UTF8_RIGHTWARDS_ARROW); - fprintf(fh, "\"%s\"%c", csv_str, sep); + if (print_separator) { + fprintf(fh, "%c\"%s\"", sep, csv_str); + } else { + fprintf(fh, "\"%s\"", csv_str); + } g_free(csv_str); } @@ -1649,26 +1653,36 @@ void write_csv_column_titles(column_info *cinfo, FILE *fh) { gint i; + gboolean print_separator = FALSE; + // Avoid printing separator for first column - for (i = 0; i < cinfo->num_cols - 1; i++) { + for (i = 0; i < cinfo->num_cols; i++) { if (!get_column_visible(i)) continue; - csv_write_str(cinfo->columns[i].col_title, ',', fh); + csv_write_str(cinfo->columns[i].col_title, ',', fh, print_separator); + print_separator = TRUE; + } + if (print_separator) { // Only add line break if anything was output + fprintf(fh, "\n"); } - csv_write_str(cinfo->columns[i].col_title, '\n', fh); } void write_csv_columns(epan_dissect_t *edt, FILE *fh) { gint i; + gboolean print_separator = FALSE; + // Avoid printing separator for first column - for (i = 0; i < edt->pi.cinfo->num_cols - 1; i++) { + for (i = 0; i < edt->pi.cinfo->num_cols; i++) { if (!get_column_visible(i)) continue; - csv_write_str(get_column_text(edt->pi.cinfo, i), ',', fh); + csv_write_str(get_column_text(edt->pi.cinfo, i), ',', fh, print_separator); + print_separator = TRUE; + } + if (print_separator) { // Only add line break if anything was output + fprintf(fh, "\n"); } - csv_write_str(get_column_text(edt->pi.cinfo,i), '\n', fh); } void