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

2013-01-30 Thread Gregory Hosler
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On 01/30/2013 07:39 AM, Rudra Banerjee 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 am coming late into this thread, so please forgive the following question(s).

I would like clarification opening the file from buffer or still using the 
file.

In C, there is an api that allows us to open a file, and then associate a
memory location with the file's contents. There after, any modification to the
file, automatically shows in the buffer (i.e. no read() or re-read is
required) and any modification to the file buffer will automatically show on
disk (without the necessity of a write()).

Is this what you are trying to do ?

If yes, then the below will not accomplish this. The C api is to do a standard
open(2) and then a mmap(2) to associate the file's buffer to a fixed memory
location - I am happy to supply more details upon a confirmation.

All the best,

- -Greg

 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
 


- -- 
+-+

Please also check the log file at /dev/null for additional information.
(from /var/log/Xorg.setup.log)

| Greg Hosler   ghos...@redhat.com|
+-+
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.13 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEARECAAYFAlEIYewACgkQ404fl/0CV/TEZwCgrUZ9iXok5ykJLMAg0dn8QfAU
EDsAn2oAgw6aoXMG39yWQSu1RW7o8Gux
=b+22
-END PGP SIGNATURE-
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


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

2013-01-30 Thread Rudra Banerjee
Well, 
my final goal is to able to edit the file, but not save every time
something  has changed. 
So, in irc and other place, i was instructed to use get_contents method.
Can you kindly clarify where I went wrong?
On Wed, 2013-01-30 at 07:57 +0800, Gregory Hosler wrote:
 On 01/30/2013 07:39 AM, Rudra Banerjee 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 am coming late into this thread, so please forgive the following 
 question(s).
 
 I would like clarification opening the file from buffer or still using the 
 file.
 
 In C, there is an api that allows us to open a file, and then associate a
 memory location with the file's contents. There after, any modification to the
 file, automatically shows in the buffer (i.e. no read() or re-read is
 required) and any modification to the file buffer will automatically show on
 disk (without the necessity of a write()).
 
 Is this what you are trying to do ?
 
 If yes, then the below will not accomplish this. The C api is to do a standard
 open(2) and then a mmap(2) to associate the file's buffer to a fixed memory
 location - I am happy to supply more details upon a confirmation.
 
 All the best,
 
 -Greg
 
  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
  
 
 


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


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

2013-01-30 Thread Tristan Van Berkom
On Wed, Jan 30, 2013 at 8:39 AM, Rudra Banerjee
rudra.baner...@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
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list