#include <stdio.h>
#include <glib.h>
#include <string.h>

struct datax
{
    int  iSeqno ;
    char namex[28] ;
} ;
typedef struct datax mydatastruc ;

void testfunc(gpointer key )
{
    int *i = key ;
    printf("...%d...\n",*i) ;
}

gboolean iter_all(gpointer key, gpointer value, gpointer data) {
  int *itmp = key ;
  mydatastruc localdata ;
  localdata.iSeqno = (int) *itmp ;
  strcpy(localdata.namex, value) ;
  //printf("%d, %s\n", *itmp, value);
  printf("(%d), (%s)\n", localdata.iSeqno,localdata.namex);
  return FALSE;
}
gboolean iter_some(gpointer key, gpointer value, gpointer data) {
  int *itmp = key ;
  mydatastruc localdata ;
  localdata.iSeqno = (int) *itmp ;
  strcpy(localdata.namex, value) ;
  printf("(%d), (%s)\n", localdata.iSeqno,localdata.namex);
  int isearchnum= 10  ;
  return myowncompare(key, &isearchnum) == 0;
 //return g_ascii_strcasecmp(key, "b") == 0;
}


int myowncompare(int *s1,int *s2)
{
    int iret = 0 ;
    if(*s1==*s2)
    iret=0;
    else if(*s1>*s2)
    iret=1;
    else
    iret=-1 ;
    return iret ;
}


int main(int argc, char** argv) {
   //GTree* t = g_tree_new((GCompareFunc)g_ascii_strcasecmp);
   GTree* t = g_tree_new((GCompareFunc)myowncompare );

   mydatastruc data[100] ;
   char strtmp[8] ;
   int idx ;
   for(idx=0;idx<100;idx=idx+2)
   {
  data[idx].iSeqno = idx ;
  //testfunc(&(data[idx].iSeqno)) ;
  strcpy(data[idx].namex,"Name") ;
  sprintf(strtmp,"%02d",idx) ;
  strcat(data[idx].namex,strtmp) ;
  g_tree_insert(t,&(data[idx].iSeqno),data[idx].namex) ;
   }
   for(idx=1;idx<100;idx=idx+2)
   {
  data[idx].iSeqno = idx ;
  //testfunc(&(data[idx].iSeqno)) ;
  strcpy(data[idx].namex,"Name") ;
  sprintf(strtmp,"%02d",idx) ;
  strcat(data[idx].namex,strtmp) ;
  g_tree_insert(t,&(data[idx].iSeqno),data[idx].namex) ;
   }
   printf("Iterating all nodes\n");
   g_tree_foreach(t, (GTraverseFunc)iter_all, NULL);
   printf("Iterating some of the nodes\n");
   g_tree_foreach(t, (GTraverseFunc)iter_some, NULL);
   g_tree_destroy(t);
   return 0;
}

 

全站熱搜

hedgezzz 發表在 痞客邦 留言(0) 人氣()