GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Nov 24, 2014 4:23 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: utf-8 pango error
PostPosted: Fri Sep 28, 2012 2:09 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Dear friends,
with the help of Tadej and others, I have written the lexical scanner for bibtex that will read from file. For the time being, its writing in a file, and treeview is reading the file.
The problem is when I am reading those files(say,fauth.dat) as array element(aauth[i]), and showing on treeview, its giving box like character, giving warning:
Quote:
Pango-WARNING **: Invalid UTF-8 string passed to pango_layout_set_text()

but if I print them using g_print, they are printed correctly,
Quote:
$ ./mkbib
Content9
Chowdhury, D.


All the files are in ASCII
Quote:
$ file fauth.dat
fauth.dat: ASCII text


So, not really sure what is causing the trouble.
I am listing all the codes that are involved(in my opinion), which is making it too large a post. But will probably you can diagnose the source of error with ease.
Please Help.
The code for the parsing and tree store is as:
Code:
  GScanner *scanner;
  GHashTable *table;
  guint ret;
  scanner = g_scanner_new (NULL);
  gint ttest=-1;
  ttest=g_open(filename,O_RDONLY);

  g_scanner_input_file (scanner, ttest);

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

      if (ret == G_TOKEN_ERROR)
        continue;
      g_scanner_peek_next_token (scanner);
    }
  while (scanner->next_token != G_TOKEN_EOF &&
         scanner->next_token != G_TOKEN_ERROR);
//if(g_ascii_strcasecmp (key,"author")==0)g_print("%s frm_main %s\n",key,val);
  /* finsish parsing */
  g_scanner_destroy (scanner);
  g_hash_table_destroy (table);

    }
    int i=0;//,j=0,k=0;
    int num_line=-1, ch;
    FILE *fauth, *fyear, *ftitle;
GString *content=g_string_new(NULL);
GIOChannel *tfauth;
gsize bytes;
tfauth=g_io_channel_new_file("fauth.dat","r",NULL);
do{
  num_line++;
}while(g_io_channel_read_line_string(tfauth, content,&bytes, NULL) != G_IO_STATUS_EOF);
g_print("Content%d\n", num_line);

    char aauth[num_line][128],ayear[num_line][4],atitle[num_line][128];//, buffer[500];
    fauth=g_fopen("fauth.dat","r");
    fyear=g_fopen("fyear.dat","r");
    ftitle=g_fopen("ftitle.dat","r");
    if(!fauth||!ftitle||!fyear){
            printf("fauth failed\n");
    }
    else{
  while (i < num_line && fgets(aauth[i],  sizeof aauth[i], fauth)
        && fgets(ayear[i],  sizeof ayear[i], fyear)
        && fgets(atitle[i],  sizeof atitle[i], ftitle))
    g_printf("%s\n",aauth[i]);
     i++;
    }
   fclose(fauth);
   fclose(fyear);
   fclose(ftitle);
    for (i = 0; i < num_line; i++){
                gtk_tree_store_append(GTK_TREE_STORE(treestore), &toplevel,NULL);
                gtk_tree_store_set(treestore, &toplevel,
                      COL_BIB_TYPE, NULL,
                      COL_BIB_NAME, aauth[i],
                      COL_BIB_YEAR, ayear[i],
                      COL_BIB_PUB,  atitle[i],
                      -1);
    }


The treestore is defined as:
Code:
static GtkTreeModel *
create_and_fill_model(void) {

    treestore = gtk_tree_store_new(NUM_COLS,
            G_TYPE_STRING,
            G_TYPE_STRING,
            G_TYPE_STRING,
            G_TYPE_STRING);


    return GTK_TREE_MODEL(treestore);
}

