文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>glib学习

glib学习

时间:2006-10-27  来源:kf701

     1  /*
     2     glib test program for study,compile cmd:
     3     cc `pkg-config --cflags --libs glib-2.0` test.c
     4
     5   */
     6  #include <glib.h>
     7  #include <stdint.h>
     8  #include <sys/types.h>
     9  #include <sys/stat.h>
    10  #include <fcntl.h>
    11
    12  #define TIMESTAMP_IS_NEWER_THAN(ts1,ts2) \
    13          ((uint32_t)((uint32_t)(ts1) - (uint32_t)(ts2))< (uint32_t)(1<<31))    14
    15  typedef struct{
    16          gchar* name;
    17          gchar* mail;
    18          gchar* address;
    19  } my_struct;
    20
    21  void free_key( gpointer value )
    22  {
    23          g_free( value );
    24  }
    25
    26  void free_my_value( gpointer value )
    27  {
    28          my_struct* p = (my_struct*)value;
    29          g_free( p->name );
    30          g_free( p->mail );
    31          g_free( p->address );
    32          g_free( value );
    33  }
    34
    35  int main()
    36  {
    37          /* Utility Functions */
    38          g_set_application_name("Test-Glib-App");
    39          const gchar* appname = g_get_application_name();
    40          g_printf("The Application name: %s\n", appname);
    41          g_set_prgname("test.c");
    42          gchar* pname = g_get_prgname();
    43          g_printf("The program name: %s\n", pname);
    44          gchar* cur_dir = g_get_current_dir();
    45          g_printf("Current Dir: %s\n", cur_dir);
    46
    47          /* Read from oRTP lib */
    48          if( TIMESTAMP_IS_NEWER_THAN( 3, 5 ) )
    49                  g_printf("cmp micro: newer\n");
    50          else
    51                  g_printf("cmp micro: no\n");
    52
    53          /* Micro */
    54  #ifdef G_OS_UNIX
    55          g_printf("I am unix\n");
    56  #endif
    57
    58          /* Debug message */
    59          g_debug("%s,%d: only for test",__FILE__,__LINE__);
    60          g_warning("%s,%d: only for test",__FILE__,__LINE__);
    61          g_critical("%s,%d: only for test",__FILE__,__LINE__);
    62
    63          GTimeVal tv;
    64          g_get_current_time(&tv);
    65          //char *time_str = g_time_val_to_iso8601(&tv);
    66          g_printf("time: %d\n", tv.tv_sec);
    67
    68          /* Rand number */
    69          GRand* grand = g_rand_new();
    70          guint32 i = g_rand_int(grand);
    71          g_printf("rand number: %u\n", i);
    72          g_rand_free(grand);
    73
    74          /* Timer */
    75          GTimer* timer1 = g_timer_new();
    76          g_usleep( 1*1000000 );
    77          g_timer_stop(timer1);
    78          gdouble et = g_timer_elapsed(timer1, NULL);
    79          g_printf("elapsed time: %u\n", (guint32)et);
    80          g_timer_destroy(timer1);
    81
    82          /* temp file and channel */
    83          char tempfilename[] = "XXXXXX-test.txt";
    84          gint pfd = g_mkstemp(tempfilename);
    85          g_printf("temp file name: %s\n", tempfilename);
    86          GIOChannel* pfile = g_io_channel_unix_new(pfd);
    87          char buf[] = "only for test; int open\n";
    88          gsize nwrite = 0;
    89          GIOStatus iost = g_io_channel_write_chars(pfile,buf,-1,&nwrite,NULL);    90          if( G_IO_STATUS_NORMAL != iost )
    91                  g_error("%s,%d: write channel error",__FILE__,__LINE__);    92          else
    93                  g_printf("nwrite = %d\n", nwrite);
    94          g_io_channel_shutdown(pfile, TRUE, NULL); // close pfd implicitly    95          pfd = -1; // for security
    96
    97          /* Lexical Scanner */
    98          g_message("\nNow,test scanner provide by glib");
    99          /* you can use a C source file instead, just like:
   100          pfd = g_open( "test.c", O_RDONLY);              */
   101          pfd = g_open( tempfilename, O_RDONLY);
   102          if( -1 == pfd )
   103                  g_error("%s,%d: open file error",__FILE__,__LINE__);
   104          GScanner* scaner = g_scanner_new(NULL);
   105          g_scanner_input_file(scaner, pfd);
   106          GTokenType gtt = G_TOKEN_NONE;
   107          do{
   108                  gtt = g_scanner_get_next_token(scaner);
   109                  //g_printf("token type: %d\n", gtt);
   110                  GTokenValue gtv = g_scanner_cur_value(scaner);
   111                  switch( gtt )
   112                  {
   113                          case G_TOKEN_IDENTIFIER:
   114                          case G_TOKEN_STRING:
   115                          case G_TOKEN_COMMENT_SINGLE:
   116                          case G_TOKEN_COMMENT_MULTI:
   117                                  g_printf("token value: %s\n", gtv);
   118                                  break;
   119                          /*
   120                          default:
   121                                  g_printf("token is not identifier\n");
   122                                  break;
   123                          */
   124                  }
   125          }while( G_TOKEN_EOF != gtt );
   126          close( pfd );
   127          pfd = -1;
   128          g_unlink( tempfilename );
   129
   130          /* Hash Table */
   131          g_printf("Test Hash Table now\n");
   132  #define TMP_LEN 64
   133          GHashTable * ht = NULL;
   134          ht = g_hash_table_new_full(g_str_hash,g_str_equal,free_key,free_my_value);   135
   136          my_struct * my_value = g_malloc0(sizeof(my_struct));
   137          my_value->name = g_malloc0( TMP_LEN );
   138          my_value->mail = g_malloc0( TMP_LEN );
   139          my_value->address = g_malloc0( TMP_LEN );
   140          g_strlcpy( my_value->name, "kf701", TMP_LEN );
   141          g_strlcpy( my_value->mail, "kf701.ye at gmail", TMP_LEN );
   142          g_strlcpy( my_value->address, "hefei of china", TMP_LEN );
   143          gchar* my_key = g_strdup( my_value->name );
   144          g_printf("my key: %s\n", my_key);
   145          /* insert into hash table */
   146          g_hash_table_insert( ht, my_key, my_value );
   147          /* try to lookup it */
   148          my_struct* htp = (my_struct*)g_hash_table_lookup(ht, "kf701");
   149          if( htp != NULL )
   150                  g_printf("%s : %s : %s\n", htp->name, htp->mail, htp->address);   151          /* try remove the node */
   152          if( FALSE == g_hash_table_remove(ht, "kf701") )
   153                  g_warning("%s,%d: remove hash table",__FILE__,__LINE__);   154
   155
   156
   157          return 0;
   158  }
相关阅读 更多 +
排行榜 更多 +
狂热战车

狂热战车

飞行射击 下载
士兵和小鸡

士兵和小鸡

飞行射击 下载
西部枪射击目标

西部枪射击目标

飞行射击 下载