#眉標=Open Source #副標=EeePC應用程式開發系列(4) #大標=GLIB進階應用 #作者=文/Quark #引言= ==<反灰>=========== #include int main(int argc, char ** argv) { GArray* a = g_array_new(FALSE, FALSE, sizeof(char*)); char* first = "hello", *second = "there", *third = "world"; g_array_append_val(a, first); g_array_append_val(a, second); g_array_append_val(a, third); g_print("There are now %d items in the array\n", a->len); g_print("The first item is '%s'\n", g_array_index(a, char*, 0)); g_print("The third item is '%s'\n", g_array_index(a, char*, 2)); g_array_remove_index(a, 1); g_print("There are now %d items in the array\n", a->len); g_array_free(a, FALSE); return 0; } ================ ==<反灰>=========== #include void prt(GArray* a) { g_print("Array holds: "); int i; for (i = 0; i < a->len; i++) g_print("%d ", g_array_index(a, int, i)); g_print("\n "); } int main(int argc, char ** argv) { GArray* a = g_array_new(FALSE, FALSE, sizeof(int)); g_print("Array is empty, so appending some values\n"); int x[2] = {3,4}; g_array_append_vals(a, &x, 2); prt(a); g_print("Now to prepend some values\n"); int y[2] = {1,2}; g_array_prepend_vals(a, &y, 2); prt(a); g_print("And one more prepend\n"); int z = 0; g_array_prepend_val(a, z); prt(a); g_array_free(a, FALSE); return 0; } ================ ==<反灰>=========== #include #include int main(int argc, char ** argv) { GSList* list = NULL; g_print("The list is now %d items long\n", g_slist_length(list)); list = g_slist_append(list, "first"); list = g_slist_append(list, "second"); g_print("The list is now %d items long\n", g_slist_length(list)); g_slist_free(list); return 0; } ================ ==<反灰>=========== #include #include typedef struct { char* name; int shoe_size; } Person; int main(int argc, char ** argv) { GSList* list = NULL; Person* tom = (Person*)malloc(sizeof(Person)); tom->name = "Tom"; tom->shoe_size = 12; list = g_slist_append(list, tom); Person* fred = g_slice_new(Person); fred->name = "Fred"; fred->shoe_size = 11; list = g_slist_append(list, fred); g_print("Tom's shoe size is '%d'\n", ((Person*)list->data)->shoe_size); g_print("The last Person's name is '%s'\n", ((Person*)g_slist_last(list)->data)->name); g_slist_free(list); free(tom); g_slice_free(Person, fred); return 0; } ================ ==<反灰>=========== #include static void handle_error (GError*); int main (int argc, char *argv[]) { gchar *filename, *content; gsize bytes; GError *error = NULL; filename = g_build_filename (".", "temp.txt", NULL); g_file_set_contents (filename, "Hello World!", -1, &error); handle_error (error); if (!g_file_test (filename, G_FILE_TEST_EXISTS)) g_error ("Error: File does not exist!"); g_file_get_contents (filename, &content, &bytes, &error); handle_error (error); g_print ("%s\n", content); g_free (content); g_free (filename); return 0; } static void handle_error (GError *error) { if (error != NULL) { g_printf (error->message); g_clear_error (&error); } } ================ ==<反灰>=========== #include int main (int argc, char *argv[]) { GError *error = NULL; gchar *test1 = g_build_filename (".", "test1.txt", NULL); gchar *test3 = g_build_filename (".", "test3.txt", NULL); g_file_set_contents(test1, "test1", -1, &error); g_free(test1); g_file_set_contents(test3, "test3", -1, &error); g_free(test3); g_rename("test1.txt","test2.txt"); g_remove("test3.txt"); g_mkdir("testdir1"); g_mkdir("testdir2"); g_rmdir("testdir2"); return 0; } ================ ==<反灰>=========== #include int main (int argc, char *argv[]) { GDir *dir = g_dir_open (g_get_home_dir (), 0, NULL); const gchar *file; if (!g_file_test (g_get_home_dir (), G_FILE_TEST_IS_DIR)) g_error ("Error: You do not have a home directory!"); while ((file = g_dir_read_name (dir))) g_print ("%s\n", file); g_dir_close (dir); return 0; } ================ ==<反灰>=========== #include int main (int argc, char *argv[]) { GTimer *timer; gdouble start_time = 0.0; gdouble end_time = 0.0; gint count = 0; timer = g_timer_new (); start_time = g_timer_elapsed (timer, NULL); sleep(5); end_time = g_timer_elapsed (timer, NULL); g_print ("Elapsed Time: %.2f\n", end_time - start_time); return 0; } ================ ==<反灰>=========== #include gboolean callback(gpointer data) { g_print("Callback called\n"); return TRUE; } int main() { GMainLoop *loop; loop = g_main_loop_new(NULL, FALSE); g_print("Starting loop...\n"); g_timeout_add(1000, (GSourceFunc)callback, NULL); g_main_loop_run(loop); return 0; } ================