Added option to choose how to export blobs

Supported modes are:
- strip (don't export)
- raw (compatibility)
- octal (export as \ooo)

Note that gmdb2 dialog where updated, but not the code. Many fixes to do
there first :/
This commit is contained in:
Nirgal Vourgère
2012-12-02 20:52:21 +01:00
parent 473d820239
commit a66531c74a
6 changed files with 110 additions and 30 deletions

View File

@@ -2,7 +2,7 @@ NAME
mdb-export - Export data in an MDB database table to CSV format. mdb-export - Export data in an MDB database table to CSV format.
SYNOPSIS SYNOPSIS
mdb-export [-H] [-d delimiter] [-R row_delim] [[-Q] | [-q quote [-X escape]]] [-I backend] [-D format] [-N namespace] database table mdb-export [-H] [-d delimiter] [-R row_delim] [[-Q] | [-q quote [-X escape]]] [-I backend] [-D format] [-N namespace] [-b strip|raw|octal] database table
DESCRIPTION DESCRIPTION
mdb-export is a utility program distributed with MDB Tools. mdb-export is a utility program distributed with MDB Tools.
@@ -19,6 +19,7 @@ OPTIONS
-q Use to wrap text-like fields. Default is ". -q Use to wrap text-like fields. Default is ".
-X Use to escape quoted characters within a field. Default is doubling. -X Use to escape quoted characters within a field. Default is doubling.
-N namespace Prefix identifiers with namespace. -N namespace Prefix identifiers with namespace.
-b strip|raw|octal Binary export mode: strip binaries, export as-is, or output \ooo style octal data.
NOTES NOTES

View File

@@ -13,7 +13,7 @@
<widget class="GtkTable" id="table1"> <widget class="GtkTable" id="table1">
<property name="visible">True</property> <property name="visible">True</property>
<property name="border_width">30</property> <property name="border_width">30</property>
<property name="n_rows">6</property> <property name="n_rows">7</property>
<property name="n_columns">2</property> <property name="n_columns">2</property>
<property name="column_spacing">30</property> <property name="column_spacing">30</property>
<property name="row_spacing">8</property> <property name="row_spacing">8</property>
@@ -93,8 +93,8 @@
<property name="use_markup">True</property> <property name="use_markup">True</property>
</widget> </widget>
<packing> <packing>
<property name="top_attach">5</property> <property name="top_attach">6</property>
<property name="bottom_attach">6</property> <property name="bottom_attach">7</property>
<property name="x_options">GTK_FILL</property> <property name="x_options">GTK_FILL</property>
<property name="y_options"></property> <property name="y_options"></property>
</packing> </packing>
@@ -112,8 +112,8 @@
<packing> <packing>
<property name="left_attach">1</property> <property name="left_attach">1</property>
<property name="right_attach">2</property> <property name="right_attach">2</property>
<property name="top_attach">5</property> <property name="top_attach">6</property>
<property name="bottom_attach">6</property> <property name="bottom_attach">7</property>
<property name="x_options">GTK_FILL</property> <property name="x_options">GTK_FILL</property>
<property name="y_options"></property> <property name="y_options"></property>
</packing> </packing>
@@ -176,6 +176,32 @@
<property name="bottom_attach">5</property> <property name="bottom_attach">5</property>
</packing> </packing>
</child> </child>
<child>
<widget class="GtkLabel" id="label8">
<property name="visible">True</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">&lt;b&gt;Binaries:&lt;/b&gt;</property>
<property name="use_markup">True</property>
</widget>
<packing>
<property name="top_attach">5</property>
<property name="bottom_attach">6</property>
<property name="x_options">GTK_FILL</property>
<property name="y_options"></property>
</packing>
</child>
<child>
<widget class="GtkComboBox" id="bin_combo">
<property name="visible">True</property>
<property name="items" translatable="yes"></property>
</widget>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">5</property>
<property name="bottom_attach">6</property>
</packing>
</child>
</widget> </widget>
<packing> <packing>
<property name="position">2</property> <property name="position">2</property>

View File

@@ -88,6 +88,7 @@ void gmdb_export_get_delimiter(GladeXML *xml, gchar *delimiter, int max_buf);
void gmdb_export_get_lineterm(GladeXML *xml, gchar *lineterm, int max_buf); void gmdb_export_get_lineterm(GladeXML *xml, gchar *lineterm, int max_buf);
int gmdb_export_get_quote(GladeXML *xml); int gmdb_export_get_quote(GladeXML *xml);
char gmdb_export_get_quotechar(GladeXML *xml); char gmdb_export_get_quotechar(GladeXML *xml);
int gmdb_export_get_binmode(GladeXML *xml);
int gmdb_export_get_headers(GladeXML *xml); int gmdb_export_get_headers(GladeXML *xml);
gchar *gmdb_export_get_filepath(GladeXML *xml); gchar *gmdb_export_get_filepath(GladeXML *xml);

View File

@@ -60,6 +60,7 @@ gmdb_sql_write_rslt_cb(GtkWidget *w, GladeXML *xml)
int need_quote = 0; int need_quote = 0;
gchar delimiter[11]; gchar delimiter[11];
gchar quotechar; gchar quotechar;
//TODO int binmode;
gchar lineterm[5]; gchar lineterm[5];
gchar *str; gchar *str;
int rows=0, n_columns; int rows=0, n_columns;
@@ -78,6 +79,7 @@ gmdb_sql_write_rslt_cb(GtkWidget *w, GladeXML *xml)
gmdb_export_get_lineterm(xml, lineterm, 5); gmdb_export_get_lineterm(xml, lineterm, 5);
need_quote = gmdb_export_get_quote(xml); need_quote = gmdb_export_get_quote(xml);
quotechar = gmdb_export_get_quotechar(xml); quotechar = gmdb_export_get_quotechar(xml);
//TODO binmode = gmdb_export_get_binmode(xml);
need_headers = gmdb_export_get_headers(xml); need_headers = gmdb_export_get_headers(xml);
file_path = gmdb_export_get_filepath(xml); file_path = gmdb_export_get_filepath(xml);

View File

@@ -42,6 +42,10 @@ MdbCatalogEntry *cat_entry;
#define NEVER "Never" #define NEVER "Never"
#define AUTOMAT "Automatic (where necessary)" #define AUTOMAT "Automatic (where necessary)"
#define BIN_STRIP "Strip"
#define BIN_RAW "Raw"
#define BIN_OCTAL "Octal"
void void
gmdb_print_quote(FILE *outfile, int need_quote, char quotechar, char *colsep, char *str) gmdb_print_quote(FILE *outfile, int need_quote, char quotechar, char *colsep, char *str)
{ {
@@ -116,6 +120,22 @@ gmdb_export_get_quotechar(GladeXML *xml)
return quotechar; return quotechar;
} }
int
gmdb_export_get_binmode(GladeXML *xml)
{
GtkComboBox *combobox;
gchar *str;
combobox = GTK_COMBO_BOX(glade_xml_get_widget(xml, "bin_combo"));
str = gtk_combo_box_get_active_text (combobox);
if (!strcmp(str,BIN_STRIP)) return 1;
else if (!strcmp(str,BIN_OCTAL)) return 2;
else return 0;
}
int int
gmdb_export_get_headers(GladeXML *xml) gmdb_export_get_headers(GladeXML *xml)
{ {
@@ -161,6 +181,7 @@ int need_quote = 0;
gchar delimiter[11]; gchar delimiter[11];
gchar quotechar; gchar quotechar;
gchar lineterm[5]; gchar lineterm[5];
int binmode = 1;
int rows=0; int rows=0;
GtkWidget *exportwin, *dlg; GtkWidget *exportwin, *dlg;
@@ -170,6 +191,7 @@ int rows=0;
need_quote = gmdb_export_get_quote(exportwin_xml); need_quote = gmdb_export_get_quote(exportwin_xml);
quotechar = gmdb_export_get_quotechar(exportwin_xml); quotechar = gmdb_export_get_quotechar(exportwin_xml);
need_headers = gmdb_export_get_headers(exportwin_xml); need_headers = gmdb_export_get_headers(exportwin_xml);
binmode = gmdb_export_get_binmode(exportwin_xml);
file_path = gmdb_export_get_filepath(exportwin_xml); file_path = gmdb_export_get_filepath(exportwin_xml);
// printf("file path %s\n",file_path); // printf("file path %s\n",file_path);
@@ -271,4 +293,10 @@ gmdb_table_export_populate_dialog(GladeXML *xml)
gtk_combo_box_append_text(combobox, "'"); gtk_combo_box_append_text(combobox, "'");
gtk_combo_box_append_text(combobox, "`"); gtk_combo_box_append_text(combobox, "`");
gtk_combo_box_set_active(combobox, 0); gtk_combo_box_set_active(combobox, 0);
combobox = GTK_COMBO_BOX(glade_xml_get_widget(xml, "bin_combo"));
gtk_combo_box_append_text(combobox, BIN_STRIP);
gtk_combo_box_append_text(combobox, BIN_RAW);
gtk_combo_box_append_text(combobox, BIN_OCTAL);
gtk_combo_box_set_active(combobox, 1);
} }

View File

@@ -28,11 +28,15 @@
#define is_quote_type(x) (x==MDB_TEXT || x==MDB_OLE || x==MDB_MEMO || x==MDB_DATETIME || x==MDB_BINARY || x==MDB_REPID) #define is_quote_type(x) (x==MDB_TEXT || x==MDB_OLE || x==MDB_MEMO || x==MDB_DATETIME || x==MDB_BINARY || x==MDB_REPID)
#define is_binary_type(x) (x==MDB_OLE || x==MDB_BINARY || x==MDB_REPID) #define is_binary_type(x) (x==MDB_OLE || x==MDB_BINARY || x==MDB_REPID)
#define BIN_MODE_STRIP 0
#define BIN_MODE_RAW 1
#define BIN_MODE_OCTAL 2
static char *escapes(char *s); static char *escapes(char *s);
//#define DONT_ESCAPE_ESCAPE //#define DONT_ESCAPE_ESCAPE
static void static void
print_col(gchar *col_val, int quote_text, int col_type, int bin_len, char *quote_char, char *escape_char) print_col(gchar *col_val, int quote_text, int col_type, int bin_len, char *quote_char, char *escape_char, int bin_mode)
{ {
size_t quote_len = strlen(quote_char); /* multibyte */ size_t quote_len = strlen(quote_char); /* multibyte */
@@ -46,6 +50,8 @@ print_col(gchar *col_val, int quote_text, int col_type, int bin_len, char *quote
fputs(quote_char,stdout); fputs(quote_char,stdout);
while (1) { while (1) {
if (is_binary_type(col_type)) { if (is_binary_type(col_type)) {
if (bin_mode == BIN_MODE_STRIP)
break;
if (!bin_len--) if (!bin_len--)
break; break;
} else /* use \0 sentry */ } else /* use \0 sentry */
@@ -60,7 +66,9 @@ print_col(gchar *col_val, int quote_text, int col_type, int bin_len, char *quote
fprintf(stdout, "%s%s", escape_char, escape_char); fprintf(stdout, "%s%s", escape_char, escape_char);
col_val += orig_escape_len; col_val += orig_escape_len;
#endif #endif
} else } else if (is_binary_type(col_type) && *col_val < 0 && bin_mode == BIN_MODE_OCTAL)
fprintf(stdout, "\\%03o", *(unsigned char*)col_val++);
else
putc(*col_val++, stdout); putc(*col_val++, stdout);
} }
fputs(quote_char, stdout); fputs(quote_char, stdout);
@@ -84,11 +92,12 @@ main(int argc, char **argv)
char quote_text = 1; char quote_text = 1;
char *insert_dialect = NULL; char *insert_dialect = NULL;
char *namespace = NULL; char *namespace = NULL;
int bin_mode = BIN_MODE_RAW;
int opt; int opt;
char *value; char *value;
size_t length; size_t length;
while ((opt=getopt(argc, argv, "HQq:X:d:D:R:I:N:"))!=-1) { while ((opt=getopt(argc, argv, "HQq:X:d:D:R:I:N:b:"))!=-1) {
switch (opt) { switch (opt) {
case 'H': case 'H':
header_row = 0; header_row = 0;
@@ -118,6 +127,18 @@ main(int argc, char **argv)
case 'N': case 'N':
namespace = (char *) g_strdup(optarg); namespace = (char *) g_strdup(optarg);
break; break;
case 'b':
if (!strcmp(optarg, "strip"))
bin_mode = BIN_MODE_STRIP;
else if (!strcmp(optarg, "raw"))
bin_mode = BIN_MODE_RAW;
else if (!strcmp(optarg, "octal"))
bin_mode = BIN_MODE_OCTAL;
else {
fprintf(stderr, "Invalid binary mode\n");
exit(1);
}
break;
default: default:
break; break;
} }
@@ -148,6 +169,7 @@ main(int argc, char **argv)
fprintf(stderr," -q <char> Use <char> to wrap text-like fields. Default is \".\n"); fprintf(stderr," -q <char> Use <char> to wrap text-like fields. Default is \".\n");
fprintf(stderr," -X <char> Use <char> to escape quoted characters within a field. Default is doubling.\n"); fprintf(stderr," -X <char> Use <char> to escape quoted characters within a field. Default is doubling.\n");
fprintf(stderr," -N <namespace> Prefix identifiers with namespace\n"); fprintf(stderr," -N <namespace> Prefix identifiers with namespace\n");
fprintf(stderr," -b strip|raw|octal Binary export mode.\n");
g_free (delimiter); g_free (delimiter);
g_free (row_delimiter); g_free (row_delimiter);
g_free (quote_char); g_free (quote_char);
@@ -231,7 +253,7 @@ main(int argc, char **argv)
value = bound_values[j]; value = bound_values[j];
length = bound_lens[j]; length = bound_lens[j];
} }
print_col(value, quote_text, col->col_type, length, quote_char, escape_char); print_col(value, quote_text, col->col_type, length, quote_char, escape_char, bin_mode);
if (col->col_type == MDB_OLE) if (col->col_type == MDB_OLE)
free(value); free(value);
} }