📄 gtk_tut-17.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Draft//EN"><HTML><HEAD><meta http-equiv="pragma" content="no-cache"><TITLE>GTK导引: glib</TITLE></HEAD><BODY><A HREF="tppmsgs/msgs0.htm#17" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/gtk_tut-16.html"><IMG SRC="prev.gif" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/prev.gif" ALT="Previous"></A><A HREF="gtk_tut-18.html" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/gtk_tut-18.html"><IMG SRC="next.gif" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/next.gif" ALT="Next"></A><A HREF="gtk_tut.html#toc17" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/gtk_tut.html#toc17"><IMG SRC="toc.gif" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/toc.gif" ALT="Contents"></A><HR><H2><A NAME="sec_glib"></A> <A NAME="s17">17. glib</A></H2><P>glib提供许多有用的函数及定义.我把它们列在这里并做简短的解释.很多都是与libc重复, 对这些我不再详述.这些大致上是用来参考, 您知道有什麽东西可以用就好.<P><H2><A NAME="ss17.1">17.1 定义</A></H2><P>为保持资料型态的一致, 这里有一些定义:<P><BLOCKQUOTE><CODE><PRE>G_MINFLOATG_MAXFLOATG_MINDOUBLEG_MAXDOUBLEG_MINSHORTG_MAXSHORTG_MININTG_MAXINTG_MINLONGG_MAXLONG</PRE></CODE></BLOCKQUOTE><P>此外, 以下的typedefs. 没有列出来的是会变的, 要看是在那一种平台上.如果您想要具有可移植性, 记得避免去使用sizeof(pointer).例如, 一个指标在Alpha上是8 bytes, 但在Inter上为4 bytes.<P><BLOCKQUOTE><CODE><PRE>char gchar;short gshort;long glong;int gint;char gboolean;unsigned char guchar;unsigned short gushort;unsigned long gulong;unsigned int guint;float gfloat;double gdouble;long double gldouble;void* gpointer;gint8guint8gint16guint16gint32guint32</PRE></CODE></BLOCKQUOTE><P><H2><A NAME="ss17.2">17.2 双向链结串列</A></H2><P>以下函数用来产生, 管理及销毁双向链结串列.<P><BLOCKQUOTE><CODE><PRE>GList* g_list_alloc (void);void g_list_free (GList *list);void g_list_free_1 (GList *list);GList* g_list_append (GList *list, gpointer data); GList* g_list_prepend (GList *list, gpointer data); GList* g_list_insert (GList *list, gpointer data, gint position);GList* g_list_remove (GList *list, gpointer data); GList* g_list_remove_link (GList *list, GList *link);GList* g_list_reverse (GList *list);GList* g_list_nth (GList *list, gint n); GList* g_list_find (GList *list, gpointer data);GList* g_list_last (GList *list);GList* g_list_first (GList *list);gint g_list_length (GList *list);void g_list_foreach (GList *list, GFunc func, gpointer user_data);</PRE></CODE></BLOCKQUOTE> <P><P><H2><A NAME="ss17.3">17.3 单向链结串列</A></H2><P>以下函数是用来管理单向链结串列:<BLOCKQUOTE><CODE><PRE>GSList* g_slist_alloc (void);void g_slist_free (GSList *list);void g_slist_free_1 (GSList *list);GSList* g_slist_append (GSList *list, gpointer data); GSList* g_slist_prepend (GSList *list, gpointer data); GSList* g_slist_insert (GSList *list, gpointer data, gint position); GSList* g_slist_remove (GSList *list, gpointer data); GSList* g_slist_remove_link (GSList *list, GSList *link); GSList* g_slist_reverse (GSList *list);GSList* g_slist_nth (GSList *list, gint n); GSList* g_slist_find (GSList *list, gpointer data); GSList* g_slist_last (GSList *list);gint g_slist_length (GSList *list);void g_slist_foreach (GSList *list, GFunc func, gpointer user_data); </PRE></CODE></BLOCKQUOTE><P><H2><A NAME="ss17.4">17.4 记忆体管理</A></H2><P><BLOCKQUOTE><CODE><PRE>gpointer g_malloc (gulong size);</PRE></CODE></BLOCKQUOTE><P>这是替代malloc()用的.你不需要去检查返回值, 因为它已经帮你做好了, 保证. <P><BLOCKQUOTE><CODE><PRE>gpointer g_malloc0 (gulong size);</PRE></CODE></BLOCKQUOTE><P>一样, 不过会在返回之前将记忆体归零.<P><BLOCKQUOTE><CODE><PRE>gpointer g_realloc (gpointer mem, gulong size);</PRE></CODE></BLOCKQUOTE><P>重定记忆体大小. <P><BLOCKQUOTE><CODE><PRE>void g_free (gpointer mem);</PRE></CODE></BLOCKQUOTE><BLOCKQUOTE><CODE><PRE>void g_mem_profile (void);</PRE></CODE></BLOCKQUOTE><P>将记忆体的使用状况写到一个档案, 不过您必须要在glib/gmem.c里面,加#define MEM_PROFILE, 然後重新编译.<P><BLOCKQUOTE><CODE><PRE>void g_mem_check (gpointer mem);</PRE></CODE></BLOCKQUOTE><P>检查记忆体位置是否有效.您必须要在glib/gmem.c上加#define MEM_CHECK, 然後重新编译.<P><H2><A NAME="ss17.5">17.5 Timers</A></H2><P>Timer函数..<P><BLOCKQUOTE><CODE><PRE>GTimer* g_timer_new (void);void g_timer_destroy (GTimer *timer);void g_timer_start (GTimer *timer);void g_timer_stop (GTimer *timer);void g_timer_reset (GTimer *timer);gdouble g_timer_elapsed (GTimer *timer, gulong *microseconds);</PRE></CODE></BLOCKQUOTE> <P><H2><A NAME="ss17.6">17.6 字串处理</A></H2><P><BLOCKQUOTE><CODE><PRE>GString* g_string_new (gchar *init);void g_string_free (GString *string, gint free_segment); GString* g_string_assign (GString *lval, gchar *rval); GString* g_string_truncate (GString *string, gint len); GString* g_string_append (GString *string, gchar *val); GString* g_string_append_c (GString *string, gchar c); GString* g_string_prepend (GString *string, gchar *val); GString* g_string_prepend_c (GString *string, gchar c); void g_string_sprintf (GString *string, gchar *fmt, ...); void g_string_sprintfa (GString *string, gchar *fmt, ...);</PRE></CODE></BLOCKQUOTE> <P><H2><A NAME="ss17.7">17.7 工具及除错函数</A></H2><P><BLOCKQUOTE><CODE><PRE>gchar* g_strdup (const gchar *str);</PRE></CODE></BLOCKQUOTE><P><BLOCKQUOTE><CODE><PRE>gchar* g_strerror (gint errnum);</PRE></CODE></BLOCKQUOTE><P>我建议您使用这个来做所有错误讯息.这玩意好多了. 它比perror()来的具有可移植性.输出为以下形式:<P><BLOCKQUOTE><CODE><PRE>program name:function that failed:file or further description:strerror</PRE></CODE></BLOCKQUOTE><P>这里是"hello world"用到的一些函数:<P><BLOCKQUOTE><CODE><PRE>g_print("hello_world:open:%s:%s\n", filename, g_strerror(errno));</PRE></CODE></BLOCKQUOTE><P><BLOCKQUOTE><CODE><PRE>void g_error (gchar *format, ...);</PRE></CODE></BLOCKQUOTE><P>显示错误讯息, 其格式与printf一样,但会加个"** ERROR **: ", 然後离开程式. 只在严重错误时使用.<P><BLOCKQUOTE><CODE><PRE>void g_warning (gchar *format, ...);</PRE></CODE></BLOCKQUOTE><P>跟上面一样, 但加个"** WARNING **: ", 不离开程式.<P><BLOCKQUOTE><CODE><PRE>void g_message (gchar *format, ...);</PRE></CODE></BLOCKQUOTE><P>加个"message: ".<P><BLOCKQUOTE><CODE><PRE>void g_print (gchar *format, ...);</PRE></CODE></BLOCKQUOTE><P>printf()的替代品.<P>最後一个:<P><BLOCKQUOTE><CODE><PRE>gchar* g_strsignal (gint signum);</PRE></CODE></BLOCKQUOTE><P>列印Unix系统的信号名称, 在信号处理时很有用.<P>这些大都从glib.h中而来.<P><HR><A HREF="tppmsgs/msgs0.htm#17" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/gtk_tut-16.html"><IMG SRC="prev.gif" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/prev.gif" ALT="Previous"></A><A HREF="gtk_tut-18.html" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/gtk_tut-18.html"><IMG SRC="next.gif" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/next.gif" ALT="Next"></A><A HREF="gtk_tut.html#toc17" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/gtk_tut.html#toc17"><IMG SRC="toc.gif" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/toc.gif" ALT="Contents"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -