GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sun Dec 21, 2014 11:56 pm

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: gscan text from textview and store it in treestore
PostPosted: Tue Feb 05, 2013 4:09 am 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Dear friends,
In a code, I am trying to scan text entered in textview and will like to store it in a treestore. the scan and output file(kindly shown by tadej few months back) is
Code:
void output_entry (GHashTable *table)
{
  GHashTableIter iter;
  char *key, *val;
  char *keys[] = {"id", "type", "author", "year", "title","editor", "pub"
                 "vol", "iss", "page", "mon", "note", "add", "edition", "journal"
       "series", "book", "chap", "org", NULL};
  char *vals[] = {NULL,  NULL,  NULL, NULL, NULL,
                  NULL,  NULL,  NULL, NULL, NULL,
        NULL,  NULL,  NULL, NULL, NULL,
        NULL,    NULL,  NULL, NULL, NULL};

  char **kiter;
  int i;

  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 (!g_ascii_strcasecmp(*kiter, key))
            {
              vals[i] = g_strndup(val,slen);
              break;
            }
        }
    }

  gtk_list_store_append (store, &siter);
  gtk_list_store_set (store, &siter,
                      COL_BIB_TYPE, vals[COL_BIB_TYPE],
                      COL_BIB_KEY, vals[COL_BIB_KEY],
                      COL_BIB_AUTHOR, vals[COL_BIB_AUTHOR],
                      COL_BIB_YEAR, vals[COL_BIB_YEAR],
                      COL_BIB_TITLE, vals[COL_BIB_TITLE],
            COL_BIB_EDITOR,  vals[COL_BIB_EDITOR],
            COL_BIB_PUB, vals[COL_BIB_PUB],
            COL_BIB_VOL, vals[COL_BIB_VOL],
            COL_BIB_ISS, vals[COL_BIB_ISS],
            COL_BIB_PAGE, vals[COL_BIB_PAGE],
            COL_BIB_MON, vals[COL_BIB_MON],
            COL_BIB_NOTE, vals[COL_BIB_NOTE],
            COL_BIB_ADD, vals[COL_BIB_ADD],
            COL_BIB_EDITION, vals[COL_BIB_EDITION],
            COL_BIB_JOURNAL, vals[COL_BIB_JOURNAL],
            COL_BIB_SERIES, vals[COL_BIB_SERIES],
            COL_BIB_BOOK, vals[COL_BIB_BOOK],
            COL_BIB_ORG, vals[COL_BIB_ORG],
            COL_BIB_CHAP, vals[COL_BIB_CHAP],
            COL_BIB_SCHOOL, vals[COL_BIB_SCHOOL],
                      -1);
}

guint parse_entry (GScanner   *scanner,
             GHashTable *table)
{
  int tokount;
  /* 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;}
  else
    tokount += tokount;

  /* 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_peek_next_token (scanner);
      if (scanner->token == G_TOKEN_LEFT_CURLY)
   tokount += tokount;

      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;
    }

  g_scanner_get_next_token (scanner);
  return G_TOKEN_NONE;
}


This will parse the file entered in texview and store it in the gs_tree as below:
Code:
void gen_gs(GtkWidget *widget, gpointer data) {
  void gs_open(GtkWidget *widget, gpointer data);
   GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER_ON_PARENT);
  gtk_window_set_title(GTK_WINDOW(window), "Enter a bibtex entry");
  gtk_container_set_border_width(GTK_CONTAINER(window), 5);
  GtkWidget* gen_grid=gtk_grid_new();
  gtk_container_add(GTK_CONTAINER(window), gen_grid);

  gs_txt=gtk_text_view_new();
  gtk_widget_set_size_request(gs_txt, 250, 250);

  GtkWidget* con_gen= gtk_button_new_with_label("Create");
  g_signal_connect(con_gen, "clicked", G_CALLBACK(gs_open), NULL);
 
  GtkWidget *con_can= gtk_button_new_with_label("Cancel");
  g_signal_connect_swapped(con_can, "clicked", G_CALLBACK(gtk_widget_destroy), window);
  gtk_grid_attach(GTK_GRID(gen_grid), gs_txt,  0, 1, 10, 10);
  gtk_grid_attach(GTK_GRID(gen_grid), con_gen, 0, 12, 5, 1);
  gtk_grid_attach(GTK_GRID(gen_grid), con_can, 5, 12, 5, 1);
  gtk_widget_show_all(window);
}

void gs_open(GtkWidget *window, gpointer data) {
  GScanner *gs_scanner;
  GHashTable *gs_table;
  GError* error=NULL;
  GtkTextIter start, end;
  GtkListStore *gs_store;
  GtkTreeIter siter;
  GtkWidget *gs_tree;
  gboolean valid;
  GString *ustring = g_string_new ("");
  GString *str=g_string_new(NULL);

  GtkTextBuffer *gs_buf=gtk_text_view_get_buffer(GTK_TEXT_VIEW(gs_txt));
  gtk_text_buffer_get_start_iter (gs_buf, &start);
  gtk_text_buffer_get_end_iter (gs_buf, &end);
  gchar *gs_text = gtk_text_buffer_get_text (gs_buf, &start, &end, FALSE);
//  g_print(gs_text);
  GScanner* gs_scan = g_scanner_new (NULL);
  g_scanner_input_text (gs_scan, gs_text, gs_BUFF);

  gs_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
  do {
    g_hash_table_remove_all (gs_table);
    parse_entry (gs_scan, gs_table);
    output_entry (gs_table);
    g_scanner_peek_next_token (gs_scan);
  }
  while (gs_scan->next_token != G_TOKEN_EOF &&
      gs_scan->next_token != G_TOKEN_ERROR);
  g_print("Hi\n");
  gs_store = gtk_list_store_new (NUM_COLS,
      G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
      G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
      G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
      G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
      G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);

  g_print("%s\n", valid ?"true":"false");
  gs_tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (gs_store));
  for (valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (gs_store), &siter);
     valid;
     valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (gs_store), &siter)) {
    char *string0, *string1, *string2, *string3, *string4,*string5, *string6,
    *string7, *string8, *string9,*string10, *string11, *string12, *string13,
    *string14,*string15, *string16, *string17, *string18, *string19;

    gtk_tree_model_get (GTK_TREE_MODEL (gs_store), &siter,
                        0, &string0,    1, &string1,    2, &string2,    3, &string3,
         4, &string4,    5, &string5,    6, &string6,    7, &string7,
         8, &string8,    9, &string9,    10, &string10,   11, &string11,
         12, &string12,  13, &string13,   14, &string14,   15, &string15,
         16, &string16,   17, &string17,   18, &string18,   19, &string19,
                        -1);//
  g_string_append_printf (ustring, "@%s{%s,\n",string1, string0);
  if( strlen(string2)!=0 ||string2!=NULL)
    g_string_append_printf (ustring,"\tAuthor=\"%s\",\n", string2);
}
  g_string_append(str,ustring->str);
  buffer=str->str;
//buffer=gs_text->str;
  /* finish parsing */
//    g_scanner_destroy (gs_scan);
//    g_hash_table_destroy (gs_table);
//    gtk_widget_destroy(dialog);
//    g_free(gs_text);
}


but valid is always false and not entering the for loop. Kindly show me where I went wrong


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

All times are UTC


Who is online

Users browsing this forum: Yahoo [Bot] and 4 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