static GtkWidget *
create_view_and_model(void) {
    GtkTreeViewColumn *col;
    GtkCellRenderer *renderer;
    GtkWidget *view;
    GtkTreeModel *model;

    view = gtk_tree_view_new();

    /* --- Column #0 --- */
    col = gtk_tree_view_column_new();
    gtk_tree_view_column_set_title(col, "Type");
    gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
    renderer = gtk_cell_renderer_text_new();
    gtk_tree_view_column_pack_start(col, renderer, TRUE);
    gtk_tree_view_column_add_attribute(col, renderer, "text", COL_BIB_TYPE);


    /* --- Column #1 --- */
    col = gtk_tree_view_column_new();
    gtk_tree_view_column_set_title(col, "Name");
    gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
    renderer = gtk_cell_renderer_text_new();
    gtk_tree_view_column_pack_start(col, renderer, TRUE);
    gtk_tree_view_column_add_attribute(col, renderer, "text", COL_BIB_NAME);


    /* --- Column #2 --- */
    col = gtk_tree_view_column_new();
    gtk_tree_view_column_set_title(col, "Year");
    gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
    renderer = gtk_cell_renderer_text_new();
    gtk_tree_view_column_pack_start(col, renderer, TRUE);
    gtk_tree_view_column_add_attribute(col, renderer, "text", COL_BIB_YEAR);
/*    g_object_set(renderer,
            "weight", PANGO_WEIGHT_BOLD,
            "weight-set", TRUE,
            NULL);
*/      

    /* --- Column #3 --- */
    col = gtk_tree_view_column_new();
    gtk_tree_view_column_set_title(col, "Journal");
    gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
    renderer = gtk_cell_renderer_text_new();
    gtk_tree_view_column_pack_start(col, renderer, TRUE);
    gtk_tree_view_column_add_attribute(col, renderer, "text", COL_BIB_PUB);
    /* connect a cell data function */
    //  gtk_tree_view_column_set_cell_data_func(col, renderer, age_cell_data_func, NULL, NULL);


    model = create_and_fill_model();

    gtk_tree_view_set_model(GTK_TREE_VIEW(view), model);
    g_object_unref(model); /* destroy model automatically with view */
    gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(view)),
            GTK_SELECTION_NONE);

    return view;
}


and the parsing script itself is:
Code:
static guint parse_entry (GScanner   *scanner,
             GHashTable *table)
{
  /* Entry starts with @ */
  g_scanner_get_next_token (scanner);
  if (scanner->token != '@')
    return G_TOKEN_ERROR;

  /* Now get identifier */
  g_scanner_get_next_token (scanner);
  if (scanner->token != G_TOKEN_IDENTIFIER){
    return G_TOKEN_ERROR;
  }
//  g_hash_table_insert (table, g_strdup ("type"),
//                       g_print("%s ",g_strdup (scanner->value.v_identifier));

  /* Brace */
  g_scanner_get_next_token (scanner);
  if (scanner->token != G_TOKEN_LEFT_CURLY)
    return G_TOKEN_ERROR;

  /* ID */
  g_scanner_get_next_token (scanner);
  if (scanner->token != G_TOKEN_IDENTIFIER)
    return G_TOKEN_ERROR;

//  g_hash_table_insert (table, g_strdup ("id"),
//                      g_print("%s ", g_strdup (scanner->value.v_identifier));

  while (TRUE)
    {

  tauth=fopen("fauth.dat","a");
  tyear=fopen("fyear.dat","a");
  ttitle=fopen("ftitle.dat","a");
      g_scanner_get_next_token (scanner);
      if (scanner->token != G_TOKEN_COMMA)
        return G_TOKEN_ERROR;

      g_scanner_get_next_token (scanner);
      if (scanner->token != G_TOKEN_IDENTIFIER)
        return G_TOKEN_ERROR;

      key = g_strdup (scanner->value.v_identifier);
      g_scanner_get_next_token (scanner);
      if (scanner->token != '=')
        {
          g_free (key);
          return G_TOKEN_ERROR;
        }

      g_scanner_get_next_token (scanner);
      if (scanner->token != G_TOKEN_STRING)
        {
          g_free (key);
          return G_TOKEN_ERROR;
        }

      val = g_strdup (scanner->value.v_string);
//      g_hash_table_insert(table, key, val);
//g_print("%s",key);
if(g_ascii_strcasecmp (key,"author")==0)g_fprintf(tauth,"%s\n",g_strndup(val,128));
if(g_ascii_strcasecmp (key,"year")==0)g_fprintf(tyear,"%s\n",val);
if(g_ascii_strcasecmp (key,"title")==0)g_fprintf(ttitle,"%s\n",g_strndup(val,128));
      g_scanner_peek_next_token (scanner);
      if (scanner->next_token == G_TOKEN_RIGHT_CURLY)
        break;
  fclose(tauth);
  fclose(tyear);
  fclose(ttitle);
    }

  /* Eat last curly brace and return */
  g_scanner_get_next_token (scanner);
  return G_TOKEN_NONE;
}


