From aa0ce8fb3e5f951ef388c95a17aa0b68e692f574 Mon Sep 17 00:00:00 2001 From: Evan Miller Date: Wed, 2 Sep 2020 20:38:15 -0400 Subject: [PATCH] Consolidate print_col functions into backend.c --- include/mdbtools.h | 1 + src/gmdb2/table_export.c | 56 ++-------------------------------------- src/libmdb/backend.c | 50 +++++++++++++++++++++++++++++++++++ src/util/mdb-export.c | 56 +++------------------------------------- 4 files changed, 57 insertions(+), 106 deletions(-) diff --git a/include/mdbtools.h b/include/mdbtools.h index 60ed350..6b980ea 100644 --- a/include/mdbtools.h +++ b/include/mdbtools.h @@ -546,6 +546,7 @@ void mdb_register_backend(MdbHandle *mdb, char *backend_name, guint32 capabiliti gchar* (*quote_schema_name)(const gchar*, const gchar*)); int mdb_set_default_backend(MdbHandle *mdb, const char *backend_name); void mdb_print_schema(MdbHandle *mdb, FILE *outfile, char *tabname, char *dbnamespace, guint32 export_options); +void mdb_print_col(FILE *outfile, gchar *col_val, int quote_text, int col_type, int bin_len, char *quote_char, char *escape_char, int bin_mode); /* sargs.c */ int mdb_test_sargs(MdbTableDef *table, MdbField *fields, int num_fields); diff --git a/src/gmdb2/table_export.c b/src/gmdb2/table_export.c index 0c9af84..a8a9590 100644 --- a/src/gmdb2/table_export.c +++ b/src/gmdb2/table_export.c @@ -174,58 +174,6 @@ gmdb_export_help_cb(GtkWidget *w, gpointer data) } } -/* That function is a duplicate of the one in util/mdb-export.c - * They should be merged and moved in libmdb (backend.c) - */ -#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 DONT_ESCAPE_ESCAPE -void -gmdb_print_col(FILE *outfile, 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 orig_escape_len = escape_char ? strlen(escape_char) : 0; - - /* double the quote char if no escape char passed */ - if (!escape_char) - escape_char = quote_char; - - if (quote_text && is_quote_type(col_type)) { - fputs(quote_char, outfile); - while (1) { - if (is_binary_type(col_type)) { - if (bin_mode == MDB_BINEXPORT_STRIP) - break; - if (!bin_len--) - break; - } else /* use \0 sentry */ - if (!*col_val) - break; - - int is_binary_hex_col = is_binary_type(col_type) && bin_mode == MDB_BINEXPORT_HEXADECIMAL; - - if (quote_len && !strncmp(col_val, quote_char, quote_len) && !is_binary_hex_col) { - fprintf(outfile, "%s%s", escape_char, quote_char); - col_val += quote_len; -#ifndef DONT_ESCAPE_ESCAPE - } else if (orig_escape_len && !strncmp(col_val, escape_char, orig_escape_len) && !is_binary_hex_col) { - fprintf(outfile, "%s%s", escape_char, escape_char); - col_val += orig_escape_len; -#endif - } else if (is_binary_type(col_type) && *col_val <= 0 && bin_mode == MDB_BINEXPORT_OCTAL) - fprintf(outfile, "\\%03o", *(unsigned char*)col_val++); - } else if (is_binary_hex_col) - fprintf(outfilt, "%02X", *(unsigned char*)col_val++); - else - putc(*col_val++, outfile); - } - fputs(quote_char, outfile); - } else - fputs(col_val, outfile); -} - - void gmdb_table_export_button_cb(GtkWidget *w, gpointer data) { @@ -283,7 +231,7 @@ size_t length; if (i>0) fputs(delimiter, outfile); col=g_ptr_array_index(table->columns,i); - gmdb_print_col(outfile, col->name, quotechar[0]!='\0', MDB_TEXT, 0, quotechar, escape_char, bin_mode); + mdb_print_col(outfile, col->name, quotechar[0]!='\0', MDB_TEXT, 0, quotechar, escape_char, bin_mode); } } if (need_headers) fputs(lineterm, outfile); @@ -302,7 +250,7 @@ size_t length; value = bound_values[i]; length = bound_lens[i]; } - gmdb_print_col(outfile, value, quotechar[0]!='\0', col->col_type, length, quotechar, escape_char, bin_mode); + mdb_print_col(outfile, value, quotechar[0]!='\0', col->col_type, length, quotechar, escape_char, bin_mode); if (col->col_type == MDB_OLE) free(value); } diff --git a/src/libmdb/backend.c b/src/libmdb/backend.c index 1acfe5a..d5c5122 100644 --- a/src/libmdb/backend.c +++ b/src/libmdb/backend.c @@ -943,3 +943,53 @@ mdb_print_schema(MdbHandle *mdb, FILE *outfile, char *tabname, char *dbnamespace } } } + +#define is_binary_type(x) (x==MDB_OLE || x==MDB_BINARY || x==MDB_REPID) +#define is_quote_type(x) (is_binary_type(x) || x==MDB_TEXT || x==MDB_MEMO || x==MDB_DATETIME) +//#define DONT_ESCAPE_ESCAPE +void +mdb_print_col(FILE *outfile, gchar *col_val, int quote_text, int col_type, int bin_len, char *quote_char, char *escape_char, int bin_mode) +/* quote_text: Don't quote if 0. + */ +{ + size_t quote_len = strlen(quote_char); /* multibyte */ + + size_t orig_escape_len = escape_char ? strlen(escape_char) : 0; + + /* double the quote char if no escape char passed */ + if (!escape_char) + escape_char = quote_char; + + if (quote_text && is_quote_type(col_type)) { + fputs(quote_char, outfile); + while (1) { + if (is_binary_type(col_type)) { + if (bin_mode == MDB_BINEXPORT_STRIP) + break; + if (!bin_len--) + break; + } else /* use \0 sentry */ + if (!*col_val) + break; + + int is_binary_hex_col = is_binary_type(col_type) && bin_mode == MDB_BINEXPORT_HEXADECIMAL; + + if (quote_len && !strncmp(col_val, quote_char, quote_len) && !is_binary_hex_col) { + fprintf(outfile, "%s%s", escape_char, quote_char); + col_val += quote_len; +#ifndef DONT_ESCAPE_ESCAPE + } else if (orig_escape_len && !strncmp(col_val, escape_char, orig_escape_len) && !is_binary_hex_col) { + fprintf(outfile, "%s%s", escape_char, escape_char); + col_val += orig_escape_len; +#endif + } else if (is_binary_type(col_type) && bin_mode == MDB_BINEXPORT_OCTAL) { + fprintf(outfile, "\\%03o", *(unsigned char*)col_val++); + } else if (is_binary_hex_col) { + fprintf(outfile, "%02X", *(unsigned char*)col_val++); + } else + putc(*col_val++, outfile); + } + fputs(quote_char, outfile); + } else + fputs(col_val, outfile); +} diff --git a/src/util/mdb-export.c b/src/util/mdb-export.c index c30038e..c7ca9a8 100755 --- a/src/util/mdb-export.c +++ b/src/util/mdb-export.c @@ -20,58 +20,10 @@ #define EXPORT_BIND_SIZE 200000 -#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) static char *escapes(char *s); -//#define DONT_ESCAPE_ESCAPE -static void -print_col(FILE *outfile, gchar *col_val, int quote_text, int col_type, int bin_len, char *quote_char, char *escape_char, int bin_mode) -/* quote_text: Don't quote if 0. - */ -{ - size_t quote_len = strlen(quote_char); /* multibyte */ - - size_t orig_escape_len = escape_char ? strlen(escape_char) : 0; - - /* double the quote char if no escape char passed */ - if (!escape_char) - escape_char = quote_char; - - if (quote_text && is_quote_type(col_type)) { - fputs(quote_char, outfile); - while (1) { - if (is_binary_type(col_type)) { - if (bin_mode == MDB_BINEXPORT_STRIP) - break; - if (!bin_len--) - break; - } else /* use \0 sentry */ - if (!*col_val) - break; - - int is_binary_hex_col = is_binary_type(col_type) && bin_mode == MDB_BINEXPORT_HEXADECIMAL; - - if (quote_len && !strncmp(col_val, quote_char, quote_len) && !is_binary_hex_col) { - fprintf(outfile, "%s%s", escape_char, quote_char); - col_val += quote_len; -#ifndef DONT_ESCAPE_ESCAPE - } else if (orig_escape_len && !strncmp(col_val, escape_char, orig_escape_len) && !is_binary_hex_col) { - fprintf(outfile, "%s%s", escape_char, escape_char); - col_val += orig_escape_len; -#endif - } else if (is_binary_type(col_type) && bin_mode == MDB_BINEXPORT_OCTAL) { - fprintf(outfile, "\\%03o", *(unsigned char*)col_val++); - } else if (is_binary_hex_col) { - fprintf(outfile, "%02X", *(unsigned char*)col_val++); - } else - putc(*col_val++, outfile); - } - fputs(quote_char, outfile); - } else - fputs(col_val, outfile); -} int main(int argc, char **argv) { @@ -267,7 +219,7 @@ main(int argc, char **argv) value = bound_values[i]; length = bound_lens[i]; } - print_col(outfile, value, quote_text, col->col_type, length, quote_char, escape_char, bin_mode); + mdb_print_col(outfile, value, quote_text, col->col_type, length, quote_char, escape_char, bin_mode); if (col->col_type == MDB_OLE) free(value); } @@ -323,18 +275,18 @@ main(int argc, char **argv) if (!strcmp(mdb->backend_name, "sqlite") && is_binary_type(col->col_type) && bin_mode == MDB_BINEXPORT_HEXADECIMAL) { char *quote_char_binary_sqlite = (char *) g_strdup("'"); fputs("X", outfile); - print_col(outfile, value, quote_text, col->col_type, length, quote_char_binary_sqlite, escape_char, bin_mode); + mdb_print_col(outfile, value, quote_text, col->col_type, length, quote_char_binary_sqlite, escape_char, bin_mode); g_free (quote_char_binary_sqlite); /* Correctly handle insertion of binary blobs into PostgreSQL using the notation of decode('1234ABCD...', 'hex') */ } else if (!strcmp(mdb->backend_name, "postgres") && is_binary_type(col->col_type) && bin_mode == MDB_BINEXPORT_HEXADECIMAL) { char *quote_char_binary_postgres = (char *) g_strdup("'"); fputs("decode(", outfile); - print_col(outfile, value, quote_text, col->col_type, length, quote_char_binary_postgres, escape_char, bin_mode); + mdb_print_col(outfile, value, quote_text, col->col_type, length, quote_char_binary_postgres, escape_char, bin_mode); fputs(", 'hex')", outfile); g_free (quote_char_binary_postgres); /* No special treatment for other backends or when hexadecimal notation hasn't been selected with the -b hex command line option */ } else { - print_col(outfile, value, quote_text, col->col_type, length, quote_char, escape_char, bin_mode); + mdb_print_col(outfile, value, quote_text, col->col_type, length, quote_char, escape_char, bin_mode); } if (col->col_type == MDB_OLE) free(value);