GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Sep 17, 2014 5:33 am

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: help me..
PostPosted: Wed Mar 19, 2008 3:52 am 
Offline
Familiar Face

Joined: Tue Feb 12, 2008 8:47 am
Posts: 8
help me by solving this

--------------------------------------------------------------------------------

i want to display the list of partitions in a combo box reading from a file /proc/partitons.

take a look at the below code..

Code:
char *p;
n=4;
FILE *f=fopen("/proc/partitions","r");
Glist combo_items=NULL;


while(n!=0)
{
fgets(p,20,*f);
combo_items=g_list_append(combo_items,(gpointer) p);
n--;



but i cant get the expected result.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 22, 2008 8:12 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
what results are you getting?


Top
 Profile  
 
 Post subject: gtk combo
PostPosted: Mon Mar 24, 2008 7:33 am 
Offline
Familiar Face

Joined: Tue Feb 12, 2008 8:47 am
Posts: 8
i was getting some unknown characters or the last item of the list for 5 times..


example:

let us take the file contains the list

system
windows
linux
partitions


the output iam getting is the combo box containing the text "partitions" for 4 times..


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 24, 2008 2:33 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
okay, the reason that you're getting the same results for every item in the list is because they are all using the same pointer (p),
so they all have the same address.
I've put together an example program that fixes this, although there is probably a better way to do this.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>

int main(int argc, char **argv)
{
   char *p, *t;
   int n = 4;
   FILE *f=fopen("partitions","r");
   GList *combo_items=NULL;
   
   
   while(n!=0)
   {
   fgets(p,20,f);
   t = (char*)malloc(strlen(p) + 1);
   strcpy(t, p);
   combo_items=g_list_append(combo_items,(gpointer)t);
   n--;
   t = NULL;
   }
   
   n = 4;
   combo_items = g_list_first(combo_items);
   while(1)
   {
        printf("Address of data is: %p\n", combo_items->data);
        printf("Value of data is:   '%s'\n", (char*)combo_items->data);
        if(combo_items->next != NULL)
           combo_items = combo_items->next;
        else
           break;
   }
   
   return 0;
}


the "partitions" file, is a file with this contents:

[code=]system
windows
linux
partitions
[/code]


Top
 Profile  
 
 Post subject: linux partitions help
PostPosted: Tue Mar 25, 2008 3:32 am 
Offline
Familiar Face

Joined: Tue Feb 12, 2008 8:47 am
Posts: 8
ok, thank you ..

Actually i am want to create a GUI for editing FSTAB. since i use multiple linux os and Windows. i want select the linux partitions from which i want to edit the /etc/fstab file.

so can you plz give me an idea to display only the linux partitions.

i think u can help me..


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 25, 2008 3:49 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
okay, my only linux system at the moment is a virtual machine, so my fstab is a bit different,
but I looked it up to get a more standard one and for my tests, I've been using this:

[code=]/dev/hda2 / ext2 defaults 1 1
/dev/hdb1 /home ext2 defaults 1 2
/dev/cdrom /media/cdrom auto ro,noauto,user,exec 0 0
/dev/fd0 /media/floppy auto rw,noauto,user,sync 0 0
proc /proc proc defaults 0 0
/dev/hda1 swap swap pri=42 0 0
[/code]
okay now, my code that I've written takes the "fstab.txt" file (you might not want to mess with the real /etc/fstab until you're sure everything is stable),
reads every char in a line (up to the '\n' char) into gchar *p and then appends p to the combo box if it contains either "ext2", "ext3" or "reiserfs" (any of which would indicate a linux system).

Code:
#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>

int strpos(char *str, char *sub);

int main(int argc, char **argv)
{
   GtkWidget *window;
   GtkWidget *combo;
   gchar *p = (gchar*)malloc(1);
   FILE *fp;
   char c;
   int i;

   gtk_init(&argc, &argv);

   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(gtk_main_quit), NULL);

   combo = gtk_combo_box_new_text();
   fp = fopen("fstab.txt", "r");
   do
   {
      strcpy(p, "");
      for(i = 0;; i++)
      {
         c = fgetc(fp);
         p = (gchar*)realloc(p, strlen(p)+1);
         if(c == '\n' || c == EOF)
         {
            p[i] = '\0';
            break;
         }
         else
         {
            p[i] = c;
         }
      }
      if((strpos(p, "ext2") != -1) || (strpos(p, "ext3") != -1) || (strpos(p, "reiserfs") != -1))
         gtk_combo_box_append_text(GTK_COMBO_BOX(combo), p);
   }while(c != EOF);
   fclose(fp);
   gtk_container_add(GTK_CONTAINER(window), combo);
   gtk_widget_show(combo);

   gtk_widget_show(window);

   gtk_main();

   return 0;
}

/* strpos returns the char index of the first occurrence of the letter of sub if sub is in str, ex:
*
* int x = strpos("Hello", "ll");
* (x now equals 2)
* int y = strpos("Hello", "l");
* (y now equals 2, not 3)
*/
int strpos(char *str, char *sub)
{
   int i, j, k;
   int ret = -1;
   int done = 0;

   for(i = 0; i < strlen(str); i++)
   {
      /* first letter was a match, check the rest */
      if(str[i] == sub[0])
      {
         for(j = 0, k = i; j <= strlen(sub); j++, k++)
         {
            if(sub[j] == '\0')
            {
               ret = i;
               done = 1;
               break;
            }
            if(str[k] != sub[j])
            {
               break;
            }
         }
      }
      if(done == 1)
         break;
   }

   return ret;
}