Top
 Profile  
 
 Post subject: Re: utf-8 pango error
PostPosted: Fri Sep 28, 2012 4:28 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

Where do you get your original bibtex file and more importantly, what encoding is that file using? If this file is not ASCII or UTF-8, then you're in trouble since GTK+ will not work with anything else.

Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: utf-8 pango error
PostPosted: Fri Sep 28, 2012 6:10 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
The original file is also ASCII:
Quote:
$ file biblio.bib
biblio.bib: ASCII text

Can there be problem when I am writing the parsed text?
I am asking this because, if I parse the biblio.bib file and write fauth.dat etc via shell script, then the files treeview rendering is fine.
Not when I am creating fauth.dat etc via C.


Top
 Profile  
 
 Post subject: Re: utf-8 pango error
PostPosted: Fri Sep 28, 2012 8:26 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

Not completely sure what could be the problem here. But one things is certain: saving data on disk and then loading it again is a complete waste of time. Why don't you simply copy the data that is being parsed into your store for tree view?

Just to show you how simple can things be when you don't mess around with files on disk, I created this small demo app that should also give you an idea on how to approach similar problems in the future.

Code:
#include <gtk/gtk.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

typedef struct _Data
{
  GtkWidget *window;
  GtkListStore *store;
}
Data;

enum
{
  C_TYPE = 0,
  C_NAME,
  C_YEAR,
  C_TITLE,
  NO_COLS
};


static void
output_entry (GHashTable *table,
              Data       *data)
{
  GHashTableIter iter;
  char *key, *val;
  char *keys[] = {"type", "author", "year", "title", NULL};
  char *vals[] = {NULL,   NULL,     NULL,   NULL   };
  char **kiter;
  int i;
  GtkTreeIter siter;

  g_hash_table_iter_init (&iter, table);
  while (g_hash_table_iter_next (&iter, (void **)&key, (void **)&val))
    {
      for (kiter = keys, i = 0; *kiter; kiter++, i++)
        {
          if (!strcmp (*kiter, key))
            {
              vals[i] = val;
              break;
            }
        }
    }

  gtk_list_store_append (data->store, &siter);
  gtk_list_store_set (data->store, &siter,
                      C_TYPE, vals[C_TYPE],
                      C_NAME, vals[C_NAME],
                      C_YEAR, vals[C_YEAR],
                      C_TITLE, vals[C_TITLE],
                      -1);
}

