From b973375af88f3baf08d03deaec176a521f41862f Mon Sep 17 00:00:00 2001 From: Frank Lanitz Date: Mon, 28 Jan 2013 22:19:09 +0100 Subject: [PATCH 1/7] Rework TableConvert-plugin to be a little more generic. --- tableconvert/src/tableconvert.c | 161 ++++++++++++++++++++++++-------- 1 file changed, 121 insertions(+), 40 deletions(-) diff --git a/tableconvert/src/tableconvert.c b/tableconvert/src/tableconvert.c index 34f6c9f67..ee6ef6df3 100644 --- a/tableconvert/src/tableconvert.c +++ b/tableconvert/src/tableconvert.c @@ -1,7 +1,7 @@ /* * tableconvert.c * - * Copyright 2011-2012 Frank Lanitz + * Copyright 2011-2013 Frank Lanitz * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -41,9 +41,126 @@ enum COUNT_KB }; +typedef struct { + const gchar *start; + const gchar *header_start; + const gchar *header_stop; + const gchar *body_start; + const gchar *body_end; + const gchar *columnsplit; + const gchar *linestart; + const gchar *lineend; + const gchar *linesplit; + const gchar *end; +} TableConvertRule; + +enum { + TC_LATEX = 0, + TC_HTML, + TC_SQL +}; + +TableConvertRule tablerules[] = { + /* LaTeX */ + { + "\\begin{table}[h]\n\\begin{tabular}{}\n", + "", + "", + "", + "", + " & ", + "\t", + "\\\\", + "\n", + "\\end{tabular}\n\\end{table}" + }, + /* HTML */ + { + "\n", + "\n", + "\n", + "\n\t\t\n\n", + "\n\t\n\t\n", + "\n", + "\n
", + "
", + "
", + "
" + }, + /* SQL */ + { + "", + "", + "", + "", + "", + ",", + "\t(", + ")", + ",\n", + ";" + } +}; static GtkWidget *main_menu_item = NULL; +static GString* convert_to_table_worker(gchar **rows, gboolean header, gint type) +{ + guint i; + guint j; + GString *replacement_str = NULL; + + g_return_val_if_fail(rows != NULL, NULL); + + /* Adding start of table to replacement */ + replacement_str = g_string_new(tablerules[type].start); + + /* Adding special header if requested + * e.g. */ + if (header == TRUE) + { + g_string_append(replacement_str, tablerules[type].header_start); + } + + /* Iteration onto rows and building up lines of table for + * replacement */ + for (i = 0; rows[i] != NULL ; i++) + { + gchar **columns = NULL; + columns = g_strsplit_set(rows[i], "\t", -1); + + if (i == 0 && + header == TRUE) + { + g_string_append(replacement_str, tablerules[type].header_stop); + } + + g_string_append(replacement_str, tablerules[type].linestart); + + for (j = 0; columns[j] != NULL; j++) + { + if (j > 0) + { + g_string_append(replacement_str, tablerules[type].columnsplit); + } + g_string_append(replacement_str, columns[j]); + } + + g_string_append(replacement_str, tablerules[type].lineend); + + if (rows[i+1] != NULL) + { + g_string_append(replacement_str, tablerules[type].linesplit); + } + g_free(columns); + } + + /* Adding the footer of table */ + + g_string_append(replacement_str, tablerules[type].end); + return replacement_str; +} + static GString* convert_to_table_html(gchar **rows, gboolean header) { @@ -108,42 +225,6 @@ static GString* convert_to_table_html(gchar **rows, gboolean header) return replacement_str; } -static GString* convert_to_table_latex(gchar** rows, gboolean header) -{ - guint i; - guint j; - GString *replacement_str = NULL; - - g_return_val_if_fail(rows != NULL, NULL); - - /* Adding header to replacement */ - replacement_str = g_string_new("\\begin{table}[h]\n\\begin{tabular}{}\n"); - - /* Iteration onto rows and building up lines of table for - * replacement */ - for (i = 0; rows[i] != NULL ; i++) - { - gchar **columns = NULL; - columns = g_strsplit_set(rows[i], "\t", -1); - - for (j = 0; columns[j] != NULL; j++) - { - if (j > 0) - { - g_string_append(replacement_str, " & "); - } - g_string_append(replacement_str, columns[j]); - } - - g_string_append(replacement_str, "\\\\\n"); - - g_free(columns); - } - /* Adding the footer of table */ - - g_string_append(replacement_str, "\\end{tabular}\n\\end{table}"); - return replacement_str; -} static GString* convert_to_table_sql(gchar** rows) { @@ -220,17 +301,17 @@ static void convert_to_table(gboolean header) } case GEANY_FILETYPES_HTML: { - replacement_str = convert_to_table_html(rows, header); + replacement_str = convert_to_table_worker(rows, header, TC_HTML); break; } case GEANY_FILETYPES_LATEX: { - replacement_str = convert_to_table_latex(rows, header); + replacement_str = convert_to_table_worker(rows, header, TC_LATEX); break; } case GEANY_FILETYPES_SQL: { - replacement_str = convert_to_table_sql(rows); + replacement_str = convert_to_table_worker(rows, header, TC_SQL); break; } default: From 82cbec5920f47bab96a3cd0df86619ba4b180ea8 Mon Sep 17 00:00:00 2001 From: Frank Lanitz Date: Wed, 30 Jan 2013 22:20:18 +0100 Subject: [PATCH 2/7] Purge unneeded code --- tableconvert/src/tableconvert.c | 107 -------------------------------- 1 file changed, 107 deletions(-) diff --git a/tableconvert/src/tableconvert.c b/tableconvert/src/tableconvert.c index ee6ef6df3..954291702 100644 --- a/tableconvert/src/tableconvert.c +++ b/tableconvert/src/tableconvert.c @@ -161,113 +161,6 @@ static GString* convert_to_table_worker(gchar **rows, gboolean header, gint type return replacement_str; } - -static GString* convert_to_table_html(gchar **rows, gboolean header) -{ - guint i; - guint j; - GString *replacement_str = NULL; - - g_return_val_if_fail(rows != NULL, NULL); - - /* Adding header to replacement */ - replacement_str = g_string_new("\n"); - - /* Adding if requested */ - if (header == TRUE) - { - g_string_append(replacement_str, "\n"); - } - - /* Iteration onto rows and building up lines of table for - * replacement */ - for (i = 0; rows[i] != NULL ; i++) - { - gchar **columns = NULL; - columns = g_strsplit_set(rows[i], "\t", -1); - - /* Adding after first line if header and body - * is requested */ - if (i == 1 && - header == TRUE) - { - g_string_append(replacement_str, "\n"); - } - - g_string_append(replacement_str, "\t\n"); - for (j = 0; columns[j] != NULL; j++) - { - g_string_append(replacement_str, "\t\t\n"); - } - - g_string_append(replacement_str, "\t\n"); - - /* Adding closing after first row if header - * is requested */ - if (i == 0 && - header == TRUE) - { - g_string_append(replacement_str, "\n"); - } - g_free(columns); - } - - /* Adding the footer of table */ - /* Closing if requested */ - if (header == TRUE) - { - g_string_append(replacement_str, "\n"); - } - - g_string_append(replacement_str, "
"); - g_string_append(replacement_str, columns[j]); - g_string_append(replacement_str, "
\n"); - return replacement_str; -} - - -static GString* convert_to_table_sql(gchar** rows) -{ - guint i; - guint j; - GString *replacement_str = NULL; - - g_return_val_if_fail(rows != NULL, NULL); - - /* Adding start */ - replacement_str = g_string_new(""); - - /* Iteration onto rows and building up lines for replacement */ - for (i = 0; rows[i] != NULL ; i++) - { - gchar **columns = NULL; - - g_string_append(replacement_str, "\t('"); - columns = g_strsplit_set(rows[i], "\t", -1); - - for (j = 0; columns[j] != NULL; j++) - { - if (j > 0) - { - g_string_append(replacement_str, "','"); - } - g_string_append(replacement_str, columns[j]); - } - - if (rows[i+1] != NULL) - { - g_string_append(replacement_str, "'),\n"); - } - else - { - g_string_append(replacement_str, "')\n"); - } - - g_free(columns); - } - return replacement_str; -} - static void convert_to_table(gboolean header) { GeanyDocument *doc = NULL; From ba57dad437a8481ceaafc66812a2d3fb0e2fe5c7 Mon Sep 17 00:00:00 2001 From: Frank Lanitz Date: Wed, 30 Jan 2013 22:21:18 +0100 Subject: [PATCH 3/7] Added testfiles --- tableconvert/testfiles/test.html | 10 ++++++++++ tableconvert/testfiles/test.sql | 10 ++++++++++ tableconvert/testfiles/test.tex | 10 ++++++++++ 3 files changed, 30 insertions(+) create mode 100644 tableconvert/testfiles/test.html create mode 100644 tableconvert/testfiles/test.sql create mode 100644 tableconvert/testfiles/test.tex diff --git a/tableconvert/testfiles/test.html b/tableconvert/testfiles/test.html new file mode 100644 index 000000000..a4e0b1da9 --- /dev/null +++ b/tableconvert/testfiles/test.html @@ -0,0 +1,10 @@ +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff diff --git a/tableconvert/testfiles/test.sql b/tableconvert/testfiles/test.sql new file mode 100644 index 000000000..a4e0b1da9 --- /dev/null +++ b/tableconvert/testfiles/test.sql @@ -0,0 +1,10 @@ +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff diff --git a/tableconvert/testfiles/test.tex b/tableconvert/testfiles/test.tex new file mode 100644 index 000000000..a4e0b1da9 --- /dev/null +++ b/tableconvert/testfiles/test.tex @@ -0,0 +1,10 @@ +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff From c9dfcb54a2147ca0593b1c8a599748f6483a6c2b Mon Sep 17 00:00:00 2001 From: Frank Lanitz Date: Wed, 30 Jan 2013 23:07:22 +0100 Subject: [PATCH 4/7] Fix possible memory leaks --- tableconvert/src/tableconvert.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tableconvert/src/tableconvert.c b/tableconvert/src/tableconvert.c index 954291702..425589a88 100644 --- a/tableconvert/src/tableconvert.c +++ b/tableconvert/src/tableconvert.c @@ -152,7 +152,7 @@ static GString* convert_to_table_worker(gchar **rows, gboolean header, gint type { g_string_append(replacement_str, tablerules[type].linesplit); } - g_free(columns); + g_strfreev(columns); } /* Adding the footer of table */ @@ -229,7 +229,7 @@ static void convert_to_table(gboolean header) replacement = g_string_free(replacement_str, FALSE); sci_replace_sel(doc->editor->sci, replacement); } - g_free(rows); + g_strfreev(rows); g_free(replacement); } /* in case of there was no selection we are just doing nothing */ From 09dc4907d6ee09698e8f59a03fe5f8b664a55da4 Mon Sep 17 00:00:00 2001 From: Frank Lanitz Date: Wed, 30 Jan 2013 23:16:53 +0100 Subject: [PATCH 5/7] Use correct callback function --- tableconvert/src/tableconvert.c | 54 ++++++++++++++++----------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/tableconvert/src/tableconvert.c b/tableconvert/src/tableconvert.c index 425589a88..d2dc09eb6 100644 --- a/tableconvert/src/tableconvert.c +++ b/tableconvert/src/tableconvert.c @@ -63,12 +63,12 @@ enum { TableConvertRule tablerules[] = { /* LaTeX */ { - "\\begin{table}[h]\n\\begin{tabular}{}\n", - "", - "", - "", - "", - " & ", + "\\begin{table}[h]\n\\begin{tabular}{}\n", + "", + "", + "", + "", + " & ", "\t", "\\\\", "\n", @@ -76,11 +76,11 @@ TableConvertRule tablerules[] = { }, /* HTML */ { - "\n", - "\n", - "\n", - "\n\t\t\n\n", + "
", - "
\n", + "\n", + "\n", + "\n\t\t\n\n", "\n\t\n\t\n", @@ -89,15 +89,15 @@ TableConvertRule tablerules[] = { }, /* SQL */ { - "", - "", - "", - "", - "", - ",", + "", + "", + "", + "", + "", + ",", "\t(", - ")", - ",\n", + ")", + ",\n", ";" } }; @@ -111,11 +111,11 @@ static GString* convert_to_table_worker(gchar **rows, gboolean header, gint type GString *replacement_str = NULL; g_return_val_if_fail(rows != NULL, NULL); - + /* Adding start of table to replacement */ replacement_str = g_string_new(tablerules[type].start); - /* Adding special header if requested + /* Adding special header if requested * e.g. */ if (header == TRUE) { @@ -128,7 +128,7 @@ static GString* convert_to_table_worker(gchar **rows, gboolean header, gint type { gchar **columns = NULL; columns = g_strsplit_set(rows[i], "\t", -1); - + if (i == 0 && header == TRUE) { @@ -136,7 +136,7 @@ static GString* convert_to_table_worker(gchar **rows, gboolean header, gint type } g_string_append(replacement_str, tablerules[type].linestart); - + for (j = 0; columns[j] != NULL; j++) { if (j > 0) @@ -147,16 +147,16 @@ static GString* convert_to_table_worker(gchar **rows, gboolean header, gint type } g_string_append(replacement_str, tablerules[type].lineend); - + if (rows[i+1] != NULL) { g_string_append(replacement_str, tablerules[type].linesplit); } g_strfreev(columns); } - + /* Adding the footer of table */ - + g_string_append(replacement_str, tablerules[type].end); return replacement_str; } @@ -266,7 +266,7 @@ void plugin_init(GeanyData *data) gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu), main_menu_item); ui_widget_set_tooltip_text(main_menu_item, _("Converts current marked list to a table.")); - g_signal_connect(G_OBJECT(main_menu_item), "activate", G_CALLBACK(convert_to_table), NULL); + g_signal_connect(G_OBJECT(main_menu_item), "activate", G_CALLBACK(cb_table_convert), NULL); gtk_widget_show_all(main_menu_item); ui_add_document_sensitive(main_menu_item); } From 9e8b050c51e4ca8001c51d97fe17ee878fef8a70 Mon Sep 17 00:00:00 2001 From: Frank Lanitz Date: Thu, 31 Jan 2013 18:57:38 +0100 Subject: [PATCH 6/7] Improve way how worker is being called. Thanks to b4n and elextr. --- tableconvert/src/tableconvert.c | 67 ++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/tableconvert/src/tableconvert.c b/tableconvert/src/tableconvert.c index d2dc09eb6..ea021d183 100644 --- a/tableconvert/src/tableconvert.c +++ b/tableconvert/src/tableconvert.c @@ -79,8 +79,8 @@ TableConvertRule tablerules[] = { "
", + "
", "
", "
\n", "\n", "\n", - "\n\t\t\n\n", + "\n", + "\n", "\n\t\n\t\n", @@ -102,9 +102,10 @@ TableConvertRule tablerules[] = { } }; + static GtkWidget *main_menu_item = NULL; -static GString* convert_to_table_worker(gchar **rows, gboolean header, gint type) +static gchar* convert_to_table_worker(gchar **rows, gboolean header, TableConvertRule *rule) { guint i; guint j; @@ -113,52 +114,61 @@ static GString* convert_to_table_worker(gchar **rows, gboolean header, gint type g_return_val_if_fail(rows != NULL, NULL); /* Adding start of table to replacement */ - replacement_str = g_string_new(tablerules[type].start); + replacement_str = g_string_new(rule->start); /* Adding special header if requested * e.g. */ if (header == TRUE) { - g_string_append(replacement_str, tablerules[type].header_start); + g_string_append(replacement_str, rule->header_start); } /* Iteration onto rows and building up lines of table for * replacement */ - for (i = 0; rows[i] != NULL ; i++) + for (i = 0; rows[i] != NULL; i++) { gchar **columns = NULL; columns = g_strsplit_set(rows[i], "\t", -1); - if (i == 0 && + if (i == 1 && header == TRUE) { - g_string_append(replacement_str, tablerules[type].header_stop); + g_string_append(replacement_str, rule->header_stop); + /* We are assuming, that if someone inserts a head, + * only in this case we will insert some special body. + * Might needs to be discussed further */ + g_string_append(replacement_str, rule->body_start); } - g_string_append(replacement_str, tablerules[type].linestart); + g_string_append(replacement_str, rule->linestart); for (j = 0; columns[j] != NULL; j++) { if (j > 0) { - g_string_append(replacement_str, tablerules[type].columnsplit); + g_string_append(replacement_str, rule->columnsplit); } g_string_append(replacement_str, columns[j]); } - g_string_append(replacement_str, tablerules[type].lineend); + g_string_append(replacement_str, rule->lineend); if (rows[i+1] != NULL) { - g_string_append(replacement_str, tablerules[type].linesplit); + g_string_append(replacement_str, rule->linesplit); } g_strfreev(columns); } + if (header == TRUE) + { + g_string_append(replacement_str, rule->body_end); + } + /* Adding the footer of table */ + g_string_append(replacement_str, rule->end); - g_string_append(replacement_str, tablerules[type].end); - return replacement_str; + return g_string_free(replacement_str, FALSE); } static void convert_to_table(gboolean header) @@ -172,7 +182,6 @@ static void convert_to_table(gboolean header) { gchar *selection = NULL; gchar **rows = NULL; - GString *replacement_str = NULL; gchar *replacement = NULL; /* Actually grabbing selection and splitting it into single @@ -188,45 +197,49 @@ static void convert_to_table(gboolean header) { case GEANY_FILETYPES_NONE: { - g_free(rows); - g_free(replacement); + g_strfreev(rows); return; } case GEANY_FILETYPES_HTML: { - replacement_str = convert_to_table_worker(rows, header, TC_HTML); + replacement = convert_to_table_worker(rows, + header, + &tablerules[TC_HTML]); break; } case GEANY_FILETYPES_LATEX: { - replacement_str = convert_to_table_worker(rows, header, TC_LATEX); + replacement = convert_to_table_worker(rows, + header, + &tablerules[TC_LATEX]); break; } case GEANY_FILETYPES_SQL: { - replacement_str = convert_to_table_worker(rows, header, TC_SQL); + replacement = convert_to_table_worker(rows, + header, + &tablerules[TC_SQL]); break; } default: { - replacement_str = NULL; + /* We just don't do anything */ } } /* filetype switch */ } else { /* OK. Something went not as expected. - * We did have a selection but cannot parse it into rows. - * Aborting */ + * We did have a selection but cannot parse it into rows. + * Aborting */ g_warning(_("Something went wrong on parsing selection. Aborting")); return; } /* The replacement should have been prepared at this point. Let's go - * on and put it into document and replace selection with it. */ - if (replacement_str != NULL) + * on and put it into document and replace selection with it. */ + if (replacement != NULL) { - replacement = g_string_free(replacement_str, FALSE); sci_replace_sel(doc->editor->sci, replacement); } g_strfreev(rows); @@ -236,9 +249,11 @@ static void convert_to_table(gboolean header) return; } + static void kb_convert_to_table(G_GNUC_UNUSED guint key_id) { g_return_if_fail(document_get_current() != NULL); + convert_to_table(TRUE); } From 904b07a618060e900b4d5fe764ef67e127a44824 Mon Sep 17 00:00:00 2001 From: Frank Lanitz Date: Fri, 1 Feb 2013 07:33:08 +0100 Subject: [PATCH 7/7] Make rule-parameter const --- tableconvert/src/tableconvert.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tableconvert/src/tableconvert.c b/tableconvert/src/tableconvert.c index ea021d183..8bff9bebb 100644 --- a/tableconvert/src/tableconvert.c +++ b/tableconvert/src/tableconvert.c @@ -105,7 +105,8 @@ TableConvertRule tablerules[] = { static GtkWidget *main_menu_item = NULL; -static gchar* convert_to_table_worker(gchar **rows, gboolean header, TableConvertRule *rule) +static gchar* convert_to_table_worker(gchar **rows, gboolean header, + const TableConvertRule *rule) { guint i; guint j;
", - "
", "
", "