From 3c7761f9653c7cebb9adda1d7d1afedff2e286ef Mon Sep 17 00:00:00 2001 From: Evan Miller Date: Mon, 10 Aug 2020 15:58:22 -0400 Subject: [PATCH] Support --disable-glib flag with ODBC driver --- .travis.yml | 10 ++++- include/mdbfakeglib.h | 17 ++++++++ src/libmdb/data.c | 2 +- src/libmdb/fakeglib.c | 90 ++++++++++++++++++++++++++++++++++++++++ src/odbc/connectparams.c | 2 +- src/odbc/connectparams.h | 6 ++- src/odbc/odbc.c | 5 ++- 7 files changed, 126 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3a9553f..1bdb184 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,11 +13,19 @@ jobs: os: osx osx_image: xcode11.4 +addons: + apt: + packages: + - libiodbc2-dev + homebrew: + packages: + - libiodbc + before_script: - autoreconf -i -f -Wno-portability script: - - ./configure --disable-man --disable-silent-rules --disable-glib + - ./configure --disable-man --disable-silent-rules --disable-glib --with-iodbc=/usr - make - ./src/util/mdb-array test/data/ASampleDatabase.accdb "Asset Items" - ./src/util/mdb-array test/data/nwind.mdb "Customers" diff --git a/include/mdbfakeglib.h b/include/mdbfakeglib.h index 1fbc683..980dddd 100644 --- a/include/mdbfakeglib.h +++ b/include/mdbfakeglib.h @@ -33,6 +33,12 @@ typedef void (*GFunc) (gpointer data, gpointer user_data); typedef void (*GHFunc)(gpointer key, gpointer value, gpointer data); typedef gboolean (*GHRFunc)(gpointer key, gpointer value, gpointer data); +typedef struct GString { + gchar *str; + size_t len; + size_t allocated_len; +} GString; + typedef struct GPtrArray { void **pdata; int len; @@ -121,10 +127,20 @@ char *g_strconcat(const char *first, ...); char *g_strdup(const char *src); char *g_strdup_printf(const char *format, ...); gchar *g_strdelimit(gchar *string, const gchar *delimiters, gchar new_delimiter); +void g_printerr(const gchar *format, ...); + +/* GString */ +GString *g_string_new(const gchar *init); +GString *g_string_assign(GString *string, const gchar *rval); +GString * g_string_append (GString *string, const gchar *val); +gchar *g_string_free (GString *string, gboolean free_segment); /* GHashTable */ void *g_hash_table_lookup(GHashTable *tree, const void *key); +gboolean g_hash_table_lookup_extended(GHashTable *table, const void *lookup_key, + void **orig_key, void **value); void g_hash_table_insert(GHashTable *tree, void *key, void *value); +gboolean g_hash_table_remove(GHashTable *hash_table, const void *key); GHashTable *g_hash_table_new(GHashFunc hashes, GEqualFunc equals); void g_hash_table_foreach(GHashTable *tree, GHFunc function, void *data); void g_hash_table_foreach_remove(GHashTable *tree, GHRFunc function, void *data); @@ -135,6 +151,7 @@ void g_ptr_array_sort(GPtrArray *array, GCompareFunc func); void g_ptr_array_foreach(GPtrArray *array, GFunc function, gpointer user_data); GPtrArray *g_ptr_array_new(void); void g_ptr_array_add(GPtrArray *array, void *entry); +gboolean g_ptr_array_remove (GPtrArray *array, gpointer data); void g_ptr_array_free(GPtrArray *array, gboolean something); /* GList */ diff --git a/src/libmdb/data.c b/src/libmdb/data.c index 81a8776..11f58a3 100644 --- a/src/libmdb/data.c +++ b/src/libmdb/data.c @@ -608,7 +608,7 @@ mdb_ole_read(MdbHandle *mdb, MdbColumn *col, void *ole_ptr, size_t chunk_size) * mdb_ole_read_full calls mdb_ole_read then loop over mdb_ole_read_next as much as necessary. * returns the result in a big buffer. * The call must free it. - * Note that this function is not indempotent: It may be called only once per column after each bind. + * Note that this function is not idempotent: It may be called only once per column after each bind. */ void* mdb_ole_read_full(MdbHandle *mdb, MdbColumn *col, size_t *size) diff --git a/src/libmdb/fakeglib.c b/src/libmdb/fakeglib.c index 8d7ba35..c43998c 100644 --- a/src/libmdb/fakeglib.c +++ b/src/libmdb/fakeglib.c @@ -145,6 +145,51 @@ gchar *g_strdelimit(gchar *string, const gchar *delimiters, gchar new_delimiter) return orig; } +void g_printerr(const gchar *format, ...) { + va_list argp; + va_start(argp, format); + vfprintf(stderr, format, argp); + va_end(argp); +} + +/* GString */ + +GString *g_string_new (const gchar *init) { + GString *str = calloc(1, sizeof(GString)); + str->str = strdup(init ? init : ""); + str->len = strlen(str->str); + str->allocated_len = str->len+1; + return str; +} + +GString *g_string_assign(GString *string, const gchar *rval) { + size_t len = strlen(rval); + string->str = realloc(string->str, len+1); + strncpy(string->str, rval, len+1); + string->len = len; + string->allocated_len = len+1; + return string; +} + +GString * g_string_append (GString *string, const gchar *val) { + size_t len = strlen(val); + string->str = realloc(string->str, string->len + len + 1); + strncpy(&string->str[string->len], val, len+1); + string->len += len; + string->allocated_len = string->len + len + 1; + return string; +} + +gchar *g_string_free (GString *string, gboolean free_segment) { + char *data = string->str; + free(string); + if (free_segment) { + free(data); + return NULL; + } + return data; +} + /* GHashTable */ typedef struct MyNode { @@ -162,6 +207,20 @@ void *g_hash_table_lookup(GHashTable *table, const void *key) { return NULL; } +gboolean g_hash_table_lookup_extended (GHashTable *table, const void *lookup_key, + void **orig_key, void **value) { + int i; + for (i=0; iarray->len; i++) { + MyNode *node = g_ptr_array_index(table->array, i); + if (table->compare(lookup_key, node->key)) { + *orig_key = node->key; + *value = node->value; + return TRUE; + } + } + return FALSE; +} + void g_hash_table_insert(GHashTable *table, void *key, void *value) { MyNode *node = calloc(1, sizeof(MyNode)); node->value = value; @@ -169,6 +228,22 @@ void g_hash_table_insert(GHashTable *table, void *key, void *value) { g_ptr_array_add(table->array, node); } +gboolean g_hash_table_remove(GHashTable *table, gconstpointer key) { + int found = 0; + for (int i=0; iarray->len; i++) { + MyNode *node = g_ptr_array_index(table->array, i); + if (found) { + table->array->pdata[i-1] = table->array->pdata[i]; + } else if (!found && table->compare(key, node->key)) { + found = 1; + } + } + if (found) { + table->array->len--; + } + return found; +} + GHashTable *g_hash_table_new(GHashFunc hashes, GEqualFunc equals) { GHashTable *table = calloc(1, sizeof(GHashTable)); table->array = g_ptr_array_new(); @@ -219,6 +294,21 @@ void g_ptr_array_add(GPtrArray *array, void *entry) { array->pdata[array->len++] = entry; } +gboolean g_ptr_array_remove(GPtrArray *array, gpointer data) { + int found = 0; + for (int i=0; ilen; i++) { + if (found) { + array->pdata[i-1] = array->pdata[i]; + } else if (!found && array->pdata[i] == data) { + found = 1; + } + } + if (found) { + array->len--; + } + return found; +} + void g_ptr_array_free(GPtrArray *array, gboolean something) { free(array->pdata); free(array); diff --git a/src/odbc/connectparams.c b/src/odbc/connectparams.c index ad30d88..9a8296c 100644 --- a/src/odbc/connectparams.c +++ b/src/odbc/connectparams.c @@ -51,7 +51,7 @@ extern int SQLGetPrivateProfileString( LPCSTR lpszSection, int cbRetBuffer, LPCSTR lpszFilename); #else -static GString* GetIniFileName (); +static GString* GetIniFileName (void); static int FileExists (const gchar* name); static int FindSection (FILE* stream, const char* section); static int GetNextItem (FILE* stream, char** name, char** value); diff --git a/src/odbc/connectparams.h b/src/odbc/connectparams.h index 1614435..32f66e5 100644 --- a/src/odbc/connectparams.h +++ b/src/odbc/connectparams.h @@ -19,7 +19,11 @@ #ifndef _connectparams_h_ #define _connectparams_h_ +#ifdef HAVE_GLIB #include +#else +#include +#endif typedef struct { @@ -28,7 +32,7 @@ typedef struct GHashTable* table; } ConnectParams; -ConnectParams* NewConnectParams (); +ConnectParams* NewConnectParams (void); void FreeConnectParams (ConnectParams* params); gboolean LookupDSN (ConnectParams* params, const gchar* dsnName); diff --git a/src/odbc/odbc.c b/src/odbc/odbc.c index 50d8d3e..4b22f63 100644 --- a/src/odbc/odbc.c +++ b/src/odbc/odbc.c @@ -25,6 +25,7 @@ #include #include #include +#include #include "mdbodbc.h" //#define TRACE(x) fprintf(stderr,"Function %s\n", x); @@ -101,7 +102,7 @@ TypeInfo type_info[] = { #define MAX_TYPE_INFO 11 #ifdef ENABLE_ODBC_W -void my_fini(); +void my_fini(void); MDB_CONSTRUCTOR(my_init) { @@ -784,7 +785,7 @@ static SQLRETURN SQL_API _SQLDescribeCol( strcpy(sqlState, "HY090"); // Invalid string or buffer length return SQL_ERROR; } - if (snprintf(szColName, cbColNameMax, "%s", sqlcol->name) + 1 > cbColNameMax) { + if (snprintf((char *)szColName, cbColNameMax, "%s", sqlcol->name) + 1 > cbColNameMax) { strcpy(sqlState, "01004"); // String data, right truncated ret = SQL_SUCCESS_WITH_INFO; }