/* callbacks */ static void gmdb_sql_write_rslt_cb(GtkWidget *w, GladeXML *xml) { gchar *file_path; GladeXML *sql_xml; GtkWidget *filesel, *dlg; FILE *outfile; int i; int need_headers = 0; int need_quote = 0; gchar delimiter[11]; gchar quotechar; gchar lineterm[5]; gchar *str, *buf; int rows=0, n_columns; GtkWidget *treeview; GtkTreeViewColumn *col; GList *glist; GtkTreeStore *store; GtkTreeIter iter; GValue value = { 0, }; filesel = glade_xml_get_widget (xml, "export_dialog"); sql_xml = g_object_get_data(G_OBJECT(filesel), "sql_xml"); printf("sql_xml %lu\n",sql_xml); gmdb_export_get_delimiter(xml, delimiter, 10); gmdb_export_get_lineterm(xml, lineterm, 5); need_quote = gmdb_export_get_quote(xml); quotechar = gmdb_export_get_quotechar(xml); need_headers = gmdb_export_get_headers(xml); file_path = gmdb_export_get_filepath(xml); if ((outfile=fopen(file_path, "w"))==NULL) { 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; } treeview = glade_xml_get_widget (sql_xml, "sql_results"); glist = gtk_tree_view_get_columns(GTK_TREE_VIEW(treeview)); i = 0; if (need_headers) { while (col = g_list_nth_data(glist, i)) { gchar *title; if (i>0) fputs(delimiter, outfile); title = g_strdup(gtk_tree_view_column_get_title(col)); gmdb_print_quote(outfile, need_quote, quotechar, delimiter, title); fputs(title, outfile); gmdb_print_quote(outfile, need_quote, quotechar, delimiter, title); g_free(title); i++; } fputs(lineterm, outfile); g_list_free(glist); } store = (GtkTreeStore *) gtk_tree_view_get_model(GTK_TREE_VIEW(treeview)); gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter); rows=0; g_value_init (&value, G_TYPE_STRING); do { rows++; n_columns = gtk_tree_model_get_n_columns(GTK_TREE_MODEL(store)); for (i=0; i < n_columns; i++) { if (i>0) fputs(delimiter, outfile); gtk_tree_model_get_value(GTK_TREE_MODEL(store), &iter, i, &value); str = (gchar *) g_value_get_string(&value); gmdb_print_quote(outfile, need_quote, quotechar, delimiter, str); fputs(str, outfile); gmdb_print_quote(outfile, need_quote, quotechar, delimiter, str); g_value_unset(&value); } fputs(lineterm, outfile); } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter)); fclose(outfile); gtk_widget_destroy(filesel); dlg = gtk_message_dialog_new (GTK_WINDOW (gtk_widget_get_toplevel (w)), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_WARNING, GTK_BUTTONS_CLOSE, _("%d rows successfully exported."), rows); gtk_dialog_run (GTK_DIALOG (dlg)); gtk_widget_destroy (dlg); }
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); }
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); }