Re: is this correct way to open file in buffer?



On Wed, Jan 30, 2013 at 8:39 AM, Rudra Banerjee
<rudra banerjee aol co uk> wrote:
Dear friends,
I am trying hard to get rid of file reading and editing (as evident from
my previous post)
Here is a small code where I tried to open my file in a buffer and scan.
Its small, 50 line code.
I will be grateful if anybody kindly have a look and tell if this is
really opening the file from buffer or still using the file. Please
help.

I can't tell you what it is 'still' doing, however in the code portions
which you've posted here, you are first reading the contents of the
file into a buffer using g_file_get_contents(); and then parsing
that buffer afterwards.

I assume this is what you're after, however I should point out
that naming your allocated buffer 'fd' is thoroughly confusing
to the reader.

At first glance, it would appear that your scanner is operating
on an fd, i.e. a file descriptor, not an allocated buffer.

Cheers,
        -Tristan



void open_file(GtkWidget *widget, gpointer data){
  GScanner *scanner;
  GHashTable *table;
  char* fd;
  gsize length;
  GError* error=NULL;
    GtkWidget *dialog; //, *entry;
    GtkFileFilter *filter;
    dialog = gtk_file_chooser_dialog_new("Open File", NULL,
            GTK_FILE_CHOOSER_ACTION_OPEN,
            GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
            NULL);

    filter = gtk_file_filter_new();
    gtk_file_filter_set_name(filter, "All files (*.*)");
    gtk_file_filter_add_pattern(filter, "*");
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);

    filter = gtk_file_filter_new();
    gtk_file_filter_set_name(filter, "Bibtex file (*.bib)");
    gtk_file_filter_add_pattern(filter, "*.bib");
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
    gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);

    if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
    filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
    gtk_list_store_clear (store);
    g_file_get_contents(filename, &fd, &length , &error);
    g_assert(!error);

     scanner = g_scanner_new (NULL);
     g_scanner_input_text (scanner, fd, CHAR_BUFF);

     table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
g_free);
     do
       {
         g_hash_table_remove_all (table);
           parse_entry (scanner, table);
           output_entry (table);

         g_scanner_peek_next_token (scanner);
       }
     while (scanner->next_token != G_TOKEN_EOF &&
            scanner->next_token != G_TOKEN_ERROR);


  /* finsish parsing */
     g_scanner_destroy (scanner);
     g_hash_table_destroy (table);

    gtk_label_set_text(GTK_LABEL(flabel), filename);
    gtk_widget_destroy(dialog);
    }
    else
    gtk_widget_destroy(dialog);
}

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]