void print_table(MdbTableDef *table) { int j; /* doesn't handle tables > 256 columns. Can that happen? */ char *bound_values[256]; MdbColumn *col; MdbSarg sarg; mdb_read_columns(table); sarg.op = MDB_EQUAL; // sarg.value.i = 11070; strcpy(sarg.value.s, "Reggiani Caseifici"); mdb_add_sarg_by_name(table, "ShipName", &sarg); mdb_rewind_table(table); for (j=0;j<table->num_cols;j++) { bound_values[j] = (char *) g_malloc(MDB_BIND_SIZE); bound_values[j][0] = '\0'; mdb_bind_column(table, j+1, bound_values[j], NULL); } /* print header */ col=g_ptr_array_index(table->columns,0); fprintf(stdout,"%s",col->name); for (j=1;j<table->num_cols;j++) { col=g_ptr_array_index(table->columns,j); fprintf(stdout,"%s%s",DELIMETER,col->name); } fprintf(stdout,"\n"); /* print each row */ while(mdb_fetch_row(table)) { fprintf(stdout,"%s",bound_values[0]); for (j=1;j<table->num_cols;j++) { fprintf(stdout,"%s%s",DELIMETER,bound_values[j]); } fprintf(stdout,"\n"); } /* clean up */ for (j=0;j<table->num_cols;j++) { g_free(bound_values[j]); } }
void mdb_data_dump(MdbTableDef *table) { unsigned int i; char *bound_values[MDB_MAX_COLS]; for (i=0;i<table->num_cols;i++) { bound_values[i] = (char *) g_malloc(256); mdb_bind_column(table, i+1, bound_values[i], NULL); } mdb_rewind_table(table); while (mdb_fetch_row(table)) { for (i=0;i<table->num_cols;i++) { fprintf(stdout, "column %d is %s\n", i+1, bound_values[i]); } } for (i=0;i<table->num_cols;i++) { g_free(bound_values[i]); } }
void gmdb_table_export_button_cb(GtkWidget *w, gpointer data) { gchar *file_path; FILE *outfile; gchar *bound_data[256]; MdbTableDef *table; MdbColumn *col; int i; int need_headers = 0; int need_quote = 0; gchar delimiter[11]; gchar quotechar; gchar lineterm[5]; gchar *str; int rows=0; GtkWidget *exportwin, *dlg; gmdb_export_get_delimiter(exportwin_xml, delimiter, 10); gmdb_export_get_lineterm(exportwin_xml, lineterm, 5); need_quote = gmdb_export_get_quote(exportwin_xml); quotechar = gmdb_export_get_quotechar(exportwin_xml); need_headers = gmdb_export_get_headers(exportwin_xml); file_path = gmdb_export_get_filepath(exportwin_xml); // printf("file path %s\n",file_path); if ((outfile=fopen(file_path, "w"))==NULL) { GtkWidget* dlg = gtk_message_dialog_new (GTK_WINDOW (gtk_widget_get_toplevel (w)), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_WARNING, GTK_BUTTONS_CLOSE, _("Unable to open file.")); gtk_dialog_run (GTK_DIALOG (dlg)); gtk_widget_destroy (dlg); return; } /* read table */ table = mdb_read_table(cat_entry); mdb_read_columns(table); mdb_rewind_table(table); for (i=0;i<table->num_cols;i++) { /* bind columns */ bound_data[i] = (char *) g_malloc0(MDB_BIND_SIZE); mdb_bind_column(table, i+1, bound_data[i], NULL); /* display column titles */ col=g_ptr_array_index(table->columns,i); if (need_headers) { if (i>0) fprintf(outfile,delimiter); gmdb_print_quote(outfile, need_quote, quotechar, delimiter, col->name); fprintf(outfile,"%s", col->name); gmdb_print_quote(outfile, need_quote, quotechar, delimiter, col->name); } } if (need_headers) fprintf(outfile,lineterm); /* fetch those rows! */ while(mdb_fetch_row(table)) { for (i=0;i<table->num_cols;i++) { if (i>0) fprintf(outfile,delimiter); gmdb_print_quote(outfile, need_quote, quotechar, delimiter, bound_data[i]); fprintf(outfile,"%s", bound_data[i]); gmdb_print_quote(outfile, need_quote, quotechar, delimiter, bound_data[i]); } fprintf(outfile,lineterm); rows++; } /* free the memory used to bind */ for (i=0;i<table->num_cols;i++) { g_free(bound_data[i]); } fclose(outfile); exportwin = glade_xml_get_widget (exportwin_xml, "export_dialog"); gtk_widget_destroy(exportwin); dlg = gtk_message_dialog_new (GTK_WINDOW (gtk_widget_get_toplevel (w)), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, _("%d rows successfully exported."), rows); gtk_dialog_run (GTK_DIALOG (dlg)); gtk_widget_destroy (dlg); }
int main (int argc, char **argv) { unsigned int j; MdbHandle *mdb; MdbTableDef *table; MdbColumn *col; /* doesn't handle tables > 256 columns. Can that happen? */ char *bound_values [256]; char delimiter [] = ", "; char quote_text = 1; int count = 0; int started; if (argc < 3) { fprintf (stderr, "Usage: %s <file> <table>\n", argv [0]); exit (1); } mdb_init(); mdb = mdb_open (argv [1], MDB_NOFLAGS); table = mdb_read_table_by_name (mdb, argv[2], MDB_TABLE); if (table) { mdb_read_columns (table); mdb_rewind_table (table); for (j = 0; j < table->num_cols; j++) { bound_values [j] = (char *) g_malloc (MDB_BIND_SIZE); bound_values [j] [0] = '\0'; mdb_bind_column (table, j+1, bound_values[j], NULL); } fprintf (stdout, "/******************************************************************/\n"); fprintf (stdout, "/* THIS IS AN AUTOMATICALLY GENERATED FILE. DO NOT EDIT IT!!!!!! */\n"); fprintf (stdout, "/******************************************************************/\n"); fprintf (stdout, "\n"); fprintf (stdout, "#include <stdio.h>\n"); fprintf (stdout, "#include \"types.h\"\n"); fprintf (stdout, "#include \"dump.h\"\n"); fprintf (stdout, "\n"); fprintf (stdout, "const %s %s_array [] = {\n", argv [2], argv [2]); count = 0; started = 0; while (mdb_fetch_row (table)) { if (started != 0) { fprintf (stdout, ",\n"); } started = 1; fprintf (stdout, "{\t\t\t\t/* %6d */\n\t", count); for (j = 0; j < table->num_cols; j++) { fprintf (stdout, "\t"); col = g_ptr_array_index (table->columns, j); if (quote_text && (col->col_type == MDB_TEXT || col->col_type == MDB_MEMO)) { fprintf (stdout, "\"%s\"", bound_values [j]); } else { fprintf (stdout, "%s", bound_values [j]); } if (j != table->num_cols - 1) { fprintf (stdout, "%s\n", delimiter); } else { fprintf (stdout, "\n"); } } fprintf (stdout, "}"); count++; } fprintf (stdout, "\n};\n\n"); for (j = 0; j < table->num_cols; j++) { g_free (bound_values [j]); } mdb_free_tabledef(table); } mdb_close (mdb); mdb_exit(); fprintf (stdout, "const int %s_array_length = %d;\n", argv [2], count); exit(0); }
void gmdb_table_export_button_cb(GtkWidget *w, gpointer data) { gchar *file_path; FILE *outfile; gchar *bound_data[256]; MdbTableDef *table; MdbColumn *col; int i; int need_headers = 0; int need_quote = 0; gchar delimiter[11]; gchar quotechar; gchar lineterm[5]; gchar *str; int rows=0; char msg[100]; GtkWidget *exportwin; gmdb_export_get_delimiter(exportwin_xml, delimiter, 10); gmdb_export_get_lineterm(exportwin_xml, lineterm, 5); need_quote = gmdb_export_get_quote(exportwin_xml); quotechar = gmdb_export_get_quotechar(exportwin_xml); need_headers = gmdb_export_get_headers(exportwin_xml); file_path = gmdb_export_get_filepath(exportwin_xml); // printf("file path %s\n",file_path); if ((outfile=fopen(file_path, "w"))==NULL) { gnome_warning_dialog("Unable to Open File!"); return; } /* read table */ table = mdb_read_table(cat_entry); mdb_read_columns(table); mdb_rewind_table(table); for (i=0;i<table->num_cols;i++) { /* bind columns */ bound_data[i] = (char *) malloc(MDB_BIND_SIZE); bound_data[i][0] = '\0'; mdb_bind_column(table, i+1, bound_data[i]); /* display column titles */ col=g_ptr_array_index(table->columns,i); if (need_headers) { if (i>0) fprintf(outfile,delimiter); gmdb_print_quote(outfile, need_quote, quotechar, delimiter, col->name); fprintf(outfile,"%s", col->name); gmdb_print_quote(outfile, need_quote, quotechar, delimiter, col->name); } } if (need_headers) fprintf(outfile,lineterm); /* fetch those rows! */ while(mdb_fetch_row(table)) { for (i=0;i<table->num_cols;i++) { if (i>0) fprintf(outfile,delimiter); gmdb_print_quote(outfile, need_quote, quotechar, delimiter, bound_data[i]); fprintf(outfile,"%s", bound_data[i]); gmdb_print_quote(outfile, need_quote, quotechar, delimiter, bound_data[i]); } fprintf(outfile,lineterm); rows++; } /* free the memory used to bind */ for (i=0;i<table->num_cols;i++) { free(bound_data[i]); } fclose(outfile); exportwin = glade_xml_get_widget (exportwin_xml, "export_dialog"); gtk_widget_destroy(exportwin); sprintf(msg,"%d Rows exported successfully.\n", rows); gnome_ok_dialog(msg); }
/* functions */ GtkWidget * gmdb_table_data_new(MdbCatalogEntry *entry) { MdbTableDef *table; MdbColumn *col; GtkWidget *clist; GtkWidget *scroll; int i, rownum; long row, maxrow; gchar *bound_data[256]; GMdbDataWindow *dataw = NULL; /* do we have an active window for this object? if so raise it */ for (i=0;i<g_list_length(window_list);i++) { dataw = g_list_nth_data(window_list, i); if (!strcmp(dataw->table_name, entry->object_name)) { gdk_window_raise (dataw->window->window); return dataw->window; } } dataw = g_malloc(sizeof(GMdbDataWindow)); strcpy(dataw->table_name, entry->object_name); dataw->window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(dataw->window), entry->object_name); gtk_widget_set_usize(dataw->window, 300,200); gtk_widget_set_uposition(dataw->window, 50,50); gtk_widget_show(dataw->window); gtk_signal_connect (GTK_OBJECT (dataw->window), "delete_event", GTK_SIGNAL_FUNC (gmdb_table_data_close), dataw); scroll = gtk_scrolled_window_new(NULL,NULL); gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); gtk_widget_show (scroll); gtk_container_add(GTK_CONTAINER(dataw->window), scroll); /* read table */ table = mdb_read_table(entry); mdb_read_columns(table); mdb_rewind_table(table); clist = gtk_clist_new(table->num_cols); gtk_widget_show(clist); gtk_container_add(GTK_CONTAINER(scroll),clist); for (i=0;i<table->num_cols;i++) { /* bind columns */ bound_data[i] = (char *) g_malloc0(MDB_BIND_SIZE); mdb_bind_column(table, i+1, bound_data[i], NULL); /* display column titles */ col=g_ptr_array_index(table->columns,i); gtk_clist_set_column_title(GTK_CLIST(clist), i, col->name); } gtk_clist_column_titles_show(GTK_CLIST(clist)); maxrow = gmdb_prefs_get_maxrows(); /* fetch those rows! */ row = 0; while(mdb_fetch_row(table) && (!maxrow || (row < maxrow))) { row++; rownum = gtk_clist_append(GTK_CLIST(clist), bound_data); } /* free the memory used to bind */ for (i=0;i<table->num_cols;i++) { g_free(bound_data[i]); } /* add this one to the window list */ window_list = g_list_append(window_list, dataw); return dataw->window; }