static guint
parse_entry (GScanner   *scanner,
             GHashTable *table)
{
  /* Entry starts with @ */
  g_scanner_get_next_token (scanner);
  if (scanner->token != '@')
    return G_TOKEN_ERROR;

  /* Now get identifier */
  g_scanner_get_next_token (scanner);
  if (scanner->token != G_TOKEN_IDENTIFIER)
    return G_TOKEN_ERROR;

  g_hash_table_insert (table, g_strdup ("type"),
                       g_strdup (scanner->value.v_identifier));

  /* Brace */
  g_scanner_get_next_token (scanner);
  if (scanner->token != G_TOKEN_LEFT_CURLY)
    return G_TOKEN_ERROR;

  /* ID */
  g_scanner_get_next_token (scanner);
  if (scanner->token != G_TOKEN_IDENTIFIER)
    return G_TOKEN_ERROR;

  g_hash_table_insert (table, g_strdup ("id"),
                       g_strdup (scanner->value.v_identifier));

  while (TRUE)
    {
      char *key, *val;

      g_scanner_get_next_token (scanner);
      if (scanner->token != G_TOKEN_COMMA)
        return G_TOKEN_ERROR;

      g_scanner_get_next_token (scanner);
      if (scanner->token != G_TOKEN_IDENTIFIER)
        return G_TOKEN_ERROR;

      key = g_strdup (scanner->value.v_identifier);

      g_scanner_get_next_token (scanner);
      if (scanner->token != '=')
        {
          g_free (key);
          return G_TOKEN_ERROR;
        }

      g_scanner_get_next_token (scanner);
      if (scanner->token != G_TOKEN_STRING)
        {
          g_free (key);
          return G_TOKEN_ERROR;
        }

      val = g_strdup (scanner->value.v_string);
      g_hash_table_insert(table, key, val);

      g_scanner_peek_next_token (scanner);
      if (scanner->next_token == G_TOKEN_RIGHT_CURLY)
        break;
    }

  /* Eat last curly brace and return */
  g_scanner_get_next_token (scanner);
  return G_TOKEN_NONE;
}


static void
cb_file_set (GtkFileChooser *chooser,
             Data           *data)
{
  GScanner *scanner;
  GHashTable *table;
  guint ret;
  int fd;
  char *fname;


  fname = gtk_file_chooser_get_filename (chooser);
  fd = open (fname, O_RDONLY);

  scanner = g_scanner_new (NULL);
  g_scanner_input_file (scanner, fd);

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

      if (ret == G_TOKEN_ERROR)
        break;
      else
        output_entry (table, data);

      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);
  g_free (fname);
  close (fd);
}

static void
create_gui (Data *data)
{
  GtkWidget *vbox,
            *cbutton,
            *swindow,
            *tree;
  GtkCellRenderer *cell;

  data->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size (GTK_WINDOW (data->window), 400, 400);
  g_signal_connect (data->window, "destroy", gtk_main_quit, NULL);

  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
  gtk_container_add (GTK_CONTAINER (data->window), vbox);

  cbutton = gtk_file_chooser_button_new ("Load bib file",
                                         GTK_FILE_CHOOSER_ACTION_OPEN);
  g_signal_connect (cbutton, "file-set", G_CALLBACK (cb_file_set), data);
  gtk_box_pack_start (GTK_BOX (vbox), cbutton, FALSE, FALSE, 0);

  swindow = gtk_scrolled_window_new (NULL, NULL);
  gtk_box_pack_start (GTK_BOX (vbox), swindow, TRUE, TRUE, 0);

  data->store = gtk_list_store_new (NO_COLS, G_TYPE_STRING, G_TYPE_STRING,
                                    G_TYPE_STRING, G_TYPE_STRING);

  tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (data->store));
  gtk_container_add (GTK_CONTAINER (swindow), tree);

  cell = gtk_cell_renderer_text_new ();
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree), -1,
                                               "Type", cell,
                                               "text", C_TYPE,
                                               NULL);

  cell = gtk_cell_renderer_text_new ();
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree), -1,
                                               "Name", cell,
                                               "text", C_NAME,
                                               NULL);

  cell = gtk_cell_renderer_text_new ();
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree), -1,
                                               "Year", cell,
                                               "text", C_YEAR,
                                               NULL);

  cell = gtk_cell_renderer_text_new ();
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree), -1,
                                               "Title", cell,
                                               "text", C_TITLE,
                                               NULL);

  gtk_widget_show_all (vbox);
}

int
main (int    argc,
      char **argv)
{
  Data *data;


  gtk_init (&argc, &argv);
  data = g_slice_new (Data);
  create_gui (data);
  gtk_widget_show (data->window);

  gtk_main ();

  return 0;
}


Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: utf-8 pango error
PostPosted: Fri Sep 28, 2012 10:35 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Dear Tadej,
Thanks a lot!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group