glib는 GDK 및 GTK 어플을 개발할 때 많은 유용한 함수와 정의들을 제공한다. 나는 여기서 그들을 간단한 설명과 함께 보일 것이다. 상당수는 표준의 libc와 중복되기 때문에 그들에 대해 자세히 다루진 않는다. 이것은 주로 하나의 참고로서, 어떤 것을 이용할 수 있는가를 파악하게 할 것이다.
많은 자료형들에 대한 extreme 값들의 정의는 이렇다.
G_MINFLOAT
G_MAXFLOAT
G_MINDOUBLE
G_MAXDOUBLE
G_MINSHORT
G_MAXSHORT
G_MININT
G_MAXINT
G_MINLONG
G_MAXLONG
또한, typedef들도 있다. 왼쪽의 것들은 기계의 architecture에 의해 다르게 세팅된다. 호환성을 유지하려면 포인터의 크기를 재어서는 안된다는 것을 명심 하라. 하나의 포인터는 Alpha 에서 8바이트, 하지만 Intel에서는 4바이트이다.
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;
gint8
guint8
gint16
guint16
gint32
guint32
아래의 함수들은 이중 연결 리스트들을 만들고, 관리하고, 또 파괴하기 위해 쓰이는 것들이다. 이 문서에서 연결리스트가 무엇인지 다룰 순 없기에, 나는 여러분이 그것을 이미 알고 있다고 가정한다. 물론, 그것은 GTK의 일반적인 쓰임새를 알기 위해 꼭 필요한 것은 아니지만, 그래도 알면 좋은 것이다.
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);
위에 있는 많은 함수들은 단일 연결리스트들에 대해서도 쓰일 수 있는 것들 이다. 여기 완전한 함수의 리스트가 있다.
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);
gpointer g_malloc (gulong size);
이것은 malloc()을 대체하는 것이다. 이 함수의 내부에서 리턴값에 대한 체크가 이루어지므로 우리가 따로 그것을 해줄 필요는 없다.
gpointer g_malloc0 (gulong size);
위와 같지만, 그것을 향한 포인터를 리턴하기 전까진 0이다.
gpointer g_realloc (gpointer mem,
gulong size);
"mem"에서 시작하여 "size"만큼의 바이트를 다시 할당한다. 물론 그 메모리는 이전에 할당되어 있어야 한다.
void g_free (gpointer mem);
간단하다, 메모리를 반납한다.
void g_mem_profile (void);
사용된 메모리의 profile을 쏟아내 준다. 하지만 이것을 쓰기 위해선 우리는 glib/gmem.c의 제일 윗부분에 #define MEM_PROFILE을 더해주고 make를 다시 해야 한다.
void g_mem_check (gpointer mem);
메모리의 위치가 유효한 것인지 체크한다. gmem.c의 제일 윗부분에 #define MEM_CHECK을 더해주고 make를 다시 해야 한다.
타이머 함수들이다.
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);
문자열을 다루는 함수들이다. 이들은 모두 상당히 흥미롭고, 또한 표준 C 문자열 함수들보다 더 많은 기능들이 있을 것이다. 하지만 이들에 문서가 필요 할 것이다.
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,
...);
gchar* g_strdup (const gchar *str);
strdup함수를 대체한다. 원래의 문자열을 새롭게 할당된 메모리에 복사하고, 그 곳에 대한 포인터를 리턴한다.
gchar* g_strerror (gint errnum);
모든 에러 메시지에 이 함수를 이용할 것을 권한다. 이것은 perror()나 다른 것보다 훨씬 멋지고, 확장성 좋은 것이다. 출력은 보통 이런 형식이다.
program name:function that failed:file or further description:strerror
이것은 우리의 hello_world 프로그램에 쓰인 예제이다.
g_print("hello_world:open:%s:%s\n", filename, g_strerror(errno));
void g_error (gchar *format, ...);
에러 메시지를 출력한다. 형식은 printf와 같지만, 출력의 앞머리에 "** ERROR **: "를 붙이고, 그리고 프로그램을 끝낸다. 치명적인 에러에만 사용하라.
void g_warning (gchar *format, ...);
위와 같다, 하지만 "** WARNING **: "을 앞머리에 붙이고, 또한 프로그램을 끝내진 않는다.
void g_message (gchar *format, ...);
우리가 출력하려는 문자열에 "message: "를 앞에서 붙여 출력해준다.
void g_print (gchar *format, ...);
printf를 대체한다.
그리고 우리의 마지막 함수는 이것이다.
gchar* g_strsignal (gint signum);
인자로 준 시그널 번호에 해당하는 유닉스 시스템의 시그널 이름을 출력해준다. 대다수의 시그널 다루는 함수들에게 유용하다.
위에 소개된 모든 것들은 glib.h에서 마구잡이로 인용한 것이다. 여러분 중 누구라도 어떤 함수에 대한 문서화를 하겠다면, 나에게 email을 보내주길!