You could do further work on p before you append it as well (such as separating the different items based on whitespace).
I hope that this helps you...


Top
 Profile  
 
 Post subject: gtk combo
PostPosted: Wed Mar 26, 2008 4:48 am 
Offline
Familiar Face

Joined: Tue Feb 12, 2008 8:47 am
Posts: 8
the code which you have given before doesn't solve me check this code:
it was displaying same item in the combo box for 4 times,But it was printing the list correctly. I even tried pointer array it also not solving the problem....


Code:
#include <gtk/gtk.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <gdk/gdkkeysyms.h>

GtkWidget* createcombo (void)
{
  GtkWidget *window1;
  GtkWidget *fixed1;
  GtkWidget *combo1;
  GList *combo1_items = NULL;
  FILE *f;

int n=4;
char *q;
char *t;

//the filesystem.txt contains the list of linux partitions..

f=fopen("/root/Desktop/untitled/filesystem.txt","r");

  window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window1), "window1");
  g_signal_connect (G_OBJECT (window1), "delete_event", G_CALLBACK (gtk_main_quit), NULL);
  gtk_widget_show (window1);

  fixed1 = gtk_fixed_new ();
  gtk_widget_show (fixed1);
  gtk_container_add (GTK_CONTAINER (window1), fixed1);

  combo1 = gtk_combo_new ();
   
  gtk_fixed_put (GTK_FIXED (fixed1), combo1, 80, 56);
  gtk_widget_set_size_request (combo1, 187, 27);
  gtk_widget_show (combo1);

while(n!=0)
{

fgets(q,15,f);
t[n]= (char *) malloc (strlen(q)+1);
printf("%s",q);
strcpy(t,q);
combo1_items = g_list_append (combo1_items, (gpointer) t);

n--;
t=NULL;
printf("%s",t);
}
gtk_combo_set_popdown_strings (GTK_COMBO (combo1), combo1_items);
g_list_free (combo1_items);

return window1;
}


int main (int argc, char *argv[])
{
  gtk_init (&argc, &argv);

  GtkWidget *window1;

window1=createcombo();
  gtk_main ();
  return 0;
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 26, 2008 8:00 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
okay, I've redone your code and it works for me currently. (I'm using my fstab file that I posted earlier)

I'd like to point out a few spots that were causing troubles in your code:
line 46: t is a char*, so t[n] is assigning the nth character in the character array, you would need a char** to do an array of strings instead of characters.
line 52,53: in 52, you set t to NULL, then in 53, you print the value of t which would cause a runtime error.

Anyway, here's my redone version:

Code:
#include <gtk/gtk.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <gdk/gdkkeysyms.h>

GtkWidget* createcombo (void)
{
   GtkWidget *window1;
   GtkWidget *fixed1;
   GtkWidget *combo1;
   GList *combo1_items = NULL;
   FILE *f;

   char c;
   int i = 0, j = 0;
   char *q;

   window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   gtk_window_set_title (GTK_WINDOW (window1), "window1");
   g_signal_connect (G_OBJECT (window1), "delete_event", G_CALLBACK (gtk_main_quit), NULL);
   gtk_widget_show (window1);

   fixed1 = gtk_fixed_new ();
   gtk_widget_show (fixed1);
   gtk_container_add (GTK_CONTAINER (window1), fixed1);

   combo1 = gtk_combo_new ();

   gtk_fixed_put (GTK_FIXED (fixed1), combo1, 80, 56);
   gtk_widget_set_size_request (combo1, 187, 27);
   gtk_widget_show (combo1);

   /* I'm using an fstab again */
   f = fopen("fstab.txt", "r");
   if(f == NULL)
   {
      fprintf(stderr, "Could not open file!\n");
      return NULL;
   }

   q = NULL;
   do
   {
      if(q == NULL)
      {
         q = (char*)malloc(1);
         if(q == NULL)
         {
            fprintf(stderr, "Not enough memory!\n");
            return NULL;
         }
         strcpy(q, "");
      }
      c = fgetc(f);
      if(c != '\n')
      {
         q = (char*)realloc(q, strlen(q) + 2);
         if(q == NULL)
         {
            fprintf(stderr, "Not enough memory!\n");
            return NULL;
         }
         q[i] = c;
         q[i+1] = '\0';
      }
      else
      {
         q[i] = '\0';
         combo1_items = g_list_append(combo1_items, (gpointer)q);
         q = NULL;
         i = j = -1;
      }

      i++; j++;
   }while(c != EOF);
   fclose(f);
   gtk_combo_set_popdown_strings (GTK_COMBO (combo1), combo1_items);
   g_list_free (combo1_items);

   return window1;
}


int main (int argc, char *argv[])
{
   gtk_init (&argc, &argv);

   GtkWidget *window1;

   window1=createcombo();
   gtk_main ();
   return 0;
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group