継続は力にゃりん。毎日続けることができるのか? とばし * 型変換のマクロ * 配列で gint 以外使えるの? GLib のデータ型残り * 双方向連結リスト * トラッシュ・スタック * バイト配列 * キーつきデータ・リスト * データセット * リレーショナルとタプル * キャッシュ * メモリ・アロケータ GLib のユーティリティ残り * フック関数 * プロセスの生成 * シンプルな XML サブセットのパーサ * Windows 互換の関数 進捗 * 2006-12-22 終了 * 2006-12-04 Glib コア・アプリケーションのサポート メッセージの記録 * 2006-11-23 Glib コア・アプリケーションのサポート メッセージ出力とデバッグ関数 * 2006-10-04 GLib のユーティリティ Unicode の取り扱い * 2006-09-25 GLib のデータ型 両端キュー * 2006-09-21 GLib のデータ型 クォーク * 2006-08-30 Ruby との比較 Hash * 2006-07-27 GLib のデータ型 N 分木 * 2006-07-15 GLib のデータ型 平衡二分木 * 2006-07-10 GLib のデータ型 文字列チャンク * 2006-06-28 GLib のデータ型 メモリ・チャンク * 2006-04-22 Ruby との比較 String(正規表現以外) * 2006-04-10 Ruby との比較 List(Enumerable。GArray で) * 2006-02-13 Ruby との比較 List(GArray で) * 2006-01-31 Ruby との比較 List(Enumerable) * 2005-12-03 Ruby との比較 List * 2005-10-25 GLib のユーティリティ レキシカル・スキャナ * 2005-10-12 GLib のユーティリティ 文字セットの変換 * 2005-10-09 GLib のユーティリティ タイマー * 2005-10-05 GLib のユーティリティ 文字列の自動補完 * 2005-10-02 GLib のユーティリティ Shell 関連のユーティリティ * 2005-09-25 GLib のユーティリティ ファイル・ユーティリティ * 2005-09-21 GLib のユーティリティ Glob スタイルのパターン・マッチング * 2005-08-31 GLib のユーティリティ その他のユーティリティ関数 * 2005-08-19 GLib のユーティリティ 乱数 * 2005-07-04 GLib のユーティリティ 日付と時刻の関数 * 2005-06-24 GLib のデータ型 ポインタ配列 * 2005-06-22 ぶらぶら GSList, GArray で double * 2005-04-25 GLib のユーティリティ 文字列ユーティリティ関数 * 2005-04-12 GLib のデータ型 配列 * 2005-03-31 GLib のデータ型 ハッシュ・テーブル * 2005-03-09 GLib のデータ型 単方向連結リスト * 2005-02-17 GLib のデータ型 文字列 * 2005-02-16 数値の定義 * 2005-02-13 バイトオーダーに関するマクロ * 2005-02-12 標準のマクロ * 2005-02-11 基本型の限界 * 2005-02-08 基本的な型 !2006-12-22 Fri #include int main() { g_log_default_handler(NULL, G_LOG_LEVEL_WARNING, "hoge", "foo"); return 0; } で、 ** (process:23490): WARNING **: hoge !2006-12-21 Thu #include #define G_LOG_DOMAIN "foo" static void my_log_handler(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data) { g_print("%s\n", log_domain); g_print("%d\n", log_level); g_print("%s\n", message); g_print("%s\n", (gchar*)user_data); } int main() { g_warning("hoge1"); g_log_set_handler("foo", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, my_log_handler, "foo"); g_log_set_fatal_mask("foo", G_LOG_LEVEL_WARNING); g_warning("hoge2"); g_warning("hoge3"); return 0; } で、 (process:23449): foo-WARNING **: hoge1 foo 18 hoge2 foo アボートしました 「#define G_LOG_DOMAIN "foo"」は良くないみたい 「-DG_LOG_DOMAIN=\"foo\"」とやる方が良いみたい !2006-12-20 Wed #include static void my_log_handler(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data) { g_print("%s\n", log_domain); g_print("%d\n", log_level); g_print("%s\n", message); g_print("%s\n", (gchar*)user_data); } int main() { g_warning("hoge1"); g_log_set_fatal_mask("foo", G_LOG_LEVEL_WARNING); g_warning("hoge2"); g_log_set_handler("foo", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, my_log_handler, "foo"); g_warning("hoge3"); return 0; } で、 ** (process:23432): WARNING **: hoge1 ** (process:23432): WARNING **: hoge2 ** (process:23432): WARNING **: hoge3 なんか LOG_DOMAIN を勘違いしていたみたい !2006-12-19 Tue #include int main() { g_warning("hoge1"); g_log_set_fatal_mask(NULL, G_LOG_LEVEL_WARNING); g_warning("hoge2"); return 0; } で、 ** (process:23409): WARNING **: hoge1 ** WARNING **: hoge2 aborting... アボートしました !2006-12-18 Mon #include int main() { g_warning("hoge1"); g_log_set_always_fatal(G_LOG_LEVEL_WARNING); g_warning("hoge2"); return 0; } で、 ** (process:23361): WARNING **: hoge1 ** WARNING **: hoge2 aborting... アボートしました !2006-12-17 Sun #include static void my_log_handler(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data) { g_print("%s\n", log_domain); g_print("%d\n", log_level); g_print("%s\n", message); g_print("%s\n", (gchar*)user_data); } int main() { guint x; x = g_log_set_handler(NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, my_log_handler, NULL); g_warning("hoge1"); g_log_remove_handler(NULL, x); g_warning("hoge2"); return 0; } で、 (null) 16 hoge1 (null) ** (process:23347): WARNING **: hoge2 !2006-12-16 Sat #include static void my_log_handler(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data) { g_print("%s\n", log_domain); g_print("%d\n", log_level); g_print("%s\n", message); g_print("%s\n", (gchar*)user_data); } int main() { g_log_set_handler(NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, my_log_handler, "foo"); g_warning("hoge"); return 0; } で、 (null) 16 hoge foo !2006-12-15 Fri #include static void my_log_handler(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data) { g_print("%s\n", log_domain); g_print("%d\n", log_level); g_print("%s\n", message); g_print("%s\n", (gchar*)user_data); } int main() { g_log_set_handler(NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, my_log_handler, NULL); g_warning("hoge"); return 0; } で、 (null) 16 hoge (null) !2006-12-14 Thu #include int main() { g_error("foo: %d", 10); return 0; } で、 ** ERROR **: foo: 10 aborting... アボートしました !2006-12-13 Wed #include int main() { g_critical("foo: %d", 10); return 0; } で、 ** (process:20415): CRITICAL **: foo: 10 !2006-12-12 Tue #include int main() { g_warning("foo: %d", 10); return 0; } で、 ** (process:20406): WARNING **: foo: 10 !2006-12-11 Mon g_logv とばし #include int main() { g_message("foo: %d", 10); return 0; } で、 ** Message: foo: 10 !2006-12-10 Sun #include int main() { g_log(G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "foo"); return 0; } で、 ** ERROR **: foo aborting... アボートしました !2006-12-09 Sat #include int main() { g_log(G_LOG_DOMAIN, G_LOG_FLAG_FATAL, "foo"); return 0; } で、 何も出力されず !2006-12-08 Fri #include int main() { g_log(G_LOG_DOMAIN, G_LOG_FLAG_RECURSION, "foo"); return 0; } で、 何も出力されず !2006-12-07 Thu #include int main() { g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "foo"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "foo"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "foo"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "foo"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "foo"); return 0; } で、 ** (process:20333): CRITICAL **: foo ** (process:20333): WARNING **: foo ** Message: foo ** INFO: foo ** (process:20333): DEBUG: foo !2006-12-06 Wed #include int main() { g_log(G_LOG_DOMAIN, 1 << G_LOG_LEVEL_USER_SHIFT, "foo"); return 0; } で、 ** LOG-0x100: foo !2006-12-05 Tue #include int main() { g_log(G_LOG_DOMAIN, G_LOG_FATAL_MASK, "foo"); return 0; } で、 ** (process:20298): ERROR (recursed) **: foo aborting... アボートしました !2006-12-04 Mon #include int main() { g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "foo"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "foo: %d", 10); return 0; } で、 ** (process:20289): DEBUG: foo ** (process:20289): DEBUG: foo: 10 !2006-12-03 Sun #include int main() { #if 1 G_BREAKPOINT(); #endif return 0; } で、 *************** *** 58,73 **** .loc 1 9 0 pushl %ebp .LCFI0: - .loc 1 16 0 - xorl %eax, %eax - .loc 1 9 0 movl %esp, %ebp .LCFI1: subl $8, %esp .LCFI2: andl $-16, %esp .loc 1 16 0 movl %ebp, %esp popl %ebp ret .LFE11: --- 58,79 ---- .loc 1 9 0 pushl %ebp .LCFI0: movl %esp, %ebp .LCFI1: subl $8, %esp .LCFI2: andl $-16, %esp + .loc 1 11 0 + .LBB2: + .LBB3: + #APP + int $03 .loc 1 16 0 + #NO_APP movl %ebp, %esp + .LBE3: + .LBE2: + xorl %eax, %eax popl %ebp ret .LFE11: !2006-12-02 Sat #include int main() { g_on_error_stack_trace(NULL); return 0; } ? !2006-12-01 Fri #include int main() { g_on_error_query(NULL); return 0; } で、 (process:32077): [E]xit, [H]alt or [P]roceed: e !2006-11-30 Thu #include int main() { g_assert_not_reached(); return 0; } で、 ** ERROR **: file 2006113000.c: line 10 (main): should not be reached aborting... アボートしました !2006-11-29 Wed #include int main() { #if 1 g_assert(0); #else g_assert(1); #endif return 0; } で、 ** ERROR **: file 2006112900.c: line 11 (main): assertion failed: (0) aborting... アボートしました !2006-11-28 Tue #include int func() { g_return_val_if_reached(100); g_print("in func()\n"); } int main() { g_print("%d\n", func()); return 0; } で、 ** (process:32043): CRITICAL **: file 2006112800.c: line 10 (func): should not be reached 100 !2006-11-27 Mon #include void func() { g_return_if_reached(); g_print("in func()\n"); } int main() { func(); return 0; } で、 ** (process:32033): CRITICAL **: file 2006112700.c: line 10 (func): should not be reached !2006-11-26 Sun #include int func() { #if 1 g_return_val_if_fail(0, 100); #else g_return_val_if_fail(1, 100); #endif } int main() { g_print("%d\n", func()); return 0; } で、 ** (process:32024): CRITICAL **: func: assertion `0' failed 100 !2006-11-25 Sat #include void func() { #if 1 g_return_if_fail(0); #else g_return_if_fail(1); #endif g_print("in func()\n"); } int main() { func(); return 0; } で、 ** (process:32007): CRITICAL **: func: assertion `0' failed !2006-11-24 Fri #include #include void new_print(const char *str) { printf("*%s*\n", str); } int main() { g_printerr("foo\n"); g_set_printerr_handler((GPrintFunc)new_print); g_printerr("foo"); return 0; } で、 foo *foo* !2006-11-23 Thu #include #include void new_print(const char *str) { printf("*%s*\n", str); } int main() { g_print("foo\n"); g_set_print_handler((GPrintFunc)new_print); g_print("foo"); return 0; } で、 foo *foo* !2006-11-22 Wed #include int main() { gchar *new_str, c[6]; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i, j, size; new_str = g_convert("あいう", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (j = 0; j < 3; j++) { size = g_unichar_to_utf8(new_unichar[j], c); g_print("%d\n", size); for (i = 0; i < size; i++) { g_print("%x\n", c[i]); } } size = g_unichar_to_utf8('a', c); g_print("%d\n", size); for (i = 0; i < size; i++) { g_print("%x\n", c[i]); } g_free(new_str); g_free(new_unichar); return 0; } で、 3 ffffffe3 ffffff81 ffffff82 3 ffffffe3 ffffff81 ffffff84 3 ffffffe3 ffffff81 ffffff86 1 61 !2006-11-21 Tue #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("あいう", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); g_free(new_str); new_str = g_ucs4_to_utf8(new_unichar, -1, &items_read, &items_write, &error); g_print("%ld\n", items_write); for (i = 0; i < items_write; i++) { g_print("%x\n", new_str[i]); } g_free(new_str); g_free(new_unichar); return 0; } で、 9 ffffffe3 ffffff81 ffffff82 ffffffe3 ffffff81 ffffff84 ffffffe3 ffffff81 ffffff86 !2006-11-20 Mon #include int main() { gchar *new_str; gunichar *new_unichar; gunichar2 *new_unichar2; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("あいう", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); new_unichar2 = g_ucs4_to_utf16(new_unichar, -1, &items_read, &items_write, &error); g_print("%ld\n", items_write); for (i = 0; i < items_write; i++) { g_print("%x %x\n", new_unichar[i], new_unichar2[i]); } g_free(new_str); g_free(new_unichar); g_free(new_unichar2); return 0; } で、 3 3042 3042 3044 3044 3046 3046 !2006-11-19 Sun #include int main() { gchar *new_str; gunichar2 *new_unichar2; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("あいう", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar2 = g_utf8_to_utf16(new_str, -1, &items_read, &items_write, &error); g_free(new_str); new_str = g_utf16_to_utf8(new_unichar2, -1, &items_read, &items_write, &error); g_print("%ld\n", items_write); for (i = 0; i < items_write; i++) { g_print("%x\n", new_str[i]); } g_free(new_str); g_free(new_unichar2); return 0; } で、 9 ffffffe3 ffffff81 ffffff82 ffffffe3 ffffff81 ffffff84 ffffffe3 ffffff81 ffffff86 出力のとき gchar にキャストした方が良いのか? !2006-11-18 Sat #include int main() { gchar *new_str; gunichar *new_unichar; gunichar2 *new_unichar2; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("あいう", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar2 = g_utf8_to_utf16(new_str, -1, &items_read, &items_write, &error); new_unichar = g_utf16_to_ucs4(new_unichar2, -1, &items_read, &items_write, &error); g_print("%ld\n", items_write); for (i = 0; i < items_write; i++) { g_print("%x %x\n", new_unichar[i], new_unichar2[i]); } g_free(new_str); g_free(new_unichar); g_free(new_unichar2); return 0; } で、 3 3042 3042 3044 3044 3046 3046 !2006-11-17 Fri #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_write; GError *error = NULL; int i; new_str = g_convert("あいう", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4_fast(new_str, -1, &items_write); g_print("%ld\n", items_write); for (i = 0; i < items_write; i++) { g_print("%x\n", new_unichar[i]); } g_free(new_str); g_free(new_unichar); return 0; } で、 3 3042 3044 3046 !2006-11-16 Thu #include int main() { gchar *new_str; gunichar2 *new_unichar2; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("あいう", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar2 = g_utf8_to_utf16(new_str, -1, &items_read, &items_write, &error); g_print("%ld\n", items_write); for (i = 0; i < items_write; i++) { g_print("%x\n", new_unichar2[i]); } g_free(new_str); g_free(new_unichar2); return 0; } で、 3 3042 3044 3046 直接バイナリを出力するにはどうしたら良いんだっけ? !2006-11-15 Wed #include int main() { gchar *new_str, *new_str2, *p; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } new_str2 = g_utf8_collate_key(new_str, -1); for (p = new_str2, i = 0; *p != 0; p = g_utf8_next_char(p), i++) { g_print("%d %x\n", i, g_utf8_get_char(p)); } g_free(new_str); g_free(new_str2); g_free(new_unichar); return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 0 63 1 43 2 63 3 43 4 32 5 32 6 61 !2006-11-14 Tue #include int main() { gchar *str1, *str2; gsize size_read, size_write; GError *error = NULL; str1 = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); str2 = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); g_print("%d\n", g_utf8_collate(str1, str2)); g_print("%d\n", g_utf8_collate("foo", "foo")); g_print("%d\n", g_utf8_collate("bar", "foo")); g_print("%d\n", g_utf8_collate("foo", "bar")); g_free(str1); g_free(str2); return 0; } で、 0 0 -4 4 !2006-11-13 Mon #include int main() { gchar *new_str, *new_str2, *p; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } new_str2 = g_utf8_normalize(new_str, -1, G_NORMALIZE_DEFAULT); for (p = new_str2, i = 0; *p != 0; p = g_utf8_next_char(p), i++) { g_print("%d %x\n", i, g_utf8_get_char(p)); } g_free(new_str); g_free(new_str2); g_free(new_unichar); return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f ? !2006-11-12 Sun #include int main() { gchar *new_str, *new_str2, *p; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } new_str2 = g_utf8_casefold(new_str, -1); for (p = new_str2, i = 0; *p != 0; p = g_utf8_next_char(p), i++) { g_print("%d %x\n", i, g_utf8_get_char(p)); } g_free(new_str); g_free(new_str2); g_free(new_unichar); return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 0 61 1 61 2 ff41 3 ff41 4 30 5 ff10 6 5f !2006-11-11 Sat #include int main() { gchar *new_str, *new_str2, *p; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } new_str2 = g_utf8_strdown(new_str, -1); for (p = new_str2, i = 0; *p != 0; p = g_utf8_next_char(p), i++) { g_print("%d %x\n", i, g_utf8_get_char(p)); } g_free(new_str); g_free(new_str2); g_free(new_unichar); return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 0 61 1 61 2 ff41 3 ff41 4 30 5 ff10 6 5f !2006-11-10 Fri #include int main() { gchar *new_str, *new_str2, *p; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } new_str2 = g_utf8_strup(new_str, -1); for (p = new_str2, i = 0; *p != 0; p = g_utf8_next_char(p), i++) { g_print("%d %x\n", i, g_utf8_get_char(p)); } g_free(new_str); g_free(new_str2); g_free(new_unichar); return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 0 41 1 41 2 ff21 3 ff21 4 30 5 ff10 6 5f !2006-11-09 Thu #include int main() { gchar *new_str; gsize size_read, size_write; GError *error = NULL; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); g_print("%d\n", g_utf8_validate(new_str, -1, NULL)); g_print("%d\n", g_utf8_validate("foo", -1, NULL)); g_free(new_str); return 0; } で、 1 1 !2006-11-08 Wed #include int main() { gchar *new_str, *rev_str, *p; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } rev_str = g_utf8_strreverse(new_str, -1); for (p = rev_str, i = 0; *p != 0; p = g_utf8_next_char(p), i++) { g_print("%d %x\n", i, g_utf8_get_char(p)); } g_free(new_str); g_free(rev_str); g_free(new_unichar); return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 0 5f 1 ff10 2 30 3 ff21 4 ff41 5 41 6 61 今まで、g_free を付け忘れてたかも… !2006-11-07 Tue #include int main() { gchar *new_str, *p; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } p = g_utf8_strchr(new_str, -1, 'a'); g_print("%x\n", g_utf8_get_char(p)); p = g_utf8_strchr(new_str, -1, 0xff41); g_print("%x\n", g_utf8_get_char(p)); p = g_utf8_strrchr(new_str, -1, 'a'); g_print("%x\n", g_utf8_get_char(p)); p = g_utf8_strrchr(new_str, -1, 0xff41); g_print("%x\n", g_utf8_get_char(p)); return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 61 ff41 61 ff41 !2006-11-06 Mon #include int main() { gchar *new_str, cp_str[256] = {0}, *p; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } g_utf8_strncpy(cp_str, new_str, g_utf8_strlen(new_str, -1)); for (p = cp_str, i = 0; *p != 0; p = g_utf8_next_char(p), i++) { g_print("%d %x\n", i, g_utf8_get_char(p)); } return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f !2006-11-05 Sun #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } g_print("%ld\n", g_utf8_strlen(new_str, -1)); return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 7 !2006-11-04 Sat #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } g_print("%x\n", g_utf8_get_char(g_utf8_find_prev_char(new_str, g_utf8_offset_to_pointer(new_str, 6)))); return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f ff10 !2006-11-03 Fri #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } g_print("%x\n", g_utf8_get_char(g_utf8_find_next_char(new_str, g_utf8_offset_to_pointer(new_str, 6)))); return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 41 「p を UTF-8 文字の先頭にしないで下さい。」というのは本当なのだろうか? !2006-11-02 Thu #include int main() { gchar *new_str, *p; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } for (p = g_utf8_offset_to_pointer(new_str, 6), i = 6; i >= 0; p = g_utf8_prev_char(p), i--) { g_print("%d %x %x\n", i, *p, g_utf8_get_char(p)); } return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 6 5f 5f 5 ffffffef ff10 4 30 30 3 ffffffef ff21 2 ffffffef ff41 1 41 41 0 61 61 !2006-11-01 Wed #include int main() { gchar *new_str, *p; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } for (p = new_str, i = 0; *p != 0; p = g_utf8_next_char(p), i++) { g_print("%d %p\n", i, p); } for (i = 0; i < 7; i++) { g_print("%d %p %ld\n", i, g_utf8_offset_to_pointer(new_str, i), g_utf8_pointer_to_offset(new_str, g_utf8_offset_to_pointer(new_str, i))); } return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 0 0x80670b8 1 0x80670b9 2 0x80670ba 3 0x80670bd 4 0x80670c0 5 0x80670c1 6 0x80670c4 0 0x80670b8 0 1 0x80670b9 1 2 0x80670ba 2 3 0x80670bd 3 4 0x80670c0 4 5 0x80670c1 5 6 0x80670c4 6 !2006-10-31 Tue #include int main() { gchar *new_str, *p; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } for (p = new_str, i = 0; *p != 0; p = g_utf8_next_char(p), i++) { g_print("%d %p\n", i, p); } for (i = 0; i < 7; i++) { g_print("%d %p\n", i, g_utf8_offset_to_pointer(new_str, i)); } return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 0 0x8067048 1 0x8067049 2 0x806704a 3 0x806704d 4 0x8067050 5 0x8067051 6 0x8067054 0 0x8067048 1 0x8067049 2 0x806704a 3 0x806704d 4 0x8067050 5 0x8067051 6 0x8067054 !2006-10-30 Mon #include int main() { gchar *new_str, *p; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } for (p = new_str; *p != 0; p = g_utf8_next_char(p)) { g_print("%x\n", g_utf8_get_char_validated(p, -1)); } return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 61 41 ff41 ff21 30 ff10 5f !2006-10-29 Sun #include int main() { gchar *new_str, *p; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } for (p = new_str; *p != 0; p = g_utf8_next_char(p)) { g_print("%x\n", g_utf8_get_char(p)); } return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 61 41 ff41 ff21 30 ff10 5f !2006-10-28 Sat #include int main() { gchar *new_str, *p; gsize size_read, size_write; GError *error = NULL; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); for (p = new_str; *p != 0; p = g_utf8_next_char(p)) { g_print("%x\n", *p); } return 0; } で、 61 41 ffffffef ffffffef 30 ffffffef 5f 全角の出力が良く分からないや !2006-10-27 Fri #include int main() { gchar *new_str; gunichar *new_unichar, *new_unichar2; gsize size_read, size_write, result_len; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } for (i = 0; i < 7; i++) { new_unichar2 = g_unicode_canonical_decomposition(new_unichar[i], &result_len); g_print("%d %d %x\n", i, result_len, new_unichar2[0]); } return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 0 1 61 1 1 41 2 1 ff41 3 1 ff21 4 1 30 5 1 ff10 6 1 5f 何? !2006-10-26 Thu #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } g_unicode_canonical_ordering(new_unichar, 7); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 何? !2006-10-25 Wed #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aA?9aA_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %d\n", i, g_unichar_type(new_unichar[i])); } g_print("%d\n", g_unichar_type(' ')); g_print("%d\n", g_unichar_type('a')); g_print("%d\n", g_unichar_type('A')); g_print("%d\n", g_unichar_type('9')); g_print("%d\n", g_unichar_type('!')); g_print("%d\n", g_unichar_type('?')); return 0; } で、 0 5 1 9 2 21 3 13 4 5 5 9 6 16 29 5 9 13 21 21 良く分からない !2006-10-24 Tue #include int main() { g_print("G_UNICODE_BREAK_MANDATORY %d\n", G_UNICODE_BREAK_MANDATORY); g_print("G_UNICODE_BREAK_CARRIAGE_RETURN %d\n", G_UNICODE_BREAK_CARRIAGE_RETURN); g_print("G_UNICODE_BREAK_LINE_FEED %d\n", G_UNICODE_BREAK_LINE_FEED); g_print("G_UNICODE_BREAK_COMBINING_MARK %d\n", G_UNICODE_BREAK_COMBINING_MARK); g_print("G_UNICODE_BREAK_SURROGATE %d\n", G_UNICODE_BREAK_SURROGATE); g_print("G_UNICODE_BREAK_ZERO_WIDTH_SPACE %d\n", G_UNICODE_BREAK_ZERO_WIDTH_SPACE); g_print("G_UNICODE_BREAK_INSEPARABLE %d\n", G_UNICODE_BREAK_INSEPARABLE); g_print("G_UNICODE_BREAK_NON_BREAKING_GLUE %d\n", G_UNICODE_BREAK_NON_BREAKING_GLUE); g_print("G_UNICODE_BREAK_CONTINGENT %d\n", G_UNICODE_BREAK_CONTINGENT); g_print("G_UNICODE_BREAK_SPACE %d\n", G_UNICODE_BREAK_SPACE); g_print("G_UNICODE_BREAK_AFTER %d\n", G_UNICODE_BREAK_AFTER); g_print("G_UNICODE_BREAK_BEFORE %d\n", G_UNICODE_BREAK_BEFORE); g_print("G_UNICODE_BREAK_BEFORE_AND_AFTER %d\n", G_UNICODE_BREAK_BEFORE_AND_AFTER); g_print("G_UNICODE_BREAK_HYPHEN %d\n", G_UNICODE_BREAK_HYPHEN); g_print("G_UNICODE_BREAK_NON_STARTER %d\n", G_UNICODE_BREAK_NON_STARTER); g_print("G_UNICODE_BREAK_OPEN_PUNCTUATION %d\n", G_UNICODE_BREAK_OPEN_PUNCTUATION); g_print("G_UNICODE_BREAK_CLOSE_PUNCTUATION %d\n", G_UNICODE_BREAK_CLOSE_PUNCTUATION); g_print("G_UNICODE_BREAK_QUOTATION %d\n", G_UNICODE_BREAK_QUOTATION); g_print("G_UNICODE_BREAK_EXCLAMATION %d\n", G_UNICODE_BREAK_EXCLAMATION); g_print("G_UNICODE_BREAK_IDEOGRAPHIC %d\n", G_UNICODE_BREAK_IDEOGRAPHIC); g_print("G_UNICODE_BREAK_NUMERIC %d\n", G_UNICODE_BREAK_NUMERIC); g_print("G_UNICODE_BREAK_INFIX_SEPARATOR %d\n", G_UNICODE_BREAK_INFIX_SEPARATOR); g_print("G_UNICODE_BREAK_SYMBOL %d\n", G_UNICODE_BREAK_SYMBOL); g_print("G_UNICODE_BREAK_ALPHABETIC %d\n", G_UNICODE_BREAK_ALPHABETIC); g_print("G_UNICODE_BREAK_PREFIX %d\n", G_UNICODE_BREAK_PREFIX); g_print("G_UNICODE_BREAK_POSTFIX %d\n", G_UNICODE_BREAK_POSTFIX); g_print("G_UNICODE_BREAK_COMPLEX_CONTEXT %d\n", G_UNICODE_BREAK_COMPLEX_CONTEXT); g_print("G_UNICODE_BREAK_AMBIGUOUS %d\n", G_UNICODE_BREAK_AMBIGUOUS); g_print("G_UNICODE_BREAK_UNKNOWN %d\n", G_UNICODE_BREAK_UNKNOWN); return 0; } で、 G_UNICODE_BREAK_MANDATORY 0 G_UNICODE_BREAK_CARRIAGE_RETURN 1 G_UNICODE_BREAK_LINE_FEED 2 G_UNICODE_BREAK_COMBINING_MARK 3 G_UNICODE_BREAK_SURROGATE 4 G_UNICODE_BREAK_ZERO_WIDTH_SPACE 5 G_UNICODE_BREAK_INSEPARABLE 6 G_UNICODE_BREAK_NON_BREAKING_GLUE 7 G_UNICODE_BREAK_CONTINGENT 8 G_UNICODE_BREAK_SPACE 9 G_UNICODE_BREAK_AFTER 10 G_UNICODE_BREAK_BEFORE 11 G_UNICODE_BREAK_BEFORE_AND_AFTER 12 G_UNICODE_BREAK_HYPHEN 13 G_UNICODE_BREAK_NON_STARTER 14 G_UNICODE_BREAK_OPEN_PUNCTUATION 15 G_UNICODE_BREAK_CLOSE_PUNCTUATION 16 G_UNICODE_BREAK_QUOTATION 17 G_UNICODE_BREAK_EXCLAMATION 18 G_UNICODE_BREAK_IDEOGRAPHIC 19 G_UNICODE_BREAK_NUMERIC 20 G_UNICODE_BREAK_INFIX_SEPARATOR 21 G_UNICODE_BREAK_SYMBOL 22 G_UNICODE_BREAK_ALPHABETIC 23 G_UNICODE_BREAK_PREFIX 24 G_UNICODE_BREAK_POSTFIX 25 G_UNICODE_BREAK_COMPLEX_CONTEXT 26 G_UNICODE_BREAK_AMBIGUOUS 27 G_UNICODE_BREAK_UNKNOWN 28 !2006-10-23 Mon #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("8989aA_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %d\n", i, g_unichar_xdigit_value(new_unichar[i])); } g_print("%d\n", g_unichar_xdigit_value(' ')); g_print("%d\n", g_unichar_xdigit_value('a')); g_print("%d\n", g_unichar_xdigit_value('A')); g_print("%d\n", g_unichar_xdigit_value('9')); return 0; } で、 0 8 1 9 2 8 3 9 4 10 5 -1 6 -1 -1 10 10 9 !2006-10-22 Sun #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("8989aA_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %d\n", i, g_unichar_digit_value(new_unichar[i])); } g_print("%d\n", g_unichar_digit_value(' ')); g_print("%d\n", g_unichar_digit_value('a')); g_print("%d\n", g_unichar_digit_value('A')); g_print("%d\n", g_unichar_digit_value('9')); return 0; } で、 0 8 1 9 2 8 3 9 4 -1 5 -1 6 -1 -1 -1 -1 9 !2006-10-21 Sat #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } for (i = 0; i < 7; i++) { g_print("%d %x\n", i, g_unichar_totitle(new_unichar[i])); } g_print("%x\n", g_unichar_totitle(' ')); g_print("%x\n", g_unichar_totitle('a')); g_print("%x\n", g_unichar_totitle('A')); g_print("%x\n", g_unichar_totitle('0')); return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 0 41 1 41 2 ff21 3 ff21 4 30 5 ff10 6 5f 20 41 41 30 !2006-10-20 Fri #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } for (i = 0; i < 7; i++) { g_print("%d %x\n", i, g_unichar_tolower(new_unichar[i])); } g_print("%x\n", g_unichar_tolower(' ')); g_print("%x\n", g_unichar_tolower('a')); g_print("%x\n", g_unichar_tolower('A')); g_print("%x\n", g_unichar_tolower('0')); return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 0 61 1 61 2 ff41 3 ff41 4 30 5 ff10 6 5f 20 61 61 30 !2006-10-19 Thu #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } for (i = 0; i < 7; i++) { g_print("%d %x\n", i, g_unichar_toupper(new_unichar[i])); } g_print("%x\n", g_unichar_toupper(' ')); g_print("%x\n", g_unichar_toupper('a')); g_print("%x\n", g_unichar_toupper('A')); g_print("%x\n", g_unichar_toupper('0')); return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 0 41 1 41 2 ff21 3 ff21 4 30 5 ff10 6 5f 20 41 41 30 !2006-10-18 Wed #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %d\n", i, g_unichar_iswide(new_unichar[i])); } return 0; } で、 0 0 1 0 2 1 3 1 4 0 5 1 6 0 !2006-10-17 Tue #include int main() { g_print("%d\n", g_unichar_isdefined(' ')); g_print("%d\n", g_unichar_isdefined('a')); g_print("%d\n", g_unichar_isdefined('\t')); g_print("%d\n", g_unichar_isdefined(0)); g_print("%d\n", g_unichar_isdefined(0xffff)); return 0; } で、 1 1 1 1 0 !2006-10-16 Mon #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); g_print("%d\n", g_unichar_istitle(' ')); g_print("%d\n", g_unichar_istitle('a')); g_print("%d\n", g_unichar_istitle('A')); g_print("%d\n", g_unichar_istitle('0')); for (i = 0; i < 7; i++) { g_print("%d %d\n", i, g_unichar_istitle(new_unichar[i])); } return 0; } で、 0 0 0 0 0 0 1 0 2 0 3 0 4 0 5 0 6 0 そもそも意味わからん !2006-10-15 Sun #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("xXaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); g_print("%d\n", g_unichar_isxdigit(' ')); g_print("%d\n", g_unichar_isxdigit('a')); g_print("%d\n", g_unichar_isxdigit('A')); g_print("%d\n", g_unichar_isxdigit('F')); g_print("%d\n", g_unichar_isxdigit('Z')); g_print("%d\n", g_unichar_isxdigit('0')); for (i = 0; i < 7; i++) { g_print("%d %d\n", i, g_unichar_isxdigit(new_unichar[i])); } return 0; } で、 0 1 1 1 0 1 0 0 1 0 2 0 3 0 4 1 5 1 6 0 「a」「A」は対象外なのか。なぜ? !2006-10-14 Sat #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); g_print("%d\n", g_unichar_isupper(' ')); g_print("%d\n", g_unichar_isupper('a')); g_print("%d\n", g_unichar_isupper('A')); g_print("%d\n", g_unichar_isupper('0')); for (i = 0; i < 7; i++) { g_print("%d %d\n", i, g_unichar_isupper(new_unichar[i])); } return 0; } で、 0 0 1 0 0 0 1 1 2 0 3 1 4 0 5 0 6 0 !2006-10-13 Fri #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00  ", 13, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); g_print("%d\n", g_unichar_isspace(' ')); g_print("%d\n", g_unichar_isspace('\t')); g_print("%d\n", g_unichar_isspace('a')); g_print("%d\n", g_unichar_isspace('A')); g_print("%d\n", g_unichar_isspace('0')); for (i = 0; i < 8; i++) { g_print("%d %d\n", i, g_unichar_isspace(new_unichar[i])); } return 0; } で、 1 1 0 0 0 0 0 1 0 2 0 3 0 4 0 5 0 6 1 7 1 !2006-10-12 Thu #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00。.", 13, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); g_print("%d\n", g_unichar_ispunct(' ')); g_print("%d\n", g_unichar_ispunct('.')); g_print("%d\n", g_unichar_ispunct(',')); g_print("%d\n", g_unichar_ispunct('a')); g_print("%d\n", g_unichar_ispunct('A')); g_print("%d\n", g_unichar_ispunct('0')); for (i = 0; i < 8; i++) { g_print("%d %d\n", i, g_unichar_ispunct(new_unichar[i])); } return 0; } で、 0 1 1 0 0 0 0 0 1 0 2 0 3 0 4 0 5 0 6 1 7 1 !2006-10-11 Wed #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); g_print("%d\n", g_unichar_isprint(' ')); g_print("%d\n", g_unichar_isprint('\t')); g_print("%d\n", g_unichar_isprint('a')); g_print("%d\n", g_unichar_isprint('A')); g_print("%d\n", g_unichar_isprint('0')); for (i = 0; i < 7; i++) { g_print("%d %d\n", i, g_unichar_isprint(new_unichar[i])); } return 0; } で、 1 0 1 1 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 !2006-10-10 Tue #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); g_print("%d\n", g_unichar_islower(' ')); g_print("%d\n", g_unichar_islower('a')); g_print("%d\n", g_unichar_islower('A')); g_print("%d\n", g_unichar_islower('0')); for (i = 0; i < 7; i++) { g_print("%d %d\n", i, g_unichar_islower(new_unichar[i])); } return 0; } で、 0 1 0 0 0 1 1 0 2 1 3 0 4 0 5 0 6 0 !2006-10-09 Mon #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00  ", 13, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); g_print("%d\n", g_unichar_isgraph(' ')); g_print("%d\n", g_unichar_isgraph('\t')); g_print("%d\n", g_unichar_isgraph('\n')); g_print("%d\n", g_unichar_isgraph('a')); g_print("%d\n", g_unichar_isgraph('0')); for (i = 0; i < 8; i++) { g_print("%d %d\n", i, g_unichar_isgraph(new_unichar[i])); } return 0; } で、 0 0 0 1 1 0 1 1 1 2 1 3 1 4 1 5 1 6 0 7 0 !2006-10-08 Sun #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } g_print("%d\n", g_unichar_isdigit(' ')); g_print("%d\n", g_unichar_isdigit('a')); g_print("%d\n", g_unichar_isdigit('0')); for (i = 0; i < 7; i++) { g_print("%d %d\n", i, g_unichar_isdigit(new_unichar[i])); } return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 0 0 1 0 0 1 0 2 0 3 0 4 1 5 1 6 0 !2006-10-07 Sat #include int main() { g_print("%d\n", g_unichar_iscntrl(' ')); g_print("%d\n", g_unichar_iscntrl('a')); g_print("%d\n", g_unichar_iscntrl('\t')); return 0; } で、 0 0 1 !2006-10-06 Fri #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } g_print("%d\n", g_unichar_isalpha(' ')); g_print("%d\n", g_unichar_isalpha('a')); g_print("%d\n", g_unichar_isalpha('0')); for (i = 0; i < 7; i++) { g_print("%d %d\n", i, g_unichar_isalpha(new_unichar[i])); } return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 0 1 0 0 1 1 1 2 1 3 1 4 0 5 0 6 0 !2006-10-05 Thu #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; int i; new_str = g_convert("aAaA00_", 11, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); for (i = 0; i < 7; i++) { g_print("%d %x\n", i, new_unichar[i]); } g_print("%d\n", g_unichar_isalnum(' ')); g_print("%d\n", g_unichar_isalnum('a')); g_print("%d\n", g_unichar_isalnum('0')); for (i = 0; i < 7; i++) { g_print("%d %d\n", i, g_unichar_isalnum(new_unichar[i])); } return 0; } で、 0 61 1 41 2 ff41 3 ff21 4 30 5 ff10 6 5f 0 1 1 0 1 1 1 2 1 3 1 4 1 5 1 6 0 !2006-10-04 Wed #include int main() { gchar *new_str; gunichar *new_unichar; gsize size_read, size_write; glong items_read, items_write; GError *error = NULL; new_str = g_convert("あいう", 7, "UTF-8", "EUC-JP", &size_read, &size_write, &error); new_unichar = g_utf8_to_ucs4(new_str, -1, &items_read, &items_write, &error); g_print("%x\n", new_unichar[0]); g_print("%x\n", new_unichar[1]); g_print("%d\n", g_unichar_validate(new_unichar[0])); g_print("%d\n", g_unichar_validate(' ')); g_print("%d\n", g_unichar_validate('a')); g_print("%d\n", g_unichar_validate(1)); g_print("%d\n", g_unichar_validate(0)); return 0; } で、 3042 3044 1 1 1 1 1 なんでも valid になってしまうの??? !2006-10-03 Tue #include int main() { GQueue *q; GList *list; gpointer p; q = g_queue_new(); g_queue_push_tail(q, GINT_TO_POINTER(1)); g_queue_push_tail(q, GINT_TO_POINTER(2)); g_queue_push_tail(q, GINT_TO_POINTER(3)); list = g_queue_pop_tail_link(q); while ((p = g_queue_pop_head(q))) { g_print("%d\n", GPOINTER_TO_INT(p)); } g_list_free(list); g_queue_free(q); return 0; } で、 1 2 そういえば、要素数を返す関数がないな。 直接構造体にアクセスしろってことか? !2006-10-02 Mon #include int main() { GQueue *q; GList *list; gpointer p; q = g_queue_new(); g_queue_push_tail(q, GINT_TO_POINTER(1)); g_queue_push_tail(q, GINT_TO_POINTER(2)); g_queue_push_tail(q, GINT_TO_POINTER(3)); list = g_queue_pop_head_link(q); while ((p = g_queue_pop_head(q))) { g_print("%d\n", GPOINTER_TO_INT(p)); } g_list_free(list); g_queue_free(q); return 0; } で、 2 3 !2006-10-01 Sun #include int main() { GQueue *q; GList *list = NULL; gpointer p; q = g_queue_new(); g_queue_push_tail(q, GINT_TO_POINTER(1)); g_queue_push_tail(q, GINT_TO_POINTER(2)); g_queue_push_tail(q, GINT_TO_POINTER(3)); list = g_list_append(list, GINT_TO_POINTER(4)); g_queue_push_tail_link(q, list); while ((p = g_queue_pop_head(q))) { g_print("%d\n", GPOINTER_TO_INT(p)); } g_queue_free(q); return 0; } で、 1 2 3 4 !2006-09-30 Sat #include int main() { GQueue *q; GList *list = NULL; gpointer p; q = g_queue_new(); g_queue_push_tail(q, GINT_TO_POINTER(1)); g_queue_push_tail(q, GINT_TO_POINTER(2)); g_queue_push_tail(q, GINT_TO_POINTER(3)); list = g_list_append(list, GINT_TO_POINTER(-1)); g_queue_push_head_link(q, list); while ((p = g_queue_pop_head(q))) { g_print("%d\n", GPOINTER_TO_INT(p)); } g_queue_free(q); return 0; } で、 -1 1 2 3 list の内容が 0 だと、表示できないや… !2006-09-29 Fri #include int main() { GQueue *q; q = g_queue_new(); g_print("%d\n", g_queue_is_empty(q)); g_queue_push_tail(q, GINT_TO_POINTER(1)); g_print("%d\n", g_queue_is_empty(q)); g_queue_pop_tail(q); g_print("%d\n", g_queue_is_empty(q)); g_queue_free(q); return 0; } で、 1 0 1 !2006-09-28 Thu #include int main() { GQueue *q; q = g_queue_new(); g_queue_push_tail(q, GINT_TO_POINTER(1)); g_queue_push_tail(q, GINT_TO_POINTER(2)); g_queue_push_tail(q, GINT_TO_POINTER(3)); g_print("%d\n", GPOINTER_TO_INT(g_queue_pop_tail(q))); g_print("%d\n", GPOINTER_TO_INT(g_queue_peek_tail(q))); g_queue_free(q); return 0; } で、 3 2 !2006-09-27 Wed #include int main() { GQueue *q; q = g_queue_new(); g_queue_push_tail(q, GINT_TO_POINTER(1)); g_queue_push_tail(q, GINT_TO_POINTER(2)); g_queue_push_tail(q, GINT_TO_POINTER(3)); g_print("%d\n", GPOINTER_TO_INT(g_queue_pop_head(q))); g_print("%d\n", GPOINTER_TO_INT(g_queue_peek_head(q))); g_queue_free(q); return 0; } で、 1 2 !2006-09-26 Tue #include int main() { GQueue *q; q = g_queue_new(); g_queue_push_tail(q, GINT_TO_POINTER(1)); g_queue_push_tail(q, GINT_TO_POINTER(2)); g_queue_push_tail(q, GINT_TO_POINTER(3)); g_print("%d\n", GPOINTER_TO_INT(g_queue_peek_head(q))); g_queue_free(q); return 0; } で、 1 !2006-09-25 Mon STL だと、deque は vector とほぼ同じメソッドだったが、 これはかなり違うっぽい。 #include int main() { GQueue *q; q = g_queue_new(); g_queue_push_head(q, GINT_TO_POINTER(1)); g_queue_push_head(q, GINT_TO_POINTER(2)); g_queue_push_head(q, GINT_TO_POINTER(3)); g_print("%d\n", GPOINTER_TO_INT(g_queue_peek_head(q))); g_queue_free(q); return 0; } で、 3 !2006-09-24 Sun #include int main() { GQuark q1, q2; q1 = g_quark_from_string("foo"); q2 = g_quark_from_string("bar"); q1 = g_quark_from_string("foo"); g_print("%d\n", g_quark_try_string("foo")); g_print("%d\n", g_quark_try_string("bar")); g_print("%d\n", g_quark_try_string("baz")); return 0; } で、 1 2 0 !2006-09-23 Sat #include int main() { GQuark q1, q2; q1 = g_quark_from_string("foo"); g_print("%s\n", g_quark_to_string(q1)); q2 = g_quark_from_string("bar"); g_print("%s\n", g_quark_to_string(q2)); q1 = g_quark_from_string("foo"); g_print("%s\n", g_quark_to_string(q1)); return 0; } で、 foo bar foo !2006-09-22 Fri #include int main() { GQuark q1, q2; q1 = g_quark_from_static_string("foo"); g_print("%d\n", q1); q2 = g_quark_from_static_string("bar"); g_print("%d\n", q2); q1 = g_quark_from_static_string("foo"); g_print("%d\n", q1); return 0; } で、 1 2 1 !2006-09-21 Thu #include int main() { GQuark q1, q2; q1 = g_quark_from_string("foo"); g_print("%d\n", q1); q2 = g_quark_from_string("bar"); g_print("%d\n", q2); q1 = g_quark_from_string("foo"); g_print("%d\n", q1); return 0; } で、 1 2 1 どういう仕組み?グローバルに値を保持しているの??? (← ソース見ろ!) !2006-09-20 Wed #include static GPtrArray* hash_values_at(GHashTable *hash, GPtrArray *ary) { GPtrArray *ret_ary; guint i; gpointer x; ret_ary = g_ptr_array_new(); for (i = 0; i < ary->len; i++) { x = g_hash_table_lookup(hash, g_ptr_array_index(ary, i)); g_ptr_array_add(ret_ary, x); } return ret_ary; } int main() { GHashTable *hash; GPtrArray *keys; GPtrArray *ary; guint i; hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash, "Perl", GINT_TO_POINTER(5)); g_hash_table_insert(hash, "Ruby", GINT_TO_POINTER(1)); g_hash_table_insert(hash, "Python", GINT_TO_POINTER(2)); keys = g_ptr_array_new(); g_ptr_array_add(keys, "Python"); g_ptr_array_add(keys, "Java"); g_ptr_array_add(keys, "Ruby"); ary = hash_values_at(hash, keys); for (i = 0; i < ary->len; i++) { g_print("%d\n", GPOINTER_TO_INT(g_ptr_array_index(ary, i))); } g_hash_table_destroy(hash); g_ptr_array_free(ary, FALSE); return 0; } で、 2 0 1 !2006-09-19 Tue #include static void push_value(gpointer k, gpointer v, gpointer ud); static GPtrArray* hash_values(GHashTable *hash) { GPtrArray *ary; ary = g_ptr_array_new(); g_hash_table_foreach(hash, push_value, ary); return ary; } static void push_value(gpointer k, gpointer v, gpointer ud) { g_ptr_array_add((GPtrArray *)ud, v); } int main() { GHashTable *hash; GPtrArray *ary; guint i; hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash, "Perl", GINT_TO_POINTER(5)); g_hash_table_insert(hash, "Ruby", GINT_TO_POINTER(1)); g_hash_table_insert(hash, "Python", GINT_TO_POINTER(2)); ary = hash_values(hash); for (i = 0; i < ary->len; i++) { g_print("%d\n", GPOINTER_TO_INT(g_ptr_array_index(ary, i))); } g_hash_table_destroy(hash); g_ptr_array_free(ary, FALSE); return 0; } で、 1 2 5 !2006-09-18 Mon #include static void push_value(gpointer k, gpointer v, gpointer ud); static void hash_update(GHashTable *hash1, GHashTable *hash2) { g_hash_table_foreach(hash2, push_value, hash1); } static void push_value(gpointer k, gpointer v, gpointer ud) { g_hash_table_insert((GHashTable *)ud, k, v); } static void print_func(gpointer k, gpointer v, gpointer ud) { g_print("%-6s -> %d\n", (gchar*)k, GPOINTER_TO_INT(v)); } int main() { GHashTable *hash1, *hash2; hash1 = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash1, "Perl", GINT_TO_POINTER(5)); g_hash_table_insert(hash1, "Ruby", GINT_TO_POINTER(1)); g_hash_table_insert(hash1, "Python", GINT_TO_POINTER(2)); hash2 = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash2, "foo", GINT_TO_POINTER(0)); g_hash_table_insert(hash2, "bar", GINT_TO_POINTER(1)); g_hash_table_insert(hash2, "Perl", GINT_TO_POINTER(6)); hash_update(hash1, hash2); g_hash_table_foreach(hash1, print_func, NULL); g_print("*****\n"); g_hash_table_foreach(hash2, print_func, NULL); g_hash_table_destroy(hash1); g_hash_table_destroy(hash2); return 0; } で、 foo -> 0 Ruby -> 1 bar -> 1 Python -> 2 Perl -> 6 ***** foo -> 0 bar -> 1 Perl -> 6 !2006-09-17 Sun #include static void push_value(gpointer k, gpointer v, gpointer ud); static GPtrArray* hash_to_a(GHashTable *hash) { GPtrArray *ary; ary = g_ptr_array_new(); g_hash_table_foreach(hash, push_value, ary); return ary; } static void push_value(gpointer k, gpointer v, gpointer ud) { GPtrArray *ary; ary = g_ptr_array_new(); g_ptr_array_add(ary, k); g_ptr_array_add(ary, v); g_ptr_array_add((GPtrArray *)ud, ary); } static void print_ary(GPtrArray *ary) { guint i; if (ary->len > 0) g_print("[\n"); for (i = 0; i < ary->len; i++) { GPtrArray *a = (GPtrArray *)g_ptr_array_index(ary, i); g_print("[\"%s\", %d],\n", (gchar *)g_ptr_array_index(a, 0), GPOINTER_TO_INT(g_ptr_array_index(a, 1))); } if (ary->len > 0) g_print("]\n"); } static void free_ary(GPtrArray *ary) { guint i; for (i = 0; i < ary->len; i++) { g_ptr_array_free(g_ptr_array_index(ary, i), FALSE); } g_ptr_array_free(ary, FALSE); } int main() { GHashTable *hash; GPtrArray *ary; hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash, "Perl", GINT_TO_POINTER(5)); g_hash_table_insert(hash, "Ruby", GINT_TO_POINTER(1)); g_hash_table_insert(hash, "Python", GINT_TO_POINTER(2)); ary = hash_to_a(hash); print_ary(ary); g_hash_table_destroy(hash); free_ary(ary); return 0; } で、 [ ["Ruby", 1], ["Python", 2], ["Perl", 5], ] !2006-09-16 Sat #include static void push_value(gpointer k, gpointer v, gpointer ud); static GPtrArray* hash_shift(GHashTable *hash) { GPtrArray *ary; ary = g_ptr_array_new(); g_hash_table_foreach(hash, push_value, ary); return ary; } static void push_value(gpointer k, gpointer v, gpointer ud) { if (((GPtrArray *)ud)->len == 0) { g_ptr_array_add((GPtrArray *)ud, k); g_ptr_array_add((GPtrArray *)ud, v); } } int main() { GHashTable *hash; GPtrArray *ary; hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash, "Perl", GINT_TO_POINTER(5)); g_hash_table_insert(hash, "Ruby", GINT_TO_POINTER(1)); g_hash_table_insert(hash, "Python", GINT_TO_POINTER(2)); ary = hash_shift(hash); g_print("%s\n", (gchar *)g_ptr_array_index(ary, 0)); g_print("%d\n", GPOINTER_TO_INT(g_ptr_array_index(ary, 1))); g_hash_table_destroy(hash); g_ptr_array_free(ary, FALSE); return 0; } で、 Ruby 1 !2006-09-15 Fri #include static void push_value(gpointer k, gpointer v, gpointer ud); static void hash_replace(GHashTable *hash1, GHashTable *hash2) { g_hash_table_destroy(hash1); hash1 = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_foreach(hash2, push_value, hash1); } static void push_value(gpointer k, gpointer v, gpointer ud) { g_hash_table_insert((GHashTable *)ud, k, v); } static void print_func(gpointer k, gpointer v, gpointer ud) { g_print("%-6s -> %d\n", (gchar*)k, GPOINTER_TO_INT(v)); } int main() { GHashTable *hash1, *hash2; hash1 = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash1, "Perl", GINT_TO_POINTER(5)); g_hash_table_insert(hash1, "Ruby", GINT_TO_POINTER(1)); g_hash_table_insert(hash1, "Python", GINT_TO_POINTER(2)); hash2 = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash2, "foo", GINT_TO_POINTER(0)); g_hash_table_insert(hash2, "bar", GINT_TO_POINTER(1)); g_hash_table_insert(hash2, "baz", GINT_TO_POINTER(2)); hash_replace(hash1, hash2); g_hash_table_foreach(hash1, print_func, NULL); g_print("*****\n"); g_hash_table_foreach(hash2, print_func, NULL); g_hash_table_destroy(hash1); g_hash_table_destroy(hash2); return 0; } で、 foo -> 0 baz -> 2 bar -> 1 ***** foo -> 0 baz -> 2 bar -> 1 !2006-09-14 Thu #include static void push_value(gpointer k, gpointer v, gpointer ud); static GHashTable* hash_merge(GHashTable *hash1, GHashTable *hash2) { GHashTable *new_hash; new_hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_foreach(hash1, push_value, new_hash); g_hash_table_foreach(hash2, push_value, new_hash); return new_hash; } static void push_value(gpointer k, gpointer v, gpointer ud) { g_hash_table_insert((GHashTable *)ud, k, v); } static void print_func(gpointer k, gpointer v, gpointer ud) { g_print("%-6s -> %d\n", (gchar*)k, GPOINTER_TO_INT(v)); } int main() { GHashTable *hash1, *hash2, *new_hash; hash1 = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash1, "Perl", GINT_TO_POINTER(5)); g_hash_table_insert(hash1, "Ruby", GINT_TO_POINTER(1)); g_hash_table_insert(hash1, "Python", GINT_TO_POINTER(2)); hash2 = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash2, "foo", GINT_TO_POINTER(0)); g_hash_table_insert(hash2, "bar", GINT_TO_POINTER(1)); g_hash_table_insert(hash2, "Perl", GINT_TO_POINTER(6)); new_hash = hash_merge(hash1, hash2); g_hash_table_foreach(hash1, print_func, NULL); g_print("*****\n"); g_hash_table_foreach(hash2, print_func, NULL); g_print("*****\n"); g_hash_table_foreach(new_hash, print_func, NULL); g_hash_table_destroy(hash1); g_hash_table_destroy(hash2); g_hash_table_destroy(new_hash); return 0; } で、 Ruby -> 1 Python -> 2 Perl -> 5 ***** foo -> 0 bar -> 1 Perl -> 6 ***** foo -> 0 Ruby -> 1 bar -> 1 Python -> 2 Perl -> 6 !2006-09-13 Wed #include static guint hash_size(GHashTable *hash) { return g_hash_table_size(hash); } int main() { GHashTable *hash; hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash, "Perl", GINT_TO_POINTER(5)); g_hash_table_insert(hash, "Ruby", GINT_TO_POINTER(1)); g_hash_table_insert(hash, "Python", GINT_TO_POINTER(2)); g_print("%d\n", hash_size(hash)); g_hash_table_destroy(hash); return 0; } で、 3 !2006-09-12 Tue #include static void push_value(gpointer k, gpointer v, gpointer ud); static GPtrArray* hash_keys(GHashTable *hash) { GPtrArray *ary; ary = g_ptr_array_new(); g_hash_table_foreach(hash, push_value, ary); return ary; } static void push_value(gpointer k, gpointer v, gpointer ud) { g_ptr_array_add((GPtrArray *)ud, k); } int main() { GHashTable *hash; GPtrArray *ary; guint i; hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash, "Perl", GINT_TO_POINTER(5)); g_hash_table_insert(hash, "Ruby", GINT_TO_POINTER(1)); g_hash_table_insert(hash, "Python", GINT_TO_POINTER(2)); ary = hash_keys(hash); for (i = 0; i < ary->len; i++) { g_print("%s\n", (gchar *)g_ptr_array_index(ary, i)); } g_hash_table_destroy(hash); g_ptr_array_free(ary, FALSE); return 0; } で、 Ruby Python Perl !2006-09-11 Mon #include static void push_value(gpointer k, gpointer v, gpointer ud); static GHashTable* hash_invert(GHashTable *hash) { GHashTable *new_hash; new_hash = g_hash_table_new(g_direct_hash, g_direct_equal); g_hash_table_foreach(hash, push_value, new_hash); return new_hash; } static void push_value(gpointer k, gpointer v, gpointer ud) { g_hash_table_insert((GHashTable *)ud, v, k); } static void print_func(gpointer k, gpointer v, gpointer ud) { g_print("%d -> %-6s\n", GPOINTER_TO_INT(k), (gchar*)v); } int main() { GHashTable *hash; GHashTable *new_hash; hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash, "Perl", GINT_TO_POINTER(5)); g_hash_table_insert(hash, "Ruby", GINT_TO_POINTER(1)); g_hash_table_insert(hash, "Python", GINT_TO_POINTER(2)); new_hash = hash_invert(hash); g_hash_table_foreach(new_hash, print_func, NULL); g_hash_table_destroy(hash); g_hash_table_destroy(new_hash); return 0; } で、 1 -> Ruby 2 -> Python 5 -> Perl !2006-09-10 Sun #include static void push_value(gpointer k, gpointer v, gpointer ud); static gchar* hash_key(GHashTable *hash, int val) { GHashTable *vh; gpointer ret; vh = g_hash_table_new(g_direct_hash, g_direct_equal); g_hash_table_foreach(hash, push_value, vh); ret = g_hash_table_lookup(vh, GINT_TO_POINTER(val)); g_hash_table_destroy(vh); return ret; } static void push_value(gpointer k, gpointer v, gpointer ud) { g_hash_table_insert((GHashTable *)ud, v, k); } int main() { GHashTable *hash; hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash, "Perl", GINT_TO_POINTER(5)); g_hash_table_insert(hash, "Ruby", GINT_TO_POINTER(1)); g_hash_table_insert(hash, "Python", GINT_TO_POINTER(2)); g_print("%s\n", hash_key(hash, 1)); g_print("%s\n", hash_key(hash, 3)); g_hash_table_destroy(hash); return 0; } で、 Ruby (null) !2006-09-09 Sat glib の hash ライブラリには、value を扱う関数がほとんどないようだ。 g_hash_table_foreach でグローバルな変数に値を貯めていけば なんでもできるだろうけど、、 #include static void push_value(gpointer k, gpointer v, gpointer ud); static gboolean hash_has_value(GHashTable *hash, int val) { GHashTable *vh; gpointer ret; vh = g_hash_table_new(g_direct_hash, g_direct_equal); g_hash_table_foreach(hash, push_value, vh); ret = g_hash_table_lookup(vh, GINT_TO_POINTER(val)); g_hash_table_destroy(vh); return ret != NULL; } static void push_value(gpointer k, gpointer v, gpointer ud) { g_hash_table_insert((GHashTable *)ud, v, GINT_TO_POINTER(TRUE)); } int main() { GHashTable *hash; hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash, "Perl", GINT_TO_POINTER(5)); g_hash_table_insert(hash, "Ruby", GINT_TO_POINTER(1)); g_hash_table_insert(hash, "Python", GINT_TO_POINTER(2)); g_print("%d\n", hash_has_value(hash, 1)); g_print("%d\n", hash_has_value(hash, 3)); g_hash_table_destroy(hash); return 0; } で、 1 0 !2006-09-08 Fri #include static gboolean hash_has_key(GHashTable *hash, gchar *key) { return g_hash_table_lookup(hash, key) != NULL; } int main() { GHashTable *hash; hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash, "Perl", GINT_TO_POINTER(5)); g_hash_table_insert(hash, "Ruby", GINT_TO_POINTER(1)); g_hash_table_insert(hash, "Python", GINT_TO_POINTER(2)); g_print("%d\n", hash_has_key(hash, "Ruby")); g_print("%d\n", hash_has_key(hash, "Java")); g_hash_table_destroy(hash); return 0; } で、 1 0 !2006-09-07 Thu #include static gboolean hash_empty(GHashTable *hash) { return g_hash_table_size(hash) == 0; } int main() { GHashTable *hash; hash = g_hash_table_new(g_str_hash, g_str_equal); g_print("%d\n", hash_empty(hash)); g_hash_table_insert(hash, "Perl", GINT_TO_POINTER(5)); g_hash_table_insert(hash, "Ruby", GINT_TO_POINTER(1)); g_hash_table_insert(hash, "Python", GINT_TO_POINTER(2)); g_print("%d\n", hash_empty(hash)); g_hash_table_destroy(hash); return 0; } で、 1 0 !2006-09-06 Wed #include static gboolean is_first_P(gpointer k, gpointer v, gpointer ud) { return ((gchar *)k)[0] == 'P' && GPOINTER_TO_INT(v) < 5; } static void hash_delete_if(GHashTable *hash, GHRFunc func) { g_hash_table_foreach_remove(hash, func, NULL); } static void print_func(gpointer k, gpointer v, gpointer ud) { g_print("%-6s -> %d\n", (gchar*)k, GPOINTER_TO_INT(v)); } int main() { GHashTable *hash; hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash, "Perl", GINT_TO_POINTER(5)); g_hash_table_insert(hash, "Ruby", GINT_TO_POINTER(1)); g_hash_table_insert(hash, "Python", GINT_TO_POINTER(2)); hash_delete_if(hash, is_first_P); g_hash_table_foreach(hash, print_func, NULL); g_hash_table_destroy(hash); return 0; } で、 Ruby -> 1 Perl -> 5 !2006-09-05 Tue #include static void hash_copy_item(gpointer k, gpointer v, gpointer ud); static gboolean is_first_P(gpointer k, gpointer v, gpointer ud) { return ((gchar *)k)[0] == 'P' && GPOINTER_TO_INT(v) < 5; } static GHashTable *hash_reject(GHashTable *hash, GHRFunc func) { GHashTable *new_hash; new_hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_foreach(hash, hash_copy_item, new_hash); g_hash_table_foreach_remove(new_hash, func, NULL); return new_hash; } static void hash_copy_item(gpointer k, gpointer v, gpointer ud) { g_hash_table_insert((GHashTable *)ud, k, v); } static void print_func(gpointer k, gpointer v, gpointer ud) { g_print("%-6s -> %d\n", (gchar*)k, GPOINTER_TO_INT(v)); } int main() { GHashTable *hash; GHashTable *hash2; hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash, "Perl", GINT_TO_POINTER(5)); g_hash_table_insert(hash, "Ruby", GINT_TO_POINTER(1)); g_hash_table_insert(hash, "Python", GINT_TO_POINTER(2)); hash2 = hash_reject(hash, is_first_P); g_hash_table_foreach(hash, print_func, NULL); g_print("*****\n"); g_hash_table_foreach(hash2, print_func, NULL); g_hash_table_destroy(hash); g_hash_table_destroy(hash2); return 0; } で、 Ruby -> 1 Python -> 2 Perl -> 5 ***** Ruby -> 1 Perl -> 5 !2006-09-04 Mon #include static gboolean is_first_P(gpointer k, gpointer v, gpointer ud) { return ((gchar *)k)[0] == 'P'; } static void hash_delete(GHashTable *hash, GHRFunc func) { g_hash_table_foreach_remove(hash, func, NULL); } static void print_func(gpointer k, gpointer v, gpointer ud) { g_print("%-6s -> %d\n", (gchar*)k, GPOINTER_TO_INT(v)); } int main() { GHashTable *hash; hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash, "Perl", GINT_TO_POINTER(5)); g_hash_table_insert(hash, "Ruby", GINT_TO_POINTER(1)); g_hash_table_insert(hash, "Python", GINT_TO_POINTER(2)); hash_delete(hash, is_first_P); g_hash_table_foreach(hash, print_func, NULL); g_hash_table_destroy(hash); return 0; } で、 Ruby -> 1 !2006-09-03 Sun #include static void hash_delete(GHashTable *hash, gchar *key) { g_hash_table_remove(hash, key); } static void print_func(gpointer k, gpointer v, gpointer ud) { g_print("%-6s -> %d\n", (gchar*)k, GPOINTER_TO_INT(v)); } int main() { GHashTable *hash; hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash, "Perl", GINT_TO_POINTER(5)); g_hash_table_insert(hash, "Ruby", GINT_TO_POINTER(1)); g_hash_table_insert(hash, "Python", GINT_TO_POINTER(2)); hash_delete(hash, "Python"); hash_delete(hash, "Java"); g_hash_table_foreach(hash, print_func, NULL); g_hash_table_destroy(hash); return 0; } で、 Ruby -> 1 Perl -> 5 !2006-09-02 Sat #include static void hash_copy_item(gpointer k, gpointer v, gpointer ud); static GHashTable *hash_clone(GHashTable *hash) { GHashTable *new_hash; new_hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_foreach(hash, hash_copy_item, new_hash); return new_hash; } static void hash_copy_item(gpointer k, gpointer v, gpointer ud) { g_hash_table_insert((GHashTable *)ud, k, v); } static void print_func(gpointer k, gpointer v, gpointer ud) { g_print("%-6s -> %d\n", (gchar*)k, GPOINTER_TO_INT(v)); } int main() { GHashTable *hash1; GHashTable *hash2; hash1 = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash1, "Perl", GINT_TO_POINTER(5)); g_hash_table_insert(hash1, "Ruby", GINT_TO_POINTER(1)); g_hash_table_insert(hash1, "Python", GINT_TO_POINTER(2)); hash2 = hash_clone(hash1); g_hash_table_insert(hash1, "Java", GINT_TO_POINTER(1)); g_hash_table_foreach(hash1, print_func, NULL); g_print("*****\n"); g_hash_table_foreach(hash2, print_func, NULL); g_hash_table_destroy(hash1); g_hash_table_destroy(hash2); return 0; } で、 Ruby -> 1 Python -> 2 Perl -> 5 Java -> 1 ***** Ruby -> 1 Python -> 2 Perl -> 5 !2006-09-01 Fri #include static gboolean hash_del_item(gpointer k, gpointer v, gpointer ud) { return TRUE; } static void hash_clear(GHashTable *hash) { g_hash_table_foreach_remove(hash, hash_del_item, NULL); } static void print_func(gpointer k, gpointer v, gpointer ud) { g_print("%-6s -> %d\n", (gchar*)k, GPOINTER_TO_INT(v)); } int main() { GHashTable *hash; hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash, "Perl", GINT_TO_POINTER(5)); g_hash_table_insert(hash, "Ruby", GINT_TO_POINTER(1)); g_hash_table_insert(hash, "Python", GINT_TO_POINTER(2)); g_hash_table_foreach(hash, print_func, NULL); g_print("*****\n"); hash_clear(hash); g_hash_table_foreach(hash, print_func, NULL); g_hash_table_destroy(hash); return 0; } で、 Ruby -> 1 Python -> 2 Perl -> 5 ***** g_hash_table_foreach でたどっているときに中身を変更しちゃダメらしい !2006-08-31 Thu #include int main() { GHashTable *hash; gpointer ret; hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash, "Perl", GINT_TO_POINTER(5)); g_hash_table_insert(hash, "Ruby", GINT_TO_POINTER(1)); g_hash_table_insert(hash, "Python", GINT_TO_POINTER(2)); g_hash_table_insert(hash, "Ruby", GINT_TO_POINTER(2)); g_hash_table_insert(hash, "Java", GINT_TO_POINTER(1)); ret = g_hash_table_lookup(hash, "Ruby"); if (ret) g_print("%d\n", GPOINTER_TO_INT(ret)); ret = g_hash_table_lookup(hash, "Java"); if (ret) g_print("%d\n", GPOINTER_TO_INT(ret)); g_hash_table_destroy(hash); return 0; } で、 2 1 g_hash_table_replace を使わなくても良いのか? !2006-08-30 Wed #include int main() { GHashTable *hash; gpointer ret; hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash, "Perl", GINT_TO_POINTER(5)); g_hash_table_insert(hash, "Ruby", GINT_TO_POINTER(1)); g_hash_table_insert(hash, "Python", GINT_TO_POINTER(2)); ret = g_hash_table_lookup(hash, "Ruby"); if (ret) g_print("%d\n", GPOINTER_TO_INT(ret)); ret = g_hash_table_lookup(hash, "Java"); if (ret) g_print("%d\n", GPOINTER_TO_INT(ret)); g_hash_table_destroy(hash); return 0; } で、 1 !2006-08-29 Tue #include int main() { GNode *root, *node1, *node2, *node3; root = g_node_new("root"); node1 = g_node_new("node1"); node2 = g_node_new("node2"); node3 = g_node_new("node1-1"); g_node_append(root, node1); g_node_append(root, node2); g_node_append(node1, node3); g_node_unlink(node3); g_print("%d\n", g_node_max_height(root)); g_print("%d\n", g_node_max_height(node1)); g_print("%d\n", g_node_max_height(node2)); g_print("%d\n", g_node_max_height(node3)); g_node_destroy(root); g_node_destroy(node3); return 0; } で、 2 1 1 1 !2006-08-28 Mon #include int main() { GNode *root, *node1, *node2, *node3; root = g_node_new("root"); node1 = g_node_new("node1"); node2 = g_node_new("node2"); node3 = g_node_new("node1-1"); g_node_append(root, node1); g_node_append(root, node2); g_node_append(node1, node3); g_print("%d\n", g_node_max_height(root)); g_print("%d\n", g_node_max_height(node1)); g_print("%d\n", g_node_max_height(node2)); g_print("%d\n", g_node_max_height(node3)); g_node_destroy(root); return 0; } で、 3 2 1 1 !2006-08-27 Sun #include int main() { GNode *root, *node1, *node2, *node3; root = g_node_new("root"); node1 = g_node_new("node1"); node2 = g_node_new("node2"); node3 = g_node_new("node1-1"); g_node_append(root, node1); g_node_append(root, node2); g_node_append(node1, node3); g_print("%d\n", g_node_is_ancestor(root, node1)); g_print("%d\n", g_node_is_ancestor(root, node2)); g_print("%d\n", g_node_is_ancestor(root, node3)); g_print("%d\n", g_node_is_ancestor(node1, node3)); g_print("%d\n", g_node_is_ancestor(node1, root)); g_node_destroy(root); return 0; } で、 1 1 1 1 0 !2006-08-26 Sat #include int main() { GNode *root, *node1, *node2, *node3; root = g_node_new("root"); node1 = g_node_new("node1"); node2 = g_node_new("node2"); node3 = g_node_new("node1-1"); g_node_append(root, node1); g_node_append(root, node2); g_node_append(node1, node3); g_print("%d\n", g_node_n_children(root)); g_print("%d\n", g_node_n_children(node1)); g_print("%d\n", g_node_n_children(node2)); g_print("%d\n", g_node_n_children(node3)); g_node_destroy(root); return 0; } で、 2 1 0 0 !2006-08-25 Fri #include int main() { GNode *root, *node1, *node2, *node3; root = g_node_new("root"); node1 = g_node_new("node1"); node2 = g_node_new("node2"); node3 = g_node_new("node1-1"); g_node_append(root, node1); g_node_append(root, node2); g_node_append(node1, node3); g_print("%d\n", g_node_n_nodes(root, G_TRAVERSE_ALL)); g_print("%d\n", g_node_n_nodes(node1, G_TRAVERSE_ALL)); g_print("%d\n", g_node_n_nodes(node2, G_TRAVERSE_ALL)); g_print("%d\n", g_node_n_nodes(node3, G_TRAVERSE_ALL)); g_node_destroy(root); return 0; } で、 4 2 1 1 !2006-08-24 Thu #include int main() { GNode *root, *node1, *node2, *node3; root = g_node_new("root"); node1 = g_node_new("node1"); node2 = g_node_new("node2"); node3 = g_node_new("node1-1"); g_node_append(root, node1); g_node_append(root, node2); g_node_append(node1, node3); g_print("%d\n", G_NODE_IS_ROOT(root)); g_print("%d\n", G_NODE_IS_ROOT(node1)); g_print("%d\n", G_NODE_IS_ROOT(node2)); g_print("%d\n", G_NODE_IS_ROOT(node3)); g_node_destroy(root); return 0; } で、 1 0 0 0 !2006-08-23 Wed #include int main() { GNode *root, *node1, *node2, *node3; root = g_node_new("root"); node1 = g_node_new("node1"); node2 = g_node_new("node2"); node3 = g_node_new("node1-1"); g_node_append(root, node1); g_node_append(root, node2); g_node_append(node1, node3); g_print("%d\n", G_NODE_IS_LEAF(root)); g_print("%d\n", G_NODE_IS_LEAF(node1)); g_print("%d\n", G_NODE_IS_LEAF(node2)); g_print("%d\n", G_NODE_IS_LEAF(node3)); g_node_destroy(root); return 0; } で、 0 0 1 1 !2006-08-22 Tue #include int main() { GNode *root, *node1, *node2, *node3, *node; root = g_node_new("root"); node1 = g_node_new("node1"); node2 = g_node_new("node2"); node3 = g_node_new("node3"); g_node_append(root, node1); g_node_append(root, node2); g_node_append(root, node3); node = g_node_last_sibling(root); g_print("%s\n", (gchar *)node->data); node = g_node_last_sibling(node1); g_print("%s\n", (gchar *)node->data); node = g_node_last_sibling(node3); g_print("%s\n", (gchar *)node->data); g_node_destroy(root); return 0; } で、 root node3 node3 !2006-08-21 Mon #include int main() { GNode *root, *node1, *node2, *node3, *node; root = g_node_new("root"); node1 = g_node_new("node1"); node2 = g_node_new("node2"); node3 = g_node_new("node3"); g_node_append(root, node1); g_node_append(root, node2); g_node_append(root, node3); node = g_node_prev_sibling(root); if (node) g_print("1 %s\n", (gchar *)node->data); node = g_node_prev_sibling(node1); if (node) g_print("2 %s\n", (gchar *)node->data); node = g_node_prev_sibling(node2); if (node) g_print("3 %s\n", (gchar *)node->data); node = g_node_prev_sibling(node3); if (node) g_print("4 %s\n", (gchar *)node->data); g_node_destroy(root); return 0; } で、 3 node1 4 node2 !2006-08-20 Sun #include int main() { GNode *root, *node1, *node2, *node3, *node; root = g_node_new("root"); node1 = g_node_new("node1"); node2 = g_node_new("node2"); node3 = g_node_new("node3"); g_node_append(root, node1); g_node_append(root, node2); g_node_append(root, node3); node = g_node_next_sibling(root); if (node) g_print("1 %s\n", (gchar *)node->data); node = g_node_next_sibling(node1); if (node) g_print("2 %s\n", (gchar *)node->data); node = g_node_next_sibling(node2); if (node) g_print("3 %s\n", (gchar *)node->data); node = g_node_next_sibling(node3); if (node) g_print("4 %s\n", (gchar *)node->data); g_node_destroy(root); return 0; } で、 2 node2 3 node3 !2006-08-19 Sat #include int main() { GNode *root, *node1, *node2, *node3, *node; root = g_node_new("root"); node1 = g_node_new("node1"); node2 = g_node_new("node2"); node3 = g_node_new("node3"); g_node_append(root, node1); g_node_append(root, node2); g_node_append(root, node3); node = g_node_first_sibling(root); g_print("%s\n", (gchar *)node->data); node = g_node_first_sibling(node1); g_print("%s\n", (gchar *)node->data); node = g_node_first_sibling(node3); g_print("%s\n", (gchar *)node->data); g_node_destroy(root); return 0; } で、 root node1 node1 !2006-08-18 Fri #include int main() { GNode *root, *node; root = g_node_new("root"); g_node_append_data(root, "node1"); g_node_append_data(root, "node2"); g_node_append_data(root, "node3"); node = g_node_nth_child(root, 0); g_print("%s\n", (gchar *)node->data); node = g_node_nth_child(root, 1); g_print("%s\n", (gchar *)node->data); g_node_destroy(root); return 0; } で、 node1 node2 !2006-08-17 Thu #include int main() { GNode *root, *node; root = g_node_new("root"); g_node_append_data(root, "node1"); g_node_append_data(root, "node2"); g_node_append_data(root, "node3"); node = g_node_last_child(root); g_print("%s\n", (gchar *)node->data); g_node_destroy(root); return 0; } で、 node3 !2006-08-16 Wed #include int main() { GNode *root, *node; root = g_node_new("root"); g_node_append_data(root, "node1"); g_node_append_data(root, "node2"); g_node_append_data(root, "node3"); node = g_node_first_child(root); g_print("%s\n", (gchar *)node->data); g_node_destroy(root); return 0; } で、 node1 !2006-08-15 Tue #include int main() { GNode *root, *node1, *node2, *node3; root = g_node_new("root"); node1 = g_node_new("node1"); node2 = g_node_new("node2"); node3 = g_node_new("node3"); g_node_append(root, node1); g_node_append(root, node2); g_node_append(node1, node3); g_print("%d\n", g_node_child_position(root, node1)); g_print("%d\n", g_node_child_position(root, node2)); g_print("%d\n", g_node_child_position(root, node3)); g_node_destroy(root); return 0; } で、 0 1 (process:24958): GLib-CRITICAL **: g_node_child_position: assertion `child->parent == node' failed -1 !2006-08-14 Mon #include int main() { GNode *root, *node1; root = g_node_new("root"); node1 = g_node_new("node1"); g_node_append(root, node1); g_node_append_data(root, "node2"); g_node_append_data(node1, "node1-1"); g_node_append_data(node1, "node1-2"); g_print("%d\n", g_node_child_index(root, "node1")); g_print("%d\n", g_node_child_index(root, "node2")); g_print("%d\n", g_node_child_index(root, "node1-2")); g_node_destroy(root); return 0; } で、 0 1 -1 !2006-08-13 Sun #include int main() { GNode *root, *node1, *node; root = g_node_new("root"); node1 = g_node_new("node1"); g_node_append(root, node1); g_node_append_data(root, "node2"); g_node_append_data(node1, "node1-1"); g_node_append_data(node1, "node1-2"); node = g_node_find_child(root, G_TRAVERSE_ALL, "node1-2"); if (node) { g_print("* %s\n", (gchar *)node->data); } node = g_node_find_child(root, G_TRAVERSE_ALL, "node1"); if (node) { g_print("** %s\n", (gchar *)node->data); } g_node_destroy(root); return 0; } で、 ** node1 !2006-08-12 Sat #include int main() { GNode *root, *node1, *node2, *node; root = g_node_new("node"); node1 = g_node_new("node"); node2 = g_node_new("node"); g_print("%p %p %p\n", root, node1, node2); g_node_append(root, node1); g_node_append(root, node2); node = g_node_find(root, G_IN_ORDER, G_TRAVERSE_ALL, "node"); g_print("%p\n", node); node = g_node_find(root, G_PRE_ORDER, G_TRAVERSE_ALL, "node"); g_print("%p\n", node); node = g_node_find(root, G_POST_ORDER, G_TRAVERSE_ALL, "node"); g_print("%p\n", node); node = g_node_find(root, G_LEVEL_ORDER, G_TRAVERSE_ALL, "node"); g_print("%p\n", node); g_node_destroy(root); return 0; } で、 0x8049918 0x804992c 0x8049940 0x804992c 0x8049918 0x804992c 0x8049918 !2006-08-11 Fri #include int main() { GNode *root, *node1, *node; root = g_node_new("root"); node1 = g_node_new("node1"); g_node_append(root, node1); g_node_append_data(root, "node2"); g_node_append_data(node1, "node1-1"); g_node_append_data(node1, "node1-2"); node = g_node_find(root, G_IN_ORDER, G_TRAVERSE_ALL, "node1-2"); g_print("%s\n", (gchar *)node->data); g_node_destroy(root); return 0; } で、 node1-2 !2006-08-10 Thu #include int main() { GNode *root, *node1, *node1_1; root = g_node_new("root"); node1 = g_node_new("node1"); g_node_append(root, node1); node1_1 = g_node_new("node1-1"); g_node_append(node1, node1_1); g_print("%p\n", root); g_print("%p\n", g_node_get_root(node1)); g_print("%p\n", g_node_get_root(node1_1)); g_node_destroy(root); return 0; } で、 0x8049880 0x8049880 0x8049880 !2006-08-09 Wed #include gboolean func(GNode *node, gpointer data) { g_print("%d %s\n", g_node_depth(node), (gchar *)node->data); return FALSE; } int main() { GNode *root, *node1; root = g_node_new("root"); node1 = g_node_new("node1"); g_node_append(root, node1); g_node_append_data(root, "node2"); g_node_append_data(node1, "node1-1"); g_node_append_data(node1, "node1-2"); g_node_children_foreach(root, G_TRAVERSE_ALL, (GNodeForeachFunc)func, "hoge"); g_node_destroy(root); return 0; } で、 2 node1 2 node2 !2006-08-08 Tue #include gboolean func(GNode *node, gpointer data) { g_print("%d %s\n", g_node_depth(node), (gchar *)node->data); return FALSE; } int main() { GNode *root; root = g_node_new("root"); g_node_append_data(root, "node1"); g_node_append_data(root, "node2"); g_node_reverse_children(root); g_node_traverse(root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, (GNodeTraverseFunc)func, "hoge"); g_node_destroy(root); return 0; } で、 1 root 2 node2 2 node1 !2006-08-07 Mon #include gboolean func(GNode *node, gpointer data) { g_print("%d %s\n", g_node_depth(node), (gchar *)node->data); return FALSE; } int main() { GNode *root, *node1; root = g_node_new("root"); node1 = g_node_new("node1"); g_node_append(root, node1); g_node_prepend_data(root, "node2"); g_node_traverse(root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, (GNodeTraverseFunc)func, "hoge"); g_node_destroy(root); return 0; } で、 1 root 2 node2 2 node1 !2006-08-06 Sun #include gboolean func(GNode *node, gpointer data) { g_print("%d %s\n", g_node_depth(node), (gchar *)node->data); return FALSE; } int main() { GNode *root, *node1; root = g_node_new("root"); node1 = g_node_new("node1"); g_node_append(root, node1); g_node_append_data(root, "node2"); g_node_traverse(root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, (GNodeTraverseFunc)func, "hoge"); g_node_destroy(root); return 0; } で、 1 root 2 node1 2 node2 !2006-08-05 Sat #include gboolean func(GNode *node, gpointer data) { g_print("%d %s\n", g_node_depth(node), (gchar *)node->data); return FALSE; } int main() { GNode *root, *node1; root = g_node_new("root"); node1 = g_node_new("node1"); g_node_append(root, node1); g_node_insert_data_before(root, node1, "node2"); g_node_traverse(root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, (GNodeTraverseFunc)func, "hoge"); g_node_destroy(root); return 0; } で、 1 root 2 node2 2 node1 !2006-08-04 Fri #include gboolean func(GNode *node, gpointer data) { g_print("%d %s\n", g_node_depth(node), (gchar *)node->data); return FALSE; } int main() { GNode *root; root = g_node_new("root"); g_node_insert_data(root, -1, "node1"); g_node_insert_data(root, 0, "node2"); g_node_traverse(root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, (GNodeTraverseFunc)func, "hoge"); g_node_destroy(root); return 0; } で、 1 root 2 node2 2 node1 !2006-08-03 Thu #include gboolean func(GNode *node, gpointer data) { g_print("%d %s\n", g_node_depth(node), (gchar *)node->data); return FALSE; } int main() { GNode *root, *node1, *node2; root = g_node_new("root"); node1 = g_node_new("node1"); node2 = g_node_new("node2"); g_node_prepend(root, node1); g_node_prepend(root, node2); g_node_traverse(root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, (GNodeTraverseFunc)func, "hoge"); g_node_destroy(root); return 0; } で、 1 root 2 node2 2 node1 !2006-08-02 Wed #include gboolean func(GNode *node, gpointer data) { g_print("%d %s\n", g_node_depth(node), (gchar *)node->data); return FALSE; } int main() { GNode *root, *node1, *node2; root = g_node_new("root"); node1 = g_node_new("node1"); node2 = g_node_new("node2"); g_node_append(root, node1); g_node_append(root, node2); g_node_traverse(root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, (GNodeTraverseFunc)func, "hoge"); g_node_destroy(root); return 0; } で、 1 root 2 node1 2 node2 !2006-08-01 Tue #include gboolean func(GNode *node, gpointer data) { g_print("%d %s\n", g_node_depth(node), (gchar *)node->data); return FALSE; } int main() { GNode *root, *node1, *node2; root = g_node_new("root"); node1 = g_node_new("node1"); node2 = g_node_new("node2"); g_node_insert(root, -1, node1); g_node_insert_after(root, node1, node2); g_node_traverse(root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, (GNodeTraverseFunc)func, "hoge"); g_node_destroy(root); return 0; } で、 1 root 2 node1 2 node2 !2006-07-31 Mon #include gboolean func(GNode *node, gpointer data) { g_print("%d %s\n", g_node_depth(node), (gchar *)node->data); return FALSE; } int main() { GNode *root, *node1, *node2; root = g_node_new("root"); node1 = g_node_new("node1"); node2 = g_node_new("node2"); g_node_insert(root, -1, node1); g_node_insert_before(root, node1, node2); g_node_traverse(root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, (GNodeTraverseFunc)func, "hoge"); g_node_destroy(root); return 0; } で、 1 root 2 node2 2 node1 !2006-07-30 Sun #include gboolean func(GNode *node, gpointer data) { g_print("%d %s\n", g_node_depth(node), (gchar *)node->data); return FALSE; } int main() { GNode *root, *node1, *node2; root = g_node_new("root"); node1 = g_node_new("node1"); node2 = g_node_new("node2"); g_node_insert(root, -1, node1); /*g_node_insert(root, -1, node2);*/ g_node_insert(root, 0, node2); g_node_traverse(root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, (GNodeTraverseFunc)func, "hoge"); g_node_destroy(root); return 0; } で、 1 root 2 node2 2 node1 !2006-07-29 Sat #include gboolean func(GNode *node, gpointer data) { g_print("%d %s\n", g_node_depth(node), (gchar *)node->data); return FALSE; } int main() { GNode *root, *node1; root = g_node_new("root"); node1 = g_node_new("node1"); g_node_insert(root, -1, node1); g_node_traverse(root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, (GNodeTraverseFunc)func, "hoge"); g_node_destroy(root); return 0; } で、 1 root 2 node1 !2006-07-28 Fri #include int main() { GNode *node, *node2; node = g_node_new("root"); node2 = g_node_copy(node); g_node_destroy(node); return 0; } !2006-07-27 Thu #include int main() { GNode *node; node = g_node_new("root"); g_node_destroy(node); return 0; } !2006-07-26 Wed #include static int i_cmp(gpointer p, gpointer q) { if (GPOINTER_TO_INT(p) > GPOINTER_TO_INT(q)) { return 1; } else if (GPOINTER_TO_INT(p) < GPOINTER_TO_INT(q)) { return -1; } else { return 0; } } int main() { GTree *tree; tree = g_tree_new((GCompareFunc)i_cmp); g_tree_insert(tree, GINT_TO_POINTER(1), "10"); g_tree_insert(tree, GINT_TO_POINTER(2), "20"); g_tree_insert(tree, GINT_TO_POINTER(3), "30"); g_print("%s\n", (gchar *)g_tree_lookup(tree, GINT_TO_POINTER(1))); g_tree_steal(tree, GINT_TO_POINTER(1)); g_print("%s\n", (gchar *)g_tree_lookup(tree, GINT_TO_POINTER(1))); g_tree_destroy(tree); return 0; } で、 10 (null) !2006-07-25 Tue #include static int i_cmp(gpointer p, gpointer q) { if (GPOINTER_TO_INT(p) > GPOINTER_TO_INT(q)) { return 1; } else if (GPOINTER_TO_INT(p) < GPOINTER_TO_INT(q)) { return -1; } else { return 0; } } int main() { GTree *tree; tree = g_tree_new((GCompareFunc)i_cmp); g_tree_insert(tree, GINT_TO_POINTER(1), "10"); g_tree_insert(tree, GINT_TO_POINTER(2), "20"); g_tree_insert(tree, GINT_TO_POINTER(3), "30"); g_print("%s\n", (gchar *)g_tree_lookup(tree, GINT_TO_POINTER(1))); g_tree_remove(tree, GINT_TO_POINTER(1)); g_print("%s\n", (gchar *)g_tree_lookup(tree, GINT_TO_POINTER(1))); g_tree_destroy(tree); return 0; } で、 10 (null) !2006-07-24 Mon #include static gint func(gpointer a, gpointer b) { g_print("%d %d\n", (gint)a, (gint)b); if ((gint)a == (gint)b) { return 0; } else if ((gint)a < (gint)b) { return 1; } else { return -1; } } static int i_cmp(gpointer p, gpointer q) { if (GPOINTER_TO_INT(p) > GPOINTER_TO_INT(q)) { return 1; } else if (GPOINTER_TO_INT(p) < GPOINTER_TO_INT(q)) { return -1; } else { return 0; } } int main() { GTree *tree; gpointer p; tree = g_tree_new((GCompareFunc)i_cmp); g_tree_insert(tree, GINT_TO_POINTER(1), "10"); g_tree_insert(tree, GINT_TO_POINTER(2), "20"); g_tree_insert(tree, GINT_TO_POINTER(3), "30"); p = g_tree_search(tree, (GCompareFunc)func, GINT_TO_POINTER(1)); if (p) { g_print("%s\n", (gchar *)p); } g_tree_destroy(tree); return 0; } で、 2 1 1 1 10 !2006-07-23 Sun #include static gboolean func(gpointer key, gpointer value, gpointer data) { g_print("%d %s %s\n", (gint)key, (char *)value, (char *)data); return FALSE; } static int i_cmp(gpointer p, gpointer q) { if (GPOINTER_TO_INT(p) > GPOINTER_TO_INT(q)) { return 1; } else if (GPOINTER_TO_INT(p) < GPOINTER_TO_INT(q)) { return -1; } else { return 0; } } int main() { GTree *tree; tree = g_tree_new((GCompareFunc)i_cmp); g_tree_insert(tree, GINT_TO_POINTER(1), "10"); g_tree_insert(tree, GINT_TO_POINTER(2), "20"); g_tree_insert(tree, GINT_TO_POINTER(3), "30"); g_tree_foreach(tree, (GTraverseFunc)func, "foo"); g_tree_destroy(tree); return 0; } で、 1 10 foo 2 20 foo 3 30 foo !2006-07-22 Sat #include static int i_cmp(gpointer p, gpointer q) { if (GPOINTER_TO_INT(p) > GPOINTER_TO_INT(q)) { return 1; } else if (GPOINTER_TO_INT(p) < GPOINTER_TO_INT(q)) { return -1; } else { return 0; } } int main() { GTree *tree; tree = g_tree_new((GCompareFunc)i_cmp); g_print("%d\n", g_tree_lookup_extended(tree, GINT_TO_POINTER(1), NULL, NULL)); g_tree_insert(tree, GINT_TO_POINTER(1), "1"); g_tree_insert(tree, GINT_TO_POINTER(2), "2"); g_tree_insert(tree, GINT_TO_POINTER(3), "3"); g_print("%d\n", g_tree_lookup_extended(tree, GINT_TO_POINTER(1), NULL, NULL)); g_tree_destroy(tree); return 0; } で、 0 1 #include static int i_cmp(gpointer p, gpointer q) { if (GPOINTER_TO_INT(p) > GPOINTER_TO_INT(q)) { return 1; } else if (GPOINTER_TO_INT(p) < GPOINTER_TO_INT(q)) { return -1; } else { return 0; } } int main() { GTree *tree; gpointer orig_key, value; tree = g_tree_new((GCompareFunc)i_cmp); g_print("%d\n", g_tree_lookup_extended(tree, GINT_TO_POINTER(1), &orig_key, &value)); g_tree_insert(tree, GINT_TO_POINTER(1), "1"); g_tree_insert(tree, GINT_TO_POINTER(2), "2"); g_tree_insert(tree, GINT_TO_POINTER(3), "3"); g_print("%d\n", g_tree_lookup_extended(tree, GINT_TO_POINTER(1), &orig_key, &value)); g_tree_destroy(tree); return 0; } で、 0 1 !2006-07-21 Fri #include static int i_cmp(gpointer p, gpointer q) { if (GPOINTER_TO_INT(p) > GPOINTER_TO_INT(q)) { return 1; } else if (GPOINTER_TO_INT(p) < GPOINTER_TO_INT(q)) { return -1; } else { return 0; } } int main() { GTree *tree; tree = g_tree_new((GCompareFunc)i_cmp); g_print("%d\n", g_tree_nnodes(tree)); g_tree_insert(tree, GINT_TO_POINTER(1), "1"); g_print("%d\n", g_tree_nnodes(tree)); g_tree_insert(tree, GINT_TO_POINTER(2), "2"); g_print("%d\n", g_tree_nnodes(tree)); g_tree_insert(tree, GINT_TO_POINTER(3), "3"); g_print("%d\n", g_tree_nnodes(tree)); g_tree_destroy(tree); return 0; } で、 0 1 2 3 !2006-07-20 Thu #include static int i_cmp(gpointer p, gpointer q) { if (GPOINTER_TO_INT(p) > GPOINTER_TO_INT(q)) { return 1; } else if (GPOINTER_TO_INT(p) < GPOINTER_TO_INT(q)) { return -1; } else { return 0; } } int main() { GTree *tree; tree = g_tree_new((GCompareFunc)i_cmp); g_tree_insert(tree, GINT_TO_POINTER(1), "1"); g_tree_insert(tree, GINT_TO_POINTER(2), "2"); g_tree_insert(tree, GINT_TO_POINTER(3), "3"); g_tree_replace(tree, GINT_TO_POINTER(4), "4"); g_tree_replace(tree, GINT_TO_POINTER(1), "5"); g_print("%s\n", (gchar *)g_tree_lookup(tree, GINT_TO_POINTER(1))); g_print("%s\n", (gchar *)g_tree_lookup(tree, GINT_TO_POINTER(4))); g_tree_destroy(tree); return 0; } で、 5 4 insert との違いは、キー・値の解放のしかた??? !2006-07-19 Wed #include static int i_cmp(gpointer p, gpointer q) { if (GPOINTER_TO_INT(p) > GPOINTER_TO_INT(q)) { return 1; } else if (GPOINTER_TO_INT(p) < GPOINTER_TO_INT(q)) { return -1; } else { return 0; } } int main() { GTree *tree; tree = g_tree_new((GCompareFunc)i_cmp); g_tree_insert(tree, GINT_TO_POINTER(1), "1"); g_tree_insert(tree, GINT_TO_POINTER(2), "2"); g_tree_insert(tree, GINT_TO_POINTER(3), "3"); g_tree_insert(tree, GINT_TO_POINTER(1), "4"); g_print("%s\n", (gchar *)g_tree_lookup(tree, GINT_TO_POINTER(1))); g_tree_destroy(tree); return 0; } で、 4 !2006-07-18 Tue #include static int i_cmp(gpointer p, gpointer q, gpointer ud) { /*g_print("%d %d %s\n", (gint)p, (gint)q, (char *)ud);*/ if (p > q) { return 1; } else if (p < q) { return -1; } else { return 0; } } static void key_destroy_func(gpointer p) { g_print("key: %d\n", (gint)p); } static void value_destroy_func(gpointer p) { g_print("value: %s\n", (gchar *)p); } int main() { GTree *tree; tree = g_tree_new_full((GCompareDataFunc)i_cmp, "foo", (GDestroyNotify)key_destroy_func, (GDestroyNotify)value_destroy_func); g_tree_insert(tree, GINT_TO_POINTER(1), "10"); g_tree_insert(tree, GINT_TO_POINTER(2), "20"); g_tree_insert(tree, GINT_TO_POINTER(3), "30"); g_tree_destroy(tree); return 0; } で、 key: 3 value: 30 key: 1 value: 10 key: 2 value: 20 !2006-07-17 Mon #include static int i_cmp(gpointer p, gpointer q, gpointer ud) { g_print("%d %d %s\n", (gint)p, (gint)q, (char *)ud); if (p > q) { return 1; } else if (p < q) { return -1; } else { return 0; } } int main() { GTree *tree; tree = g_tree_new_full((GCompareDataFunc)i_cmp, "foo", NULL, NULL); g_tree_insert(tree, GINT_TO_POINTER(1), "1"); g_tree_insert(tree, GINT_TO_POINTER(2), "2"); g_tree_insert(tree, GINT_TO_POINTER(3), "3"); g_tree_destroy(tree); return 0; } で、 2 1 foo 3 1 foo 3 2 foo !2006-07-16 Sun #include static int i_cmp(gpointer p, gpointer q, gpointer ud) { g_print("%d %d %s\n", (gint)p, (gint)q, (char *)ud); if (p > q) { return 1; } else if (p < q) { return -1; } else { return 0; } } int main() { GTree *tree; tree = g_tree_new_with_data((GCompareDataFunc)i_cmp, "foo"); g_tree_insert(tree, GINT_TO_POINTER(1), "1"); g_tree_insert(tree, GINT_TO_POINTER(2), "2"); g_tree_insert(tree, GINT_TO_POINTER(3), "3"); g_tree_destroy(tree); return 0; } で、 2 1 foo 3 1 foo 3 2 foo !2006-07-15 Sat #include static int i_cmp(gpointer p, gpointer q) { if (GPOINTER_TO_INT(p) > GPOINTER_TO_INT(q)) { return 1; } else if (GPOINTER_TO_INT(p) < GPOINTER_TO_INT(q)) { return -1; } else { return 0; } } int main() { GTree *tree; tree = g_tree_new((GCompareFunc)i_cmp); g_print("%d\n", g_tree_height(tree)); g_tree_insert(tree, GINT_TO_POINTER(1), "1"); g_print("%d\n", g_tree_height(tree)); g_tree_insert(tree, GINT_TO_POINTER(2), "2"); g_print("%d\n", g_tree_height(tree)); g_tree_insert(tree, GINT_TO_POINTER(3), "3"); g_print("%d\n", g_tree_height(tree)); g_tree_insert(tree, GINT_TO_POINTER(4), "4"); g_print("%d\n", g_tree_height(tree)); g_tree_destroy(tree); return 0; } で、 0 1 2 2 3 free と destroy の用語の使い分けって、どうなっているのだろうか? !2006-07-14 Fri #include int main() { GStringChunk *string_chunk; gchar *p1, *p2, *p3; string_chunk = g_string_chunk_new(1024); p1 = g_string_chunk_insert_const(string_chunk, "foo"); p2 = g_string_chunk_insert_const(string_chunk, "foo"); p3 = g_string_chunk_insert_const(string_chunk, "foo"); g_print("%p\n", p1); g_print("%p\n", p2); g_print("%p\n", p3); g_string_chunk_free(string_chunk); return 0; } で、 0x8049810 0x8049810 0x8049810 !2006-07-13 Thu #include int main() { GStringChunk *string_chunk; gchar *p1, *p2, *p3; string_chunk = g_string_chunk_new(1024); p1 = g_string_chunk_insert(string_chunk, "foo"); p2 = g_string_chunk_insert(string_chunk, "bar"); p3 = g_string_chunk_insert(string_chunk, "baz"); strcpy(p1, "FOO"); g_print("%s\n", p1); g_print("%s\n", p2); g_print("%s\n", p3); g_string_chunk_free(string_chunk); return 0; } で、 FOO bar baz !2006-07-12 Wed #include int main() { GStringChunk *string_chunk; gchar *p1, *p2, *p3; string_chunk = g_string_chunk_new(1024); p1 = g_string_chunk_insert(string_chunk, "foo"); p2 = g_string_chunk_insert(string_chunk, "bar"); p3 = g_string_chunk_insert(string_chunk, "baz"); g_print("%p\n", p1); g_print("%p\n", p2); g_print("%p\n", p3); g_string_chunk_free(string_chunk); return 0; } で、 0x80497c8 0x80497cc 0x80497d0 !2006-07-11 Tue #include int main() { GStringChunk *string_chunk; string_chunk = g_string_chunk_new(1024); g_string_chunk_insert(string_chunk, "foo"); g_string_chunk_insert(string_chunk, "bar"); g_string_chunk_insert(string_chunk, "baz"); g_string_chunk_free(string_chunk); return 0; } !2006-07-10 Mon #include int main() { GStringChunk *string_chunk; string_chunk = g_string_chunk_new(1024); g_string_chunk_free(string_chunk); return 0; } !2006-07-09 Sun #include int main() { GMemChunk *array_mem_chunk; gchar *array[2048]; gint i; array_mem_chunk = g_mem_chunk_create (gchar, 1024, G_ALLOC_AND_FREE); g_mem_chunk_print(array_mem_chunk); for (i = 0; i < 2048; i++) { array[i] = g_chunk_new (gchar, array_mem_chunk); } for (i = 0; i < 2048; i++) { g_mem_chunk_free (array_mem_chunk, array[i]); } g_print("*****\n"); g_mem_chunk_print(array_mem_chunk); g_mem_chunk_destroy (array_mem_chunk); return 0; } で、 GLib-INFO: gchar mem chunks (1024): 0 bytes using 0 mem areas ***** GLib-INFO: gchar mem chunks (1024): 8192 bytes using 9 mem areas !2006-07-08 Sat #include int main() { GMemChunk *array_mem_chunk; gchar *array[2048]; gint i; array_mem_chunk = g_mem_chunk_create (gchar, 1024, G_ALLOC_AND_FREE); g_mem_chunk_info(); for (i = 0; i < 2048; i++) { array[i] = g_chunk_new (gchar, array_mem_chunk); } for (i = 0; i < 2048; i++) { g_mem_chunk_free (array_mem_chunk, array[i]); } g_print("*****\n"); g_mem_chunk_info(); g_mem_chunk_destroy (array_mem_chunk); return 0; } で、 GLib-INFO: 1 mem chunks GLib-INFO: GLib default GList allocator: 12 bytes using 1 mem areas GLib-INFO: hash node mem chunk: 24 bytes using 1 mem areas GLib-INFO: array mem chunk: 20 bytes using 1 mem areas GLib-INFO: GLib default GSList allocator: 8 bytes using 1 mem areas GLib-INFO: GLib GTreeNode mem chunk: 40 bytes using 1 mem areas GLib-INFO: string mem chunk: 24 bytes using 1 mem areas GLib-INFO: gchar mem chunks (1024): 0 bytes using 0 mem areas ***** GLib-INFO: 7 mem chunks GLib-INFO: GLib default GList allocator: 12 bytes using 1 mem areas GLib-INFO: hash node mem chunk: 24 bytes using 1 mem areas GLib-INFO: array mem chunk: 20 bytes using 1 mem areas GLib-INFO: GLib default GSList allocator: 8 bytes using 1 mem areas GLib-INFO: GLib GTreeNode mem chunk: 220 bytes using 1 mem areas GLib-INFO: string mem chunk: 24 bytes using 1 mem areas GLib-INFO: gchar mem chunks (1024): 8192 bytes using 9 mem areas !2006-07-07 Fri #include int main() { GMemChunk *array_mem_chunk; gchar *array[2048]; gint i; array_mem_chunk = g_mem_chunk_create (gchar, 1024, G_ALLOC_AND_FREE); g_blow_chunks(); for (i = 0; i < 2048; i++) { array[i] = g_chunk_new (gchar, array_mem_chunk); } for (i = 0; i < 2048; i++) { g_mem_chunk_free (array_mem_chunk, array[i]); } g_mem_chunk_destroy (array_mem_chunk); return 0; } !2006-07-06 Thu #include int main() { GMemChunk *array_mem_chunk; gchar *array[2048]; gint i; array_mem_chunk = g_mem_chunk_create (gchar, 1024, G_ALLOC_AND_FREE); g_mem_chunk_clean(array_mem_chunk); for (i = 0; i < 2048; i++) { array[i] = g_chunk_new (gchar, array_mem_chunk); } for (i = 0; i < 2048; i++) { g_mem_chunk_free (array_mem_chunk, array[i]); } g_mem_chunk_destroy (array_mem_chunk); return 0; } g_mem_chunk_clean と g_mem_chunk_destroy の違いは? 使っていないメモリを解放するのかなあ??? !2006-07-05 Wed #include int main() { GMemChunk *array_mem_chunk; gchar *array[2048]; gint i; array_mem_chunk = g_mem_chunk_create (gchar, 1024, G_ALLOC_AND_FREE); g_mem_chunk_reset(array_mem_chunk); for (i = 0; i < 2048; i++) { array[i] = g_chunk_new (gchar, array_mem_chunk); } for (i = 0; i < 2048; i++) { g_mem_chunk_free (array_mem_chunk, array[i]); } g_mem_chunk_destroy (array_mem_chunk); return 0; } g_mem_chunk_reset と g_mem_chunk_destroy の違いは? !2006-07-04 Tue #include int main() { GMemChunk *array_mem_chunk; gchar *array[2048]; gint i; array_mem_chunk = g_mem_chunk_create (gchar, 1024, G_ALLOC_AND_FREE); for (i = 0; i < 2048; i++) { array[i] = g_chunk_new (gchar, array_mem_chunk); } for (i = 0; i < 2048; i++) { g_mem_chunk_free (array_mem_chunk, array[i]); } g_mem_chunk_destroy (array_mem_chunk); return 0; } * 各ブロックの中に格納するアトムの数って何だ? * 確保するアトムに上限があるわけでもなさそうだし? * ブロックの中にアトム(↑の場合 1024 個)があって、メモリの確保の単位がブロックということで良いのか??? !2006-07-03 Mon #include /* from garray.c */ typedef struct _GRealArray GRealArray; struct _GRealArray { guint8 *data; guint len; guint alloc; guint elt_size; guint zero_terminated : 1; guint clear : 1; }; int main() { GMemChunk *array_mem_chunk; GRealArray *array; array_mem_chunk = g_mem_chunk_create (GRealArray, 1024, G_ALLOC_AND_FREE); array = g_chunk_new0 (GRealArray, array_mem_chunk); array->data = NULL; array->len = 0; array->alloc = 0; #if 0 array->zero_terminated = (zero_terminated ? 1 : 0); array->clear = (clear ? 1 : 0); array->elt_size = elt_size; #endif g_chunk_free (array, array_mem_chunk); g_mem_chunk_destroy (array_mem_chunk); return 0; } !2006-07-02 Sun #include /* from garray.c */ typedef struct _GRealArray GRealArray; struct _GRealArray { guint8 *data; guint len; guint alloc; guint elt_size; guint zero_terminated : 1; guint clear : 1; }; int main() { GMemChunk *array_mem_chunk; GRealArray *array; array_mem_chunk = g_mem_chunk_create (GRealArray, 1024, G_ALLOC_AND_FREE); #if 0 array = g_chunk_new (GRealArray, array_mem_chunk); #else array = (GRealArray *)g_mem_chunk_alloc0(array_mem_chunk); #endif array->data = NULL; array->len = 0; array->alloc = 0; #if 0 array->zero_terminated = (zero_terminated ? 1 : 0); array->clear = (clear ? 1 : 0); array->elt_size = elt_size; #endif g_chunk_free (array, array_mem_chunk); g_mem_chunk_destroy (array_mem_chunk); return 0; } !2006-07-01 Sat #include /* from garray.c */ typedef struct _GRealArray GRealArray; struct _GRealArray { guint8 *data; guint len; guint alloc; guint elt_size; guint zero_terminated : 1; guint clear : 1; }; int main() { GMemChunk *array_mem_chunk; GRealArray *array; array_mem_chunk = g_mem_chunk_create (GRealArray, 1024, G_ALLOC_AND_FREE); #if 0 array = g_chunk_new (GRealArray, array_mem_chunk); #else array = (GRealArray *)g_mem_chunk_alloc(array_mem_chunk); #endif array->data = NULL; array->len = 0; array->alloc = 0; #if 0 array->zero_terminated = (zero_terminated ? 1 : 0); array->clear = (clear ? 1 : 0); array->elt_size = elt_size; #endif g_chunk_free (array, array_mem_chunk); g_mem_chunk_destroy (array_mem_chunk); return 0; } g_chunk_new が便利というのは、キャストしてくれるという意味のようだ !2006-06-30 Fri #include /* from garray.c */ typedef struct _GRealArray GRealArray; struct _GRealArray { guint8 *data; guint len; guint alloc; guint elt_size; guint zero_terminated : 1; guint clear : 1; }; int main() { GMemChunk *array_mem_chunk; GRealArray *array; #if 0 array_mem_chunk = g_mem_chunk_create (GRealArray, 1024, G_ALLOC_AND_FREE); #else array_mem_chunk = g_mem_chunk_create (GRealArray, 1024, G_ALLOC_ONLY); #endif array = g_chunk_new (GRealArray, array_mem_chunk); array->data = NULL; array->len = 0; array->alloc = 0; #if 0 array->zero_terminated = (zero_terminated ? 1 : 0); array->clear = (clear ? 1 : 0); array->elt_size = elt_size; #endif g_chunk_free (array, array_mem_chunk); g_mem_chunk_destroy (array_mem_chunk); return 0; } G_ALLOC_ONLY でも、g_chunk_free でエラーとなるわけではないらしい。 !2006-06-29 Thu まんま #include /* from garray.c */ typedef struct _GRealArray GRealArray; struct _GRealArray { guint8 *data; guint len; guint alloc; guint elt_size; guint zero_terminated : 1; guint clear : 1; }; int main() { GMemChunk *array_mem_chunk; GRealArray *array; /* GRealArray 構造体を格納するために便利なマクロ g_mem_chunk_create() を使って GMemChunk を生成する。メモリ・ブロックの中には 1024 個のアトムを用意して、 アトムをそれぞれ解放できるようにする。*/ array_mem_chunk = g_mem_chunk_create (GRealArray, 1024, G_ALLOC_AND_FREE); /* 便利なマクロ g_chunk_new() を使ってアトムを一つ確保する。*/ array = g_chunk_new (GRealArray, array_mem_chunk); /* ここで、構造体を指す普通のポインタのように配列を利用することができる。*/ array->data = NULL; array->len = 0; array->alloc = 0; #if 0 array->zero_terminated = (zero_terminated ? 1 : 0); array->clear = (clear ? 1 : 0); array->elt_size = elt_size; #endif /* 要素を解放することができるので、その要素を再利用することもできる。*/ g_chunk_free (array, array_mem_chunk); /* GMemChunk を使い終わったら破壊する。*/ g_mem_chunk_destroy (array_mem_chunk); return 0; } garray.c の g_array_sized_new に似た記述があるみたい !2006-06-28 Wed まんま(j の宣言を追加) #include int main() { GMemChunk *mem_chunk; gchar *mem[10000]; gint i, j; /* 50 バイト長のアトムで GMemChunk を生成し、メモリ・ブロックを 100 バイト確保する。 これは、2 つだけのアトムでメモリ・ブロックに合わせているので あまり効率的とはいえない点に注意して下さい。*/ mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE); /* ここで 10000 子のアトムを確保する。 */ for (i = 0; i < 10000; i++) { mem[i] = g_chunk_new (gchar, mem_chunk); /* なにかゴミでアトムのメモリを一杯にする。*/ for (j = 0; j < 50; j++) mem[i][j] = i * j; } /* ここで全てのアトムを解放する。 GMemChunk を破壊することになるので、通常はこのような使い方はしない点に注意すること。*/ for (i = 0; i < 10000; i++) { g_mem_chunk_free (mem_chunk, mem[i]); } /* GMemChunk を使い終わって、破壊する。*/ g_mem_chunk_destroy (mem_chunk); return 0; } * 「メモリ・ブロック」とは何? * 「50 バイト長のアトムで 〜 注意して下さい。」の意味がまったく分からない… !2006-06-27 Tue 2006-06-01 のコードを利用して #include #include static void string_succ_c_prepend(GString *new_str, gchar c) { if (c == '9') { g_string_prepend_c(new_str, '1'); } else if (c == 'z') { g_string_prepend_c(new_str, 'a'); } else if (c == 'Z') { g_string_prepend_c(new_str, 'A'); } } static int string_not_include_alnum(GString *str, guint i) { guint j; for (j = 0; j < i; j++) { if (g_ascii_isalnum(*(str->str + j))) { return 0; } } return 1; } static GString *string_succ(GString *str) { guint i, carry = 1; gchar c = 0, prev_c = 0; GString *new_str = g_string_new(""); for (i = 0; i < str->len; i++) { c = *(str->str + str->len - 1 - i); if (carry) { if (c >= '0' && c <= '9') { carry = (c == '9') ? 1 : 0; g_string_prepend_c(new_str, carry ? '0' : c + 1); } else if (c >= 'a' && c <= 'z') { carry = (c == 'z') ? 1 : 0; g_string_prepend_c(new_str, carry ? 'a' : c + 1); } else if (c >= 'A' && c <= 'Z') { carry = (c == 'Z') ? 1 : 0; g_string_prepend_c(new_str, carry ? 'A' : c + 1); } else { if (string_not_include_alnum(str, str->len - 1 - i)) { string_succ_c_prepend(new_str, prev_c); } g_string_prepend_c(new_str, c); } } else { g_string_prepend_c(new_str, c); } prev_c = c; } if (carry) { string_succ_c_prepend(new_str, c); } return new_str; } static void func(gchar *x) { g_print("%s\n", x); } static void string_upto(GString *str, GString *max, void (*pfunc)(gchar *)) { GString *succ_str; (*pfunc)(str->str); succ_str = string_succ(str); while (strcmp(succ_str->str, max->str) <= 0) { (*pfunc)(succ_str->str); g_string_free(str, TRUE); str = succ_str; succ_str = string_succ(str); } } int main() { GString *str; GString *str2; str = g_string_new("aa"); str2 = g_string_new("ac"); string_upto(str, str2, func); g_string_free(str, TRUE); g_string_free(str2, TRUE); return 0; } で、 aa ab ac !2006-06-26 Mon #include static void string_upcase_bang(GString *str) { g_string_ascii_up(str); } int main() { GString *str1 = g_string_new("foo"); string_upcase_bang(str1); g_print("%s\n", str1->str); g_string_free(str1, TRUE); return 0; } で、 FOO !2006-06-25 Sun #include static GString *string_upcase(GString *str) { guint i; GString *new_str = g_string_new(""); for (i = 0; i < str->len; i++) { g_string_append_c(new_str, g_ascii_toupper(*(str->str + i))); } return new_str; } int main() { GString *str1 = g_string_new("foo"); GString *str2 = string_upcase(str1); g_print("%s %s\n", str1->str, str2->str); g_string_free(str1, TRUE); g_string_free(str2, TRUE); return 0; } で、 foo FOO !2006-06-24 Sat #include static GString *string_tr(GString *str, GString *search, GString *replace) { guint i, j; gint flag; GString *new_str = g_string_new(""); for (i = 0; i < str->len; i++) { gchar c = *(str->str + i); flag = FALSE; for (j = 0; j < search->len; j++) { if (c == *(search->str + j) && j < replace->len) { g_string_append_c(new_str, *(replace->str + j)); flag = TRUE; break; } } if (flag == FALSE) { g_string_append_c(new_str, c); } } return new_str; } int main() { GString *str1 = g_string_new("abc DEF ghi 02468"); GString *search = g_string_new("abcdefg"); GString *replace = g_string_new("ABCDEFG"); GString *str2; str2 = string_tr(str1, search, replace); g_print("%s\n", str2->str); g_string_free(str1, TRUE); g_string_free(str2, TRUE); g_string_free(search, TRUE); g_string_free(replace, TRUE); return 0; } で、 ABC DEF Ghi 02468 全然まともに対応できていないけど… !2006-06-23 Fri #include static int string_to_i(GString *str) { if (str->len > 0 && *(str->str) == '0') { if (str->len > 1) { switch (*(str->str+1)) { case 'b': case 'B': return g_ascii_strtoull(str->str + 2, NULL, 2); break; case 'o': case 'O': return g_ascii_strtoull(str->str + 2, NULL, 8); break; case 'd': case 'D': return g_ascii_strtoull(str->str + 2, NULL, 10); break; case 'x': case 'X': return g_ascii_strtoull(str->str + 2, NULL, 16); break; default: return g_ascii_strtoull(str->str + 2, NULL, 10); break; } } else { return g_ascii_strtoull(str->str, NULL, 10); } } else { return g_ascii_strtoull(str->str, NULL, 10); } } int main() { GString *str1 = g_string_new("0b10"); g_print("%d\n", string_to_i(str1)); g_string_free(str1, TRUE); str1 = g_string_new("0o10"); g_print("%d\n", string_to_i(str1)); g_string_free(str1, TRUE); str1 = g_string_new("010"); g_print("%d\n", string_to_i(str1)); g_string_free(str1, TRUE); str1 = g_string_new("0d10"); g_print("%d\n", string_to_i(str1)); g_string_free(str1, TRUE); str1 = g_string_new("0x10"); g_print("%d\n", string_to_i(str1)); g_string_free(str1, TRUE); return 0; } で、 2 8 0 10 16 うっ、マイナスに対応できていない… !2006-06-22 Thu #include static int string_to_i(GString *str) { return g_ascii_strtoull(str->str, NULL, 10); } int main() { GString *str1 = g_string_new("0b10"); g_print("%d\n", string_to_i(str1)); g_string_free(str1, TRUE); str1 = g_string_new("0o10"); g_print("%d\n", string_to_i(str1)); g_string_free(str1, TRUE); str1 = g_string_new("010"); g_print("%d\n", string_to_i(str1)); g_string_free(str1, TRUE); str1 = g_string_new("0d10"); g_print("%d\n", string_to_i(str1)); g_string_free(str1, TRUE); str1 = g_string_new("0x10"); g_print("%d\n", string_to_i(str1)); g_string_free(str1, TRUE); return 0; } で、 0 0 10 0 0 !2006-06-21 Wed #include static int string_to_i(GString *str) { return g_ascii_strtoull(str->str, NULL, 10); } int main() { GString *str1 = g_string_new(" 10"); g_print("%d\n", string_to_i(str1)); g_string_free(str1, TRUE); str1 = g_string_new("010"); g_print("%d\n", string_to_i(str1)); g_string_free(str1, TRUE); str1 = g_string_new("-010"); g_print("%d\n", string_to_i(str1)); g_string_free(str1, TRUE); str1 = g_string_new("0x11"); g_print("%d\n", string_to_i(str1)); g_string_free(str1, TRUE); return 0; } で、 10 10 -10 0 !2006-06-20 Tue #include static float string_to_f(GString *str) { return g_ascii_strtod(str->str, NULL); } int main() { GString *str1 = g_string_new("10"); g_print("%e\n", string_to_f(str1)); g_string_free(str1, TRUE); str1 = g_string_new("10e2"); g_print("%e\n", string_to_f(str1)); g_string_free(str1, TRUE); str1 = g_string_new("1e-2"); g_print("%e\n", string_to_f(str1)); g_string_free(str1, TRUE); str1 = g_string_new(".1"); g_print("%e\n", string_to_f(str1)); g_string_free(str1, TRUE); str1 = g_string_new("0xa.a"); g_print("%e\n", string_to_f(str1)); g_string_free(str1, TRUE); return 0; } で、 1.000000e+01 1.000000e+03 1.000000e-02 1.000000e-01 1.062500e+01 !2006-06-19 Mon #include static void string_swapcase_bang(GString *str) { guint i; for (i = 0; i < str->len; i++) { gchar c = *(str->str + i); if (g_ascii_islower(c)) { *(str->str + i) = g_ascii_toupper(c); } else if (g_ascii_isupper(c)) { *(str->str + i) = g_ascii_tolower(c); } } } int main() { GString *str1 = g_string_new("abc DEF ghi"); string_swapcase_bang(str1); g_print("%s\n", str1->str); g_string_free(str1, TRUE); return 0; } で、 ABC def GHI !2006-06-18 Sun #include static GString *string_swapcase(GString *str) { guint i; GString *new_str = g_string_new(""); for (i = 0; i < str->len; i++) { gchar c = *(str->str + i); if (g_ascii_islower(c)) { g_string_append_c(new_str, g_ascii_toupper(c)); } else if (g_ascii_isupper(c)) { g_string_append_c(new_str, g_ascii_tolower(c)); } else { g_string_append_c(new_str, c); } } return new_str; } int main() { GString *str1 = g_string_new("abc DEF ghi"); GString *str2; str2 = string_swapcase(str1); g_print("%s\n", str1->str); g_print("%s\n", str2->str); g_string_free(str1, TRUE); g_string_free(str2, TRUE); return 0; } で、 abc DEF ghi ABC def GHI !2006-06-17 Sat #include #include static GString *string_sub_bang(GString *str, GString *pattern, GString *replace) { gchar *p, *strp; strp = str->str; if ((p = strstr(strp, pattern->str))) { g_string_erase (str, p - str->str, pattern->len); g_string_insert(str, p - str->str, replace->str); return str; } else { return NULL; } } int main() { GString *str1 = g_string_new("abcdef abcdef"); GString *str_pattern = g_string_new("def"); GString *str_replace = g_string_new("*FED*"); GString *str2; str2 = string_sub_bang(str1, str_pattern, str_replace); g_print("%s\n", str1->str); g_print("%s\n", str2->str); g_string_free(str1, TRUE); g_string_free(str_pattern, TRUE); g_string_free(str_replace, TRUE); g_string_free(str2, TRUE); return 0; } で、 abc*FED* abcdef abc*FED* abcdef !2006-06-16 Fri #include #include static GString *string_sub(GString *str, GString *pattern, GString *replace) { gchar *p, *strp; GString *new_str = g_string_new(""); strp = str->str; if ((p = strstr(strp, pattern->str))) { if (p != strp) { g_string_append_len(new_str, strp, p - strp); } g_string_append(new_str, replace->str); strp = p + pattern->len; } if (strp < str->str + str->len) { g_string_append(new_str, strp); } return new_str; } int main() { GString *str1 = g_string_new("abcdef abcdef"); GString *str_pattern = g_string_new("def"); GString *str_replace = g_string_new("*FED*"); GString *str2; str2 = string_sub(str1, str_pattern, str_replace); g_print("%s\n", str2->str); g_string_free(str_pattern, TRUE); str_pattern = g_string_new("DEF"); str2 = string_sub(str1, str_pattern, str_replace); g_print("%s\n", str2->str); g_string_free(str1, TRUE); g_string_free(str_pattern, TRUE); g_string_free(str_replace, TRUE); g_string_free(str2, TRUE); return 0; } で、 abc*FED* abcdef abcdef abcdef !2006-06-15 Thu #include static GString *string_rstrip(GString *str) { GString *new_str = g_string_new(str->str); g_strchomp(new_str->str); return new_str; } int main() { GString *str = g_string_new(" abc\n"); GString *str2; str2 = string_rstrip(str); g_print("%s\n", str2->str); g_string_free(str, TRUE); g_string_free(str2, TRUE); str = g_string_new(" abc \t\r\n"); str2 = string_rstrip(str); g_print("%s\n", str2->str); g_string_free(str, TRUE); g_string_free(str2, TRUE); return 0; } で、 abc abc !2006-06-14 Wed #include static GString *string_lstrip(GString *str) { GString *new_str = g_string_new(str->str); g_strchug(new_str->str); return new_str; } int main() { GString *str = g_string_new("\t abc\n"); GString *str2; str2 = string_lstrip(str); g_print("%s\n", str2->str); g_string_free(str, TRUE); g_string_free(str2, TRUE); str = g_string_new("\nabc"); str2 = string_lstrip(str); g_print("%s\n", str2->str); g_string_free(str, TRUE); g_string_free(str2, TRUE); return 0; } で、 abc abc !2006-06-13 Tue #include static GString *string_strip(GString *str) { GString *new_str = g_string_new(str->str); g_strstrip(new_str->str); return new_str; } int main() { GString *str = g_string_new(" abc \r\n"); GString *str2; str2 = string_strip(str); g_print("%s\n", str2->str); g_string_free(str, TRUE); g_string_free(str2, TRUE); str = g_string_new("\tabc\n"); str2 = string_strip(str); g_print("%s\n", str2->str); g_string_free(str, TRUE); g_string_free(str2, TRUE); return 0; } で、 abc abc !2006-06-12 Mon #include static GString *string_squeeze(GString *str, GString *pat) { GString *new_str = g_string_new(""); guint i, j, include; gchar prev_c = 0; for (i = 0; i < str->len; i++) { gchar c = *(str->str + i); include = FALSE; for (j = 0; j < pat->len; j++) { if (c == *(pat->str + j)) { include = TRUE; break; } } if (include == FALSE || (include == TRUE && c != prev_c)) { g_string_append_c(new_str, c); } prev_c = c; } return new_str; } int main() { GString *str = g_string_new("112233445566778899"); GString *pat = g_string_new("2378"); GString *str2; str2 = string_squeeze(str, pat); g_print("%s\n", str->str); g_print("%s\n", str2->str); g_string_free(str, TRUE); g_string_free(pat, TRUE); g_string_free(str2, TRUE); return 0; } で、 112233445566778899 11234455667899 「-」とか「^」とかには未対応… !2006-06-11 Sun #include static GString *string_squeeze(GString *str) { GString *new_str = g_string_new(""); guint i; gchar prev_c = 0; for (i = 0; i < str->len; i++) { gchar c = *(str->str + i); if (c != prev_c) { g_string_append_c(new_str, c); } prev_c = c; } return new_str; } int main() { GString *str = g_string_new("112233445566778899"); GString *str2; str2 = string_squeeze(str); g_print("%s\n", str->str); g_print("%s\n", str2->str); g_string_free(str, TRUE); g_string_free(str2, TRUE); return 0; } で、 112233445566778899 123456789 「-」とか「^」とかには未対応… !2006-06-10 Sat #include static GPtrArray *string_split(GString *str, GString *sep, gint limit) { gchar **tokens; guint i = 0; GPtrArray *new_ary = g_ptr_array_new(); tokens = g_strsplit(str->str, sep->str, limit); while (tokens[i]) { g_ptr_array_add(new_ary, g_strdup(tokens[i])); i++; } g_strfreev(tokens); return new_ary; } static void print_ary(GPtrArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("\"%s\"", (gchar *)g_ptr_array_index(ary, i)); } g_print("]\n"); } int main() { GString *str = g_string_new("abc:def:ghi"); GString *sep = g_string_new(":"); GPtrArray *ary; ary= string_split(str, sep, 0); print_ary(ary); g_ptr_array_free(ary, TRUE); ary= string_split(str, sep, 1); print_ary(ary); g_ptr_array_free(ary, TRUE); ary= string_split(str, sep, 2); print_ary(ary); g_ptr_array_free(ary, TRUE); g_string_free(str, TRUE); g_string_free(sep, TRUE); str = g_string_new("abc::def::ghi"); sep = g_string_new("::"); ary= string_split(str, sep, 0); print_ary(ary); g_ptr_array_free(ary, TRUE); g_string_free(str, TRUE); g_string_free(sep, TRUE); return 0; } で、 ["abc", "def", "ghi"] ["abc:def:ghi"] ["abc", "def:ghi"] ["abc", "def", "ghi"] free まわり 怪しい? !2006-06-09 Fri #include static GString *string_slice_bang(GString *str, guint start, guint end) { GString *new_str = g_string_new_len(str->str + start, end - start + 1); g_string_erase(str, start, end - start + 1); return new_str; } int main() { GString *str = g_string_new("abcdefghi"); GString *str2; str2 = string_slice_bang(str, 0, 0); g_print("%s %s\n", str2->str, str->str); g_string_free(str2, TRUE); str2 = string_slice_bang(str, 0, 1); g_print("%s %s\n", str2->str, str->str); g_string_free(str2, TRUE); str2 = string_slice_bang(str, 0, 2); g_print("%s %s\n", str2->str, str->str); g_string_free(str2, TRUE); str2 = string_slice_bang(str, 1, 2); g_print("%s %s\n", str2->str, str->str); g_string_free(str2, TRUE); g_string_free(str, TRUE); return 0; } で、 a bcdefghi bc defghi def ghi hi g !2006-06-08 Thu #include static GString *string_slice_bang(GString *str, guint nth, guint len) { GString *new_str = g_string_new_len(str->str + nth, len); g_string_erase(str, nth, len); return new_str; } int main() { GString *str = g_string_new("abcdefghi"); GString *str2; str2 = string_slice_bang(str, 0, 0); g_print("%s %s\n", str2->str, str->str); g_string_free(str2, TRUE); str2 = string_slice_bang(str, 0, 1); g_print("%s %s\n", str2->str, str->str); g_string_free(str2, TRUE); str2 = string_slice_bang(str, 0, 2); g_print("%s %s\n", str2->str, str->str); g_string_free(str2, TRUE); str2 = string_slice_bang(str, 1, 2); g_print("%s %s\n", str2->str, str->str); g_string_free(str2, TRUE); g_string_free(str, TRUE); return 0; } で、 abcdefghi a bcdefghi bc defghi ef dghi !2006-06-07 Wed #include static int string_slice_bang(GString *str, guint nth) { gchar c = str->str[nth]; g_string_erase(str, nth, 1); return c; } int main() { GString *str = g_string_new("abc"); g_print("%d\n", string_slice_bang(str, 0)); g_print("%s\n", str->str); g_print("%d\n", string_slice_bang(str, 1)); g_print("%s\n", str->str); g_string_free(str, TRUE); return 0; } で、 97 bc 99 b !2006-06-06 Tue #include static int string_rindex(GString *str, GString *substr, gint pos) { gchar *p; guint len; if (pos < 0) { len = str->len; } { len = pos; } if ((p = g_strrstr_len(str->str, len, substr->str))) { return p - str->str; } else { return -1; } } int main() { GString *str1 = g_string_new("foobarfoobar"); GString *str2 = g_string_new("bar"); g_print("%d\n", string_rindex(str1, str2, -1)); g_print("%d\n", string_rindex(str1, str2, 6)); g_string_free(str1, TRUE); g_string_free(str2, TRUE); return 0; } で、 9 3 これで良い? !2006-06-05 Mon #include static int string_rindex(GString *str, GString *substr) { gchar *p; if ((p = g_strrstr(str->str, substr->str))) { return p - str->str; } else { return -1; } } int main() { GString *str1 = g_string_new("astrochemistry"); GString *str2 = g_string_new("str"); g_print("%d\n", string_rindex(str1, str2)); g_string_free(str1, TRUE); g_string_free(str2, TRUE); str1 = g_string_new("character"); str2 = g_string_new("c"); g_print("%d\n", string_rindex(str1, str2)); g_string_free(str1, TRUE); g_string_free(str2, TRUE); str1 = g_string_new("foo"); str2 = g_string_new("bar"); g_print("%d\n", string_rindex(str1, str2)); g_string_free(str1, TRUE); g_string_free(str2, TRUE); return 0; } で、 10 5 -1 !2006-06-04 Sun #include static GString *string_reverse(GString *str) { guint i; GString *new_str = g_string_new(""); for (i = 0; i < str->len; i++) { gchar c = *(str->str + str->len - 1 - i); g_string_append_c(new_str, c); } return new_str; } int main() { GString *str1 = g_string_new("foobar"); GString *str2; str2 = string_reverse(str1); g_print("%s\n", str1->str); g_print("%s\n", str2->str); g_string_free(str1, TRUE); g_string_free(str2, TRUE); return 0; } で、 foobar raboof !2006-06-03 Sat #include static void string_replace(GString *str, GString *other) { g_string_erase(str, 0, str->len); g_string_insert(str, 0, other->str); } int main() { GString *str1 = g_string_new("foo"); GString *str2 = g_string_new("bar"); string_replace(str1, str2); g_print("%s\n", str1->str); g_print("%s\n", str2->str); g_string_free(str1, TRUE); g_string_free(str2, TRUE); return 0; } で、 bar bar !2006-06-02 Fri #include #include static int string_oct(GString *str) { int ret = 0; sscanf(str->str, "%o", &ret); return ret; } int main() { GString *str1 = g_string_new("10"); g_print("%d\n", string_oct(str1)); g_string_free(str1, TRUE); str1 = g_string_new("010"); g_print("%d\n", string_oct(str1)); g_string_free(str1, TRUE); str1 = g_string_new("8"); g_print("%d\n", string_oct(str1)); g_string_free(str1, TRUE); str1 = g_string_new("0b10"); g_print("%d\n", string_oct(str1)); g_string_free(str1, TRUE); str1 = g_string_new("10"); g_print("%d\n", string_oct(str1)); g_string_free(str1, TRUE); str1 = g_string_new("0x10"); g_print("%d\n", string_oct(str1)); g_string_free(str1, TRUE); return 0; } で、 8 8 0 0 8 0 同じにはならない !2006-06-01 Thu #include static void string_succ_c_prepend(GString *new_str, gchar c) { if (c == '9') { g_string_prepend_c(new_str, '1'); } else if (c == 'z') { g_string_prepend_c(new_str, 'a'); } else if (c == 'Z') { g_string_prepend_c(new_str, 'A'); } } static int string_not_include_alnum(GString *str, guint i) { guint j; for (j = 0; j < i; j++) { if (g_ascii_isalnum(*(str->str + j))) { return 0; } } return 1; } static GString *string_succ(GString *str) { guint i, carry = 1; gchar c = 0, prev_c = 0; GString *new_str = g_string_new(""); for (i = 0; i < str->len; i++) { c = *(str->str + str->len - 1 - i); if (carry) { if (c >= '0' && c <= '9') { carry = (c == '9') ? 1 : 0; g_string_prepend_c(new_str, carry ? '0' : c + 1); } else if (c >= 'a' && c <= 'z') { carry = (c == 'z') ? 1 : 0; g_string_prepend_c(new_str, carry ? 'a' : c + 1); } else if (c >= 'A' && c <= 'Z') { carry = (c == 'Z') ? 1 : 0; g_string_prepend_c(new_str, carry ? 'A' : c + 1); } else { if (string_not_include_alnum(str, str->len - 1 - i)) { string_succ_c_prepend(new_str, prev_c); } g_string_prepend_c(new_str, c); } } else { g_string_prepend_c(new_str, c); } prev_c = c; } if (carry) { string_succ_c_prepend(new_str, c); } return new_str; } int main() { GString *str; GString *str2; str = g_string_new("aa"); str2 = string_succ(str); g_print("%s\n", str2->str); g_string_free(str, TRUE); g_string_free(str2, TRUE); str = g_string_new("99"); str2 = string_succ(str); g_print("%s\n", str2->str); g_string_free(str, TRUE); g_string_free(str2, TRUE); str = g_string_new("a9"); str2 = string_succ(str); g_print("%s\n", str2->str); g_string_free(str, TRUE); g_string_free(str2, TRUE); str = g_string_new("Az"); str2 = string_succ(str); g_print("%s\n", str2->str); g_string_free(str, TRUE); g_string_free(str2, TRUE); str = g_string_new("zz"); str2 = string_succ(str); g_print("%s\n", str2->str); g_string_free(str, TRUE); g_string_free(str2, TRUE); str = g_string_new("-9"); str2 = string_succ(str); g_print("%s\n", str2->str); g_string_free(str, TRUE); g_string_free(str2, TRUE); str = g_string_new("9"); str2 = string_succ(str); g_print("%s\n", str2->str); g_string_free(str, TRUE); g_string_free(str2, TRUE); str = g_string_new("09"); str2 = string_succ(str); g_print("%s\n", str2->str); g_string_free(str, TRUE); g_string_free(str2, TRUE); str = g_string_new("1.9.9"); str2 = string_succ(str); g_print("%s\n", str2->str); g_string_free(str, TRUE); g_string_free(str2, TRUE); return 0; } で、 ab 100 b0 Ba aaa -10 10 10 2.0.0 それっぽく実装してみたが、おそらくバグがありそう。 !2006-05-31 Wed #include static int string_length(GString *str) { return str->len; } int main() { GString *str = g_string_new("foo"); g_print("%d\n", string_length(str)); g_string_free(str, TRUE); return 0; } で、 3 !2006-05-30 Tue #include static void string_insert(GString *str, guint nth, GString *val) { if (nth <= str->len) { g_string_insert(str, nth, val->str); } } int main() { GString *str = g_string_new("foo"); GString *str2; str2 = g_string_new("AAA"); string_insert(str, 0, str2); g_string_free(str2, TRUE); g_print("%s\n", str->str); str2 = g_string_new("BBB"); string_insert(str, 3, str2); g_string_free(str2, TRUE); g_print("%s\n", str->str); str2 = g_string_new("CCC"); string_insert(str, 9, str2); g_string_free(str2, TRUE); g_print("%s\n", str->str); g_string_free(str, TRUE); return 0; } で、 AAAfoo AAABBBfoo AAABBBfooCCC !2006-05-29 Mon #include #include static int string_index(GString *str, GString *substr, guint pos) { gchar *p; if (pos >= str->len) { return -1; } else if ((p = strstr(str->str + pos, substr->str))) { return p - str->str; } else { return -1; } } int main() { GString *str1 = g_string_new("abcdef abcdef"); GString *substr = g_string_new("abc"); g_print("%d\n", string_index(str1, substr, 1)); g_string_free(substr, TRUE); substr = g_string_new("def"); g_print("%d\n", string_index(str1, substr, 4)); g_string_free(substr, TRUE); substr = g_string_new("x"); g_print("%d\n", string_index(str1, substr, 0)); g_string_free(str1, TRUE); g_string_free(substr, TRUE); return 0; } で、 7 10 -1 !2006-05-28 Sun #include #include static int string_index(GString *str, GString *substr) { gchar *p; if ((p = strstr(str->str, substr->str))) { return p - str->str; } else { return -1; } } int main() { GString *str1 = g_string_new("abcdef abcdef"); GString *substr = g_string_new("abc"); g_print("%d\n", string_index(str1, substr)); g_string_free(substr, TRUE); substr = g_string_new("def"); g_print("%d\n", string_index(str1, substr)); g_string_free(substr, TRUE); substr = g_string_new("x"); g_print("%d\n", string_index(str1, substr)); g_string_free(str1, TRUE); g_string_free(substr, TRUE); return 0; } で、 0 3 -1 !2006-05-27 Sat #include #include static int string_include(GString *str, GString *substr) { return strstr(str->str, substr->str) != NULL; } int main() { GString *str1 = g_string_new("abcdef abcdef"); GString *substr = g_string_new("abc"); g_print("%d\n", string_include(str1, substr)); g_string_free(substr, TRUE); substr = g_string_new("x"); g_print("%d\n", string_include(str1, substr)); g_string_free(str1, TRUE); g_string_free(substr, TRUE); return 0; } で、 1 0 !2006-05-26 Fri #include #include static int string_hex(GString *str) { int ret = 0; sscanf(str->str, "%x", &ret); return ret; } int main() { GString *str1 = g_string_new("10"); g_print("%d\n", string_hex(str1)); g_string_free(str1, TRUE); str1 = g_string_new("ff"); g_print("%d\n", string_hex(str1)); g_string_free(str1, TRUE); str1 = g_string_new("0x10"); g_print("%d\n", string_hex(str1)); g_string_free(str1, TRUE); str1 = g_string_new("-0x10"); g_print("%d\n", string_hex(str1)); g_string_free(str1, TRUE); str1 = g_string_new("xyz"); g_print("%d\n", string_hex(str1)); g_string_free(str1, TRUE); str1 = g_string_new("10z"); g_print("%d\n", string_hex(str1)); g_string_free(str1, TRUE); str1 = g_string_new("1_0"); g_print("%d\n", string_hex(str1)); g_string_free(str1, TRUE); return 0; } で、 16 255 16 -16 0 16 1 「_」はダメみたい。まあ、当然か。 !2006-05-25 Thu #include static GString *string_dump(GString *str) { gchar *p = g_strescape(str->str, NULL); GString *new_str = g_string_new(p); g_free(p); return new_str; } int main() { GString *str1 = g_string_new("abc\r\n\f\b10\\\""); GString *str2; str2 = string_dump(str1); g_print("%s\n", str2->str); g_string_free(str1, TRUE); g_string_free(str2, TRUE); return 0; } で、 abc\r\n\f\b10\\\" Ruby と同じようにはならない !2006-05-24 Wed #include #include static GString *string_gsub(GString *str, GString *pattern, GString *replace) { gchar *p, *strp; GString *new_str = g_string_new(""); strp = str->str; while ((p = strstr(strp, pattern->str))) { if (p != strp) { g_string_append_len(new_str, strp, p - strp); } g_string_append_len(new_str, replace->str, replace->len); strp = p + pattern->len; if (strp >= str->str + str->len) break; } if (strp < str->str + str->len) { g_string_append_len(new_str, strp, str->str + str->len - strp); } return new_str; } int main() { GString *str1 = g_string_new("abcdef abcdef"); GString *str_pattern = g_string_new("abc"); GString *str_replace = g_string_new("ABCBA"); GString *str2; str2 = string_gsub(str1, str_pattern, str_replace); g_print("%s\n", str2->str); g_string_free(str1, TRUE); g_string_free(str_pattern, TRUE); g_string_free(str_replace, TRUE); g_string_free(str2, TRUE); return 0; } で、 ABCBAdef ABCBAdef g_string_append_len を使わなくても良いところに g_string_append_len を使ってしまったっぽい !2006-05-23 Tue #include static int string_empty(GString *str) { return str->len == 0; } int main() { GString *str1 = g_string_new(""); GString *str2 = g_string_new("abc"); g_print("%d\n", string_empty(str1)); g_print("%d\n", string_empty(str2)); g_string_free(str1, TRUE); g_string_free(str2, TRUE); return 0; } で、 1 0 !2006-05-22 Mon #include static void func(guint x) { g_print("%d\n", x); } static void string_each_byte(GString *str, void (*pfunc)(guint x)) { guint i; for (i = 0; i < str->len; i++) { (*pfunc)(*(str->str + i)); } } int main() { GString *str = g_string_new("abc"); string_each_byte(str, func); g_string_free(str, TRUE); return 0; } で、 97 98 99 !2006-05-21 Sun #include static void func(gchar *x) { g_print("%s\n", x); } static void string_each(GString *str, void (*pfunc)(gchar *)) { guint i, str_len; gchar *start, *line_str; start = str->str; str_len = 0; for (i = 0; i < str->len; i++) { if (*(str->str + i) == '\n') { line_str = g_strndup(start, str_len); (*pfunc)(line_str); g_free(line_str); str_len = 0; start = str->str + i + 1; } else if (i == str->len - 1) { line_str = g_strndup(start, str_len + 1); (*pfunc)(line_str); g_free(line_str); } else { str_len++; } } } int main() { GString *str = g_string_new("abc\ndef\nghi\n"); string_each(str, func); g_string_free(str, TRUE); return 0; } で、 abc def ghi !2006-05-20 Sat #include static void string_downcase_bang(GString *str) { g_string_ascii_down(str); } int main() { GString *str1 = g_string_new("FOO"); string_downcase_bang(str1); g_print("%s\n", str1->str); g_string_free(str1, TRUE); return 0; } で、 foo !2006-05-19 Fri static GString *string_downcase(GString *str) { guint i; GString *new_str = g_string_new(""); for (i = 0; i < str->len; i++) { g_string_append_c(new_str, g_ascii_tolower(*(str->str + i))); } return new_str; } int main() { GString *str1 = g_string_new("FOO"); GString *str2 = string_downcase(str1); g_print("%s %s\n", str1->str, str2->str); g_string_free(str1, TRUE); g_string_free(str2, TRUE); return 0; } で、 FOO foo !2006-05-18 Thu #include static GString *string_dup(GString *str) { return g_string_new(str->str); } int main() { GString *str = g_string_new("foo"); GString *str2; str2 = string_dup(str); g_string_set_size(str, 2); g_print("%s\n", str->str); g_print("%s\n", str2->str); g_string_free(str, TRUE); g_string_free(str2, TRUE); return 0; } で、 fo foo !2006-05-17 Wed #include static void string_clear(GString *str) { g_string_set_size(str, 0); } int main() { GString *str = g_string_new("foo\n"); string_clear(str); g_print("%s\n", str->str); g_string_free(str, TRUE); return 0; } で、 ばいばいきーん g_string_erase, g_string_truncate あたりでも良いのかも !2006-05-16 Tue #include static void string_chomp(GString *str) { if (str->len >= 2) { if (*(str->str + str->len - 2) == '\r' && *(str->str + str->len - 1) == '\n') { g_string_set_size(str, str->len - 2); return; } } if (str->len >= 1 && (*(str->str + str->len - 1) == '\r' || *(str->str + str->len - 1) == '\n')) { g_string_set_size(str, str->len - 1); } } static void puts(GString *str) { guint i; for (i = 0; i < str->len; i++) { char c = *(str->str + i); if (c == '\r') { g_print("\\r"); } else if (c == '\n') { g_print("\\n"); } else { g_print("%c", c); } } g_print("\n"); } int main() { GString *str = g_string_new("abc\ndef\n"); string_chomp(str); puts(str); g_string_free(str, TRUE); str = g_string_new("abcr\rdef\r"); string_chomp(str); puts(str); g_string_free(str, TRUE); str = g_string_new("abcr\r\ndef\r\n"); string_chomp(str); puts(str); g_string_free(str, TRUE); str = g_string_new("abcr\n\rdef\n\r"); string_chomp(str); puts(str); g_string_free(str, TRUE); str = g_string_new(""); string_chomp(str); puts(str); g_string_free(str, TRUE); str = g_string_new("a"); string_chomp(str); puts(str); g_string_free(str, TRUE); str = g_string_new("\n"); string_chomp(str); puts(str); g_string_free(str, TRUE); str = g_string_new("\r"); string_chomp(str); puts(str); g_string_free(str, TRUE); str = g_string_new("\r\n"); string_chomp(str); puts(str); g_string_free(str, TRUE); str = g_string_new("\n\r"); string_chomp(str); puts(str); g_string_free(str, TRUE); return 0; } で、 abc\ndef abcr\rdef abcr\r\ndef abcr\n\rdef\n a \n !2006-05-15 Mon #include static GString *string_rjust(GString *str, guint width, GString *padding) { guint i; guint len = str->len; GString *new_str = g_string_sized_new(width > len ? width : len); g_string_assign(new_str, str->str); if (width > len) { guint insert_len; insert_len = width - len; for (i = 0; i < insert_len; i += padding->len) g_string_insert_len(new_str, i, padding->str, i + padding->len <= insert_len ? padding->len : insert_len - i); } return new_str; } int main() { GString *str1 = g_string_new("foo"); GString *str2 = g_string_new("*"); GString *str3; str3 = string_rjust(str1, 10, str2); g_print("%s*\n", str3->str); g_string_free(str3, TRUE); str3 = string_rjust(str1, 11, str2); g_print("%s*\n", str3->str); g_string_free(str3, TRUE); g_string_free(str2, TRUE); str2 = g_string_new("123"); str3 = string_rjust(str1, 10, str2); g_print("%s*\n", str3->str); g_string_free(str3, TRUE); str3 = string_rjust(str1, 11, str2); g_print("%s*\n", str3->str); g_string_free(str3, TRUE); str3 = string_rjust(str1, 12, str2); g_print("%s*\n", str3->str); g_string_free(str1, TRUE); g_string_free(str2, TRUE); g_string_free(str3, TRUE); return 0; } で、 *******foo* ********foo* 1231231foo* 12312312foo* 123123123foo* !2006-05-14 Sun #include static GString *string_ljust(GString *str, guint width, GString *padding) { guint i; guint len = str->len; GString *new_str = g_string_sized_new(width > len ? width : len); g_string_assign(new_str, str->str); if (width > len) { guint insert_len; insert_len = width - len; for (i = 0; i < insert_len; i += padding->len) g_string_append_len(new_str, padding->str, i + padding->len <= insert_len ? padding->len : insert_len - i); } return new_str; } int main() { GString *str1 = g_string_new("foo"); GString *str2 = g_string_new("*"); GString *str3; str3 = string_ljust(str1, 10, str2); g_print("%s*\n", str3->str); g_string_free(str3, TRUE); str3 = string_ljust(str1, 11, str2); g_print("%s*\n", str3->str); g_string_free(str3, TRUE); g_string_free(str2, TRUE); str2 = g_string_new("123"); str3 = string_ljust(str1, 10, str2); g_print("%s*\n", str3->str); g_string_free(str3, TRUE); str3 = string_ljust(str1, 11, str2); g_print("%s*\n", str3->str); g_string_free(str3, TRUE); str3 = string_ljust(str1, 12, str2); g_print("%s*\n", str3->str); g_string_free(str1, TRUE); g_string_free(str2, TRUE); g_string_free(str3, TRUE); return 0; } で、 foo******** foo********* foo1231231* foo12312312* foo123123123* !2006-05-13 Sat #include static GString *string_center(GString *str, guint width, GString *padding) { guint i; guint len = str->len; GString *new_str = g_string_sized_new(width > len ? width : len); g_string_assign(new_str, str->str); if (width > len) { guint insert_len; insert_len = (width - len) / 2; for (i = 0; i < insert_len; i += padding->len) g_string_insert_len(new_str, i, padding->str, i + padding->len <= insert_len ? padding->len : insert_len - i); insert_len = (width - len + 1) / 2; for (i = 0; i < insert_len; i += padding->len) g_string_append_len(new_str, padding->str, i + padding->len <= insert_len ? padding->len : insert_len - i); } return new_str; } int main() { GString *str1 = g_string_new("foo"); GString *str2 = g_string_new("*"); GString *str3; str3 = string_center(str1, 10, str2); g_print("%s*\n", str3->str); g_string_free(str3, TRUE); str3 = string_center(str1, 11, str2); g_print("%s*\n", str3->str); g_string_free(str3, TRUE); g_string_free(str2, TRUE); str2 = g_string_new("123"); str3 = string_center(str1, 10, str2); g_print("%s*\n", str3->str); g_string_free(str3, TRUE); str3 = string_center(str1, 11, str2); g_print("%s*\n", str3->str); g_string_free(str1, TRUE); g_string_free(str2, TRUE); g_string_free(str3, TRUE); return 0; } で、 ***foo***** ****foo***** 123foo1231* 1231foo1231* !2006-05-12 Fri #include static GString *string_rjust(GString *str, guint width) { guint i; guint len = str->len; GString *new_str = g_string_sized_new(width > len ? width : len); g_string_assign(new_str, str->str); if (width > len) { for (i = 0; i < width - len; i++) g_string_prepend_c(new_str, ' '); } return new_str; } int main() { GString *str1 = g_string_new("foo"); GString *str2 = string_rjust(str1, 10); g_print("%s*\n", str1->str); g_print("%s*\n", str2->str); g_string_free(str1, TRUE); g_string_free(str2, TRUE); return 0; } で、 foo* foo* !2006-05-11 Thu #include static GString *string_ljust(GString *str, guint width) { guint i; guint len = str->len; GString *new_str = g_string_sized_new(width > len ? width : len); g_string_assign(new_str, str->str); if (width > len) { for (i = 0; i < width - len; i++) g_string_append_c(new_str, ' '); } return new_str; } int main() { GString *str1 = g_string_new("foo"); GString *str2 = string_ljust(str1, 10); g_print("%s*\n", str1->str); g_print("%s*\n", str2->str); g_string_free(str1, TRUE); g_string_free(str2, TRUE); return 0; } で、 foo* foo * !2006-05-10 Wed #include static GString *string_center(GString *str, guint width) { guint i; guint len = str->len; GString *new_str = g_string_sized_new(width > len ? width : len); g_string_assign(new_str, str->str); if (width > len) { for (i = 0; i < (width - len) / 2; i++) g_string_prepend_c(new_str, ' '); for (i = 0; i < (width - len + 1) / 2; i++) g_string_append_c(new_str, ' '); } return new_str; } int main() { GString *str1 = g_string_new("foo"); GString *str2 = string_center(str1, 10); g_print("%s*\n", str1->str); g_print("%s*\n", str2->str); g_string_free(str2, TRUE); str2 = string_center(str1, 11); g_print("%s*\n", str2->str); g_string_free(str2, TRUE); g_string_free(str1, TRUE); return 0; } で、 foo* foo * foo * !2006-05-09 Tue #include int string_casecmp(GString *str1, GString *str2) { return g_ascii_strcasecmp(str1->str, str2->str); } int main() { GString *str1 = g_string_new("foo"); GString *str2 = g_string_new("foo"); g_print("%d\n", string_casecmp(str1, str2)); g_string_free(str2, TRUE); str2 = g_string_new("Foo"); g_print("%d\n", string_casecmp(str1, str2)); g_string_free(str1, TRUE); g_string_free(str2, TRUE); return 0; } で、 0 0 !2006-05-08 Mon #include static void string_capitalize_bang(GString *str) { guint i; if (str->len > 0) { *(str->str) = g_ascii_toupper(*(str->str)); } for (i = 1; i < str->len; i++) { *(str->str + i) = g_ascii_tolower(*(str->str + i)); } } int main() { GString *str1 = g_string_new("foo"); string_capitalize_bang(str1); g_print("%s\n", str1->str); g_string_free(str1, TRUE); str1 = g_string_new("Foo"); string_capitalize_bang(str1); g_print("%s\n", str1->str); g_string_free(str1, TRUE); str1 = g_string_new("FOO"); string_capitalize_bang(str1); g_print("%s\n", str1->str); g_string_free(str1, TRUE); return 0; } で、 Foo Foo Foo !2006-05-07 Sun #include static GString *string_capitalize(GString *str) { guint i; GString *new_str = g_string_new(""); if (str->len > 0) { g_string_append_c(new_str, g_ascii_toupper(*(str->str))); } for (i = 1; i < str->len; i++) { g_string_append_c(new_str, g_ascii_tolower(*(str->str + i))); } return new_str; } int main() { GString *str1 = g_string_new("foo"); GString *str2 = string_capitalize(str1); g_print("%s %s\n", str1->str, str2->str); g_string_free(str1, TRUE); g_string_free(str2, TRUE); str1 = g_string_new("Foo"); str2 = string_capitalize(str1); g_print("%s %s\n", str1->str, str2->str); g_string_free(str1, TRUE); g_string_free(str2, TRUE); str1 = g_string_new("FOO"); str2 = string_capitalize(str1); g_print("%s %s\n", str1->str, str2->str); g_string_free(str1, TRUE); g_string_free(str2, TRUE); return 0; } で、 foo Foo Foo Foo FOO Foo !2006-05-06 Sat #include #include int string_cmp(GString *str1, GString *str2) { return strcmp(str1->str, str2->str); } int main() { GString *str1 = g_string_new("foo"); GString *str2 = g_string_new("foo"); g_print("%d\n", string_cmp(str1, str2)); g_string_free(str2, TRUE); str2 = g_string_new("bar"); g_print("%d\n", string_cmp(str1, str2)); g_string_free(str2, TRUE); str2 = g_string_new("hoge"); g_print("%d\n", string_cmp(str1, str2)); g_string_free(str1, TRUE); g_string_free(str2, TRUE); return 0; } で、 0 4 -2 !2006-05-05 Fri #include static void string_aset(GString *str, guint first, guint last, GString *val) { if (first < str->len && last < str->len) { g_string_erase(str, first, last - first + 1); g_string_insert(str, first, val->str); } } int main() { GString *str = g_string_new("bar baz"); GString *str2; str2 = g_string_new("AAA"); string_aset(str, 0, 1, str2); g_string_free(str2, TRUE); g_print("%s\n", str->str); g_string_free(str, TRUE); return 0; } で、 AAAar baz !2006-05-04 Thu #include static void string_aset(GString *str, GString *substr, GString *val) { gchar *p; p = g_strstr_len(str->str, str->len, substr->str); if (p) { guint nth = p - str->str; g_string_erase(str, nth, substr->len); g_string_insert(str, nth, val->str); } else { /* ??? */ } } int main() { GString *str = g_string_new("foo bar"); GString *str2, *str3; str2 = g_string_new("ba"); str3 = g_string_new("AAA"); string_aset(str, str2, str3); g_print("%s\n", str->str); g_string_free(str, TRUE); g_string_free(str2, TRUE); g_string_free(str3, TRUE); return 0; } で、 foo AAAr !2006-05-03 Wed #include static void string_aset(GString *str, guint nth, guint len, GString *val) { if (nth < str->len) { g_string_erase(str, nth, len); g_string_insert(str, nth, val->str); } } int main() { GString *str = g_string_new("foo"); GString *str2; str2 = g_string_new("AAA"); string_aset(str, 0, 2, str2); g_string_free(str2, TRUE); g_print("%s\n", str->str); g_string_free(str, TRUE); return 0; } で、 AAAo !2006-05-02 Tue #include static void string_aset(GString *str, guint nth, GString *val) { if (nth < str->len) { g_string_erase(str, nth, 1); g_string_insert(str, nth, val->str); } } int main() { GString *str = g_string_new("foo"); GString *str2; str2 = g_string_new("AAA"); string_aset(str, 0, str2); g_string_free(str2, TRUE); g_print("%s\n", str->str); str2 = g_string_new("BBB"); string_aset(str, 3, str2); g_string_free(str2, TRUE); g_print("%s\n", str->str); g_string_free(str, TRUE); return 0; } で、 AAAoo AAABBBo !2006-05-01 Mon #include static GString *string_aref(GString *str, guint start, guint end) { return g_string_new_len(str->str + start, end - start + 1); } int main() { GString *str = g_string_new("foo"); GString *str2; str2 = string_aref(str, 0, 0); g_print("%s\n", str2->str); g_string_free(str2, TRUE); str2 = string_aref(str, 0, 1); g_print("%s\n", str2->str); g_string_free(str2, TRUE); str2 = string_aref(str, 0, 2); g_print("%s\n", str2->str); g_string_free(str2, TRUE); str2 = string_aref(str, 1, 2); g_print("%s\n", str2->str); g_string_free(str2, TRUE); g_string_free(str, TRUE); return 0; } で、 f fo foo oo !2006-04-30 Sun #include static GString *string_aref(GString *str, guint nth, guint len) { return g_string_new_len(str->str + nth, len); } int main() { GString *str = g_string_new("foo"); GString *str2; str2 = string_aref(str, 0, 0); g_print("%s\n", str2->str); g_string_free(str2, TRUE); str2 = string_aref(str, 0, 1); g_print("%s\n", str2->str); g_string_free(str2, TRUE); str2 = string_aref(str, 0, 2); g_print("%s\n", str2->str); g_string_free(str2, TRUE); str2 = string_aref(str, 1, 2); g_print("%s\n", str2->str); g_string_free(str2, TRUE); g_string_free(str, TRUE); return 0; } で、 f fo oo !2006-04-29 Sat #include static int string_aref(GString *str, guint nth) { return str->str[nth]; } int main() { GString *str = g_string_new("foo"); g_print("%d\n", string_aref(str, 0)); g_print("%d\n", string_aref(str, 1)); g_print("%d\n", string_aref(str, 2)); g_print("%d\n", string_aref(str, 3)); g_string_free(str, TRUE); return 0; } で、 102 111 111 0 !2006-04-28 Fri #include static void string_concat(GString *str, GString *str2) { g_string_append(str, str2->str); } int main() { GString *str1 = g_string_new("foo"); GString *str2 = g_string_new(" bar"); string_concat(str1, str2); g_print("%s\n", str1->str); g_print("%s\n", str2->str); g_string_free(str1, TRUE); g_string_free(str2, TRUE); return 0; } で、 foo bar bar !2006-04-27 Thu #include #include int string_gt(GString *str1, GString *str2) { return strcmp(str1->str, str2->str) > 0; } int main() { GString *str1 = g_string_new("foo"); GString *str2 = g_string_new("foo"); g_print("%d\n", string_gt(str1, str2)); g_string_free(str2, TRUE); str2 = g_string_new("bar"); g_print("%d\n", string_gt(str1, str2)); g_string_free(str1, TRUE); g_string_free(str2, TRUE); return 0; } で、 0 1 g_ascii_strcasecmp は見つかったけど、 casecmp でないのが見当たらなかったので、strcmp を使っておいた。 !2006-04-26 Wed #include int string_equal(GString *str1, GString *str2) { return g_string_equal(str1, str2); } int main() { GString *str1 = g_string_new("foo"); GString *str2 = g_string_new("foo"); g_print("%d\n", string_equal(str1, str2)); g_string_free(str2, TRUE); str2 = g_string_new("bar"); g_print("%d\n", string_equal(str1, str2)); g_string_free(str1, TRUE); g_string_free(str2, TRUE); return 0; } で、 1 0 !2006-04-25 Tue #include int main() { GString *str = g_string_new(""); g_string_printf(str, "%s %d\n", "foo", 1); g_print("%s", str->str); g_string_free(str, TRUE); return 0; } で、 foo 1 !2006-04-24 Mon #include static GString *string_times(GString *str, int times) { int i; GString *new_str = g_string_new(""); for (i = 0; i < times; i++) { g_string_append(new_str, str->str); } return new_str; } int main() { GString *str1 = g_string_new("foo"); GString *str2 = string_times(str1, 3); g_print("%s\n", str1->str); g_print("%s\n", str2->str); g_string_free(str1, TRUE); g_string_free(str2, TRUE); return 0; } で、 foo foofoofoo !2006-04-23 Sun #include static GString *string_plus(GString *str, GString *str2) { GString *new_str = g_string_new(str->str); return g_string_append(new_str, str2->str); } int main() { GString *str1 = g_string_new("foo"); GString *str2 = g_string_new(" bar"); GString *str3 = string_plus(str1, str2); g_print("%s\n", str1->str); g_print("%s\n", str2->str); g_print("%s\n", str3->str); g_string_free(str1, TRUE); g_string_free(str2, TRUE); g_string_free(str3, TRUE); return 0; } で、 foo bar foo bar !2006-04-22 Sat #include static GString *string_new(gchar *str) { return g_string_new(str); } int main() { GString *str = string_new("foo"); g_print("%s\n", str->str); g_string_free(str, TRUE); return 0; } で、 foo !2006-04-21 Fri #include static GPtrArray *list_zip(GArray *ary, GPtrArray *aary) { guint i, j; GArray *new_ary; GPtrArray *new_aary; new_aary = g_ptr_array_new(); for (i = 0; i < ary->len; i++) { int val = g_array_index(ary, gint, i); new_ary = g_array_new(FALSE, FALSE, sizeof(gint)); g_array_append_val(new_ary, val); for (j = 0; j < aary->len; j++) { GArray *val_ary = g_ptr_array_index(aary, j); if (i >= val_ary->len) { val = 0; g_array_append_val(new_ary, val); } else { val = g_array_index(val_ary, gint, i); g_array_append_val(new_ary, val); } } g_ptr_array_add(new_aary, new_ary); } return new_aary; } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]"); } static void print_llist(GPtrArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(",\n"); print_list(g_ptr_array_index(ary, i)); } g_print("]\n"); } static void free_llist(GPtrArray *ary) { guint i; for (i = 0; i < ary->len; i++) { g_array_free(g_ptr_array_index(ary, i), FALSE); } g_ptr_array_free(ary, FALSE); } int main() { int i; GPtrArray *aary, *aary2; GArray *ary1, *ary2; aary = g_ptr_array_new(); ary1 = g_array_new(FALSE, FALSE, sizeof(gint)); i = 1; g_array_append_val(ary1, i); i = 2; g_array_append_val(ary1, i); i = 3; g_array_append_val(ary1, i); ary2 = g_array_new(FALSE, FALSE, sizeof(gint)); i = 4; g_array_append_val(ary2, i); i = 5; g_array_append_val(ary2, i); i = 6; g_array_append_val(ary2, i); g_ptr_array_add(aary, ary2); ary2 = g_array_new(FALSE, FALSE, sizeof(gint)); i = 7; g_array_append_val(ary2, i); i = 8; g_array_append_val(ary2, i); i = 9; g_array_append_val(ary2, i); g_ptr_array_add(aary, ary2); aary2 = list_zip(ary1, aary); print_llist(aary2); g_array_free(ary1, FALSE); free_llist(aary); free_llist(aary2); return 0; } で、 [[1, 4, 7], [2, 5, 8], [3, 6, 9]] !2006-04-20 Thu #include static int func(int x) { return x % 2 == 0; } static GArray *list_reject(GArray *ary, int (*pfunc)(int)) { guint i; GArray *new_ary; new_ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < ary->len; i++) { int val = g_array_index(ary, gint, i); if (!(*pfunc)(val)) { g_array_append_val(new_ary, val); } } return new_ary; } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary, *ary2; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 1; i < 4; i++) g_array_append_val(ary, i); ary2 = list_reject(ary, func); print_list(ary2); g_array_free(ary, FALSE); g_array_free(ary2, FALSE); return 0; } で、 [1, 3] !2006-04-19 Wed #include static int func(int x) { return x % 3 == 0; } static GPtrArray *list_partition(GArray *ary, int (*pfunc)(int)) { GPtrArray *new_aary; GArray *new_ary_t, *new_ary_f; guint i; new_aary = g_ptr_array_new(); new_ary_t = g_array_new(FALSE, FALSE, sizeof(gint)); new_ary_f = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < ary->len; i++) { int val = g_array_index(ary, gint, i); if ((*pfunc)(val)) { g_array_append_val(new_ary_t, val); } else { g_array_append_val(new_ary_f, val); } } g_ptr_array_add(new_aary, new_ary_t); g_ptr_array_add(new_aary, new_ary_f); return new_aary; } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]"); } static void print_llist(GPtrArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(",\n"); print_list(g_ptr_array_index(ary, i)); } g_print("]\n"); } static void free_llist(GPtrArray *ary) { guint i; for (i = 0; i < ary->len; i++) { g_array_free(g_ptr_array_index(ary, i), FALSE); } g_ptr_array_free(ary, FALSE); } int main() { GArray *ary; GPtrArray *aary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 10; i >= 0; i--) g_array_append_val(ary, i); aary = list_partition(ary, func); print_llist(aary); g_array_free(ary, FALSE); free_llist(aary); return 0; } で、 [[9, 6, 3, 0], [10, 8, 7, 5, 4, 2, 1]] !2006-04-18 Tue #include static int list_min(GArray *ary) { guint i; int first = TRUE; int min = -1; /* xxx */ for (i = 0; i < ary->len; i++) { int val = g_array_index(ary, gint, i); if (first) { min = val; first = FALSE; } else if (val < min) { min = val; } } return min; } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 1; i < 6; i++) g_array_append_val(ary, i); g_print("%d\n", list_min(ary)); g_array_free(ary, FALSE); return 0; } で、 1 !2006-04-17 Mon #include static int cmp(int a, int b) { if (a > b) { return 1; } else if (a < b) { return -1; } else { return 0; } } static int cmp2(int a, int b) { if (a > b) { return -1; } else if (a < b) { return 1; } else { return 0; } } static int list_max(GArray *ary, int (*pfunc)(int, int)) { guint i; int max = -1; /* xxx */ int prev_val; if (ary->len > 0) { max = g_array_index(ary, gint, 0); prev_val = g_array_index(ary, gint, 0); for (i = 1; i < ary->len; i++) { int val = g_array_index(ary, gint, i); if ((*pfunc)(val, prev_val) > 0) { max = val; } prev_val = val; } } return max; } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 1; i < 6; i++) g_array_append_val(ary, i); g_print("%d\n", list_max(ary, cmp)); g_print("%d\n", list_max(ary, cmp2)); g_array_free(ary, FALSE); return 0; } で、 5 1 !2006-04-16 Sun #include static int list_max(GArray *ary) { guint i; int first = TRUE; int max = -1; /* xxx */ for (i = 0; i < ary->len; i++) { int val = g_array_index(ary, gint, i); if (first) { max = val; first = FALSE; } else if (val > max) { max = val; } } return max; } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 1; i < 6; i++) g_array_append_val(ary, i); g_print("%d\n", list_max(ary)); g_array_free(ary, FALSE); return 0; } で、 5 !2006-04-15 Sat #include static int add(int result, int item) { return result + item; } static int list_inject(GArray *ary, int init, int (*pfunc)(int, int)) { guint i; int val = init; for (i = 0; i < ary->len; i++) { val = (*pfunc)(val, g_array_index(ary, gint, i)); } return val; } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 1; i < 6; i++) g_array_append_val(ary, i); g_print("%d\n", list_inject(ary, 0, add)); g_array_free(ary, FALSE); return 0; } で、 15 !2006-04-14 Fri #include static int func1(int x) { return x == 1; } static int func2(int x) { return x > 1; } static int func3(int x) { return x == 0; } static GArray *list_find_all(GArray *ary, int (*pfunc)(int)) { GArray *new_ary; guint i; new_ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < ary->len; i++) { int val = g_array_index(ary, gint, i); if ((*pfunc)(val)) { g_array_append_val(new_ary, val); } } return new_ary; } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary1, *ary2; int i; ary1 = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 1; i < 4; i++) g_array_append_val(ary1, i); for (i = 1; i < 4; i++) g_array_append_val(ary1, i); ary2 = list_find_all(ary1, func1); print_list(ary2); g_array_free(ary2, FALSE); ary2 = list_find_all(ary1, func2); print_list(ary2); g_array_free(ary2, FALSE); ary2 = list_find_all(ary1, func3); print_list(ary2); g_array_free(ary2, FALSE); g_array_free(ary1, FALSE); return 0; } で、 [1, 1] [2, 3, 2, 3] [] !2006-04-13 Thu #include static int func1(int x) { return x == 1; } static int func2(int x) { return x > 1; } static int func3(int x) { return x == 0; } static int list_find(GArray *ary, int (*pfunc)(int), int *ret) { guint i; for (i = 0; i < ary->len; i++) { int val = g_array_index(ary, gint, i); if ((*pfunc)(val)) { *ret = val; return TRUE; } } return FALSE; } int main() { GArray *ary; int i, ret; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 1; i < 4; i++) g_array_append_val(ary, i); for (i = 1; i < 4; i++) g_array_append_val(ary, i); g_print("%d %d\n", list_find(ary, func1, &ret), ret); g_print("%d %d\n", list_find(ary, func2, &ret), ret); g_print("%d %d\n", list_find(ary, func3, &ret), ret); g_array_free(ary, FALSE); return 0; } で、 1 1 1 2 0 2 !2006-04-12 Wed #include static void func(int x, int i) { g_print("%d %d\n", i, x); } static void list_each_with_index(GArray *ary, void (*pfunc)(int, int)) { guint i; for (i = 0; i < ary->len; i++) { (*pfunc)(g_array_index(ary, gint, i), i); } } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 10; i < 14; i++) g_array_append_val(ary, i); list_each_with_index(ary, func); g_array_free(ary, FALSE); return 0; } で、 0 10 1 11 2 12 3 13 !2006-04-11 Tue #include static int func1(int x) { return x > 0; } static int func2(int x) { return x < 0; } static int list_any(GArray *ary, int (*pfunc)(int)) { guint i; for (i = 0; i < ary->len; i++) { if ((*pfunc)(g_array_index(ary, gint, i))) return TRUE; } return FALSE; } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 1; i < 4; i++) g_array_append_val(ary, i); g_print("%d\n", list_any(ary, func1)); g_print("%d\n", list_any(ary, func2)); i = 0; g_array_append_val(ary, i); g_print("%d\n", list_any(ary, func1)); g_print("%d\n", list_any(ary, func2)); g_array_free(ary, FALSE); return 0; } で、 1 0 1 0 !2006-04-10 Mon #include static int func1(int x) { return x > 0; } static int func2(int x) { return x < 0; } static int list_all(GArray *ary, int (*pfunc)(int)) { guint i; for (i = 0; i < ary->len; i++) { if (!(*pfunc)(g_array_index(ary, gint, i))) return FALSE; } return TRUE; } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 1; i < 4; i++) g_array_append_val(ary, i); g_print("%d\n", list_all(ary, func1)); g_print("%d\n", list_all(ary, func2)); i = 0; g_array_append_val(ary, i); g_print("%d\n", list_all(ary, func1)); g_array_free(ary, FALSE); return 0; } で、 1 0 0 !2006-04-09 Sun #include static GArray *list_values_at(GArray *ary, GArray *indices) { guint i, x; GArray *new_ary; new_ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < indices->len; i++) { int index = g_array_index(indices, gint, i); if (index >= ary->len || index < 0) { x = 0; g_array_append_val(new_ary, x); } else { g_array_append_val(new_ary, g_array_index(ary, gint, index)); } } return new_ary; } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary1, *ary2, *ary3; int i; ary1 = g_array_new(FALSE, FALSE, sizeof(gint)); ary2 = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary1, i); i = 0; g_array_append_val(ary2, i); i = 3; g_array_append_val(ary2, i); i = 5; g_array_append_val(ary2, i); i = -1; g_array_append_val(ary2, i); ary3 = list_values_at(ary1, ary2); print_list(ary3); g_array_free(ary1, FALSE); g_array_free(ary2, FALSE); g_array_free(ary3, FALSE); return 0; } で、 [0, 3, 0, 0] !2006-04-08 Sat #include static void list_unshift(GArray *ary, GArray *val) { gint i; if (val->len == 0) return; for (i = val->len - 1; i >= 0; i--) { g_array_prepend_val(ary, g_array_index(val, gint, i)); } } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary1, *ary2; int i; ary1 = g_array_new(FALSE, FALSE, sizeof(gint)); ary2 = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary1, i); for (i = 10; i < 14; i++) g_array_append_val(ary2, i); list_unshift(ary1, ary2); print_list(ary1); g_array_free(ary1, FALSE); g_array_free(ary2, FALSE); return 0; } で、 [10, 11, 12, 13, 0, 1, 2, 3] !2006-04-07 Fri #include static void list_unshift(GArray *ary, int val) { g_array_prepend_val(ary, val); } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) list_unshift(ary, i); print_list(ary); g_array_free(ary, FALSE); return 0; } で、 [3, 2, 1, 0] !2006-04-06 Thu #include static GArray *list_uniq(GArray *ary) { guint i; GArray *new_ary; GHashTable *hash; gchar* ret; new_ary = g_array_new(FALSE, FALSE, sizeof(gint)); hash = g_hash_table_new(g_direct_hash, g_direct_equal); for (i = 0; i < ary->len; i++) { ret = g_hash_table_lookup(hash, GINT_TO_POINTER(g_array_index(ary, gint, i))); if (ret == NULL) { g_array_append_val(new_ary, g_array_index(ary, gint, i)); g_hash_table_insert(hash, GINT_TO_POINTER(g_array_index(ary, gint, i)), ""); } } return new_ary; } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { guint i; GArray *ary1, *ary2; ary1 = g_array_new(FALSE, FALSE, sizeof(gint)); i = 1; g_array_append_val(ary1, i); i = 1; g_array_append_val(ary1, i); i = 1; g_array_append_val(ary1, i); ary2 = list_uniq(ary1); print_list(ary2); g_array_free(ary1, FALSE); g_array_free(ary2, FALSE); ary1 = g_array_new(FALSE, FALSE, sizeof(gint)); i = 1; g_array_append_val(ary1, i); i = 4; g_array_append_val(ary1, i); i = 1; g_array_append_val(ary1, i); ary2 = list_uniq(ary1); print_list(ary2); g_array_free(ary1, FALSE); g_array_free(ary2, FALSE); ary1 = g_array_new(FALSE, FALSE, sizeof(gint)); i = 1; g_array_append_val(ary1, i); i = 3; g_array_append_val(ary1, i); i = 2; g_array_append_val(ary1, i); i = 2; g_array_append_val(ary1, i); i = 3; g_array_append_val(ary1, i); ary2 = list_uniq(ary1); print_list(ary2); g_array_free(ary1, FALSE); g_array_free(ary2, FALSE); return 0; } で、 [1] [1, 4] [1, 3, 2] !2006-04-05 Wed #include static GPtrArray *list_transpose(GPtrArray *aary) { GPtrArray *new_aary; GArray *new_ary; GArray *ary; guint i, j, size; new_aary = g_ptr_array_new(); ary = g_ptr_array_index(aary, 0); size = ary->len; for (i = 0; i < size; i++) { new_ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (j = 0; j < aary->len; j++) { ary = g_ptr_array_index(aary, j); if (i < ary->len) { g_array_append_val(new_ary, g_array_index(ary, gint, i)); } } g_ptr_array_add(new_aary, new_ary); } return new_aary; } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]"); } static void print_llist(GPtrArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(",\n"); print_list(g_ptr_array_index(ary, i)); } g_print("]\n"); } static void free_llist(GPtrArray *ary) { guint i; for (i = 0; i < ary->len; i++) { g_array_free(g_ptr_array_index(ary, i), FALSE); } g_ptr_array_free(ary, FALSE); } int main() { GPtrArray *aary, *aary2; GArray *ary; int i; aary = g_ptr_array_new(); ary = g_array_new(FALSE, FALSE, sizeof(gint)); i = 1; g_array_append_val(ary, i); i = 2; g_array_append_val(ary, i); g_ptr_array_add(aary, ary); ary = g_array_new(FALSE, FALSE, sizeof(gint)); i = 3; g_array_append_val(ary, i); i = 4; g_array_append_val(ary, i); g_ptr_array_add(aary, ary); ary = g_array_new(FALSE, FALSE, sizeof(gint)); i = 5; g_array_append_val(ary, i); i = 6; g_array_append_val(ary, i); g_ptr_array_add(aary, ary); aary2 = list_transpose(aary); print_llist(aary2); free_llist(aary); free_llist(aary2); g_array_free(ary, FALSE); return 0; } で、 [[1, 3, 5], [2, 4, 6]] !2006-04-04 Tue #include static int i_cmp(gpointer p, gpointer q) { if (*(gint *)p > *(gint *)q) { return 1; } else if (*(gint *)p < *(gint *)q) { return -1; } else { return 0; } } static GArray *list_sort(GArray *ary, int (*pfunc)(gpointer, gpointer)) { guint i; GArray *new_ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < ary->len; i++) { g_array_append_val(new_ary, g_array_index(ary, gint, i)); } g_array_sort(new_ary, (GCompareFunc)pfunc); return new_ary; } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary, *ary2; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); i = 3; g_array_append_val(ary, i); i = 1; g_array_append_val(ary, i); i = 10; g_array_append_val(ary, i); i = 5; g_array_append_val(ary, i); ary2 = list_sort(ary, i_cmp); print_list(ary); print_list(ary2); g_array_free(ary, FALSE); g_array_free(ary2, FALSE); return 0; } で、 [3, 1, 10, 5] [1, 3, 5, 10] !2006-04-03 Mon #include static int i_cmp(gpointer p, gpointer q) { if (*(gint *)p > *(gint *)q) { return 1; } else if (*(gint *)p < *(gint *)q) { return -1; } else { return 0; } } static GArray *list_sort(GArray *ary) { guint i; GArray *new_ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < ary->len; i++) { g_array_append_val(new_ary, g_array_index(ary, gint, i)); } g_array_sort(new_ary, (GCompareFunc)i_cmp); return new_ary; } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary, *ary2; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); i = 3; g_array_append_val(ary, i); i = 1; g_array_append_val(ary, i); i = 10; g_array_append_val(ary, i); i = 5; g_array_append_val(ary, i); ary2 = list_sort(ary); print_list(ary); print_list(ary2); g_array_free(ary, FALSE); g_array_free(ary2, FALSE); return 0; } で、 [3, 1, 10, 5] [1, 3, 5, 10] !2006-04-02 Sun #include static void list_slice_bang(GArray *ary, int start, int last) { gint i; for (i = last; i >= start; i--) { if (i >= 0 && i < ary->len) { g_array_remove_index(ary, i); } } } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 10; i++) g_array_append_val(ary, i); list_slice_bang(ary, 0, 0); print_list(ary); list_slice_bang(ary, 0, 1); print_list(ary); list_slice_bang(ary, 20, 30); print_list(ary); list_slice_bang(ary, 2, 3); print_list(ary); list_slice_bang(ary, 2, 30); print_list(ary); g_array_free(ary, FALSE); return 0; } で、 [1, 2, 3, 4, 5, 6, 7, 8, 9] [3, 4, 5, 6, 7, 8, 9] [3, 4, 5, 6, 7, 8, 9] [3, 4, 7, 8, 9] [3, 4] !2006-04-01 Sat #include static void list_slice_bang(GArray *ary, gint pos, guint len) { gint i; for (i = pos + len - 1; i >= pos; i--) { if (i >= 0 && i < ary->len) { g_array_remove_index(ary, i); } } } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 10; i++) g_array_append_val(ary, i); list_slice_bang(ary, 0, 0); print_list(ary); list_slice_bang(ary, 0, 1); print_list(ary); list_slice_bang(ary, 20, 10); print_list(ary); list_slice_bang(ary, 2, 3); print_list(ary); list_slice_bang(ary, 2, 30); print_list(ary); g_array_free(ary, FALSE); return 0; } で、 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 2, 6, 7, 8, 9] [1, 2] * guint pos だとダメだった * signed and unsigned のワーニングが出てしまうなあ〜 !2006-03-31 Fri #include static int list_shift(GArray *ary) { int ret; if (ary->len == 0) { return 0; /* ??? */ } else { ret = g_array_index(ary, gint, 0); g_array_remove_index(ary, 0); return ret; } } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); g_print("%d\n", list_shift(ary)); print_list(ary); g_array_free(ary, FALSE); return 0; } で、 0 [1, 2, 3] !2006-03-30 Thu #include static int func(int x) { return x > 0; } static int list_rindex(GArray *ary, int (*pfunc)(int)) { gint i; for (i = ary->len - 1; i >= 0; i--) { if ((*pfunc)(g_array_index(ary, gint, i))) return i; } return -1; } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); i = 1; g_array_append_val(ary, i); i = 0; g_array_append_val(ary, i); i = 0; g_array_append_val(ary, i); i = 1; g_array_append_val(ary, i); i = 0; g_array_append_val(ary, i); g_print("%d\n", list_rindex(ary, func)); g_array_free(ary, FALSE); return 0; } で、 3 !2006-03-29 Wed #include static int list_rindex(GArray *ary, int val) { gint i; for (i = ary->len - 1; i >= 0; i--) { if (g_array_index(ary, gint, i) == val) return i; } return -1; } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); i = 1; g_array_append_val(ary, i); i = 0; g_array_append_val(ary, i); i = 0; g_array_append_val(ary, i); i = 1; g_array_append_val(ary, i); i = 0; g_array_append_val(ary, i); g_print("%d\n", list_rindex(ary, 1)); g_array_free(ary, FALSE); ary = g_array_new(FALSE, FALSE, sizeof(gint)); i = 1; g_array_append_val(ary, i); i = 0; g_array_append_val(ary, i); i = 0; g_array_append_val(ary, i); i = 0; g_array_append_val(ary, i); i = 0; g_array_append_val(ary, i); g_print("%d\n", list_rindex(ary, 1)); g_array_free(ary, FALSE); ary = g_array_new(FALSE, FALSE, sizeof(gint)); i = 0; g_array_append_val(ary, i); i = 0; g_array_append_val(ary, i); i = 0; g_array_append_val(ary, i); i = 0; g_array_append_val(ary, i); i = 0; g_array_append_val(ary, i); g_print("%d\n", list_rindex(ary, 1)); g_array_free(ary, FALSE); return 0; } で、 3 0 -1 !2006-03-28 Tue #include static void func(int x) { g_print("%d\n", x); } static void list_reverse_each(GArray *ary, void (*pfunc)(int)) { gint i; for (i = ary->len - 1; i >= 0; i--) { (*pfunc)(g_array_index(ary, gint, i)); } } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); list_reverse_each(ary, func); g_array_free(ary, FALSE); return 0; } で、 3 2 1 0 !2006-03-27 Mon #include static void list_reverse_bang(GArray *ary) { guint i; int *x1, *x2, tmp; for (i = 0; i < ary->len / 2; i++) { x1 = &g_array_index(ary, gint, i); x2 = &g_array_index(ary, gint, ary->len - 1 - i); tmp = *x1; *x1 = *x2; *x2 = tmp; } } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); list_reverse_bang(ary); print_list(ary); i = 4; g_array_prepend_val(ary, i); list_reverse_bang(ary); print_list(ary); g_array_free(ary, FALSE); return 0; } で、 [3, 2, 1, 0] [0, 1, 2, 3, 4] !2006-03-26 Sun #include static GArray *list_reverse(GArray *ary) { GArray *new_ary = g_array_new(FALSE, FALSE, sizeof(gint)); gint i; for (i = ary->len - 1; i >= 0; i--) { g_array_append_val(new_ary, g_array_index(ary, gint, i)); } return new_ary; } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary1, *ary2; int i; ary1 = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary1, i); ary2 = list_reverse(ary1); print_list(ary1); print_list(ary2); g_array_free(ary1, FALSE); g_array_free(ary2, FALSE); return 0; } で、 [0, 1, 2, 3] [3, 2, 1, 0] !2006-03-25 Sat #include static GArray *list_rassoc(GPtrArray *aary, int val) { GArray *new_ary; guint i, j; new_ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < aary->len; i++) { GArray *ary = g_ptr_array_index(aary, i); if (ary->len < 2) continue; if (g_array_index(ary, gint, 1) == val) { for (j = 0; j < ary->len; j++) { g_array_append_val(new_ary, g_array_index(ary, gint, j)); } return new_ary; } } return new_ary; } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } static void free_llist(GPtrArray *ary) { guint i; for (i = 0; i < ary->len; i++) { g_array_free(g_ptr_array_index(ary, i), FALSE); } g_ptr_array_free(ary, FALSE); } int main() { GPtrArray *aary; GArray *ary, *ary2; int i; aary = g_ptr_array_new(); ary = g_array_new(FALSE, FALSE, sizeof(gint)); i = 15; g_array_append_val(ary, i); i = 1; g_array_append_val(ary, i); g_ptr_array_add(aary, ary); ary = g_array_new(FALSE, FALSE, sizeof(gint)); i = 25; g_array_append_val(ary, i); i = 2; g_array_append_val(ary, i); g_ptr_array_add(aary, ary); ary = g_array_new(FALSE, FALSE, sizeof(gint)); i = 35; g_array_append_val(ary, i); i = 3; g_array_append_val(ary, i); g_ptr_array_add(aary, ary); ary2 = list_rassoc(aary, 2); print_list(ary2); free_llist(aary); g_array_free(ary2, FALSE); return 0; } で、 [25, 2] !2006-03-24 Fri #include static void list_push(GArray *ary, GArray *val) { guint i; for (i = 0; i < val->len; i++) { g_array_append_val(ary, g_array_index(val, gint, i)); } } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary1, *ary2; int i; ary1 = g_array_new(FALSE, FALSE, sizeof(gint)); ary2 = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary1, i); for (i = 10; i < 14; i++) g_array_append_val(ary2, i); list_push(ary1, ary2); print_list(ary1); g_array_free(ary1, FALSE); g_array_free(ary2, FALSE); return 0; } で、 [0, 1, 2, 3, 10, 11, 12, 13] !2006-03-23 Thu #include static void list_push(GArray *ary, int val) { g_array_append_val(ary, val); } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) list_push(ary, i); print_list(ary); g_array_free(ary, FALSE); return 0; } で、 [0, 1, 2, 3] !2006-03-22 Wed #include static int list_pop(GArray *ary) { int ret; if (ary->len == 0) { return 0; /* ??? */ } else { ret = g_array_index(ary, gint, ary->len - 1); g_array_remove_index(ary, ary->len - 1); return ret; } } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); g_print("%d\n", list_pop(ary)); print_list(ary); g_array_free(ary, FALSE); return 0; } で、 3 [0, 1, 2] !2006-03-21 Tue #include static int list_length(GArray *ary) { return ary->len; } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); g_print("%d\n", list_length(ary)); g_array_free(ary, FALSE); return 0; } で、 4 !2006-03-20 Mon #include static GArray *list_last(GArray *ary, guint n) { GArray *new_ary; guint i, size; new_ary = g_array_new(FALSE, FALSE, sizeof(gint)); if (n >= ary->len) { for (i = 0; i < ary->len; i++) { g_array_append_val(new_ary, g_array_index(ary, gint, i)); } } else { size = ary->len; for (i = 0; i < ary->len; i++) { if (i >= size - n) { g_array_append_val(new_ary, g_array_index(ary, gint, i)); } } } return new_ary; } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary1, *ary2; int i; ary1 = g_array_new(FALSE, FALSE, sizeof(gint)); ary2 = list_last(ary1, 0); print_list(ary2); ary2 = list_last(ary1, 1); print_list(ary2); for (i = 0; i < 4; i++) g_array_append_val(ary1, i); ary2 = list_last(ary1, 0); print_list(ary2); g_array_free(ary2, FALSE); ary2 = list_last(ary1, 1); print_list(ary2); g_array_free(ary2, FALSE); ary2 = list_last(ary1, 2); print_list(ary2); g_array_free(ary2, FALSE); ary2 = list_last(ary1, 5); print_list(ary2); g_array_free(ary2, FALSE); g_array_free(ary1, FALSE); return 0; } で、 [] [] [] [3] [2, 3] [0, 1, 2, 3] !2006-03-19 Sun #include static int list_last(GArray *ary) { if (ary->len == 0) { /* ??? */ return 0; } else { return g_array_index(ary, gint, ary->len - 1); } } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); g_print("%d\n", list_last(ary)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); g_print("%d\n", list_last(ary)); g_array_free(ary, FALSE); return 0; } で、 0 3 !2006-03-18 Sat #include static GString *list_join(GPtrArray *ary, char *sep) { guint i; GString *str; str = g_string_new(""); for (i = 0; i < ary->len; i++) { if (i != 0) { g_string_append(str, sep); } g_string_append(str, (gchar *)g_ptr_array_index(ary, i)); } return str; } int main() { GPtrArray *ary; GString *str; ary = g_ptr_array_new(); g_ptr_array_add(ary, "0"); g_ptr_array_add(ary, "1"); g_ptr_array_add(ary, "2"); str = list_join(ary, ""); g_print("%s\n", str->str); g_string_free(str, TRUE); str = list_join(ary, " "); g_print("%s\n", str->str); g_string_free(str, TRUE); str = list_join(ary, ":"); g_print("%s\n", str->str); g_string_free(str, TRUE); g_ptr_array_free(ary, TRUE); return 0; } で、 012 0 1 2 0:1:2 !2006-03-17 Fri #include static void list_join(GPtrArray *ary, char *sep, GString **str) { guint i; *str = g_string_new(""); for (i = 0; i < ary->len; i++) { if (i != 0) { g_string_append(*str, sep); } g_string_append(*str, (gchar *)g_ptr_array_index(ary, i)); } } int main() { GPtrArray *ary; GString *str; ary = g_ptr_array_new(); g_ptr_array_add(ary, "0"); g_ptr_array_add(ary, "1"); g_ptr_array_add(ary, "2"); list_join(ary, "", &str); g_print("%s\n", str->str); g_string_free(str, TRUE); list_join(ary, " ", &str); g_print("%s\n", str->str); g_string_free(str, TRUE); list_join(ary, ":", &str); g_print("%s\n", str->str); g_string_free(str, TRUE); g_ptr_array_free(ary, TRUE); return 0; } で、 012 0 1 2 0:1:2 !2006-03-16 Thu #include static void list_insert(GArray *ary, guint n, GArray *val_ary) { guint i; if (n < ary->len) { for (i = 0; i < val_ary->len; i++) { g_array_insert_val(ary, n + i, g_array_index(val_ary, gint, i)); } } else { guint i, size; size = ary->len; for (i = 0; i < (n - size); i++) { int x = 0; g_array_append_val(ary, x); } for (i = 0; i < val_ary->len; i++) { g_array_insert_val(ary, n + i, g_array_index(val_ary, gint, i)); } } } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary1, *ary2; int i; ary1 = g_array_new(FALSE, FALSE, sizeof(gint)); ary2 = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary1, i); for (i = 10; i < 12; i++) g_array_append_val(ary2, i); list_insert(ary1, 1, ary2); print_list(ary1); list_insert(ary1, 8, ary2); print_list(ary1); g_array_free(ary1, FALSE); g_array_free(ary2, FALSE); return 0; } で、 [0, 10, 11, 1, 2, 3] [0, 10, 11, 1, 2, 3, 0, 0, 10, 11] !2006-03-15 Wed #include static void list_insert(GArray *ary, guint n, int val) { if (n < ary->len) { g_array_insert_val(ary, n, val); } else { guint i, size; size = ary->len; for (i = 0; i < (n - size); i++) { int x = 0; g_array_append_val(ary, x); } g_array_append_val(ary, val); } } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); list_insert(ary, 1, 10); print_list(ary); list_insert(ary, 6, 11); print_list(ary); g_array_free(ary, FALSE); return 0; } で、 [0, 10, 1, 2, 3] [0, 10, 1, 2, 3, 0, 11] !2006-03-14 Tue #include static int list_index(GArray *ary, int val) { guint i; for (i = 0; i < ary->len; i++) { if (g_array_index(ary, gint, i) == val) { return i; } } return -1; } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); g_print("%d\n", list_index(ary, 1)); g_print("%d\n", list_index(ary, 5)); g_array_free(ary, FALSE); return 0; } で、 1 -1 !2006-03-13 Mon #include static int list_include(GArray *ary, int val) { guint i; for (i = 0; i < ary->len; i++) { if (g_array_index(ary, gint, i) == val) { return TRUE; } } return FALSE; } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); g_print("%d\n", list_include(ary, 0)); g_print("%d\n", list_include(ary, 5)); g_array_free(ary, FALSE); return 0; } で、 1 0 !2006-03-12 Sun #include static int list_first(GArray *ary) { if (ary->len > 0) { return g_array_index(ary, gint, 0); } else { /* ... */ } } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); g_print("%d\n", list_first(ary)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); g_print("%d\n", list_first(ary)); g_array_free(ary, FALSE); return 0; } で、 134518800 0 !2006-03-11 Sat #include static void list_fill(GArray *ary, int val) { guint i; for (i = 0; i < ary->len; i++) { int *x; x = &g_array_index(ary, gint, i); *x = val; } } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); list_fill(ary, 3); print_list(ary); g_array_free(ary, FALSE); return 0; } で、 [3, 3, 3, 3] !2006-03-10 Fri #include static int list_empty(GArray *ary) { return ary->len == 0; } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); g_print("%d\n", list_empty(ary)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); g_print("%d\n", list_empty(ary)); g_array_free(ary, FALSE); return 0; } で、 1 0 !2006-03-09 Thu #include static void func(int x) { g_print("%d\n", x); } static void list_each_index(GArray *ary, void (*pfunc)(int)) { guint i; for (i = 0; i < ary->len; i++) { (*pfunc)(i); } } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 10; i < 14; i++) g_array_append_val(ary, i); list_each_index(ary, func); g_array_free(ary, FALSE); return 0; } で、 0 1 2 3 !2006-03-08 Wed #include static void func(int x) { g_print("%d\n", x); } static void list_each(GArray *ary, void (*pfunc)(int)) { guint i; for (i = 0; i < ary->len; i++) { (*pfunc)(g_array_index(ary, gint, i)); } } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); list_each(ary, func); g_array_free(ary, FALSE); return 0; } で、 0 1 2 3 !2006-03-07 Tue #include static int func(int x) { return x % 2 == 0; } static void list_delete_if(GArray *ary, int (*pfunc)(int)) { gint i; if (ary->len == 0) return; for (i = ary->len - 1; i >= 0; i--) { if ((*pfunc)(g_array_index(ary, gint, i))) { g_array_remove_index(ary, i); } } } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); for (i = 0; i < 4; i++) g_array_append_val(ary, i); list_delete_if(ary, func); print_list(ary); g_array_free(ary, FALSE); return 0; } で、 [1, 3, 1, 3] !2006-03-06 Mon #include static void list_delete_at(GArray *ary, guint n) { if (n >= ary->len) return; g_array_remove_index(ary, n); } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); list_delete_at(ary, 5); list_delete_at(ary, 0); print_list(ary); g_array_free(ary, FALSE); return 0; } で、 [1, 2, 3] !2006-03-05 Sun #include static void list_delete(GArray *ary, int val) { gint i; if (ary->len == 0) return; for (i = ary->len - 1; i >= 0; i--) { if (g_array_index(ary, gint, i) == val) { g_array_remove_index(ary, i); } } } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); for (i = 0; i < 4; i++) g_array_append_val(ary, i); list_delete(ary, 2); print_list(ary); g_array_free(ary, FALSE); return 0; } で、 [0, 1, 3, 0, 1, 3] !2006-03-04 Sat #include static void list_concat(GArray *ary1, GArray *ary2) { guint i; for (i = 0; i < ary2->len; i++) { g_array_append_val(ary1, g_array_index(ary2, gint, i)); } } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary1, *ary2; int i; ary1 = g_array_new(FALSE, FALSE, sizeof(gint)); ary2 = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary1, i); for (i = 4; i < 10; i++) g_array_append_val(ary2, i); list_concat(ary1, ary2); print_list(ary1); print_list(ary2); g_array_free(ary1, FALSE); g_array_free(ary2, FALSE); return 0; } で、 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [4, 5, 6, 7, 8, 9] !2006-03-03 Fri #include static int func(int x) { return x * 2; } static void list_map(GArray *ary, int (*pfunc)(int)) { guint i; for (i = 0; i < ary->len; i++) { int *x; x = &g_array_index(ary, gint, i); *x = (*pfunc)(*x); } } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); list_map(ary, func); print_list(ary); g_array_free(ary, FALSE); return 0; } で、 [0, 2, 4, 6] !2006-03-02 Thu #include static GArray *list_dup(GArray *ary) { guint i; GArray *new_ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < ary->len; i++) { g_array_append_val(new_ary, g_array_index(ary, gint, i)); } return new_ary; } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary1, *ary2; int i; ary1 = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary1, i); ary2 = list_dup(ary1); print_list(ary1); g_array_free(ary1, FALSE); g_array_free(ary2, FALSE); return 0; } で、 [0, 1, 2, 3] !2006-03-01 Wed #include static void list_clear(GArray *ary) { gint i; for (i = ary->len - 1; i >= 0; i--) { g_array_remove_index(ary, i); } } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); list_clear(ary); print_list(ary); g_array_free(ary, FALSE); return 0; } で、 [] !2006-02-28 Tue #include static GArray *list_assoc(GPtrArray *aary, int val) { GArray *new_ary; guint i, j; new_ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < aary->len; i++) { int ok = FALSE; GArray *ary = g_ptr_array_index(aary, i); for (j = 0; j < ary->len; j++) { int aval = g_array_index(ary, gint, j); if (j == 0) { if (aval == val) { ok = TRUE; } else { break; } } g_array_append_val(new_ary, aval); } if (ok == TRUE) { return new_ary; } } return new_ary; } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } static void free_llist(GPtrArray *ary) { guint i; for (i = 0; i < ary->len; i++) { g_array_free(g_ptr_array_index(ary, i), FALSE); } g_ptr_array_free(ary, FALSE); } int main() { GPtrArray *aary; GArray *ary, *ary2; int i; aary = g_ptr_array_new(); ary = g_array_new(FALSE, FALSE, sizeof(gint)); i = 1; g_array_append_val(ary, i); i = 15; g_array_append_val(ary, i); g_ptr_array_add(aary, ary); ary = g_array_new(FALSE, FALSE, sizeof(gint)); i = 2; g_array_append_val(ary, i); i = 25; g_array_append_val(ary, i); g_ptr_array_add(aary, ary); ary = g_array_new(FALSE, FALSE, sizeof(gint)); i = 3; g_array_append_val(ary, i); i = 35; g_array_append_val(ary, i); g_ptr_array_add(aary, ary); ary2 = list_assoc(aary, 2); print_list(ary2); free_llist(aary); g_array_free(ary2, FALSE); return 0; } で、 [2, 25] !2006-02-27 Mon #include static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]"); } static void print_llist(GPtrArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(",\n"); print_list(g_ptr_array_index(ary, i)); } g_print("]\n"); } static void free_llist(GPtrArray *ary) { guint i; for (i = 0; i < ary->len; i++) { g_array_free(g_ptr_array_index(ary, i), FALSE); } g_ptr_array_free(ary, FALSE); } int main() { GPtrArray *aary; GArray *ary; int i; aary = g_ptr_array_new(); ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); g_ptr_array_add(aary, ary); ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 4; i < 8; i++) g_array_append_val(ary, i); g_ptr_array_add(aary, ary); ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 8; i < 12; i++) g_array_append_val(ary, i); g_ptr_array_add(aary, ary); print_llist(aary); free_llist(aary); return 0; } で、 [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]] !2006-02-26 Sun #include static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]"); } static void print_llist(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(",\n"); print_list(g_array_index(ary, gpointer, i)); } g_print("]\n"); } static void free_llist(GArray *ary) { guint i; for (i = 0; i < ary->len; i++) { g_array_free(g_array_index(ary, gpointer, i), FALSE); } g_array_free(ary, FALSE); } int main() { GArray *aary, *ary; int i; aary = g_array_new(FALSE, FALSE, sizeof(GArray*)); ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); g_array_append_val(aary, ary); ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 4; i < 8; i++) g_array_append_val(ary, i); g_array_append_val(aary, ary); ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 8; i < 12; i++) g_array_append_val(ary, i); g_array_append_val(aary, ary); print_llist(aary); free_llist(aary); return 0; } で、 [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]] !2006-02-25 Sat #include static int list_equal(GArray *ary1, GArray *ary2) { guint i; for (i = 0; i < ary1->len; i++) { int val1, val2; if (i >= ary2->len) return FALSE; val1 = g_array_index(ary1, gint, i); val2 = g_array_index(ary2, gint, i); if (val1 != val2) return FALSE; } if (ary1->len == ary2->len) { return TRUE; } else { return FALSE; } } int main() { GArray *ary1, *ary2; int i; ary1 = g_array_new(FALSE, FALSE, sizeof(gint)); ary2 = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary1, i); for (i = 0; i < 4; i++) g_array_append_val(ary2, i); g_print("%d\n", list_equal(ary1, ary2)); i = 4; g_array_append_val(ary1, i); i = 1; g_array_append_val(ary2, i); g_print("%d\n", list_equal(ary1, ary2)); g_array_remove_index(ary2, ary2->len - 1); g_print("%d\n", list_equal(ary1, ary2)); g_array_remove_index(ary1, ary1->len - 1); i = 1; g_array_append_val(ary2, i); g_print("%d\n", list_equal(ary1, ary2)); g_array_free(ary1, FALSE); g_array_free(ary2, FALSE); return 0; } で、 1 0 0 0 !2006-02-24 Fri #include static int list_cmp(GArray *ary1, GArray *ary2) { guint i; for (i = 0; i < ary1->len; i++) { int val1, val2; if (i >= ary2->len) return 1; val1 = g_array_index(ary1, gint, i); val2 = g_array_index(ary2, gint, i); if (val1 > val2) { return 1; } else if (val1 < val2) { return -1; } } if (ary1->len == ary2->len) { return 0; } else { return -1; } } int main() { GArray *ary1, *ary2; int i; ary1 = g_array_new(FALSE, FALSE, sizeof(gint)); ary2 = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary1, i); for (i = 0; i < 4; i++) g_array_append_val(ary2, i); g_print("%d\n", list_cmp(ary1, ary2)); i = 1; g_array_append_val(ary2, i); g_print("%d\n", list_cmp(ary1, ary2)); /* l2 length */ i = 4; g_array_append_val(ary1, i); g_print("%d\n", list_cmp(ary1, ary2)); /* l1 size */ g_array_remove_index(ary1, ary1->len - 1); g_array_remove_index(ary2, ary2->len - 1); i = 1; g_array_append_val(ary1, i); i = 4; g_array_append_val(ary2, i); g_print("%d\n", list_cmp(ary1, ary2)); /* l2 size */ g_array_remove_index(ary2, ary2->len - 1); g_print("%d\n", list_cmp(ary1, ary2)); /* l2 size */ g_array_free(ary1, FALSE); g_array_free(ary2, FALSE); return 0; } で、 0 -1 1 -1 1 !2006-02-23 Thu #include static void list_push(GArray *ary, int val) { g_array_append_val(ary, val); } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); list_push(ary, 10); print_list(ary); g_array_free(ary, FALSE); return 0; } で、 [0, 1, 2, 3, 10] !2006-02-22 Wed #include static void insert_val(GArray *new_ary, GHashTable *hash, GArray *ary) { guint i; for (i = 0; i < ary->len; i++) { gpointer data = GINT_TO_POINTER(g_array_index(ary, gint, i)); if (g_hash_table_lookup(hash, data) == NULL) { g_array_append_val(new_ary, g_array_index(ary, gint, i)); g_hash_table_insert(hash, data, GINT_TO_POINTER(TRUE)); } } } static GArray *list_or(GArray *ary1, GArray *ary2) { GHashTable *h; GArray *new_ary; new_ary = g_array_new(FALSE, FALSE, sizeof(gint)); h = g_hash_table_new(g_direct_hash, g_direct_equal); insert_val(new_ary, h, ary1); insert_val(new_ary, h, ary2); g_hash_table_destroy(h); return new_ary; } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary1, *ary2, *ary3; int i; ary1 = g_array_new(FALSE, FALSE, sizeof(gint)); ary2 = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 3; i++) g_array_append_val(ary1, i); for (i = 4; i > 0; i--) g_array_append_val(ary1, i); for (i = 2; i < 6; i++) g_array_append_val(ary2, i); ary3 = list_or(ary1, ary2); print_list(ary3); g_array_free(ary1, FALSE); g_array_free(ary2, FALSE); g_array_free(ary3, FALSE); return 0; } で、 [0, 1, 2, 4, 3, 5] !2006-02-21 Tue #include static void insert_hash(GHashTable *hash, GArray *ary) { guint i; for (i = 0; i < ary->len; i++) { gpointer data = GINT_TO_POINTER(g_array_index(ary, gint, i)); g_hash_table_insert(hash, data, GINT_TO_POINTER(TRUE)); } } static GArray *list_and(GArray *ary1, GArray *ary2) { GHashTable *h1, *h2; GArray *new_ary; guint i; new_ary = g_array_new(FALSE, FALSE, sizeof(gint)); h1 = g_hash_table_new(g_direct_hash, g_direct_equal); insert_hash(h1, ary1); h2 = g_hash_table_new(g_direct_hash, g_direct_equal); insert_hash(h2, ary2); for (i = 0; i < ary1->len; i++) { gpointer data = GINT_TO_POINTER(g_array_index(ary1, gint, i)); if (g_hash_table_lookup(h1, data) && g_hash_table_lookup(h2, data)) { g_array_append_val(new_ary, g_array_index(ary1, gint, i)); g_hash_table_remove(h1, data); } } g_hash_table_destroy(h1); g_hash_table_destroy(h2); return new_ary; } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary1, *ary2, *ary3; int i; ary1 = g_array_new(FALSE, FALSE, sizeof(gint)); ary2 = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 3; i++) g_array_append_val(ary1, i); for (i = 4; i > 0; i--) g_array_append_val(ary1, i); for (i = 2; i < 5; i++) g_array_append_val(ary2, i); ary3 = list_and(ary1, ary2); print_list(ary3); g_array_free(ary1, FALSE); g_array_free(ary2, FALSE); g_array_free(ary3, FALSE); return 0; } で、 [2, 4, 3] !2006-02-20 Mon #include static GArray *list_diff(GArray *ary1, GArray *ary2) { GArray *new_ary; guint i1, i2; new_ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i1 = 0; i1 < ary1->len; i1++) { int found = FALSE; int val1 = g_array_index(ary1, gint, i1); for (i2 = 0; i2 < ary2->len; i2++) { int val2 = g_array_index(ary2, gint, i2); if (val1 == val2) { found = TRUE; break; } } if (found == FALSE) { g_array_append_val(new_ary, val1); } } return new_ary; } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary1, *ary2, *ary3; int i; ary1 = g_array_new(FALSE, FALSE, sizeof(gint)); ary2 = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary1, i); for (i = 0; i < 4; i++) g_array_append_val(ary1, i); for (i = 3; i < 5; i++) g_array_append_val(ary2, i); ary3 = list_diff(ary1, ary2); print_list(ary3); g_array_free(ary1, FALSE); g_array_free(ary2, FALSE); g_array_free(ary3, FALSE); return 0; } で、 [0, 1, 2, 0, 1, 2] !2006-02-19 Sun #include static GArray *list_times(GArray *ary, guint times) { GArray *new_ary; guint i, j; new_ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < times; i++) { for (j = 0; j < ary->len; j++) { g_array_append_val(new_ary, g_array_index(ary, gint, j)); } } return new_ary; } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary1, *ary2; int i; ary1 = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary1, i); ary2 = list_times(ary1, 3); print_list(ary2); g_array_free(ary1, FALSE); g_array_free(ary2, FALSE); return 0; } で、 [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3] !2006-02-18 Sat #include static GArray *list_plus(GArray *ary1, GArray *ary2) { GArray *new_ary; guint i; new_ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < ary1->len; i++) { g_array_append_val(new_ary, g_array_index(ary1, gint, i)); } for (i = 0; i < ary2->len; i++) { g_array_append_val(new_ary, g_array_index(ary2, gint, i)); } return new_ary; } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary1, *ary2, *ary3; int i; ary1 = g_array_new(FALSE, FALSE, sizeof(gint)); ary2 = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary1, i); for (i = 10; i < 12; i++) g_array_append_val(ary2, i); ary3 = list_plus(ary1, ary2); print_list(ary3); g_array_free(ary1, FALSE); g_array_free(ary2, FALSE); g_array_free(ary3, FALSE); return 0; } で、 [0, 1, 2, 3, 10, 11] !2006-02-17 Fri #include static void list_aset(GArray *ary, guint start, guint end, GArray *vals) { guint i; if (start > end) return; if (start < ary->len) { for (i = end; i >= start; i--) { if (i >= ary->len) continue; g_array_remove_index(ary, i); if (i == 0) break; } } else { int x; for (i = ary->len; i < start; i++) { x = 0; g_array_append_val(ary, x); } } for (i = 0; i < vals->len; i++) { g_array_insert_val(ary, start + i, g_array_index(vals, gint, i)); } } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary1, *ary2; int i; ary1 = g_array_new(FALSE, FALSE, sizeof(gint)); ary2 = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 5; i++) g_array_append_val(ary1, i); for (i = 10; i < 12; i++) g_array_append_val(ary2, i); list_aset(ary1, 0, 1, ary2); print_list(ary1); list_aset(ary1, 1, 6, ary2); print_list(ary1); list_aset(ary1, 1, 1, ary2); print_list(ary1); list_aset(ary1, 10, 12, ary2); print_list(ary1); g_array_free(ary1, FALSE); g_array_free(ary2, FALSE); return 0; } で、 [10, 11, 2, 3, 4] [10, 10, 11] [10, 10, 11, 11] [10, 10, 11, 11, 0, 0, 0, 0, 0, 0, 10, 11] !2006-02-16 Thu #include static void list_aset(GArray *ary, guint start, guint end, int val) { guint i; if (start > end) return; if (start < ary->len) { int *x; x = &g_array_index(ary, gint, start); *x = val; for (i = end; i > start; i--) { if (i >= ary->len) continue; g_array_remove_index(ary, i); } } else { int x; for (i = ary->len; i < start; i++) { x = 0; g_array_append_val(ary, x); } g_array_append_val(ary, val); } } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 5; i++) g_array_append_val(ary, i); list_aset(ary, 0, 1, 10); print_list(ary); list_aset(ary, 1, 6, 20); print_list(ary); list_aset(ary, 1, 1, 30); print_list(ary); list_aset(ary, 10, 12, 40); print_list(ary); g_array_free(ary, FALSE); return 0; } で、 [10, 2, 3, 4] [10, 20] [10, 30] [10, 30, 0, 0, 0, 0, 0, 0, 0, 0, 40] サイズ外は、nil の代りに 0 埋めしてみた。 !2006-02-15 Wed #include static GArray *list_aref(GArray *ary, guint start, guint end) { guint i; GArray *new_ary; new_ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = start; i <= end; i++) { if (i >= ary->len) break; g_array_append_val(new_ary, g_array_index(ary, gint, i)); } return new_ary; } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary, *new_ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); new_ary = list_aref(ary, 0, 1); print_list(new_ary); g_array_free(new_ary, FALSE); new_ary = list_aref(ary, 1, 1); print_list(new_ary); g_array_free(new_ary, FALSE); new_ary = list_aref(ary, 1, 5); print_list(new_ary); g_array_free(new_ary, FALSE); g_array_free(ary, FALSE); return 0; } で、 [0, 1] [1] [1, 2, 3] !2006-02-14 Tue #include static void list_aset(GArray *ary, guint n, int val) { int *i; if (n >= ary->len) return; i = &g_array_index(ary, gint, n); *i = val; } static void print_list(GArray *ary) { guint i; g_print("["); for (i = 0; i < ary->len; i++) { if (i != 0) g_print(", "); g_print("%d", g_array_index(ary, gint, i)); } g_print("]\n"); } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); list_aset(ary, 0, 10); list_aset(ary, 1, 11); list_aset(ary, 2, 12); list_aset(ary, 5, 13); print_list(ary); g_array_free(ary, FALSE); return 0; } で、 [10, 11, 12, 3] !2006-02-13 Mon #include static int list_nth(GArray *ary, gint n) { return g_array_index(ary, gint, n); } int main() { GArray *ary; int i; ary = g_array_new(FALSE, FALSE, sizeof(gint)); for (i = 0; i < 4; i++) g_array_append_val(ary, i); g_print("%d\n", list_nth(ary, 0)); g_print("%d\n", list_nth(ary, 1)); g_print("%d\n", list_nth(ary, 3)); g_print("%d\n", list_nth(ary, 4)); g_array_free(ary, FALSE); return 0; } で、 0 1 3 0 !2006-02-12 Sun #include static GSList *list_zip(GSList *list, GSList *llist) { GSList *new_llist = NULL; GSList *new_list; GSList *next_list, *next_list2, *nth_list; int i; for (next_list = list, i = 0; next_list; next_list = g_slist_next(next_list), i++) { new_list = NULL; new_list = g_slist_append(new_list, next_list->data); for (next_list2 = llist; next_list2; next_list2 = g_slist_next(next_list2)) { nth_list = g_slist_nth(next_list2->data, i); new_list = g_slist_append(new_list, nth_list->data); } new_llist = g_slist_append(new_llist, new_list); } return new_llist; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]"); } static void print_llist(GSList *list) { GSList *next_list; g_print("["); for (next_list = list; next_list; next_list = g_slist_next(next_list)) { if (next_list != list) g_print(",\n"); print_list(next_list->data); } g_print("]\n"); } static void free_llist(GSList *list) { GSList *next_list; for (next_list = list; next_list; next_list = g_slist_next(next_list)) { g_slist_free(next_list->data); } g_slist_free(list); } int main() { GSList *llist = NULL; GSList *list = NULL; GSList *list2 = NULL; GSList *llist2; list2 = g_slist_append(list2, GINT_TO_POINTER(1)); list2 = g_slist_append(list2, GINT_TO_POINTER(2)); list2 = g_slist_append(list2, GINT_TO_POINTER(3)); list = g_slist_append(list, GINT_TO_POINTER(4)); list = g_slist_append(list, GINT_TO_POINTER(5)); list = g_slist_append(list, GINT_TO_POINTER(6)); llist = g_slist_append(llist, list); list = NULL; list = g_slist_append(list, GINT_TO_POINTER(7)); list = g_slist_append(list, GINT_TO_POINTER(8)); list = g_slist_append(list, GINT_TO_POINTER(9)); llist = g_slist_append(llist, list); llist2 = list_zip(list2, llist); print_llist(llist2); g_slist_free(list2); free_llist(llist); free_llist(llist2); return 0; } で、 [[1, 4, 7], [2, 5, 8], [3, 6, 9]] !2006-02-11 Sat #include static int func(int x) { return x % 2 == 0; } static GSList *list_reject(GSList *list, int (*pfunc)(int)) { GSList *new_list = NULL; GSList *next_list; for (next_list = list; next_list; next_list = g_slist_next(next_list)) { if (!(*pfunc)(GPOINTER_TO_INT(next_list->data))) { new_list = g_slist_append(new_list, next_list->data); } } return new_list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; GSList *list2; int i; for (i = 1; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); list2 = list_reject(list1, func); print_list(list2); g_slist_free(list1); g_slist_free(list2); return 0; } で、 [1, 3] !2006-02-10 Fri #include static int func(int x) { return x % 3 == 0; } static GSList *list_partition(GSList *list, int (*pfunc)(int)) { GSList *new_list_t = NULL; GSList *new_list_f = NULL; GSList *new_llist = NULL; GSList *next_list; for (next_list = list; next_list; next_list = g_slist_next(next_list)) { if ((*pfunc)(GPOINTER_TO_INT(next_list->data))) { new_list_t = g_slist_append(new_list_t, next_list->data); } else { new_list_f = g_slist_append(new_list_f, next_list->data); } } new_llist = g_slist_append(new_llist, new_list_t); new_llist = g_slist_append(new_llist, new_list_f); return new_llist; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]"); } static void print_llist(GSList *list) { GSList *next_list; g_print("["); for (next_list = list; next_list; next_list = g_slist_next(next_list)) { if (next_list != list) g_print(",\n"); print_list(next_list->data); } g_print("]\n"); } static void free_llist(GSList *list) { GSList *next_list; for (next_list = list; next_list; next_list = g_slist_next(next_list)) { g_slist_free(next_list->data); } g_slist_free(list); } int main() { GSList *list1 = NULL; GSList *llist; int i; for (i = 10; i >= 0; i--) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); llist = list_partition(list1, func); print_llist(llist); g_slist_free(list1); free_llist(llist); return 0; } で、 [[9, 6, 3, 0], [10, 8, 7, 5, 4, 2, 1]] !2006-02-09 Thu #include static GSList *list_min(GSList *list) { GSList *next_list; GSList *min_item = list; if (g_slist_length(list) > 1) { for (next_list = list; next_list; next_list = g_slist_next(next_list)) { if (GPOINTER_TO_INT(next_list->data) < GPOINTER_TO_INT(min_item->data)) { min_item = next_list; } } } return min_item; } int main() { GSList *list1 = NULL; GSList *list2; int i; list2 = list_min(list1); if (list2) { g_print("%d\n", GPOINTER_TO_INT(list2->data)); } else { g_print("array is empty\n"); } for (i = 0; i < 6; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); list2 = list_min(list1); if (list2) { g_print("%d\n", GPOINTER_TO_INT(list2->data)); } else { g_print("array is empty\n"); } g_slist_free(list1); return 0; } で、 array is empty 0 !2006-02-08 Wed #include static int cmp(int a, int b) { if (a > b) { return 1; } else if (a < b) { return -1; } else { return 0; } } static int cmp2(int a, int b) { if (a > b) { return -1; } else if (a < b) { return 1; } else { return 0; } } static GSList *list_max(GSList *list, int (*pfunc)(int, int)) { GSList *next_list; GSList *max_item = list; int prev_val; if (g_slist_length(list) > 1) { prev_val = GPOINTER_TO_INT(list->data); for (next_list = list; next_list; next_list = g_slist_next(next_list)) { int val = GPOINTER_TO_INT(next_list->data); if ((*pfunc)(val, prev_val) > 0) { max_item = next_list; } prev_val = val; } } return max_item; } int main() { GSList *list1 = NULL; GSList *list2; int i; for (i = 0; i < 6; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); list2 = list_max(list1, cmp); g_print("%d\n", GPOINTER_TO_INT(list2->data)); list2 = list_max(list1, cmp2); g_print("%d\n", GPOINTER_TO_INT(list2->data)); g_slist_free(list1); return 0; } で、 5 0 !2006-02-07 Tue #include static GSList *list_max(GSList *list) { GSList *next_list; GSList *max_item = list; if (g_slist_length(list) > 1) { for (next_list = list; next_list; next_list = g_slist_next(next_list)) { if (GPOINTER_TO_INT(next_list->data) > GPOINTER_TO_INT(max_item->data)) { max_item = next_list; } } } return max_item; } int main() { GSList *list1 = NULL; GSList *list2; int i; list2 = list_max(list1); if (list2) { g_print("%d\n", GPOINTER_TO_INT(list2->data)); } else { g_print("array is empty\n"); } for (i = 0; i < 6; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); list2 = list_max(list1); if (list2) { g_print("%d\n", GPOINTER_TO_INT(list2->data)); } else { g_print("array is empty\n"); } g_slist_free(list1); return 0; } で、 array is empty 5 !2006-02-06 Mon #include static int list_max(GSList *list) { GSList *next_list; int first = TRUE; int max; for (next_list = list; next_list; next_list = g_slist_next(next_list)) { int val = GPOINTER_TO_INT(next_list->data); if (first) { max = val; first = FALSE; } else if (val > max) { max = val; } } return max; } int main() { GSList *list1 = NULL; int i; for (i = 1; i < 6; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); g_print("%d\n", list_max(list1)); g_slist_free(list1); return 0; } で、 5 大きさが 0 だと、ダメだな…。 !2006-02-05 Sun #include static int add(int result, int item) { return result + item; } static int list_inject(GSList *list, int init, int (*pfunc)(int, int)) { GSList *next_list; int val = init; for (next_list = list; next_list; next_list = g_slist_next(next_list)) { val = (*pfunc)(val, GPOINTER_TO_INT(next_list->data)); } return val; } int main() { GSList *list1 = NULL; int i; for (i = 1; i < 6; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); g_print("%d\n", list_inject(list1, 0, add)); g_slist_free(list1); return 0; } で、 15 !2006-02-04 Sat #include static int func1(int x) { return x == 1; } static int func2(int x) { return x > 1; } static int func3(int x) { return x == 0; } static GSList *list_find_all(GSList *list, int (*pfunc)(int)) { GSList *new_list = NULL; GSList *next_list; for (next_list = list; next_list; next_list = g_slist_next(next_list)) { int val = GPOINTER_TO_INT(next_list->data); if ((*pfunc)(val)) { new_list = g_slist_append(new_list, next_list->data); } } return new_list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; GSList *list2; int i; for (i = 1; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); for (i = 1; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); list2 = list_find_all(list1, func1); print_list(list2); g_slist_free(list2); list2 = list_find_all(list1, func2); print_list(list2); g_slist_free(list2); list2 = list_find_all(list1, func3); print_list(list2); g_slist_free(list1); g_slist_free(list2); return 0; } で、 [1, 1] [2, 3, 2, 3] [] !2006-02-03 Fri #include static int func1(int x) { return x == 1; } static int func2(int x) { return x > 1; } static int func3(int x) { return x == 0; } static int list_find(GSList *list, int (*pfunc)(int), int *ret) { GSList *next_list; for (next_list = list; next_list; next_list = g_slist_next(next_list)) { int val = GPOINTER_TO_INT(next_list->data); if ((*pfunc)(val)) { *ret = val; return TRUE; } } return FALSE; } int main() { GSList *list1 = NULL; int i, ret; for (i = 1; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); for (i = 1; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); g_print("%d %d\n", list_find(list1, func1, &ret), ret); g_print("%d %d\n", list_find(list1, func2, &ret), ret); g_print("%d %d\n", list_find(list1, func3, &ret), ret); g_slist_free(list1); return 0; } で、 1 1 1 2 0 2 !2006-02-02 Thu #include static void func(int x, int i) { g_print("%d %d\n", i, x); } static void list_each_with_index(GSList *list, void (*pfunc)(int, int)) { GSList *next_list; int i = 0; for (next_list = list; next_list; next_list = g_slist_next(next_list)) { (*pfunc)(GPOINTER_TO_INT(next_list->data), i++); } } int main() { GSList *list = NULL; int i; for (i = 10; i < 14; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); list_each_with_index(list, func); g_slist_free(list); return 0; } で、 0 10 1 11 2 12 3 13 !2006-02-01 Wed #include static int func1(int x) { return x > 0; } static int func2(int x) { return x < 0; } static int list_any(GSList *list, int (*pfunc)(int)) { GSList *next_list; for (next_list = list; next_list; next_list = g_slist_next(next_list)) { if ((*pfunc)(GPOINTER_TO_INT(next_list->data))) return TRUE; } return FALSE; } int main() { GSList *list1 = NULL; int i; for (i = 1; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); g_print("%d\n", list_any(list1, func1)); g_print("%d\n", list_any(list1, func2)); list1 = g_slist_append(list1, GINT_TO_POINTER(0)); g_print("%d\n", list_any(list1, func1)); g_print("%d\n", list_any(list1, func2)); g_slist_free(list1); return 0; } で、 1 0 1 0 !2006-01-31 Tue #include static int func1(int x) { return x > 0; } static int func2(int x) { return x < 0; } static int list_all(GSList *list, int (*pfunc)(int)) { GSList *next_list; for (next_list = list; next_list; next_list = g_slist_next(next_list)) { if (!(*pfunc)(GPOINTER_TO_INT(next_list->data))) return FALSE; } return TRUE; } int main() { GSList *list1 = NULL; int i; for (i = 1; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); g_print("%d\n", list_all(list1, func1)); g_print("%d\n", list_all(list1, func2)); list1 = g_slist_append(list1, GINT_TO_POINTER(0)); g_print("%d\n", list_all(list1, func1)); g_slist_free(list1); return 0; } で、 1 0 0 !2006-01-30 Mon #include static GSList *list_values_at(GSList *list, GSList *indices) { GSList *new_list = NULL; GSList *next_list; int size; size = g_slist_length(list); for (next_list = indices; next_list; next_list = g_slist_next(next_list)) { int index = GPOINTER_TO_INT(next_list->data); if (index >= size || index < 0) { new_list = g_slist_append(new_list, GINT_TO_POINTER(0)); } else { new_list = g_slist_append(new_list, g_slist_nth_data(list, index)); } } return new_list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; GSList *list2 = NULL; GSList *list3; int i; for (i = 0; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); list2 = g_slist_append(list2, GINT_TO_POINTER(0)); list2 = g_slist_append(list2, GINT_TO_POINTER(3)); list2 = g_slist_append(list2, GINT_TO_POINTER(5)); list2 = g_slist_append(list2, GINT_TO_POINTER(-1)); list3 = list_values_at(list1, list2); print_list(list3); g_slist_free(list1); g_slist_free(list2); g_slist_free(list3); return 0; } で、 [0, 3, 0, 0] !2006-01-29 Sun #include static GSList *list_unshift(GSList *list, GSList *val) { GSList *dup_list = g_slist_copy(val); return g_slist_concat(dup_list, list); } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; GSList *list2 = NULL; int i; for (i = 0; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); for (i = 10; i < 14; i++) list2 = g_slist_append(list2, GINT_TO_POINTER(i)); list1 = list_unshift(list1, list2); print_list(list1); g_slist_free(list1); g_slist_free(list2); return 0; } で、 [10, 11, 12, 13, 0, 1, 2, 3] !2006-01-28 Sat 別解 #include static GSList *list_unshift(GSList *list, int val) { return g_slist_prepend(list, GINT_TO_POINTER(val)); } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; int i; for (i = 0; i < 4; i++) list1 = list_unshift(list1, i); print_list(list1); g_slist_free(list1); return 0; } で、 [3, 2, 1, 0] !2006-01-27 Fri #include static GSList *list_unshift(GSList *list, int val) { return g_slist_insert_before(list, list, GINT_TO_POINTER(val)); } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; int i; for (i = 0; i < 4; i++) list1 = list_unshift(list1, i); print_list(list1); g_slist_free(list1); return 0; } で、 [3, 2, 1, 0] !2006-01-26 Thu #include static GSList *list_uniq(GSList *list) { GSList *new_list = NULL; GSList *next_list; GHashTable *hash; gchar* ret; hash = g_hash_table_new(g_direct_hash, g_direct_equal); for (next_list = list; next_list; next_list = g_slist_next(next_list)) { ret = g_hash_table_lookup(hash, next_list->data); if (ret == NULL) { new_list = g_slist_append(new_list, next_list->data); g_hash_table_insert(hash, next_list->data, ""); } } return new_list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; GSList *list2; list1 = g_slist_append(list1, GINT_TO_POINTER(1)); list1 = g_slist_append(list1, GINT_TO_POINTER(1)); list1 = g_slist_append(list1, GINT_TO_POINTER(4)); list1 = g_slist_append(list1, GINT_TO_POINTER(1)); list2 = list_uniq(list1); print_list(list2); g_slist_free(list1); g_slist_free(list2); return 0; } で、 [1, 4] !2006-01-25 Wed #include static GSList *list_transpose(GSList *llist) { GSList *new_list = NULL; GSList *new_llist = NULL; GSList *next_list; GSList *nth_list; int i, size; size = g_slist_length(llist->data); for (i = 0; i < size; i++) { /* Not free new_list !! */ new_list = NULL; for (next_list = llist; next_list; next_list = g_slist_next(next_list)) { nth_list = g_slist_nth(next_list->data, i); if (nth_list) { new_list = g_slist_append(new_list, nth_list->data); } } new_llist = g_slist_append(new_llist, new_list); } return new_llist; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]"); } static void print_llist(GSList *list) { GSList *next_list; g_print("["); for (next_list = list; next_list; next_list = g_slist_next(next_list)) { if (next_list != list) g_print(",\n"); print_list(next_list->data); } g_print("]\n"); } static void free_llist(GSList *list) { GSList *next_list; for (next_list = list; next_list; next_list = g_slist_next(next_list)) { g_slist_free(next_list->data); } g_slist_free(list); } int main() { GSList *llist = NULL; GSList *list = NULL; GSList *llist2; list = g_slist_append(list, GINT_TO_POINTER(1)); list = g_slist_append(list, GINT_TO_POINTER(2)); llist = g_slist_append(llist, list); list = NULL; list = g_slist_append(list, GINT_TO_POINTER(3)); list = g_slist_append(list, GINT_TO_POINTER(4)); llist = g_slist_append(llist, list); list = NULL; list = g_slist_append(list, GINT_TO_POINTER(5)); list = g_slist_append(list, GINT_TO_POINTER(6)); llist = g_slist_append(llist, list); llist2 = list_transpose(llist); print_llist(llist2); free_llist(llist); free_llist(llist2); return 0; } で、 [[1, 3, 5], [2, 4, 6]] サイズはそろっていないとダメ !2006-01-24 Tue #include static int i_cmp(gpointer p, gpointer q) { if (GPOINTER_TO_INT(p) > GPOINTER_TO_INT(q)) { return -1; } else if (GPOINTER_TO_INT(p) < GPOINTER_TO_INT(q)) { return 1; } else { return 0; } } static GSList *list_sort(GSList *list, int (*pfunc)(gpointer, gpointer)) { GSList *dup_list = g_slist_copy(list); return g_slist_sort(dup_list, (GCompareFunc)pfunc); } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list = NULL; list = g_slist_append(list, GINT_TO_POINTER(3)); list = g_slist_append(list, GINT_TO_POINTER(1)); list = g_slist_append(list, GINT_TO_POINTER(10)); list = g_slist_append(list, GINT_TO_POINTER(5)); list = list_sort(list, i_cmp); print_list(list); g_slist_free(list); return 0; } で、 [10, 5, 3, 1] !2006-01-23 Mon #include static int i_cmp(gpointer p, gpointer q) { if (GPOINTER_TO_INT(p) > GPOINTER_TO_INT(q)) { return 1; } else if (GPOINTER_TO_INT(p) < GPOINTER_TO_INT(q)) { return -1; } else { return 0; } } static GSList *list_sort(GSList *list) { GSList *dup_list = g_slist_copy(list); return g_slist_sort(dup_list, (GCompareFunc)i_cmp); } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list = NULL; list = g_slist_append(list, GINT_TO_POINTER(3)); list = g_slist_append(list, GINT_TO_POINTER(1)); list = g_slist_append(list, GINT_TO_POINTER(10)); list = g_slist_append(list, GINT_TO_POINTER(5)); list = list_sort(list); print_list(list); g_slist_free(list); return 0; } で、 [1, 3, 5, 10] !2006-01-22 Sun #include static GSList *list_slice_bang(GSList *list, int start, int last) { GSList *next_list; GSList *next_list_tmp; int i = 0; for (next_list = list; next_list; next_list = next_list_tmp) { next_list_tmp = g_slist_next(next_list); if (i >= start && i <= last) { list = g_slist_delete_link(list, next_list); } i++; } return list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; int i; for (i = 0; i < 10; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); list1 = list_slice_bang(list1, 0, 0); print_list(list1); list1 = list_slice_bang(list1, 0, 1); print_list(list1); list1 = list_slice_bang(list1, 20, 30); print_list(list1); list1 = list_slice_bang(list1, 2, 3); print_list(list1); list1 = list_slice_bang(list1, 2, 30); print_list(list1); g_slist_free(list1); return 0; } で、 [1, 2, 3, 4, 5, 6, 7, 8, 9] [3, 4, 5, 6, 7, 8, 9] [3, 4, 5, 6, 7, 8, 9] [3, 4, 7, 8, 9] [3, 4] !2006-01-21 Sat #include static GSList *list_slice_bang(GSList *list, int pos, int len) { GSList *next_list; GSList *next_list_tmp; int i = 0; for (next_list = list; next_list; next_list = next_list_tmp) { next_list_tmp = g_slist_next(next_list); if (i >= pos && i < pos + len) { list = g_slist_delete_link(list, next_list); } i++; } return list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; int i; for (i = 0; i < 10; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); list1 = list_slice_bang(list1, 0, 0); print_list(list1); list1 = list_slice_bang(list1, 0, 1); print_list(list1); list1 = list_slice_bang(list1, 20, 10); print_list(list1); list1 = list_slice_bang(list1, 2, 3); print_list(list1); list1 = list_slice_bang(list1, 2, 30); print_list(list1); g_slist_free(list1); return 0; } で、 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 2, 6, 7, 8, 9] [1, 2] !2006-01-20 Fri shift した値も使えるように #include static int list_shift(GSList **list) { int ret = GPOINTER_TO_INT((*list)->data); *list = g_slist_delete_link(*list, *list); return ret; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; int i; for (i = 0; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); g_print("%d\n", list_shift(&list1)); print_list(list1); g_slist_free(list1); return 0; } で、 0 [1, 2, 3] !2006-01-19 Thu #include static GSList *list_shift(GSList *list) { list = g_slist_delete_link(list, list); return list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; GSList *list2; int i; for (i = 0; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); list2 = list_shift(list1); print_list(list2); g_slist_free(list2); g_slist_free(list1); return 0; } で、 [1, 2, 3] !2006-01-18 Wed #include static int func(int x) { return x > 0; } static int list_rindex(GSList *list, int (*pfunc)(int)) { GSList *next_list; GSList *dup_list = g_slist_copy(list); dup_list = g_slist_reverse(dup_list); int i = g_slist_length(list) - 1; for (next_list = dup_list; next_list; next_list = g_slist_next(next_list)) { if ((*pfunc)(GPOINTER_TO_INT(next_list->data))) return i; i--; } return -1; } int main() { GSList *list = NULL; list = g_slist_append(list, GINT_TO_POINTER(1)); list = g_slist_append(list, GINT_TO_POINTER(0)); list = g_slist_append(list, GINT_TO_POINTER(0)); list = g_slist_append(list, GINT_TO_POINTER(1)); list = g_slist_append(list, GINT_TO_POINTER(0)); g_print("%d\n", list_rindex(list, func)); g_slist_free(list); return 0; } で、 3 !2006-01-17 Tue #include static int list_rindex(GSList *list, int val) { GSList *next_list; GSList *dup_list = g_slist_copy(list); dup_list = g_slist_reverse(dup_list); int i = g_slist_length(list) - 1; for (next_list = dup_list; next_list; next_list = g_slist_next(next_list)) { if (GPOINTER_TO_INT(next_list->data) == val) return i; i--; } return -1; } int main() { GSList *list = NULL; list = g_slist_append(list, GINT_TO_POINTER(1)); list = g_slist_append(list, GINT_TO_POINTER(0)); list = g_slist_append(list, GINT_TO_POINTER(0)); list = g_slist_append(list, GINT_TO_POINTER(1)); list = g_slist_append(list, GINT_TO_POINTER(0)); g_print("%d\n", list_rindex(list, 1)); g_slist_free(list); list = NULL; list = g_slist_append(list, GINT_TO_POINTER(1)); list = g_slist_append(list, GINT_TO_POINTER(0)); list = g_slist_append(list, GINT_TO_POINTER(0)); list = g_slist_append(list, GINT_TO_POINTER(0)); list = g_slist_append(list, GINT_TO_POINTER(0)); g_print("%d\n", list_rindex(list, 1)); g_slist_free(list); list = NULL; list = g_slist_append(list, GINT_TO_POINTER(0)); list = g_slist_append(list, GINT_TO_POINTER(0)); list = g_slist_append(list, GINT_TO_POINTER(0)); list = g_slist_append(list, GINT_TO_POINTER(0)); list = g_slist_append(list, GINT_TO_POINTER(0)); g_print("%d\n", list_rindex(list, 1)); g_slist_free(list); return 0; } で、 3 0 -1 !2006-01-16 Mon #include static void func(gpointer data, gpointer ud) { g_print("%d\n", GPOINTER_TO_INT(data)); } static void list_reverse_each(GSList *list, void (*pfunc)(gpointer, gpointer)) { GSList *dup_list = g_slist_copy(list); dup_list = g_slist_reverse(dup_list); g_slist_foreach(dup_list, pfunc, NULL); } int main() { GSList *list = NULL; int i; for (i = 0; i < 4; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); list_reverse_each(list, func); g_slist_free(list); return 0; } で、 3 2 1 0 !2006-01-15 Sun #include static void func(int x) { g_print("%d\n", x); } static void list_reverse_each(GSList *list, void (*pfunc)(int)) { GSList *next_list; GSList *dup_list = g_slist_copy(list); dup_list = g_slist_reverse(dup_list); for (next_list = dup_list; next_list; next_list = g_slist_next(next_list)) { (*pfunc)(GPOINTER_TO_INT(next_list->data)); } } int main() { GSList *list = NULL; int i; for (i = 0; i < 4; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); list_reverse_each(list, func); g_slist_free(list); return 0; } で、 3 2 1 0 !2006-01-14 Sat #include static GSList *list_reverse_bang(GSList *list) { return g_slist_reverse(list); } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; GSList *list2; int i; for (i = 0; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); list2 = list_reverse_bang(list1); print_list(list1); print_list(list2); g_slist_free(list1); g_slist_free(list2); return 0; } で、 [0] [3, 2, 1, 0] !2006-01-13 Fri どうやら、list_clear を実装していなかったようだ。 どう実装するのが正しいのだろうか? replace も必要性を含めて良く分からないなあ。 なので、とばし。 #include static GSList *list_reverse(GSList *list) { list = g_slist_copy(list); return g_slist_reverse(list); } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; GSList *list2; int i; for (i = 0; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); list2 = list_reverse(list1); print_list(list1); print_list(list2); g_slist_free(list1); g_slist_free(list2); return 0; } で、 [0, 1, 2, 3] [3, 2, 1, 0] !2006-01-12 Thu #include static GSList *list_rassoc(GSList *llist, int val) { GSList *new_list = NULL; GSList *next_list1; GSList *next_list2; for (next_list1 = llist; next_list1; next_list1 = g_slist_next(next_list1)) { int ok = FALSE; int i = 0; for (next_list2 = next_list1->data; next_list2; next_list2 = g_slist_next(next_list2)) { if (i == 1) { if (GPOINTER_TO_INT(next_list2->data) == val) { ok = TRUE; break; } else { break; } } i++; } if (ok == TRUE) { new_list = g_slist_copy(next_list1->data); return new_list; } } return new_list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } static void free_llist(GSList *list) { GSList *next_list; for (next_list = list; next_list; next_list = g_slist_next(next_list)) { g_slist_free(next_list->data); } g_slist_free(list); } int main() { GSList *llist = NULL; GSList *list = NULL; GSList *list2; list = g_slist_append(list, GINT_TO_POINTER(15)); list = g_slist_append(list, GINT_TO_POINTER(1)); llist = g_slist_append(llist, list); list = NULL; list = g_slist_append(list, GINT_TO_POINTER(25)); list = g_slist_append(list, GINT_TO_POINTER(2)); llist = g_slist_append(llist, list); list = NULL; list = g_slist_append(list, GINT_TO_POINTER(35)); list = g_slist_append(list, GINT_TO_POINTER(3)); llist = g_slist_append(llist, list); list2 = list_rassoc(llist, 2); print_list(list2); g_slist_free(list2); free_llist(llist); return 0; } で、 [25, 2] !2006-01-11 Wed #include static GSList *list_push(GSList *list, GSList *val) { GSList *next_list; for (next_list = val; next_list; next_list = g_slist_next(next_list)) { list = g_slist_append(list, GINT_TO_POINTER(next_list->data)); } return list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; GSList *list2 = NULL; int i; for (i = 0; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); for (i = 10; i < 14; i++) list2 = g_slist_append(list2, GINT_TO_POINTER(i)); list1 = list_push(list1, list2); print_list(list1); g_slist_free(list1); g_slist_free(list2); return 0; } で、 [0, 1, 2, 3, 10, 11, 12, 13] !2006-01-10 Tue #include static GSList *list_push(GSList *list, int val) { list = g_slist_append(list, GINT_TO_POINTER(val)); return list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list = NULL; int i; for (i = 0; i < 4; i++) list = list_push(list, i); print_list(list); g_slist_free(list); return 0; } で、 [0, 1, 2, 3] 気がついていなかったが、既に 2005-12-13 に list_push を作っていたようだ !2006-01-09 Mon pop した値も使いたいよな。 ということで、こう変更してみた。 #include static int list_pop(GSList **list) { GSList *last_item = g_slist_last(*list); int ret = GPOINTER_TO_INT(last_item->data); *list = g_slist_delete_link(*list, last_item); return ret; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; int i; for (i = 0; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); g_print("%d\n", list_pop(&list1)); print_list(list1); g_slist_free(list1); return 0; } で、 3 [0, 1, 2] !2006-01-08 Sun #include static GSList *list_pop(GSList *list) { list = g_slist_delete_link(list, g_slist_last(list)); return list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; GSList *list2; int i; for (i = 0; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); list2 = list_pop(list1); print_list(list2); g_slist_free(list2); g_slist_free(list1); return 0; } で、 [0, 1, 2] !2006-01-07 Sat #include static int list_length(GSList *list) { return g_slist_length(list); } int main() { GSList *list = NULL; int i; for (i = 0; i < 4; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); g_print("%d\n", list_length(list)); g_slist_free(list); return 0; } で、 4 !2006-01-06 Fri #include static GSList *list_last(GSList *list, int n) { GSList *next_list; GSList *new_list = NULL; int i = 0; int size; size = g_slist_length(list); if (n >= size) { new_list = g_slist_copy(list); } else { for (next_list = list; next_list; next_list = g_slist_next(next_list)) { if (i >= size - n) { new_list = g_slist_append(new_list, next_list->data); } i++; } } return new_list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; GSList *list2; int i; list2 = list_last(list1, 0); print_list(list2); list2 = list_last(list1, 1); print_list(list2); for (i = 0; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); list2 = list_last(list1, 0); print_list(list2); g_slist_free(list2); list2 = list_last(list1, 1); print_list(list2); g_slist_free(list2); list2 = list_last(list1, 2); print_list(list2); g_slist_free(list2); list2 = list_last(list1, 5); print_list(list2); g_slist_free(list2); g_slist_free(list1); return 0; } で、 [] [] [] [3] [2, 3] [0, 1, 2, 3] !2006-01-05 Thu #include static GSList *list_last(GSList *list) { return g_slist_last(list); } int main() { GSList *list = NULL; GSList *list2; int i; list2 = list_last(list); if (list2) { g_print("%d\n", GPOINTER_TO_INT(list2->data)); } else { g_print("size == 0\n"); } for (i = 0; i < 4; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); g_print("%d\n", GPOINTER_TO_INT(list_last(list)->data)); g_slist_free(list); return 0; } で、 size == 0 3 !2006-01-04 Wed #include static GString *list_join(GSList *list, char *sep) { GString *str; GSList *next_list; str = g_string_new(""); for (next_list = list; next_list; next_list = g_slist_next(next_list)) { if (next_list != list) { g_string_append(str, sep); } g_string_append(str, (gchar *)(next_list->data)); } return str; } int main() { GSList *list = NULL; GString *str; list = g_slist_append(list, "0"); list = g_slist_append(list, "1"); list = g_slist_append(list, "2"); str = list_join(list, ""); g_print("%s\n", str->str); g_string_free(str, TRUE); str = list_join(list, " "); g_print("%s\n", str->str); g_string_free(str, TRUE); str = list_join(list, ":"); g_print("%s\n", str->str); g_string_free(str, TRUE); g_slist_free(list); return 0; } で、 012 0 1 2 0:1:2 !2006-01-03 Tue #include static void list_join(GSList *list, char *sep, GString **str) { GSList *next_list; *str = g_string_new(""); for (next_list = list; next_list; next_list = g_slist_next(next_list)) { if (next_list != list) { g_string_append(*str, sep); } g_string_append(*str, (gchar *)(next_list->data)); } } int main() { GSList *list = NULL; GString *str; list = g_slist_append(list, "0"); list = g_slist_append(list, "1"); list = g_slist_append(list, "2"); list_join(list, "", &str); g_print("%s\n", str->str); g_string_free(str, TRUE); list_join(list, " ", &str); g_print("%s\n", str->str); g_string_free(str, TRUE); list_join(list, ":", &str); g_print("%s\n", str->str); g_string_free(str, TRUE); g_slist_free(list); return 0; } で、 012 0 1 2 0:1:2 !2006-01-02 Mon #include static GSList *list_insert(GSList *list, int n, GSList *val_l) { GSList *n_list; GSList *next_list; n_list = g_slist_nth(list, n); if (n_list) { for (next_list = val_l; next_list; next_list = g_slist_next(next_list)) { list = g_slist_insert_before(list, n_list, next_list->data); } } else { int i, size; size = g_slist_length(list); for (i = 0; i < (n - size); i++) { list = g_slist_append(list, GINT_TO_POINTER(0)); } for (next_list = val_l; next_list; next_list = g_slist_next(next_list)) { list = g_slist_append(list, next_list->data); } } return list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; GSList *list2 = NULL; int i; for (i = 0; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); for (i = 10; i < 12; i++) list2 = g_slist_append(list2, GINT_TO_POINTER(i)); list1 = list_insert(list1, 1, list2); print_list(list1); list1 = list_insert(list1, 8, list2); print_list(list1); g_slist_free(list1); g_slist_free(list2); return 0; } で、 [0, 10, 11, 1, 2, 3] [0, 10, 11, 1, 2, 3, 0, 0, 10, 11] !2006-01-01 Sun #include static GSList *list_insert(GSList *list, int n, int val) { GSList *n_list; n_list = g_slist_nth(list, n); if (n_list) { list = g_slist_insert_before(list, n_list, GINT_TO_POINTER(val)); } else { int i, size; size = g_slist_length(list); for (i = 0; i < (n - size); i++) { list = g_slist_append(list, GINT_TO_POINTER(0)); } list = g_slist_append(list, GINT_TO_POINTER(val)); } return list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list = NULL; int i; for (i = 0; i < 4; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); list = list_insert(list, 1, 10); print_list(list); list = list_insert(list, 6, 11); print_list(list); g_slist_free(list); return 0; } で、 [0, 10, 1, 2, 3] [0, 10, 1, 2, 3, 0, 11] !2005-12-31 Sat #include static int list_index(GSList *list, int val) { return g_slist_index(list, GINT_TO_POINTER(val)); } int main() { GSList *list = NULL; int i; for (i = 0; i < 4; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); g_print("%d\n", list_index(list, 1)); g_print("%d\n", list_index(list, 5)); g_slist_free(list); return 0; } で、 1 -1 !2005-12-30 Fri #include static int list_include(GSList *list, int val) { if (g_slist_find(list, GINT_TO_POINTER(val))) { return TRUE; } else { return FALSE; } } int main() { GSList *list = NULL; int i; for (i = 0; i < 4; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); g_print("%d\n", list_include(list, 0)); g_print("%d\n", list_include(list, 5)); g_slist_free(list); return 0; } で、 1 0 !2005-12-29 Thu #include static GSList *list_first(GSList *list, int n) { GSList *new_list = NULL; GSList *next_list; int i = 0; for (next_list = list; next_list; next_list = g_slist_next(next_list)) { if (i >= n) break; new_list = g_slist_append(new_list, next_list->data); i++; } return new_list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; GSList *list2; int i; for (i = 0; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); list2 = list_first(list1, 0); print_list(list2); g_slist_free(list2); list2 = list_first(list1, 1); print_list(list2); g_slist_free(list2); list2 = list_first(list1, 2); print_list(list2); g_slist_free(list2); list2 = list_first(list1, 5); print_list(list2); g_slist_free(list2); g_slist_free(list1); return 0; } で、 [] [0] [0, 1] [0, 1, 2, 3] !2005-12-28 Wed #include static int list_first(GSList *list) { GSList *first; first = g_slist_nth(list, 0); if (first) { return GPOINTER_TO_INT(first->data); } else { /* ... */ } } int main() { GSList *list = NULL; int i; g_print("%d\n", list_first(list)); for (i = 0; i < 4; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); g_print("%d\n", list_first(list)); g_slist_free(list); return 0; } で、 0 0 空のときの処理をどうすべきか? !2005-12-27 Tue #include static void list_fill(GSList *list, int val) { GSList *next_list; for (next_list = list; next_list; next_list = g_slist_next(next_list)) { next_list->data = GINT_TO_POINTER(val); } } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list = NULL; int i; for (i = 0; i < 4; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); list_fill(list, 3); print_list(list); g_slist_free(list); return 0; } で、 [3, 3, 3, 3] 今まで気がつかなかったが、 要素を置き換える前に前の要素の領域をフリーしないとダメなのかも??? !2005-12-26 Mon #include static int list_empty(GSList *list) { return g_slist_length(list) == 0; } int main() { GSList *list = NULL; int i; g_print("%d\n", list_empty(list)); for (i = 0; i < 4; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); g_print("%d\n", list_empty(list)); g_slist_free(list); return 0; } で、 1 0 !2005-12-25 Sun #include static void func(int x) { g_print("%d\n", x); } static void list_each_index(GSList *list, void (*pfunc)(int)) { GSList *next_list; int i = 0; for (next_list = list; next_list; next_list = g_slist_next(next_list)) { (*pfunc)(i); i++; } } int main() { GSList *list = NULL; int i; for (i = 10; i < 14; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); list_each_index(list, func); g_slist_free(list); return 0; } で、 0 1 2 3 !2005-12-24 Sat #include static void func(gpointer data, gpointer ud) { g_print("%d\n", GPOINTER_TO_INT(data)); } static void list_each(GSList *list, void (*pfunc)(gpointer, gpointer)) { g_slist_foreach(list, pfunc, NULL); } int main() { GSList *list = NULL; int i; for (i = 0; i < 4; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); list_each(list, func); g_slist_free(list); return 0; } で、 0 1 2 3 !2005-12-23 Fri #include static void func(int x) { g_print("%d\n", x); } static void list_each(GSList *list, void (*pfunc)(int)) { GSList *next_list; for (next_list = list; next_list; next_list = g_slist_next(next_list)) { (*pfunc)(GPOINTER_TO_INT(next_list->data)); } } int main() { GSList *list = NULL; int i; for (i = 0; i < 4; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); list_each(list, func); g_slist_free(list); return 0; } で、 0 1 2 3 !2005-12-22 Thu #include static int func(int x) { return x % 2 == 0; } static GSList *list_delete_if(GSList *list, int (*pfunc)(int)) { GSList *next_list; GSList *next_list_tmp; for (next_list = list; next_list; next_list = next_list_tmp) { next_list_tmp = g_slist_next(next_list); if ((*pfunc)(GPOINTER_TO_INT(next_list->data))) { list = g_slist_delete_link(list, next_list); } next_list = next_list_tmp; } return list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list = NULL; int i; for (i = 0; i < 4; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); for (i = 0; i < 4; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); list = list_delete_if(list, func); print_list(list); g_slist_free(list); return 0; } で、 [1, 3, 1, 3] !2005-12-21 Wed #include static GSList *list_delete_at(GSList *list, int n) { GSList *n_list; n_list = g_slist_nth(list, n); if (n_list) { return g_slist_delete_link(list, n_list); } else { return list; } } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list = NULL; int i; for (i = 0; i < 4; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); list = list_delete_at(list, 5); list = list_delete_at(list, 0); print_list(list); g_slist_free(list); return 0; } で、 [1, 2, 3] !2005-12-20 Tue #include static GSList *list_delete(GSList *list, int val) { return g_slist_remove_all(list, GINT_TO_POINTER(val)); } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list = NULL; int i; for (i = 0; i < 4; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); for (i = 0; i < 4; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); list = list_delete(list, 2); print_list(list); g_slist_free(list); return 0; } で、 [0, 1, 3, 0, 1, 3] !2005-12-19 Mon #include static GSList *list_concat(GSList *list1, GSList *list2) { GSList *dup_list = g_slist_copy(list2); return g_slist_concat(list1, dup_list); } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; GSList *list2 = NULL; int i; for (i = 0; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); for (i = 4; i < 10; i++) list2 = g_slist_append(list2, GINT_TO_POINTER(i)); list1 = list_concat(list1, list2); print_list(list1); print_list(list2); g_slist_free(list1); g_slist_free(list2); return 0; } で、 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [4, 5, 6, 7, 8, 9] !2005-12-18 Sun #include static int func(int x) { return x * 2; } static void list_map(GSList *list, int (*pfunc)(int)) { GSList *next_list; for (next_list = list; next_list; next_list = g_slist_next(next_list)) { next_list->data = GINT_TO_POINTER((*pfunc)(GPOINTER_TO_INT(next_list->data))); } } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list = NULL; int i; for (i = 0; i < 4; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); list_map(list, func); print_list(list); g_slist_free(list); return 0; } で、 [0, 2, 4, 6] !2005-12-17 Sat #include static GSList *list_assoc(GSList *llist, int val) { GSList *new_list = NULL; GSList *next_list1; GSList *next_list2; for (next_list1 = llist; next_list1; next_list1 = g_slist_next(next_list1)) { int ok = FALSE; for (next_list2 = next_list1->data; next_list2; next_list2 = g_slist_next(next_list2)) { if (next_list2 == next_list1->data) { if (GPOINTER_TO_INT(next_list2->data) == val) { ok = TRUE; } else { break; } } new_list = g_slist_append(new_list, next_list2->data); } if (ok == TRUE) { return new_list; } } return new_list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } static void free_llist(GSList *list) { GSList *next_list; for (next_list = list; next_list; next_list = g_slist_next(next_list)) { g_slist_free(next_list->data); } g_slist_free(list); } int main() { GSList *llist = NULL; GSList *list = NULL; GSList *list2; list = g_slist_append(list, GINT_TO_POINTER(1)); list = g_slist_append(list, GINT_TO_POINTER(15)); llist = g_slist_append(llist, list); list = NULL; list = g_slist_append(list, GINT_TO_POINTER(2)); list = g_slist_append(list, GINT_TO_POINTER(25)); llist = g_slist_append(llist, list); list = NULL; list = g_slist_append(list, GINT_TO_POINTER(3)); list = g_slist_append(list, GINT_TO_POINTER(35)); llist = g_slist_append(llist, list); list2 = list_assoc(llist, 2); print_list(list2); g_slist_free(list2); free_llist(llist); return 0; } で、 [2, 25] !2005-12-16 Fri まず、リストのリストを作ってみる #include static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]"); } static void print_llist(GSList *list) { GSList *next_list; g_print("["); for (next_list = list; next_list; next_list = g_slist_next(next_list)) { if (next_list != list) g_print(",\n"); print_list(next_list->data); } g_print("]\n"); } static void free_llist(GSList *list) { GSList *next_list; for (next_list = list; next_list; next_list = g_slist_next(next_list)) { g_slist_free(next_list->data); } g_slist_free(list); } int main() { GSList *llist = NULL; GSList *list = NULL; int i; for (i = 0; i < 4; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); llist = g_slist_append(llist, list); list = NULL; for (i = 4; i < 8; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); llist = g_slist_append(llist, list); list = NULL; for (i = 8; i < 12; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); llist = g_slist_append(llist, list); print_llist(llist); free_llist(llist); return 0; } で、 [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]] !2005-12-15 Thu #include static int list_equal(GSList *list1, GSList *list2) { GSList *next_list1; GSList *next_list2 = list2; for (next_list1 = list1; next_list1; next_list1 = g_slist_next(next_list1)) { if (next_list2 == NULL) return FALSE; if (GPOINTER_TO_INT(next_list1->data) != GPOINTER_TO_INT(next_list2->data)) return FALSE; next_list2 = g_slist_next(next_list2); } if (next_list2 == NULL) { return TRUE; } else { return FALSE; } } int main() { GSList *list1 = NULL; GSList *list2 = NULL; int i; for (i = 0; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); for (i = 0; i < 4; i++) list2 = g_slist_append(list2, GINT_TO_POINTER(i)); g_print("%d\n", list_equal(list1, list2)); list1 = g_slist_append(list1, GINT_TO_POINTER(4)); list2 = g_slist_append(list2, GINT_TO_POINTER(1)); g_print("%d\n", list_equal(list1, list2)); list2 = g_slist_delete_link(list2, g_slist_last(list2)); g_print("%d\n", list_equal(list1, list2)); list1 = g_slist_delete_link(list1, g_slist_last(list1)); list2 = g_slist_append(list2, GINT_TO_POINTER(1)); g_print("%d\n", list_equal(list1, list2)); g_slist_free(list1); g_slist_free(list2); return 0; } で、 1 0 0 0 !2005-12-14 Wed #include static int list_cmp(GSList *list1, GSList *list2) { GSList *next_list1; GSList *next_list2 = list2; for (next_list1 = list1; next_list1; next_list1 = g_slist_next(next_list1)) { if (next_list2 == NULL) return 1; if (GPOINTER_TO_INT(next_list1->data) > GPOINTER_TO_INT(next_list2->data)) { return 1; } else if (GPOINTER_TO_INT(next_list1->data) < GPOINTER_TO_INT(next_list2->data)) { return -1; } next_list2 = g_slist_next(next_list2); } if (next_list2 == NULL) { return 0; } else { return -1; } } int main() { GSList *list1 = NULL; GSList *list2 = NULL; int i; for (i = 0; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); for (i = 0; i < 4; i++) list2 = g_slist_append(list2, GINT_TO_POINTER(i)); g_print("%d\n", list_cmp(list1, list2)); list2 = g_slist_append(list2, GINT_TO_POINTER(1)); g_print("%d\n", list_cmp(list1, list2)); /* l2 length */ list1 = g_slist_append(list1, GINT_TO_POINTER(4)); g_print("%d\n", list_cmp(list1, list2)); /* l1 size */ list1 = g_slist_delete_link(list1, g_slist_last(list1)); list2 = g_slist_delete_link(list2, g_slist_last(list2)); list1 = g_slist_append(list1, GINT_TO_POINTER(1)); list2 = g_slist_append(list2, GINT_TO_POINTER(4)); g_print("%d\n", list_cmp(list1, list2)); /* l2 size */ list2 = g_slist_delete_link(list2, g_slist_last(list2)); g_print("%d\n", list_cmp(list1, list2)); /* l2 size */ g_slist_free(list1); g_slist_free(list2); return 0; } で、 0 -1 1 -1 1 !2005-12-13 Tue #include static GSList *list_push(GSList *list, int val) { return g_slist_append(list, GINT_TO_POINTER(val)); } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list = NULL; int i; for (i = 0; i < 4; i++) list = g_slist_append(list, GINT_TO_POINTER(i)); list = list_push(list, 10); print_list(list); g_slist_free(list); return 0; } で、 [0, 1, 2, 3, 10] !2005-12-12 Mon #include static GSList *insert_val(GSList *new_list, GHashTable *hash, GSList *list) { GSList *next_list; for (next_list = list; next_list; next_list = g_slist_next(next_list)) { if (g_hash_table_lookup(hash, next_list->data) == NULL) { new_list = g_slist_append(new_list, next_list->data); g_hash_table_insert(hash, next_list->data, GINT_TO_POINTER(TRUE)); } } return new_list; } static GSList *list_or(GSList *list1, GSList *list2) { GHashTable *h; GSList *new_list = NULL; h = g_hash_table_new(g_direct_hash, g_direct_equal); new_list = insert_val(new_list, h, list1); new_list = insert_val(new_list, h, list2); g_hash_table_destroy(h); return new_list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; GSList *list2 = NULL; GSList *list3; int i; for (i = 0; i < 3; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); for (i = 4; i > 0; i--) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); for (i = 2; i < 6; i++) list2 = g_slist_append(list2, GINT_TO_POINTER(i)); list3 = list_or(list1, list2); print_list(list3); g_slist_free(list1); g_slist_free(list2); g_slist_free(list3); return 0; } で、 [0, 1, 2, 4, 3, 5] !2005-12-11 Sun #include static void insert_hash(GHashTable *hash, GSList *list) { GSList *next_list; for (next_list = list; next_list; next_list = g_slist_next(next_list)) { g_hash_table_insert(hash, next_list->data, GINT_TO_POINTER(TRUE)); } } static GSList *list_and(GSList *list1, GSList *list2) { GHashTable *h1, *h2; GSList *new_list = NULL; GSList *next_list; h1 = g_hash_table_new(g_direct_hash, g_direct_equal); insert_hash(h1, list1); h2 = g_hash_table_new(g_direct_hash, g_direct_equal); insert_hash(h2, list2); for (next_list = list1; next_list; next_list = g_slist_next(next_list)) { if (g_hash_table_lookup(h1, next_list->data) && g_hash_table_lookup(h2, next_list->data)) { new_list = g_slist_append(new_list, next_list->data); g_hash_table_remove(h1, next_list->data); } } g_hash_table_destroy(h1); g_hash_table_destroy(h2); return new_list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; GSList *list2 = NULL; GSList *list3; int i; for (i = 0; i < 3; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); for (i = 4; i > 0; i--) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); for (i = 2; i < 5; i++) list2 = g_slist_append(list2, GINT_TO_POINTER(i)); list3 = list_and(list1, list2); print_list(list3); g_slist_free(list1); g_slist_free(list2); g_slist_free(list3); return 0; } で、 [2, 4, 3] !2005-12-10 Sat #include static GSList *list_diff(GSList *list1, GSList *list2) { GSList *new_list = NULL; GSList *next_list1 = list1; while (next_list1) { int found = FALSE; GSList *next_list2 = list2; while (next_list2) { if (next_list1->data == next_list2->data) { found = TRUE; break; } next_list2 = g_slist_next(next_list2); } if (found == FALSE) { new_list = g_slist_append(new_list, next_list1->data); } next_list1 = g_slist_next(next_list1); } return new_list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; GSList *list2 = NULL; GSList *list3; int i; for (i = 0; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); for (i = 0; i < 4; i++) list1 = g_slist_append(list1, GINT_TO_POINTER(i)); for (i = 3; i < 5; i++) list2 = g_slist_append(list2, GINT_TO_POINTER(i)); list3 = list_diff(list1, list2); print_list(list3); g_slist_free(list1); g_slist_free(list2); g_slist_free(list3); return 0; } で、 [0, 1, 2, 0, 1, 2] !2005-12-09 Fri #include static GSList *list_times(GSList *list, int times) { int i; GSList *new_list = NULL; for (i = 0; i < times; i++) { GSList *dup_list = g_slist_copy(list); new_list = g_slist_concat(new_list, dup_list); } return new_list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; GSList *list2; int i; for (i = 0; i < 4; i++) { list1 = g_slist_append(list1, GINT_TO_POINTER(i)); } list2 = list_times(list1, 3); print_list(list2); g_slist_free(list1); g_slist_free(list2); return 0; } で、 [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3] !2005-12-08 Thu #include static GSList *list_plus(GSList *list1, GSList *list2) { GSList *new_list = g_slist_copy(list1); GSList *dup_list = g_slist_copy(list2); return g_slist_concat(new_list, dup_list); } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list1 = NULL; GSList *list2 = NULL; GSList *list3; int i; for (i = 0; i < 4; i++) { list1 = g_slist_append(list1, GINT_TO_POINTER(i)); } for (i = 10; i < 12; i++) { list2 = g_slist_append(list2, GINT_TO_POINTER(i)); } list3 = list_plus(list1, list2); print_list(list1); print_list(list2); print_list(list3); list1 = g_slist_delete_link(list1, g_slist_last(list1)); list2 = g_slist_delete_link(list2, g_slist_last(list2)); print_list(list1); print_list(list2); print_list(list3); g_slist_free(list1); g_slist_free(list2); g_slist_free(list3); return 0; } で、 [0, 1, 2, 3] [10, 11] [0, 1, 2, 3, 10, 11] [0, 1, 2] [10] [0, 1, 2, 3, 10, 11] !2005-12-07 Wed #include static GSList *list_erase(GSList *list, int start, int end) { int i = 0; GSList *next_list = list; GSList *next_list_tmp; while (next_list) { next_list_tmp = g_slist_next(next_list); if (i >= start && i <= end) { list = g_slist_delete_link(list, next_list); } next_list = next_list_tmp; i++; } return list; } static GSList *list_aset(GSList *list, guint start, int end, GSList *val) { list = list_erase(list, start, end); if (start < g_slist_length(list)) { GSList *next_list = val; int i = start; while (next_list) { list = g_slist_insert(list, GINT_TO_POINTER(next_list->data), i); next_list = g_slist_next(next_list); i++; } } return list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list = NULL; GSList *list2 = NULL; int i; for (i = 0; i < 4; i++) { list = g_slist_append(list, GINT_TO_POINTER(i)); } for (i = 10; i < 12; i++) { list2 = g_slist_append(list2, GINT_TO_POINTER(i)); } list = list_aset(list, 0, 1, list2); print_list(list); list = list_aset(list, 1, 1, list2); print_list(list); list = list_aset(list, 10, 11, list2); print_list(list); g_slist_free(list); g_slist_free(list2); return 0; } で、 [10, 11, 2, 3] [10, 10, 11, 2, 3] [10, 10, 11, 2, 3] * ダメダメだが、良い方法がすぐには思いつかなかったので…。 * g_slist_insert_before を使えば良いかも? !2005-12-06 Tue #include static GSList *list_erase(GSList *list, int start, int end) { int i = 0; GSList *next_list = list; GSList *next_list_tmp; while (next_list) { next_list_tmp = g_slist_next(next_list); if (i >= start && i <= end) { list = g_slist_delete_link(list, next_list); } next_list = next_list_tmp; i++; } return list; } static GSList *list_aset(GSList *list, guint start, int end, int val) { list = list_erase(list, start, end); if (start < g_slist_length(list)) { list = g_slist_insert(list, GINT_TO_POINTER(val), start); } return list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list = NULL; int i; for (i = 0; i < 4; i++) { list = g_slist_append(list, GINT_TO_POINTER(i)); } list = list_aset(list, 0, 1, 10); print_list(list); list = list_aset(list, 1, 1, 20); print_list(list); list = list_aset(list, 10, 11, 20); print_list(list); g_slist_free(list); return 0; } で、 [10, 2, 3] [10, 20, 3] [10, 20, 3] !2005-12-05 Mon #include static GSList *list_aref(GSList *list, int start, int end) { int i = 0; GSList *new_list = NULL; GSList *next_list = list; while (next_list) { if (i >= start && i <= end) { new_list = g_slist_append(new_list, next_list->data); } next_list = g_slist_next(next_list); i++; } return new_list; } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list = NULL; GSList *new_list; int i; for (i = 0; i < 4; i++) { list = g_slist_append(list, GINT_TO_POINTER(i)); } new_list = list_aref(list, 0, 1); print_list(new_list); g_slist_free(new_list); new_list = list_aref(list, 1, 1); print_list(new_list); g_slist_free(new_list); new_list = list_aref(list, 1, 5); print_list(new_list); g_slist_free(new_list); g_slist_free(list); return 0; } で、 [0, 1] [1] [1, 2, 3] !2005-12-04 Sun #include static void list_aset(GSList *list, int n, int val) { GSList *nlist = g_slist_nth(list, n); if (nlist) { nlist->data = GINT_TO_POINTER(val); } } static void print_item(gpointer data, gpointer ud) { int *p = (int *)ud; if (*p != 0) g_print(", "); g_print("%d", (int)data); (*p)++; } static void print_list(GSList *list) { int i = 0; g_print("["); g_slist_foreach(list, print_item, &i); g_print("]\n"); } int main() { GSList *list = NULL; int i; for (i = 0; i < 4; i++) { list = g_slist_append(list, GINT_TO_POINTER(i)); } list_aset(list, 0, 10); list_aset(list, 1, 11); list_aset(list, 2, 12); list_aset(list, 5, 13); print_list(list); g_slist_free(list); return 0; } で、 [10, 11, 12, 3] * 範囲外はセットしないことにしてみた !2005-12-03 Sat #include static int list_nth(GSList *list, int n) { gpointer data = g_slist_nth_data(list, n); return (int)(data); } int main() { GSList *list = NULL; int i; for (i = 0; i < 4; i++) { list = g_slist_append(list, GINT_TO_POINTER(i)); } g_print("%d\n", list_nth(list, 0)); g_print("%d\n", list_nth(list, 1)); g_print("%d\n", list_nth(list, 3)); g_print("%d\n", list_nth(list, 4)); g_slist_free(list); return 0; } で、 0 1 3 0 !2005-12-02 Fri #include #include int main() { GScanner *scanner; gchar *str = "a b c"; GTokenType type; GScannerConfig config = { " \t\n", G_CSET_a_2_z G_CSET_A_2_Z, G_CSET_a_2_z "_0123456789" G_CSET_A_2_Z, "#\n", 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, }; scanner = g_scanner_new(&config); scanner->config->char_2_token = FALSE; g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_get_next_token(scanner); g_print("%d %c\n", type, g_scanner_cur_value(scanner).v_char); type = g_scanner_get_next_token(scanner); g_print("%d %c\n", type, g_scanner_cur_value(scanner).v_char); type = g_scanner_get_next_token(scanner); g_print("%d %c\n", type, g_scanner_cur_value(scanner).v_char); g_scanner_destroy(scanner); return 0; } で、 258 a 258 b [Invalid UTF-8] 266 \xb0 おっ、G_TOKEN_CHAR が出せた! 最後が変だけど。 !2005-12-01 Thu なーんと、numbers_2_int とか指定すると、2 進数トークンとして認識されるらしい。 #include #include int main() { GScanner *scanner; gchar *str = "0b1010"; GTokenType type; GScannerConfig config = { "\t\n", G_CSET_a_2_z G_CSET_A_2_Z, G_CSET_a_2_z "_0123456789" G_CSET_A_2_Z, "#\n", 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, }; scanner = g_scanner_new(&config); scanner->config->numbers_2_int = FALSE; scanner->config->scan_binary = TRUE; g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_get_next_token(scanner); g_print("%d %ld\n", type, g_scanner_cur_value(scanner).v_int); g_print("%d %ld\n", type, g_scanner_cur_value(scanner).v_binary); g_scanner_destroy(scanner); return 0; } で、 259 10 259 10 !2005-11-30 Wed #include #include int main() { GScanner *scanner; gchar *str = "$010"; GTokenType type; GScannerConfig config = { "\t\n", G_CSET_a_2_z G_CSET_A_2_Z, G_CSET_a_2_z "_0123456789" G_CSET_A_2_Z, "#\n", 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, }; scanner = g_scanner_new(&config); scanner->config->scan_hex_dollar = 1; g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_get_next_token(scanner); g_print("%d %ld\n", type, g_scanner_cur_value(scanner).v_int); g_scanner_destroy(scanner); return 0; } で、 261 16 $ の前置って BASIC ??? !2005-11-29 Tue #include #include int main() { GScanner *scanner; gchar *str = "010"; GTokenType type; GScannerConfig config = { "\t\n", G_CSET_a_2_z G_CSET_A_2_Z, G_CSET_a_2_z "_0123456789" G_CSET_A_2_Z, "#\n", 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, }; scanner = g_scanner_new(&config); scanner->config->scan_octal = 1; g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_get_next_token(scanner); g_print("%d %ld\n", type, g_scanner_cur_value(scanner).v_int); g_scanner_destroy(scanner); return 0; } で、 261 8 scanner->config->scan_octal = 1; としないと、 261 10 となった !2005-11-28 Mon #include #include int main() { GScanner *scanner; gchar *str = "0b1010"; GTokenType type; GScannerConfig config = { "\t\n", G_CSET_a_2_z G_CSET_A_2_Z, G_CSET_a_2_z "_0123456789" G_CSET_A_2_Z, "#\n", 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, }; scanner = g_scanner_new(&config); scanner->config->scan_binary = 1; g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_get_next_token(scanner); g_print("%d %ld\n", type, g_scanner_cur_value(scanner).v_int); g_scanner_destroy(scanner); return 0; } で、 261 10 トークン値が 261(G_TOKEN_INT) なのが良く分からないが、 scanner->config->scan_binary = 1; としないと、エラーになるので、そういう意味では一応動いているようだ。 !2005-11-27 Sun #include #include int main() { GScanner *scanner; gchar *str = "foo bar"; GTokenType type; GScannerConfig config = { "\t\n", G_CSET_a_2_z G_CSET_A_2_Z, G_CSET_a_2_z "_0123456789" G_CSET_A_2_Z, "#\n", 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, }; scanner = g_scanner_new(&config); g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_get_next_token(scanner); g_print("%d %s\n", type, g_scanner_cur_value(scanner).v_identifier); type = g_scanner_get_next_token(scanner); g_print("%d %s\n", type, g_scanner_cur_value(scanner).v_identifier); type = g_scanner_get_next_token(scanner); g_print("%d %s\n", type, g_scanner_cur_value(scanner).v_identifier); g_scanner_destroy(scanner); return 0; } で、 266 foo 32 (null) 266 bar !2005-11-26 Sat #include #include int main() { GScanner *scanner; gchar *str = "foo bar"; GTokenType type; GScannerConfig config = { " \t\n", G_CSET_a_2_z G_CSET_A_2_Z, G_CSET_a_2_z "_0123456789" G_CSET_A_2_Z, "#\n", 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, }; scanner = g_scanner_new(&config); g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_get_next_token(scanner); g_print("%d %s\n", type, g_scanner_cur_value(scanner).v_identifier); type = g_scanner_get_next_token(scanner); g_print("%d %s\n", type, g_scanner_cur_value(scanner).v_identifier); g_scanner_destroy(scanner); return 0; } で、 266 foo 266 bar !2005-11-25 Fri #include #include int main() { GScanner *scanner, *scanner2; gchar *str = "foo bar"; GTokenType type; GScannerConfig config; scanner2 = g_scanner_new(NULL); config.cset_skip_characters = scanner2->config->cset_skip_characters; config.cset_identifier_first = scanner2->config->cset_identifier_first; config.cset_identifier_nth = scanner2->config->cset_identifier_nth; config.cpair_comment_single = scanner2->config->cpair_comment_single; config.case_sensitive = scanner2->config->case_sensitive; config.skip_comment_multi = scanner2->config->skip_comment_multi; config.skip_comment_single = scanner2->config->skip_comment_single; config.scan_comment_multi = scanner2->config->scan_comment_multi; config.scan_identifier = scanner2->config->scan_identifier; config.scan_identifier_1char = scanner2->config->scan_identifier_1char; config.scan_identifier_NULL = scanner2->config->scan_identifier_NULL; config.scan_symbols = scanner2->config->scan_symbols; config.scan_binary = scanner2->config->scan_binary; config.scan_octal = scanner2->config->scan_octal; config.scan_float = scanner2->config->scan_float; config.scan_hex = scanner2->config->scan_hex; config.scan_hex_dollar = scanner2->config->scan_hex_dollar; config.scan_string_sq = scanner2->config->scan_string_sq; config.scan_string_dq = scanner2->config->scan_string_dq; config.numbers_2_int = scanner2->config->numbers_2_int; config.int_2_float = scanner2->config->int_2_float; config.identifier_2_string = scanner2->config->identifier_2_string; config.char_2_token = scanner2->config->char_2_token; config.symbol_2_token = scanner2->config->symbol_2_token; config.scope_0_fallback = scanner2->config->scope_0_fallback; scanner = g_scanner_new(&config); g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_get_next_token(scanner); g_print("%d %s\n", type, g_scanner_cur_value(scanner).v_identifier); type = g_scanner_get_next_token(scanner); g_print("%d %s\n", type, g_scanner_cur_value(scanner).v_identifier); g_scanner_destroy(scanner); return 0; } で、 266 foo 266 bar !2005-11-24 Thu #include int main() { GScanner *scanner; scanner = g_scanner_new(NULL); g_print("case_sensitive %d\n", scanner->config->case_sensitive); g_print("skip_comment_multi %d\n", scanner->config->skip_comment_multi); g_print("skip_comment_single %d\n", scanner->config->skip_comment_single); g_print("scan_comment_multi %d\n", scanner->config->scan_comment_multi); g_print("scan_identifier %d\n", scanner->config->scan_identifier); g_print("scan_identifier_1char %d\n", scanner->config->scan_identifier_1char); g_print("scan_identifier_NULL %d\n", scanner->config->scan_identifier_NULL); g_print("scan_symbols %d\n", scanner->config->scan_symbols); g_print("scan_binary %d\n", scanner->config->scan_binary); g_print("scan_octal %d\n", scanner->config->scan_octal); g_print("scan_float %d\n", scanner->config->scan_float); g_print("scan_hex %d\n", scanner->config->scan_hex); g_print("scan_hex_dollar %d\n", scanner->config->scan_hex_dollar); g_print("scan_string_sq %d\n", scanner->config->scan_string_sq); g_print("scan_string_dq %d\n", scanner->config->scan_string_dq); g_print("numbers_2_int %d\n", scanner->config->numbers_2_int); g_print("int_2_float %d\n", scanner->config->int_2_float); g_print("identifier_2_string %d\n", scanner->config->identifier_2_string); g_print("char_2_token %d\n", scanner->config->char_2_token); g_print("symbol_2_token %d\n", scanner->config->symbol_2_token); g_print("scope_0_fallback %d\n", scanner->config->scope_0_fallback); g_print("store_int64 %d\n", scanner->config->store_int64); g_scanner_destroy(scanner); return 0; } で、 case_sensitive 0 skip_comment_multi 1 skip_comment_single 1 scan_comment_multi 1 scan_identifier 1 scan_identifier_1char 0 scan_identifier_NULL 0 scan_symbols 1 scan_binary 0 scan_octal 1 scan_float 1 scan_hex 1 scan_hex_dollar 0 scan_string_sq 1 scan_string_dq 1 numbers_2_int 1 int_2_float 0 identifier_2_string 0 char_2_token 1 symbol_2_token 0 scope_0_fallback 0 store_int64 0 !2005-11-23 Wed 今日から sarge #include int main() { GScanner *scanner; scanner = g_scanner_new(NULL); g_print("1 %s\n", scanner->config->cset_skip_characters); g_print("2 %s\n", scanner->config->cset_identifier_first); g_print("3 %s\n", scanner->config->cset_identifier_nth); g_print("4 %s\n", scanner->config->cpair_comment_single); g_scanner_destroy(scanner); return 0; } で、 1 2 abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ [Invalid UTF-8] 3 abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ... 4 # !2005-11-22 Tue #include int main() { g_print("G_CSET_a_2_z %s\n", G_CSET_a_2_z); g_print("G_CSET_A_2_Z %s\n", G_CSET_A_2_Z); g_print("G_CSET_DIGITS %s\n", G_CSET_DIGITS); g_print("G_CSET_LATINC %s\n", G_CSET_LATINC); g_print("G_CSET_LATINS %s\n", G_CSET_LATINS); return 0; } で、 G_CSET_a_2_z abcdefghijklmnopqrstuvwxyz G_CSET_A_2_Z ABCDEFGHIJKLMNOPQRSTUVWXYZ G_CSET_DIGITS 0123456789 [Invalid UTF-8] G_CSET_LATINC ... [Invalid UTF-8] G_CSET_LATINS ... !2005-11-21 Mon #include int main() { GScanner *scanner; gchar *str = "0x1.1"; GTokenType type; scanner = g_scanner_new(NULL); g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_get_next_token(scanner); g_print("%d %d\n", type, g_scanner_cur_value(scanner).v_error); g_scanner_destroy(scanner); return 0; } で、 257 6 !2005-11-20 Sun #include int main() { GScanner *scanner; gchar *str = "10.9.9"; GTokenType type; scanner = g_scanner_new(NULL); g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_get_next_token(scanner); g_print("%d %d\n", type, g_scanner_cur_value(scanner).v_error); g_scanner_destroy(scanner); return 0; } で、 257 7 !2005-11-19 Sat #include int main() { GScanner *scanner; gchar *str = "10.a"; GTokenType type; scanner = g_scanner_new(NULL); g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_get_next_token(scanner); g_print("%d %d\n", type, g_scanner_cur_value(scanner).v_error); g_scanner_destroy(scanner); return 0; } で、 257 4 !2005-11-18 Fri #include int main() { GScanner *scanner; gchar *str = "'abc"; GTokenType type; scanner = g_scanner_new(NULL); g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_get_next_token(scanner); g_print("%d %d\n", type, g_scanner_cur_value(scanner).v_error); g_scanner_destroy(scanner); return 0; } で、 257 2 !2005-11-17 Thu #include int main() { g_print("G_ERR_UNKNOWN %d\n", G_ERR_UNKNOWN); g_print("G_ERR_UNEXP_EOF %d\n", G_ERR_UNEXP_EOF); g_print("G_ERR_UNEXP_EOF_IN_STRING %d\n", G_ERR_UNEXP_EOF_IN_STRING); g_print("G_ERR_UNEXP_EOF_IN_COMMENT %d\n", G_ERR_UNEXP_EOF_IN_COMMENT); g_print("G_ERR_NON_DIGIT_IN_CONST %d\n", G_ERR_NON_DIGIT_IN_CONST); g_print("G_ERR_DIGIT_RADIX %d\n", G_ERR_DIGIT_RADIX); g_print("G_ERR_FLOAT_RADIX %d\n", G_ERR_FLOAT_RADIX); g_print("G_ERR_FLOAT_MALFORMED %d\n", G_ERR_FLOAT_MALFORMED); return 0; } で、 G_ERR_UNKNOWN 0 G_ERR_UNEXP_EOF 1 G_ERR_UNEXP_EOF_IN_STRING 2 G_ERR_UNEXP_EOF_IN_COMMENT 3 G_ERR_NON_DIGIT_IN_CONST 4 G_ERR_DIGIT_RADIX 5 G_ERR_FLOAT_RADIX 6 G_ERR_FLOAT_MALFORMED 7 !2005-11-16 Wed #include int main() { GScanner *scanner; gchar *str = "if then"; scanner = g_scanner_new(NULL); g_scanner_input_text(scanner, str, strlen(str)); g_scanner_scope_add_symbol(scanner, 0, "if", "if"); g_scanner_set_scope(scanner, 0); g_print("%s\n", (gchar*)g_scanner_lookup_symbol(scanner, "if")); g_scanner_set_scope(scanner, 1); g_print("%s\n", (gchar*)g_scanner_lookup_symbol(scanner, "if")); g_scanner_destroy(scanner); return 0; } で、 if (null) !2005-11-15 Tue #include int main() { GScanner *scanner; gchar *str = "if then"; scanner = g_scanner_new(NULL); g_scanner_input_text(scanner, str, strlen(str)); g_scanner_set_scope(scanner, 0); g_scanner_scope_add_symbol(scanner, 0, "if", "if"); g_print("%s\n", (gchar *)g_scanner_scope_lookup_symbol(scanner, 0, "if")); g_scanner_scope_remove_symbol(scanner, 0, "if"); g_print("%s\n", (gchar *)g_scanner_scope_lookup_symbol(scanner, 0, "if")); g_scanner_destroy(scanner); return 0; } で、 if (null) !2005-11-14 Mon コメントってどういう場合だろう? #include int main() { GScanner *scanner; gchar *str = "if then"; scanner = g_scanner_new(NULL); g_scanner_input_text(scanner, str, strlen(str)); g_scanner_set_scope(scanner, 0); g_scanner_scope_add_symbol(scanner, 0, "if", "if"); g_print("%s\n", (gchar *)g_scanner_scope_lookup_symbol(scanner, 0, "if")); g_print("%s\n", (gchar *)g_scanner_scope_lookup_symbol(scanner, 0, "then")); g_scanner_destroy(scanner); return 0; } で、 if (null) !2005-11-13 Sun #include int main() { GScanner *scanner; gchar *str = "if then"; GTokenType type; scanner = g_scanner_new(NULL); g_scanner_input_text(scanner, str, strlen(str)); g_scanner_set_scope(scanner, 0); g_scanner_scope_add_symbol(scanner, 0, "if", "if"); type = g_scanner_get_next_token(scanner); g_print("%d %s\n", type, (gchar *)g_scanner_cur_value(scanner).v_symbol); type = g_scanner_get_next_token(scanner); g_print("%d %s\n", type, g_scanner_cur_value(scanner).v_identifier); g_scanner_destroy(scanner); return 0; } で、 265 if 266 then !2005-11-12 Sat #include int main() { GScanner *scanner; gchar *str = "\"abc\""; GTokenType type; scanner = g_scanner_new(NULL); g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_get_next_token(scanner); g_print("%d %s\n", type, g_scanner_cur_value(scanner).v_string); g_scanner_destroy(scanner); return 0; } で、 264 abc !2005-11-11 Fri #include int main() { GScanner *scanner; gchar *str = "'abc'"; GTokenType type; scanner = g_scanner_new(NULL); g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_get_next_token(scanner); g_print("%d %s\n", type, g_scanner_cur_value(scanner).v_string); g_scanner_destroy(scanner); return 0; } で、 264 abc うーん、G_TOKEN_CHAR ってどういうときなんだろう? !2005-11-10 Thu #include int main() { GScanner *scanner; gchar *str = "0xff"; GTokenType type; scanner = g_scanner_new(NULL); g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_get_next_token(scanner); g_print("%d %lx\n", type, g_scanner_cur_value(scanner).v_hex); g_scanner_destroy(scanner); return 0; } で、 261 ff うーん、これも G_TOKEN_INT になるなあ !2005-11-09 Wed #include int main() { GScanner *scanner; gchar *str = "1.23"; GTokenType type; scanner = g_scanner_new(NULL); g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_get_next_token(scanner); g_print("%d %f\n", type, g_scanner_cur_value(scanner).v_float); g_scanner_destroy(scanner); return 0; } で、 263 1.230000 !2005-11-08 Tue #include int main() { GScanner *scanner; gchar *str = "123"; GTokenType type; scanner = g_scanner_new(NULL); g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_get_next_token(scanner); g_print("%d %ld\n", type, g_scanner_cur_value(scanner).v_int); g_scanner_destroy(scanner); return 0; } で、 261 123 !2005-11-07 Mon #include int main() { GScanner *scanner; gchar *str = "010"; GTokenType type; scanner = g_scanner_new(NULL); g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_get_next_token(scanner); g_print("%d %ld\n", type, g_scanner_cur_value(scanner).v_octal); g_scanner_destroy(scanner); return 0; } で、 261 8 G_TOKEN_OCTAL のつもりだったのだが? !2005-11-06 Sun #include int main() { GScanner *scanner; gchar *str = "a"; GTokenType type; scanner = g_scanner_new(NULL); g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_get_next_token(scanner); g_print("%d %s\n", type, g_scanner_cur_value(scanner).v_identifier); g_scanner_destroy(scanner); return 0; } で、 266 a !2005-11-05 Sat g_scanner_unexp_token() とばし。 さっぱり意味が分からないんだもん。 #include int main() { GScanner *scanner; gchar *str = "(){}[]=,"; GTokenType type; unsigned int i; scanner = g_scanner_new(NULL); g_scanner_input_text(scanner, str, strlen(str)); for (i = 0; i < strlen(str); i++) { type = g_scanner_get_next_token(scanner); g_print("i: %d %d\n", i, type); } g_scanner_destroy(scanner); return 0; } で、 i: 0 40 i: 1 41 i: 2 123 i: 3 125 i: 4 91 i: 5 93 i: 6 61 i: 7 44 !2005-11-04 Fri 何か変だと思ったら、input_name を自分で設定しないとダメらしい。 #include int main() { GScanner *scanner; scanner = g_scanner_new(NULL); scanner->input_name = "FILE1"; g_scanner_warn (scanner, "foo"); g_scanner_error(scanner, "bar"); g_scanner_destroy(scanner); return 0; } で、 FILE1:1: foo FILE1:1: error: bar !2005-11-03 Thu #include int main() { GScanner *scanner; scanner = g_scanner_new(NULL); g_scanner_error(scanner, "foo"); g_scanner_destroy(scanner); return 0; } で、 (null):1: error: foo !2005-11-02 Wed #include int main() { GScanner *scanner; scanner = g_scanner_new(NULL); g_scanner_warn(scanner, "foo"); g_scanner_destroy(scanner); return 0; } で、 (null):1: foo !2005-11-01 Tue スコープの意味が分からないので、 g_scanner_set_scope(), g_scanner_scope_add_symbol(), g_scanner_scope_foreach_symbol(), g_scanner_scope_lookup_symbol(), g_scanner_scope_remove_symbol() とばし。 #include int main() { GScanner *scanner; gchar *str = "foo()"; scanner = g_scanner_new(NULL); g_scanner_input_text(scanner, str, strlen(str)); g_print("%s\n", (gchar*)g_scanner_lookup_symbol(scanner, "foo")); g_scanner_destroy(scanner); return 0; } で、 (null) うーん、これもスコープとやらを設定しないとダメなのかな? (少なくとも、シンボルの設定は必要みたい) !2005-10-31 Mon #include int main() { GScanner *scanner; gchar *str = "foo()"; GTokenType type; scanner = g_scanner_new(NULL); g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_get_next_token(scanner); g_print("%d %s\n", type, g_scanner_cur_value(scanner).v_identifier); type = g_scanner_get_next_token(scanner); g_print("%d %d\n", type, g_scanner_eof(scanner)); type = g_scanner_get_next_token(scanner); g_print("%d %d\n", type, g_scanner_eof(scanner)); type = g_scanner_get_next_token(scanner); g_print("%d %d\n", type, g_scanner_eof(scanner)); g_scanner_destroy(scanner); return 0; } で、 266 foo 40 0 41 0 0 1 !2005-10-30 Sun #include int main() { GScanner *scanner; gchar *str = "foo()"; GTokenType type; scanner = g_scanner_new(NULL); g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_get_next_token(scanner); g_print("%d %s %d %d %d\n", type, g_scanner_cur_value(scanner).v_identifier, g_scanner_cur_line(scanner), g_scanner_cur_position(scanner), g_scanner_cur_token(scanner)); type = g_scanner_get_next_token(scanner); g_print("%d %d %d %d\n", type, g_scanner_cur_line(scanner), g_scanner_cur_position(scanner), g_scanner_cur_token(scanner)); g_scanner_destroy(scanner); return 0; } で、 266 foo 1 3 266 40 1 4 40 !2005-10-29 Sat #include int main() { GScanner *scanner; gchar *str = "foo()"; GTokenType type; scanner = g_scanner_new(NULL); g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_peek_next_token(scanner); g_print("%d %s\n", type, g_scanner_cur_value(scanner).v_identifier); type = g_scanner_peek_next_token(scanner); g_print("%d %s\n", type, g_scanner_cur_value(scanner).v_identifier); g_scanner_destroy(scanner); return 0; } で、 266 (null) 266 (null) token でなくて、next_token に値が入るようだ。 なので、 #include int main() { GScanner *scanner; gchar *str = "foo()"; GTokenType type; scanner = g_scanner_new(NULL); g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_peek_next_token(scanner); g_print("%d %s\n", type, scanner->next_value.v_identifier); type = g_scanner_peek_next_token(scanner); g_print("%d %s\n", type, scanner->next_value.v_identifier); g_scanner_destroy(scanner); return 0; } で、 266 foo 266 foo !2005-10-28 Fri g_scanner_sync_file_offset 良く分からないので、とばし #include int main() { GScanner *scanner; gchar *str = "foo()"; GTokenType type; scanner = g_scanner_new(NULL); g_scanner_input_text(scanner, str, strlen(str)); type = g_scanner_get_next_token(scanner); g_print("%d %s\n", type, g_scanner_cur_value(scanner).v_identifier); type = g_scanner_get_next_token(scanner); g_print("%d\n", type); type = g_scanner_get_next_token(scanner); g_print("%d\n", type); type = g_scanner_get_next_token(scanner); g_print("%d\n", type); g_scanner_destroy(scanner); return 0; } で、 266 foo 40 41 0 !2005-10-27 Thu #include #include #include #include #include int main() { GScanner *scanner; int fd; GTokenType type; fd = open("2005102600.txt", O_RDONLY); scanner = g_scanner_new(NULL); g_scanner_input_file(scanner, fd); type = g_scanner_get_next_token(scanner); g_print("%d %s\n", type, g_scanner_cur_value(scanner).v_identifier); type = g_scanner_get_next_token(scanner); g_print("%d\n", type); type = g_scanner_get_next_token(scanner); g_print("%d\n", type); type = g_scanner_get_next_token(scanner); g_print("%d\n", type); g_scanner_destroy(scanner); close(fd); return 0; } で、 266 foo 40 41 0 !2005-10-26 Wed #include #include #include #include #include int main() { GScanner *scanner; int fd; fd = open("2005102600.txt", O_RDONLY); scanner = g_scanner_new(NULL); g_print("%d\n", g_scanner_peek_next_token(scanner)); g_print("%d\n", g_scanner_peek_next_token(scanner)); g_scanner_destroy(scanner); close(fd); return 0; } で、2005102600.txt は、 foo() で、 0 0 * 入力先を指定しないとダメのようだ。 * エラーにはならないようだ。 !2005-10-25 Tue #include int main() { g_print("G_TOKEN_EOF %d\n", G_TOKEN_EOF); g_print("G_TOKEN_LEFT_PAREN %d\n", G_TOKEN_LEFT_PAREN); g_print("G_TOKEN_RIGHT_PAREN %d\n", G_TOKEN_RIGHT_PAREN); g_print("G_TOKEN_LEFT_CURLY %d\n", G_TOKEN_LEFT_CURLY); g_print("G_TOKEN_RIGHT_CURLY %d\n", G_TOKEN_RIGHT_CURLY); g_print("G_TOKEN_LEFT_BRACE %d\n", G_TOKEN_LEFT_BRACE); g_print("G_TOKEN_RIGHT_BRACE %d\n", G_TOKEN_RIGHT_BRACE); g_print("G_TOKEN_EQUAL_SIGN %d\n", G_TOKEN_EQUAL_SIGN); g_print("G_TOKEN_COMMA %d\n", G_TOKEN_COMMA); g_print("G_TOKEN_NONE %d\n", G_TOKEN_NONE); g_print("G_TOKEN_ERROR %d\n", G_TOKEN_ERROR); g_print("G_TOKEN_CHAR %d\n", G_TOKEN_CHAR); g_print("G_TOKEN_BINARY %d\n", G_TOKEN_BINARY); g_print("G_TOKEN_OCTAL %d\n", G_TOKEN_OCTAL); g_print("G_TOKEN_INT %d\n", G_TOKEN_INT); g_print("G_TOKEN_HEX %d\n", G_TOKEN_HEX); g_print("G_TOKEN_FLOAT %d\n", G_TOKEN_FLOAT); g_print("G_TOKEN_STRING %d\n", G_TOKEN_STRING); g_print("G_TOKEN_SYMBOL %d\n", G_TOKEN_SYMBOL); g_print("G_TOKEN_IDENTIFIER %d\n", G_TOKEN_IDENTIFIER); g_print("G_TOKEN_IDENTIFIER_NULL %d\n", G_TOKEN_IDENTIFIER_NULL); g_print("G_TOKEN_COMMENT_SINGLE %d\n", G_TOKEN_COMMENT_SINGLE); g_print("G_TOKEN_COMMENT_MULTI %d\n", G_TOKEN_COMMENT_MULTI); g_print("G_TOKEN_LAST %d\n", G_TOKEN_LAST); return 0; } で、 G_TOKEN_EOF 0 G_TOKEN_LEFT_PAREN 40 G_TOKEN_RIGHT_PAREN 41 G_TOKEN_LEFT_CURLY 123 G_TOKEN_RIGHT_CURLY 125 G_TOKEN_LEFT_BRACE 91 G_TOKEN_RIGHT_BRACE 93 G_TOKEN_EQUAL_SIGN 61 G_TOKEN_COMMA 44 G_TOKEN_NONE 256 G_TOKEN_ERROR 257 G_TOKEN_CHAR 258 G_TOKEN_BINARY 259 G_TOKEN_OCTAL 260 G_TOKEN_INT 261 G_TOKEN_HEX 262 G_TOKEN_FLOAT 263 G_TOKEN_STRING 264 G_TOKEN_SYMBOL 265 G_TOKEN_IDENTIFIER 266 G_TOKEN_IDENTIFIER_NULL 267 G_TOKEN_COMMENT_SINGLE 268 G_TOKEN_COMMENT_MULTI 269 G_TOKEN_LAST 270 !2005-10-24 Mon #include int main() { gchar *charset; gboolean ret; ret = g_get_charset(&charset); g_print("%d\n", ret); g_print("%s\n", charset); return 0; } で、 0 ANSI_X3.4-1968 コード変かも !2005-10-23 Sun #include int main() { gchar *str = "foo"; gchar *new_str; gsize size_read, size_write; GError *error; new_str = g_locale_from_utf8(str, strlen(str), &size_read, &size_write, &error); g_print("%s\n", new_str); g_print("%d\n", size_read); g_print("%d\n", size_write); g_free(new_str); return 0; } で、 foo 3 3 !2005-10-22 Sat #include int main() { gchar *str = "/home/foo"; gchar *new_str; GError *error; new_str = g_filename_to_uri(str, NULL, &error); g_print("%s\n", new_str); g_free(new_str); return 0; } で、 file:///home/foo !2005-10-21 Fri #include int main() { gchar *str = "file://home/foo"; gchar *new_str; GError *error; new_str = g_filename_from_uri(str, NULL, &error); g_print("%s\n", new_str); g_free(new_str); return 0; } で、 /foo ふに? !2005-10-20 Thu #include int main() { gchar *str = "foo"; gchar *new_str; gsize size_read, size_write; GError *error; new_str = g_filename_from_utf8(str, strlen(str), &size_read, &size_write, &error); g_print("%s\n", new_str); g_print("%d\n", size_read); g_print("%d\n", size_write); g_free(new_str); return 0; } で、 foo 3 3 !2005-10-19 Wed #include int main() { gchar *str = "foo"; gchar *new_str; gsize size_read, size_write; GError *error; new_str = g_filename_to_utf8(str, strlen(str), &size_read, &size_write, &error); g_print("%s\n", new_str); g_print("%d\n", size_read); g_print("%d\n", size_write); g_free(new_str); return 0; } で、 foo 3 3 何の意味と違いがあるのか不明 !2005-10-18 Tue #include int main() { gchar *str = "foo"; gchar *new_str; gsize size_read, size_write; GError *error; new_str = g_locale_to_utf8(str, strlen(str), &size_read, &size_write, &error); g_print("%s\n", new_str); g_print("%d\n", size_read); g_print("%d\n", size_write); g_free(new_str); return 0; } で、 foo 3 3 良くない例 !2005-10-17 Mon #include #define BUF_SIZE 128 int main() { gchar str[BUF_SIZE+1] = "こんにちは"; gchar new_str[BUF_SIZE+1]; gchar *in_str = str; gchar *out_str = new_str; gsize in_left = BUF_SIZE; gsize out_left = BUF_SIZE; GIConv converter; size_t size; converter = g_iconv_open("SHIFT_JIS", "EUC-JP"); size = g_iconv(converter, &in_str, &in_left, &out_str, &out_left); g_print("%s\n", new_str); g_print("%d\n", size); g_free(new_str); g_iconv_close(converter); return 0; } で、 [Invalid UTF-8] こんにちは 0 !2005-10-16 Sun #include int main() { gchar *str = "こんにちは"; gchar *new_str; gsize size_read, size_write; GError *error; GIConv converter; converter = g_iconv_open("SHIFT_JIS", "EUC-JP"); new_str = g_convert_with_iconv(str, strlen(str), converter, &size_read, &size_write, &error); g_print("%s\n", new_str); g_free(new_str); g_iconv_close(converter); return 0; } で、 [Invalid UTF-8] こんにちは !2005-10-15 Sat #include int main() { gchar *str = "こんにちは"; gchar *new_str; gsize size_read, size_write; GError *error; new_str = g_convert_with_fallback(str, strlen(str), "SHIFT_JIS", "EUC-JP", NULL, &size_read, &size_write, &error); g_print("%s\n", new_str); g_free(new_str); return 0; } で、 $ ./a.out |nkf -e [Invalid UTF-8] こんにちは !2005-10-14 Fri #include int main() { gchar *str = "こんにちは"; gchar *new_str; gsize size_read, size_write; GError *error; new_str = g_convert(str, strlen(str), "UTF-8", "EUC-JP", &size_read, &size_write, &error); g_print("%s\n", new_str); g_print("%d\n", size_read); g_print("%d\n", size_write); g_free(new_str); return 0; } で、 ????? 10 15 そもそも、ちゃんと変換されているのかも怪しい??? !2005-10-13 Thu #include int main() { gchar *str = "こんにちは"; gchar *new_str; gsize size_read, size_write; GError *error; new_str = g_convert(str, strlen(str), "ISO-2022-JP", "EUC-JP", &size_read, &size_write, &error); g_print("%s\n", new_str); g_print("%d\n", size_read); g_print("%d\n", size_write); g_free(new_str); return 0; } で、 $ ./a.out |nkf -e こんにちは 碓 嘘 $ ./a.out こんにちは 10 13 !2005-10-12 Wed #include int main() { gchar *str = "こんにちは"; gchar *new_str; gsize size_read, size_write; GError *error; new_str = g_convert(str, strlen(str), "SHIFT_JIS", "EUC-JP", &size_read, &size_write, &error); g_print("%s\n", new_str); g_print("%d\n", size_read); g_print("%d\n", size_write); g_free(new_str); return 0; } で、 $ ./a.out |nkf -e [Invalid UTF-8] こんにちは 10 10 [Invalid UTF-8] は g_print で出力されているようだ。 どうするのが正しい? !2005-10-11 Tue #include #include int main() { GTimer *timer; gulong micro; timer = g_timer_new(); sleep(3); g_timer_stop(timer); g_print("%f\n", g_timer_elapsed(timer, NULL)); sleep(3); g_print("%f\n", g_timer_elapsed(timer, µ)); g_print("%lu\n", micro); g_timer_destroy(timer); return 0; } で、 3.000531 3.000531 531 g_timer_reset() とばし。 この関数は使い道がありません。 なら、なぜ存在するー !2005-10-10 Mon #include #include int main() { GTimer *timer; timer = g_timer_new(); sleep(3); g_timer_stop(timer); sleep(3); g_print("%f\n", g_timer_elapsed(timer, NULL)); g_timer_destroy(timer); return 0; } で、 3.005560 !2005-10-09 Sun #include #include int main() { GTimer *timer; timer = g_timer_new(); sleep(3); g_print("%f\n", g_timer_elapsed(timer, NULL)); g_timer_start(timer); g_print("%f\n", g_timer_elapsed(timer, NULL)); g_timer_destroy(timer); return 0; } で、 3.003409 0.000003 !2005-10-08 Sat #include static void print_item(gpointer data, gpointer ud) { g_print("%s\n", (char *)data); } int main() { GCompletion *comp; GList *list = NULL; GList *comp_list; gchar *str; list = g_list_append(list, "FOO BAR"); list = g_list_append(list, "FOO BAZ"); comp = g_completion_new(NULL); g_completion_set_compare(comp, (GCompletionStrncmpFunc)g_ascii_strncasecmp); g_completion_add_items(comp, list); comp_list = g_completion_complete(comp, "foo", &str); g_print("%s\n", str); g_list_foreach(comp_list, print_item, NULL); g_free(str); g_completion_free(comp); g_list_free(comp_list); g_list_free(list); return 0; } で、 foo BA FOO BAR FOO BAZ !2005-10-07 Fri #include static void print_item(gpointer data, gpointer ud) { g_print("%s\n", (char *)data); } int main() { GCompletion *comp; GList *list = NULL; GList *comp_list; gchar *str; list = g_list_append(list, "foo bar"); list = g_list_append(list, "foo baz"); comp = g_completion_new(NULL); g_completion_add_items(comp, list); g_list_free(list); list = NULL; list = g_list_append(list, "foo baz"); g_completion_remove_items(comp, list); comp_list = g_completion_complete(comp, "foo", &str); g_print("%s\n", str); g_free(str); g_list_foreach(comp_list, print_item, NULL); g_completion_free(comp); g_list_free(comp_list); g_list_free(list); return 0; } で、 foo bar foo bar !2005-10-06 Thu #include static void print_item(gpointer data, gpointer ud) { g_print("%s\n", (char *)data); } int main() { GCompletion *comp; GList *list = NULL; GList *comp_list; gchar *str; list = g_list_append(list, "foo bar"); list = g_list_append(list, "foo baz"); comp = g_completion_new(NULL); g_completion_add_items(comp, list); comp_list = g_completion_complete(comp, "foo", &str); g_print("%s\n", str); g_free(str); g_list_foreach(comp_list, print_item, NULL); g_completion_clear_items(comp); comp_list = g_completion_complete(comp, "foo", &str); g_print("%s\n", str); g_free(str); g_completion_free(comp); g_list_free(comp_list); g_list_free(list); return 0; } で、 foo ba foo bar foo baz (null) !2005-10-05 Wed #include static void print_item(gpointer data, gpointer ud) { g_print("%s\n", (char *)data); } int main() { GCompletion *comp; GList *list = NULL; GList *comp_list; gchar *str; list = g_list_append(list, "foo bar"); list = g_list_append(list, "foo baz"); comp = g_completion_new(NULL); g_completion_add_items(comp, list); comp_list = g_completion_complete(comp, "foo", &str); g_print("%s\n", str); g_list_foreach(comp_list, print_item, NULL); g_free(str); g_completion_free(comp); g_list_free(comp_list); g_list_free(list); return 0; } で、 foo ba foo bar foo baz !2005-10-04 Tue #include int main() { gchar *str1, *str2; GError *error; str2 = g_shell_unquote("foo.c", &error); g_print("%s\n", str2); g_free(str2); str1 = g_shell_quote("foo.c"); str2 = g_shell_unquote(str1, &error); g_free(str1); g_print("%s\n", str2); g_free(str2); str1 = g_shell_quote("'foo.c'"); str2 = g_shell_unquote(str1, &error); g_free(str1); g_print("%s\n", str2); g_free(str2); return 0; } で、 foo.c (process:8052): GLib-CRITICAL **: file gshell.c: line 59 (unquote_string_inplace): assertion `err == NULL || *err == NULL' failed (null) (process:8052): GLib-CRITICAL **: file gshell.c: line 59 (unquote_string_inplace): assertion `err == NULL || *err == NULL' failed (null) あれ? !2005-10-03 Mon #include int main() { gchar *str; str = g_shell_quote("foo.c"); g_print("%s\n", str); g_free(str); str = g_shell_quote("'foo.c'"); g_print("%s\n", str); g_free(str); return 0; } で、 'foo.c' ''\''foo.c'\''' !2005-10-02 Sun #include int main() { gint argc; gchar **argvp; GError *error; g_shell_parse_argv("./foo bar baz", &argc, &argvp, &error); g_print("%s\n", argvp[0]); g_print("%s\n", argvp[1]); g_print("%s\n", argvp[2]); g_strfreev(argvp); return 0; } で、 ./foo bar baz !2005-10-01 Sat #include #include int main() { GDir *dir; GError *error; dir = g_dir_open("/tmp/", 0, &error); g_print("%s\n", g_dir_read_name(dir)); g_print("%s\n", g_dir_read_name(dir)); g_dir_rewind(dir); g_print("%s\n", g_dir_read_name(dir)); g_print("%s\n", g_dir_read_name(dir)); g_dir_close(dir); return 0; } で、 .iroha_unix .font-unix .iroha_unix .font-unix !2005-09-30 Fri #include #include int main() { GDir *dir; GError *error; dir = g_dir_open("/tmp/", 0, &error); g_print("%s\n", g_dir_read_name(dir)); g_print("%s\n", g_dir_read_name(dir)); g_dir_close(dir); return 0; } で、 .iroha_unix .font-unix !2005-09-29 Thu #include #include int main() { gchar fname[256] = "fooXXXXXX"; gchar *name; gint fd; GError *error; fd = g_file_open_tmp(fname, &name, &error); g_print("%s\n", fname); g_print("%s\n", name); g_print("%d\n", fd); close(fd); g_free(name); return 0; } で、 fooXXXXXX /tmp/fookUDRnb 4 !2005-09-28 Wed #include #include int main() { gchar fname[256] = "fooXXXXXX"; gint fd; fd = g_mkstemp(fname); g_print("%s\n", fname); g_print("%d\n", fd); close(fd); return 0; } で、 fooUWlRep 4 オープンしたファイルにはどう書き込めば良いんだっけ? !2005-09-27 Tue #include int main() { g_print("%d\n", g_file_test("foo.c", G_FILE_TEST_EXISTS)); g_print("%d\n", g_file_test("2005092700.c", G_FILE_TEST_EXISTS)); g_print("%d\n", g_file_test("2005092700.c", G_FILE_TEST_IS_REGULAR)); g_print("%d\n", g_file_test("2005092700.c", G_FILE_TEST_IS_SYMLINK)); g_print("%d\n", g_file_test(".", G_FILE_TEST_IS_DIR)); g_print("%d\n", g_file_test("a.out", G_FILE_TEST_IS_EXECUTABLE)); g_print("%d\n", g_file_test("2005092700.c", G_FILE_TEST_IS_EXECUTABLE | G_FILE_TEST_EXISTS)); g_print("%d\n", g_file_test("a.out", G_FILE_TEST_IS_EXECUTABLE | G_FILE_TEST_EXISTS)); return 0; } で、 0 1 1 0 1 1 1 1 !2005-09-26 Mon #include int main() { gchar *str; gint length; GError *error; g_file_get_contents("2005092600.c", &str, &length, &error); g_print("length: %d\n", length); if (str != NULL) { g_print("%s\n", str); } g_free(str); /*g_error_free(error);*/ return 0; } error の使い方不明 !2005-09-25 Sun #include #include int main() { g_print("%d\n", g_file_error_from_errno(EEXIST)); g_print("%d\n", G_FILE_ERROR_EXIST); g_print("%d\n", g_file_error_from_errno(EISDIR)); return 0; } で、 0 0 1 EEXIST は errno.h で良いのかな? !2005-09-24 Sat #include int main() { g_print("%d\n", g_pattern_match_simple(".emacs*", ".emacs")); g_print("%d\n", g_pattern_match_simple(".emacs*", ".emacs")); g_print("%d\n", g_pattern_match_simple(".emacs*", ".emacs~")); g_print("%d\n", g_pattern_match_simple(".emacs*", ".emacs.el")); g_print("%d\n", g_pattern_match_simple(".emacs*", "foo")); return 0; } で、 1 1 1 1 0 !2005-09-23 Fri #include int main() { GPatternSpec *pattern = g_pattern_spec_new(".emacs*"); g_print("%d\n", g_pattern_match_string(pattern, ".emacs")); g_print("%d\n", g_pattern_match_string(pattern, ".emacs")); g_print("%d\n", g_pattern_match_string(pattern, ".emacs~")); g_print("%d\n", g_pattern_match_string(pattern, ".emacs.el")); g_print("%d\n", g_pattern_match_string(pattern, "foo")); g_pattern_spec_free(pattern); return 0; } で、 1 1 1 1 0 !2005-09-22 Thu #include int main() { GPatternSpec *pattern = g_pattern_spec_new(".emacs*"); g_print("%d\n", g_pattern_match(pattern, 7, ".emacs", NULL)); g_print("%d\n", g_pattern_match(pattern, 6, ".emacs", NULL)); g_print("%d\n", g_pattern_match(pattern, 7, ".emacs~", NULL)); g_print("%d\n", g_pattern_match(pattern, 7, ".emacs.el", NULL)); g_print("%d\n", g_pattern_match(pattern, 4, "foo", NULL)); g_pattern_spec_free(pattern); return 0; } で、 1 1 1 1 0 * 文字列の正しいサイズというのは、終端を含むのだろうか? * マッチさせる対象の文字列はUTF-8 エンコーディングらしいが? !2005-09-21 Wed #include int main() { GPatternSpec *pattern1 = g_pattern_spec_new(".emacs*"); GPatternSpec *pattern2 = g_pattern_spec_new(".emacs*"); g_print("%d\n", g_pattern_spec_equal(pattern1, pattern2)); g_pattern_spec_free(pattern2); pattern2 = g_pattern_spec_new(".emacs"); g_print("%d\n", g_pattern_spec_equal(pattern1, pattern2)); g_pattern_spec_free(pattern1); g_pattern_spec_free(pattern2); return 0; } で、 1 0 コンパイル前の文字列が違って、同じセットという場合があるのだろうか? !2005-09-20 Tue g_atexit とばし。 #include int main() { GDebugKey debugkey[2] = { {"foo", 1}, {"bar", 2}, }; g_print("%d\n", g_parse_debug_string("", debugkey, 2)); g_print("%d\n", g_parse_debug_string(";", debugkey, 2)); g_print("%d\n", g_parse_debug_string(";foo", debugkey, 2)); g_print("%d\n", g_parse_debug_string("-foo", debugkey, 2)); g_print("%d\n", g_parse_debug_string("foo", debugkey, 2)); g_print("%d\n", g_parse_debug_string("bar", debugkey, 2)); g_print("%d\n", g_parse_debug_string("foo;bar", debugkey, 2)); return 0; } で、 0 0 0 0 1 2 0 セミコロン ':' で区切られたデバッグ・オプションを含む文字列 というのが意味不明 g_qsort_with_data とばし。 !2005-09-19 Mon #include int main() { int i; for (i = 10; i < 20; i++) { g_print("%d %d\n", i, g_spaced_primes_closest(i)); } return 0; } で、 10 11 11 19 12 19 13 19 14 19 15 19 16 19 17 19 18 19 19 37 単純に素数を返すわけではないようだ。 !2005-09-18 Sun #include int main() { int i; for (i = 0; i < 10; i++) { g_print("%d %d\n", i, g_bit_storage(i)); } return 0; } で、 0 1 1 1 2 2 3 2 4 3 5 3 6 3 7 3 8 4 9 4 !2005-09-17 Sat #include int main() { g_print("%d\n", g_bit_nth_msf(0xff, 0)); g_print("%d\n", g_bit_nth_msf(0xff, 1)); g_print("%d\n", g_bit_nth_msf(0x0f, 8)); return 0; } で、 -1 0 3 !2005-09-16 Fri #include int main() { g_print("%d\n", g_bit_nth_lsf(0xff, 0)); g_print("%d\n", g_bit_nth_lsf(0xff, -1)); g_print("%d\n", g_bit_nth_lsf(0xf0, 0)); g_print("%d\n", g_bit_nth_lsf(0, 0)); g_print("%d\n", g_bit_nth_lsf(0, -1)); return 0; } で、 1 0 4 -1 -1 何に使うんだろう?良く使うのか? !2005-09-15 Thu #include int main() { g_print("%s\n", g_find_program_in_path("ruby")); return 0; } で、 /usr/bin/ruby !2005-09-14 Wed #include int main() { g_print("%s\n", g_build_path("/", "home", NULL)); g_print("%s\n", g_build_path("/", "home", NULL)); g_print("%s\n", g_build_path("/", "home", "foo", NULL)); g_print("%s\n", g_build_path("/", "home", "foo", "bar", "baz", NULL)); g_print("%s\n", g_build_path("/", "home", "foo", "bar", "baz", NULL)); g_print("%s\n", g_build_path("::", "home", "foo", "bar", NULL)); return 0; } で、 home home home/foo home/foo/bar/baz home/foo/bar/baz home::foo::bar !2005-09-13 Tue #include int main() { g_print("%s\n", g_build_filename("/", "home", NULL)); g_print("%s\n", g_build_filename("/", "home", "/", "/", NULL)); g_print("%s\n", g_build_filename("/", "home", "/", "/", "foo", NULL)); g_print("%s\n", g_build_filename("/", "home", "foo", "bar", "baz", NULL)); g_print("%s\n", g_build_filename("home", "foo", "bar", "baz", NULL)); g_print("%s\n", g_build_filename("", NULL)); g_print("%s\n", g_build_filename(".", NULL)); return 0; } で、 /home /home/ /home/foo /home/foo/bar/baz home/foo/bar/baz . 最後は、NULL じゃないとダメらしい。 !2005-09-12 Mon #include int main() { g_print("%s\n", g_path_get_dirname("/")); g_print("%s\n", g_path_get_dirname("/usr")); g_print("%s\n", g_path_get_dirname("/usr/local/")); g_print("%s\n", g_path_get_dirname("foo")); g_print("%s\n", g_path_get_dirname("foo.c")); g_print("%s\n", g_path_get_dirname("/usr/local/foo")); g_print("%s\n", g_path_get_dirname("/usr/local/foo.c")); g_print("%s\n", g_path_get_dirname("")); g_print("%s\n", g_path_get_dirname(".")); return 0; } で、 / / /usr/local . . /usr/local /usr/local . . !2005-09-11 Sun #include int main() { g_print("%s\n", g_path_get_basename("/")); g_print("%s\n", g_path_get_basename("/usr")); g_print("%s\n", g_path_get_basename("/usr/local/")); g_print("%s\n", g_path_get_basename("foo")); g_print("%s\n", g_path_get_basename("foo.c")); g_print("%s\n", g_path_get_basename("/usr/local/foo")); g_print("%s\n", g_path_get_basename("/usr/local/foo.c")); g_print("%s\n", g_path_get_basename("")); g_print("%s\n", g_path_get_basename(".")); return 0; } で、 / usr local foo foo.c foo foo.c . . !2005-09-10 Sat #include int main() { g_print("%s\n", g_path_skip_root("/")); g_print("%s\n", g_path_skip_root("/usr")); g_print("%s\n", g_path_skip_root("/usr/local/")); g_print("%s\n", g_path_skip_root("foo")); g_print("%s\n", g_path_skip_root("")); g_print("%s\n", g_path_skip_root(".")); g_print("%s\n", g_path_skip_root("C:/Windows")); return 0; } で、 usr usr/local/ (null) (null) (null) (null) !2005-09-09 Fri #include int main() { g_print("%d\n", g_path_is_absolute("/")); g_print("%d\n", g_path_is_absolute("/usr")); g_print("%d\n", g_path_is_absolute("foo")); g_print("%d\n", g_path_is_absolute("")); g_print("%d\n", g_path_is_absolute(".")); return 0; } で、 1 1 0 0 0 !2005-09-08 Thu #include int main() { g_print("%s\n", g_get_current_dir()); return 0; } で、 /home/nnakamur/foo/bar/c/lib_glib/glib-2.2.x-refs 返された文字列は使用なくなりしだい解放して下さい。 特別にこれだけ書いてある理由はなぜなんだ? g_get_prgname() の場合は解放しなくても良いの? 内部で作り出していない文字列は解放しなくても良いってことかな? !2005-09-07 Wed #include int main() { g_print("%s\n", g_get_tmp_dir()); return 0; } で、 /tmp !2005-09-06 Tue #include int main() { g_print("%s\n", g_get_home_dir()); return 0; } で、 /home/nnakamur !2005-09-05 Mon #include int main() { g_print("%s\n", g_get_real_name()); return 0; } で、 Noritsugu Nakamura !2005-09-04 Sun #include int main() { g_print("%s\n", g_get_user_name()); return 0; } で、 nnakamur !2005-09-03 Sat #include int main() { g_print("%s\n", g_getenv("HOME")); return 0; } で、 /home/nnakamur !2005-09-02 Fri #include int main() { g_set_prgname("hoge hoge"); g_print("%s\n", g_get_prgname()); return 0; } で、 hoge hoge GTK+ で使っている場合は、 argv[0] が自動的ににセットされるらしい !2005-09-01 Thu #include int main() { g_set_prgname("hoge hoge"); g_print("%s\n", g_get_application_name()); return 0; } で、 hoge hoge !2005-08-31 Wed #include int main() { g_print("%s\n", g_get_application_name()); return 0; } で、 (null) マシンによってはコンパイルできないや !2005-08-30 Tue #include int main() { int i; g_random_set_seed(0); for (i = 0; i < 10; i++) { g_print("%f\n", g_random_double_range(1, 4)); } return 0; } で、 2.527604 3.563732 2.826885 1.676108 3.959594 1.285097 3.903090 1.229046 1.789599 3.853420 !2005-08-29 Mon #include int main() { int i; g_random_set_seed(0); for (i = 0; i < 10; i++) { g_print("%f\n", g_random_double()); } return 0; } で、 0.509201 0.854577 0.608962 0.225369 0.986531 0.095032 0.967697 0.076349 0.263200 0.951140 !2005-08-28 Sun #include int main() { int i; g_random_set_seed(0); for (i = 0; i < 10; i++) { g_print("%d\n", g_random_int_range(1, 4)); } return 0; } で、 2 2 1 3 1 2 1 1 3 3 !2005-08-27 Sat #include int main() { int i; g_random_set_seed(0); for (i = 0; i < 10; i++) { g_print("%d\n", g_random_boolean()); } return 0; } で、 0 0 0 1 1 1 0 1 0 0 !2005-08-26 Fri #include int main() { g_random_set_seed(0); g_print("%d\n", g_random_int()); g_print("%d\n", g_random_int()); g_print("%d\n", g_random_int()); g_random_set_seed(0); g_print("%d\n", g_random_int()); g_print("%d\n", g_random_int()); g_print("%d\n", g_random_int()); return 0; } で、 -1499714605 -2107964773 472388773 -1499714605 -2107964773 472388773 !2005-08-25 Thu #include int main() { GRand *rand; int i; rand = g_rand_new(); for (i = 0; i < 10; i++) { g_print("%f\n", g_rand_double_range(rand, 1, 4)); } g_rand_free(rand); return 0; } で、 1.308318 3.103418 2.020490 3.210243 1.389571 1.662954 3.385328 3.053891 1.922313 2.492741 !2005-08-24 Wed #include int main() { GRand *rand; rand = g_rand_new(); g_print("%f\n", g_rand_double(rand)); g_print("%f\n", g_rand_double(rand)); g_print("%f\n", g_rand_double(rand)); g_rand_free(rand); return 0; } で、 0.193848 0.246011 0.180157 !2005-08-23 Tue #include int main() { GRand *rand; int i; rand = g_rand_new(); for (i = 0; i < 10; i++) { g_print("%d\n", g_rand_int_range(rand, 1, 4)); } g_rand_free(rand); return 0; } で、 2 3 1 2 1 3 2 2 2 3 !2005-08-22 Mon #include int main() { GRand *rand; int i; rand = g_rand_new(); for (i = 0; i < 10; i++) { g_print("%d\n", g_rand_boolean(rand)); } g_rand_free(rand); return 0; } で、 0 0 0 1 0 1 0 1 0 1 !2005-08-21 Sun #include int main() { GRand *rand; rand = g_rand_new(); g_rand_set_seed(rand, 0); g_print("%d\n", g_rand_int(rand)); g_print("%d\n", g_rand_int(rand)); g_print("%d\n", g_rand_int(rand)); g_rand_free(rand); rand = g_rand_new(); g_rand_set_seed(rand, 0); g_print("%d\n", g_rand_int(rand)); g_print("%d\n", g_rand_int(rand)); g_print("%d\n", g_rand_int(rand)); g_rand_free(rand); return 0; } で、 -1499714605 -2107964773 472388773 -1499714605 -2107964773 472388773 !2005-08-20 Sat #include int main() { GRand *rand; rand = g_rand_new(); g_print("%d\n", g_rand_int(rand)); g_print("%d\n", g_rand_int(rand)); g_print("%d\n", g_rand_int(rand)); g_rand_free(rand); rand = g_rand_new(); g_print("%d\n", g_rand_int(rand)); g_print("%d\n", g_rand_int(rand)); g_print("%d\n", g_rand_int(rand)); g_rand_free(rand); return 0; } で、 861768318 -1493869331 -2121977191 1369339628 882233272 1220467300 !2005-08-19 Fri #include int main() { GRand *rand; rand = g_rand_new_with_seed(0); g_print("%d\n", g_rand_int(rand)); g_print("%d\n", g_rand_int(rand)); g_print("%d\n", g_rand_int(rand)); g_rand_free(rand); rand = g_rand_new_with_seed(0); g_print("%d\n", g_rand_int(rand)); g_print("%d\n", g_rand_int(rand)); g_print("%d\n", g_rand_int(rand)); g_rand_free(rand); return 0; } で、 -1499714605 -2107964773 472388773 -1499714605 -2107964773 472388773 !2005-08-18 Thu #include #include int main() { GDate date; g_print("%d %d %d %d %d %d\n", date.julian_days, date.julian, date.dmy, date.day, date.month, date.year); g_print("%d %d %d %d\n", g_date_get_julian(&date), g_date_get_day(&date), g_date_get_month(&date), g_date_get_year(&date)); g_print("%d\n", g_date_valid(&date)); g_date_clear(&date, 1); g_print("%d\n", g_date_valid(&date)); g_print("%d %d %d %d %d %d\n", date.julian_days, date.julian, date.dmy, date.day, date.month, date.year); g_print("%d %d %d %d\n", g_date_get_julian(&date), g_date_get_day(&date), g_date_get_month(&date), g_date_get_year(&date)); return 0; } で、 1073780672 0 0 46 11 65535 (process:4011): GLib-CRITICAL **: file gdate.c: line 286 (g_date_get_year): assertion `g_date_valid (d)' failed (process:4011): GLib-CRITICAL **: file gdate.c: line 271 (g_date_get_month): assertion `g_date_valid (d)' failed (process:4011): GLib-CRITICAL **: file gdate.c: line 301 (g_date_get_day): assertion `g_date_valid (d)' failed (process:4011): GLib-CRITICAL **: file gdate.c: line 316 (g_date_get_julian): assertion `g_date_valid (d)' failed 0 0 0 0 0 0 0 0 0 0 0 0 (process:4011): GLib-CRITICAL **: file gdate.c: line 286 (g_date_get_year): assertion `g_date_valid (d)' failed (process:4011): GLib-CRITICAL **: file gdate.c: line 271 (g_date_get_month): assertion `g_date_valid (d)' failed (process:4011): GLib-CRITICAL **: file gdate.c: line 301 (g_date_get_day): assertion `g_date_valid (d)' failed (process:4011): GLib-CRITICAL **: file gdate.c: line 316 (g_date_get_julian): assertion `g_date_valid (d)' failed 0 0 0 0 いまいち良く分からない !2005-08-17 Wed #include #include int main() { g_print("%d\n", G_DATE_BAD_DAY); g_print("%d\n", G_DATE_BAD_JULIAN); g_print("%d\n", G_DATE_BAD_YEAR); return 0; } で、 0 0 0 !2005-08-16 Tue #include #include int main() { g_print("%d\n", g_date_valid_weekday(0)); g_print("%d\n", g_date_valid_weekday(1)); g_print("%d\n", g_date_valid_weekday(7)); g_print("%d\n", g_date_valid_weekday(8)); return 0; } で、 0 1 1 0 !2005-08-15 Mon #include #include int main() { g_print("%d\n", g_date_valid_julian(0)); g_print("%d\n", g_date_valid_julian(1)); return 0; } で、 0 1 !2005-08-14 Sun #include #include int main() { g_print("%d\n", g_date_valid_dmy(14, 8, 2005)); g_print("%d\n", g_date_valid_dmy(32, 8, 2005)); return 0; } で、 1 0 !2005-08-13 Sat #include #include #include int main() { g_print("%d\n", g_date_valid_year(0)); g_print("%d\n", g_date_valid_year(1)); g_print("%d\n", g_date_valid_year(pow(2, 16))); g_print("%d\n", g_date_valid_year(pow(2, 16)-1)); return 0; } で、 0 1 0 1 !2005-08-12 Fri #include #include int main() { g_print("%d\n", g_date_valid_month(0)); g_print("%d\n", g_date_valid_month(1)); g_print("%d\n", g_date_valid_month(11)); g_print("%d\n", g_date_valid_month(12)); g_print("%d\n", g_date_valid_month(13)); return 0; } で、 0 1 1 1 0 !2005-08-11 Thu g_date_to_struct_tm とばし #include #include int main() { g_print("%d\n", g_date_valid_day(0)); g_print("%d\n", g_date_valid_day(1)); g_print("%d\n", g_date_valid_day(30)); g_print("%d\n", g_date_valid_day(31)); g_print("%d\n", g_date_valid_day(32)); return 0; } で、 0 1 1 1 0 !2005-08-10 Wed #include #include int main() { GDate *date; gchar str[256]; date = g_date_new(); g_date_set_time(date, time(NULL)); g_date_strftime(str, 256, "%Y-%m-%d", date); g_print("%s\n", str); g_date_strftime(str, 256, "%a %A %b %B %c %C %d %D %e %E %G %g %h %H %I", date); g_print("%s\n", str); g_date_free(date); return 0; } で、 2005-08-15 Mon Monday Aug August Mon Aug 15 00:00:00 2005 20 15 08/15/05 15 %E 2005 05 Aug 00 12 !2005-08-09 Tue #include #include int main() { int i; for (i = 2000; i < 2010; i++) { g_print("%d %d\n", i, g_date_get_sunday_weeks_in_year(i)); } return 0; } で、 2000 53 2001 52 2002 52 2003 52 2004 52 2005 52 2006 53 2007 52 2008 52 2009 52 !2005-08-08 Mon #include #include int main() { GDate *date; date = g_date_new(); g_date_set_time(date, time(NULL)); g_print("%d\n", g_date_get_sunday_week_of_year(date)); g_date_subtract_days(date, 10); g_print("%d\n", g_date_get_sunday_week_of_year(date)); g_date_free(date); return 0; } で、 33 31 !2005-08-07 Sun #include #include int main() { int i; for (i = 2000; i < 2010; i++) { g_print("%d %d\n", i, g_date_get_monday_weeks_in_year(i)); } return 0; } で、 2000 52 2001 53 2002 52 2003 52 2004 52 2005 52 2006 52 2007 53 2008 52 2009 52 !2005-08-06 Sat #include #include int main() { GDate *date; date = g_date_new(); g_date_set_time(date, time(NULL)); g_print("%d\n", g_date_get_monday_week_of_year(date)); g_date_subtract_days(date, 10); g_print("%d\n", g_date_get_monday_week_of_year(date)); g_date_free(date); return 0; } で、 33 31 !2005-08-05 Fri #include #include int main() { GDate *date; date = g_date_new(); g_date_set_time(date, time(NULL)); g_print("%d\n", g_date_get_monday_week_of_year(date)); g_date_subtract_days(date, 10); g_print("%d\n", g_date_get_monday_week_of_year(date)); g_date_free(date); return 0; } で、 31 30 !2005-08-04 Thu #include #include int main() { int i; for (i = 2000; i < 2010; i++) { g_print("%d %d\n", i, g_date_is_leap_year(i)); } return 0; } で、 2000 1 2001 0 2002 0 2003 0 2004 1 2005 0 2006 0 2007 0 2008 1 2009 0 !2005-08-03 Wed #include #include int main() { GDate *date; date = g_date_new(); g_date_set_time(date, time(NULL)); g_print("%d\n", g_date_is_last_of_month(date)); g_date_subtract_days(date, 3); g_print("%d\n", g_date_is_last_of_month(date)); g_date_free(date); return 0; } で、 0 1 !2005-08-02 Tue #include #include int main() { GDate *date; date = g_date_new(); g_date_set_time(date, time(NULL)); g_print("%d\n", g_date_is_first_of_month(date)); g_date_subtract_days(date, 1); g_print("%d\n", g_date_is_first_of_month(date)); g_date_free(date); return 0; } で、 0 1 !2005-08-01 Mon #include #include int main() { int i; for (i = 1; i <= 12; i++) { g_print("%2d: %d\n", i, g_date_get_days_in_month(i, 2005)); } return 0; } で、 1: 31 2: 28 3: 31 4: 30 5: 31 6: 30 7: 31 8: 31 9: 30 10: 31 11: 30 12: 31 !2005-07-31 Sun #include #include int main() { GDate *date; date = g_date_new(); g_date_set_time(date, time(NULL)); g_print("%d %d\n", g_date_get_weekday(date), g_date_get_day_of_year(date)); g_date_free(date); return 0; } で、 5 210 !2005-07-30 Sat #include #include int main() { GDate *date1, *date2; date1 = g_date_new(); date2 = g_date_new(); g_date_set_time(date1, time(NULL)); g_date_set_time(date2, time(NULL)); g_date_add_days(date1, 10); g_print("%d %d %d\n", g_date_get_year(date1), g_date_get_month(date1), g_date_get_day(date1)); g_print("%d %d %d\n", g_date_get_year(date2), g_date_get_month(date2), g_date_get_day(date2)); g_date_order(date1, date2); g_print("%d %d %d\n", g_date_get_year(date1), g_date_get_month(date1), g_date_get_day(date1)); g_print("%d %d %d\n", g_date_get_year(date2), g_date_get_month(date2), g_date_get_day(date2)); g_date_free(date1); g_date_free(date2); return 0; } で、 2005 8 8 2005 7 29 2005 7 29 2005 8 8 !2005-07-29 Fri #include #include int main() { GDate *date, *min_date, *max_date; date = g_date_new(); min_date = g_date_new(); max_date = g_date_new(); g_date_set_time(date, time(NULL)); g_date_set_time(min_date, time(NULL)); g_date_set_time(max_date, time(NULL)); g_date_subtract_days(min_date, 10); g_date_add_days(max_date, 10); g_print("%d %d %d\n", g_date_get_year(date), g_date_get_month(date), g_date_get_day(date)); g_date_clamp(date, min_date, max_date); g_print("%d %d %d\n", g_date_get_year(date), g_date_get_month(date), g_date_get_day(date)); /* -20 */ g_date_set_time(date, time(NULL)); g_date_subtract_days(date, 20); g_print("%d %d %d -> ", g_date_get_year(date), g_date_get_month(date), g_date_get_day(date)); g_date_clamp(date, min_date, max_date); g_print("%d %d %d\n", g_date_get_year(date), g_date_get_month(date), g_date_get_day(date)); /* +20 */ g_date_set_time(date, time(NULL)); g_date_add_days(date, 20); g_print("%d %d %d -> ", g_date_get_year(date), g_date_get_month(date), g_date_get_day(date)); g_date_clamp(date, min_date, max_date); g_print("%d %d %d\n", g_date_get_year(date), g_date_get_month(date), g_date_get_day(date)); g_date_free(date); g_date_free(min_date); g_date_free(max_date); return 0; } で、 2005 7 29 2005 7 29 2005 7 9 -> 2005 7 19 2005 8 18 -> 2005 8 8 !2005-07-28 Thu #include #include int main() { GDate *date1, *date2; date1 = g_date_new(); date2 = g_date_new(); g_date_set_time(date1, time(NULL)); g_date_set_time(date2, time(NULL)); g_print("%d\n", g_date_compare(date1, date2)); g_date_add_days(date2, 10); g_print("%d\n", g_date_compare(date1, date2)); g_print("%d\n", g_date_compare(date2, date1)); g_date_free(date1); g_date_free(date2); return 0; } で、 0 -1 1 !2005-07-27 Wed #include #include int main() { GDate *date1, *date2; date1 = g_date_new(); date2 = g_date_new(); g_date_set_time(date1, time(NULL)); g_date_set_time(date2, time(NULL)); g_date_add_days(date2, 10); g_print("%d\n", g_date_days_between(date1, date2)); g_date_free(date1); g_date_free(date2); return 0; } で、 10 !2005-07-26 Tue #include #include int main() { GDate *date; date = g_date_new(); g_date_set_time(date, time(NULL)); g_print("%d %d %d %d\n", g_date_get_julian(date), g_date_get_day(date), g_date_get_month(date), g_date_get_year(date)); g_date_subtract_years(date, 1); g_print("%d %d %d %d\n", g_date_get_julian(date), g_date_get_day(date), g_date_get_month(date), g_date_get_year(date)); g_date_free(date); return 0; } で、 732153 26 7 2005 731788 26 7 2004 !2005-07-25 Mon #include #include int main() { GDate *date; date = g_date_new(); g_date_set_time(date, time(NULL)); g_print("%d %d %d %d\n", g_date_get_julian(date), g_date_get_day(date), g_date_get_month(date), g_date_get_year(date)); g_date_add_years(date, 1); g_print("%d %d %d %d\n", g_date_get_julian(date), g_date_get_day(date), g_date_get_month(date), g_date_get_year(date)); g_date_free(date); return 0; } で、 732152 25 7 2005 732517 25 7 2006 !2005-07-24 Sun #include #include int main() { GDate *date; date = g_date_new(); g_date_set_time(date, time(NULL)); g_print("%d %d %d %d\n", g_date_get_julian(date), g_date_get_day(date), g_date_get_month(date), g_date_get_year(date)); g_date_subtract_months(date, 1); g_print("%d %d %d %d\n", g_date_get_julian(date), g_date_get_day(date), g_date_get_month(date), g_date_get_year(date)); g_date_free(date); return 0; } で、 732149 22 7 2005 732119 22 6 2005 !2005-07-23 Sat #include #include int main() { GDate *date; date = g_date_new(); g_date_set_time(date, time(NULL)); g_print("%d %d %d %d\n", g_date_get_julian(date), g_date_get_day(date), g_date_get_month(date), g_date_get_year(date)); g_date_add_months(date, 1); g_print("%d %d %d %d\n", g_date_get_julian(date), g_date_get_day(date), g_date_get_month(date), g_date_get_year(date)); g_date_free(date); return 0; } で、 732149 22 7 2005 732180 22 8 2005 !2005-07-22 Fri #include #include int main() { GDate *date; date = g_date_new(); g_date_set_time(date, time(NULL)); g_print("%d %d %d %d\n", g_date_get_julian(date), g_date_get_day(date), g_date_get_month(date), g_date_get_year(date)); g_date_subtract_days(date, 1); g_print("%d %d %d %d\n", g_date_get_julian(date), g_date_get_day(date), g_date_get_month(date), g_date_get_year(date)); g_date_free(date); return 0; } で、 732149 22 7 2005 732148 21 7 2005 !2005-07-21 Thu #include #include int main() { GDate *date; date = g_date_new(); g_date_set_time(date, time(NULL)); g_print("%d %d %d %d\n", g_date_get_julian(date), g_date_get_day(date), g_date_get_month(date), g_date_get_year(date)); g_date_add_days(date, 1); g_print("%d %d %d %d\n", g_date_get_julian(date), g_date_get_day(date), g_date_get_month(date), g_date_get_year(date)); g_date_free(date); return 0; } で、 732148 21 7 2005 732149 22 7 2005 !2005-07-20 Wed #include #include int main() { GDate *date; date = g_date_new(); g_date_set_parse(date, "Wed Jul 20 17:40:25 JST 2005"); g_print("%d\n", g_date_valid(date)); g_date_set_parse(date, "Wed Jul 20 JST 2005"); g_print("%d\n", g_date_valid(date)); g_date_set_parse(date, "Tue Jul 20 JST 2005"); g_print("%d\n", g_date_valid(date)); g_print("%d %d %d\n", g_date_get_year(date), g_date_get_month(date), g_date_get_day(date)); g_date_free(date); return 0; } で、 0 1 1 2005 7 20 * 時間が入っているとダメなのかな? * 曜日はチェックしていないの? !2005-07-19 Tue #include #include int main() { GDate *date; date = g_date_new(); g_date_set_time(date, time(NULL)); g_print("%d %d %d %d %d %d\n", date->julian_days, date->julian, date->dmy, date->day, date->month, date->year); g_print("%d %d %d %d\n", g_date_get_julian(date), g_date_get_day(date), g_date_get_month(date), g_date_get_year(date)); g_date_free(date); return 0; } で、 0 0 1 19 7 2005 732146 19 7 2005 !2005-07-18 Mon #include int main() { GDate *date; date = g_date_new(); g_date_set_julian(date, 365 * 2000); g_print("%d %d %d %d %d %d\n", date->julian_days, date->julian, date->dmy, date->day, date->month, date->year); g_print("%d %d %d %d\n", g_date_get_julian(date), g_date_get_day(date), g_date_get_month(date), g_date_get_year(date)); g_date_free(date); return 0; } で、 730000 1 0 0 0 0 730000 3 9 1999 !2005-07-17 Snn #include int main() { GDate *date; date = g_date_new(); g_date_set_dmy(date, 17, 7, 2005); g_print("%d %d %d %d %d %d\n", date->julian_days, date->julian, date->dmy, date->day, date->month, date->year); g_print("%d %d %d %d\n", g_date_get_julian(date), g_date_get_day(date), g_date_get_month(date), g_date_get_year(date)); g_date_free(date); return 0; } で、 0 0 1 17 7 2005 732144 17 7 2005 !2005-07-16 Sat #include int main() { GDate *date; date = g_date_new(); g_date_set_day(date, 16); g_date_set_month(date, 7); g_date_set_year(date, 2005); g_print("%d %d %d %d %d %d\n", date->julian_days, date->julian, date->dmy, date->day, date->month, date->year); g_print("%d %d %d %d\n", g_date_get_julian(date), g_date_get_day(date), g_date_get_month(date), g_date_get_year(date)); g_date_free(date); return 0; } で、 0 0 1 16 7 2005 732143 16 7 2005 !2005-07-15 Fri #include int main() { GDate *date; date = g_date_new_julian(365 * 2000); g_print("%d %d %d %d\n", g_date_get_julian(date), g_date_get_day(date), g_date_get_month(date), g_date_get_year(date)); g_date_free(date); return 0; } で、 730000 3 9 1999 !2005-07-14 Thu #include int main() { GDate *date; date = g_date_new_julian(365 * 2000); g_print("%d %d %d %d %d %d\n", date->julian_days, date->julian, date->dmy, date->day, date->month, date->year); g_date_free(date); return 0; } で、 730000 1 0 0 0 0 !2005-07-13 Wed #include int main() { GDate *date; date = g_date_new_dmy(13, 7, 2005); g_print("%d %d %d %d %d %d\n", date->julian_days, date->julian, date->dmy, date->day, date->month, date->year); g_date_free(date); return 0; } で、 0 0 1 13 7 2005 dmy って Day, Month, Year か。dummy じゃなのね。 !2005-07-12 Tue #include int main() { g_print("%d\n", G_DATE_BAD_WEEKDAY); g_print("%d\n", G_DATE_MONDAY); g_print("%d\n", G_DATE_TUESDAY); g_print("%d\n", G_DATE_WEDNESDAY); g_print("%d\n", G_DATE_THURSDAY); g_print("%d\n", G_DATE_FRIDAY); g_print("%d\n", G_DATE_SATURDAY); g_print("%d\n", G_DATE_SUNDAY); return 0; } で、 0 1 2 3 4 5 6 7 !2005-07-11 Mon #include int main() { g_print("%d\n", G_DATE_BAD_MONTH); g_print("%d\n", G_DATE_JANUARY); g_print("%d\n", G_DATE_FEBRUARY); g_print("%d\n", G_DATE_MARCH); g_print("%d\n", G_DATE_APRIL); g_print("%d\n", G_DATE_MAY); g_print("%d\n", G_DATE_JUNE); g_print("%d\n", G_DATE_JULY); g_print("%d\n", G_DATE_AUGUST); g_print("%d\n", G_DATE_SEPTEMBER); g_print("%d\n", G_DATE_OCTOBER); g_print("%d\n", G_DATE_NOVEMBER); g_print("%d\n", G_DATE_DECEMBER); return 0; } で、 0 1 2 3 4 5 6 7 8 9 10 11 12 !2005-07-10 Sun #include int main() { g_print("%d\n", G_DATE_DAY); g_print("%d\n", G_DATE_MONTH); g_print("%d\n", G_DATE_YEAR); return 0; } で、 0 1 2 !2005-07-09 Sat #include int main() { GDate *date; date = g_date_new(); g_print("%d %d %d %d %d %d\n", date->julian_days, date->julian, date->dmy, date->day, date->month, date->year); g_date_free(date); return 0; } で、 0 0 0 0 0 0 !2005-07-08 Fri #include int main() { GDate *date; date = g_date_new(); g_date_free(date); return 0; } !2005-07-07 Thu #include int main() { GTimeVal timeval; g_get_current_time(&timeval); g_print("%ld %ld\n", timeval.tv_sec, timeval.tv_usec); g_time_val_add(&timeval, 10); g_print("%ld %ld\n", timeval.tv_sec, timeval.tv_usec); g_time_val_add(&timeval, 10 * G_USEC_PER_SEC); g_print("%ld %ld\n", timeval.tv_sec, timeval.tv_usec); return 0; } で、 1120705212 597689 1120705212 597699 1120705222 597699 !2005-07-06 Wed #include int main() { g_usleep(3 * G_USEC_PER_SEC); return 0; } で、 待ちました。 !2005-07-05 Tue #include int main() { GTimeVal timeval; g_get_current_time(&timeval); g_print("%ld %ld\n", timeval.tv_sec, timeval.tv_usec); return 0; } で、 1120532408 655520 !2005-07-04 Mon #include int main() { g_print("%d\n", G_USEC_PER_SEC); return 0; } で、 1000000 !2005-07-03 Sun #include int main() { GPtrArray *array; guint i; array = g_ptr_array_new(); g_ptr_array_add(array, "foo"); g_ptr_array_add(array, "bar"); g_ptr_array_add(array, "baz"); g_ptr_array_add(array, "hoge"); g_ptr_array_set_size(array, 2); for (i = 0; i < array->len; i++) { g_print("%s\n", (gchar*)g_ptr_array_index(array, i)); } return 0; } で、 foo bar !2005-07-02 Sat #include int main() { GPtrArray *array; guint i; array = g_ptr_array_new(); g_ptr_array_add(array, "foo"); g_ptr_array_add(array, "bar"); g_ptr_array_add(array, "baz"); g_ptr_array_add(array, "hoge"); g_ptr_array_sort_with_data(array, (GCompareFunc)g_ascii_strcasecmp, (char *)"foo"); for (i = 0; i < array->len; i++) { g_print("%s\n", (gchar*)g_ptr_array_index(array, i)); } return 0; } で、 foo bar baz hoge 依然として謎 GArray に比べ、微妙に関数が少ないな。なぜ?面倒だった? !2005-07-01 Fri #include int main() { GPtrArray *array; guint i; array = g_ptr_array_new(); g_ptr_array_add(array, "foo"); g_ptr_array_add(array, "bar"); g_ptr_array_add(array, "baz"); g_ptr_array_add(array, "hoge"); g_ptr_array_sort(array, (GCompareFunc)g_ascii_strcasecmp); for (i = 0; i < array->len; i++) { g_print("%s\n", (gchar*)g_ptr_array_index(array, i)); } return 0; } で、 foo bar baz hoge ? !2005-06-30 Thu #include int main() { GPtrArray *array; guint i; array = g_ptr_array_new(); g_ptr_array_add(array, "foo"); g_ptr_array_add(array, "bar"); g_ptr_array_add(array, "baz"); g_ptr_array_add(array, "hoge"); g_ptr_array_remove_index_fast(array, 1); for (i = 0; i < array->len; i++) { g_print("%s\n", (gchar*)g_ptr_array_index(array, i)); } g_ptr_array_free(array, TRUE); return 0; } で、 foo hoge baz !2005-06-29 Wed #include int main() { GPtrArray *array; guint i; array = g_ptr_array_new(); g_ptr_array_add(array, "foo"); g_ptr_array_add(array, "bar"); g_ptr_array_add(array, "baz"); g_ptr_array_add(array, "bar"); g_print("%d\n", g_ptr_array_remove_fast(array, "hoge")); g_print("%d\n", g_ptr_array_remove_fast(array, "bar")); for (i = 0; i < array->len; i++) { g_print("%s\n", (gchar*)g_ptr_array_index(array, i)); } g_ptr_array_free(array, TRUE); return 0; } で、 0 1 foo bar baz !2005-06-28 Tue #include int main() { GPtrArray *array; guint i; array = g_ptr_array_new(); g_ptr_array_add(array, "foo"); g_ptr_array_add(array, "bar"); g_ptr_array_add(array, "baz"); g_ptr_array_add(array, "bar"); g_ptr_array_remove_index(array, 1); for (i = 0; i < array->len; i++) { g_print("%s\n", (gchar*)g_ptr_array_index(array, i)); } g_ptr_array_remove_index(array, 1); g_ptr_array_remove_index(array, 1); g_ptr_array_free(array, TRUE); return 0; } で、 foo baz bar !2005-06-27 Mon #include int main() { GPtrArray *array; guint i; array = g_ptr_array_new(); g_ptr_array_add(array, "foo"); g_ptr_array_add(array, "bar"); g_ptr_array_add(array, "baz"); g_ptr_array_add(array, "bar"); g_print("%d\n", g_ptr_array_remove(array, "hoge")); g_print("%d\n", g_ptr_array_remove(array, "bar")); for (i = 0; i < array->len; i++) { g_print("%s\n", (gchar*)g_ptr_array_index(array, i)); } g_ptr_array_free(array, TRUE); return 0; } で、 0 1 foo baz bar 本当にこれで良いのか? array に入れた "bar" と後半の "bar" が同じという保証あるの? !2005-06-26 Sun #include int main() { GPtrArray *array; array = g_ptr_array_sized_new(10); g_print("%d\n", array->len); g_ptr_array_free(array, TRUE); return 0; } で、 0 !2005-06-25 Sat #include int main() { GPtrArray *array; array = g_ptr_array_new(); g_ptr_array_add(array, "foo"); g_print("%d\n", array->len); g_ptr_array_free(array, TRUE); return 0; } で、 1 !2005-06-24 Fri #include int main() { GPtrArray *array; array = g_ptr_array_new(); g_ptr_array_add(array, "foo"); g_print("%s\n", (gchar*)g_ptr_array_index(array, 0)); g_ptr_array_free(array, TRUE); return 0; } で、 foo !2005-06-23 Thu #include int main() { GArray *array; gdouble d; array = g_array_new(FALSE, FALSE, sizeof(double)); d = 1.2; g_array_append_val(array, d); d = 4.5; g_array_append_val(array, d); g_print("%f\n", g_array_index(array, gdouble, 0)); g_print("%f\n", g_array_index(array, gdouble, 1)); g_print("%f\n", g_array_index(array, gdouble, 2)); g_array_free(array, FALSE); return 0; } で、 1.200000 4.500000 0.000000 !2005-06-22 Wed #include int main() { GSList *list = NULL; int i; list = g_slist_append(list, GINT_TO_POINTER(1)); list = g_slist_append(list, GINT_TO_POINTER(2)); for (i = 0; i < (int)g_slist_length(list); i++) { g_print("%d\n", GPOINTER_TO_INT(g_slist_nth(list, i)->data)); g_print("%d\n", (int)(g_slist_nth(list, i)->data)); } g_slist_free(list); return 0; } で、 1 1 2 2 !2005-06-21 Tue #include int main() { g_print("%s\n", g_strsignal(1)); g_print("%s\n", g_strsignal(2)); return 0; } で、 Hangup Interrupt !2005-06-20 Mon #include int main() { g_print("%s\n", g_strerror(1)); g_print("%s\n", g_strerror(2)); return 0; } で、 Operation not permitted No such file or directory !2005-06-19 Sun #include int main() { gchar **tokens; gchar *str; tokens = g_strsplit("abc\ndef\nghi", "\n", 0); str = g_strjoinv(":", tokens); g_print("%s\n", str); g_strfreev(tokens); g_free(str); return 0; } で、 abc:def:ghi !2005-06-18 Sat #include int main() { gchar *str; str = g_strjoin(":", "abc", "def", "ghi", NULL); g_print("%s\n", str); g_free(str); return 0; } で、 abc:def:ghi !2005-06-17 Fri #include int main() { g_print("%s\n", g_strconcat("abc", "def", "ghi", NULL)); return 0; } で、 abcdefghi !2005-06-16 Thu #include int main() { gchar **tokens; tokens = g_strsplit("abc\ndef\nghi", "\n", 0); g_print("%s\n", tokens[0]); g_print("%s\n", tokens[1]); g_print("%s\n", tokens[2]); g_strfreev(tokens); tokens = g_strsplit("abc\ndef\nghi", "\n", 1); g_print("%s\n", tokens[0]); g_print("%s\n", tokens[1]); g_strfreev(tokens); tokens = g_strsplit("abc\ndef\nghi\n", "\n", 0); g_print("%s\n", tokens[0]); g_print("%s\n", tokens[1]); g_print("%s\n", tokens[2]); g_strfreev(tokens); tokens = g_strsplit("abc def ghi", " ", 0); g_print("%s\n", tokens[0]); g_print("%s\n", tokens[1]); g_print("%s\n", tokens[2]); g_print("%s\n", tokens[3]); g_print("%s\n", tokens[4]); g_strfreev(tokens); return 0; } で、 abc def ghi abc def ghi (null) abc def ghi abc def ghi !2005-06-15 Wed #include int main() { gchar str[256] = "abcdefghi"; g_print("%s\n", g_strcanon(str, "abc", 'X')); g_print("%s\n", str); return 0; } で、 abcXXXXXX abcXXXXXX !2005-06-14 Tue #include int main() { gchar str[256] = " \tabc \t"; g_print("%s\n", g_strcompress(g_strescape(str, NULL))); g_print("%s\n", g_strcompress(g_strescape("\t\tabc", NULL))); g_print("%s\n", g_strcompress(g_strescape("\t\tabc", "\t"))); return 0; } で、 abc abc abc !2005-06-13 Mon #include int main() { gchar str[256] = " \tabc \t"; g_print("%s\n", g_strescape(str, NULL)); g_print("%s\n", str); g_print("%s\n", g_strescape("\t\tabc", NULL)); g_print("%s\n", g_strescape("\t\tabc", "\t")); return 0; } で、 \tabc \t abc \t\tabc abc !2005-06-12 Sun #include int main() { g_print("%s\n", G_STR_DELIMITERS); return 0; } で、 _-|> <. !2005-06-11 Sat #include int main() { gchar str[256] = "abc:def:ghi"; g_print("%s\n", g_strdelimit(str, ":", '-')); g_print("%s\n", str); g_stpcpy(str, "abc::def::ghi"); g_print("%s\n", g_strdelimit(str, "::", '-')); g_stpcpy(str, "abc::def::ghi"); g_print("%s\n", g_strdelimit(str, ":", '-')); g_stpcpy(str, "abc-def-ghi"); g_print("%s\n", g_strdelimit(str, NULL, '=')); g_stpcpy(str, "abc-|def|-ghi"); g_print("%s\n", g_strdelimit(str, NULL, '=')); return 0; } で、 abc-def-ghi abc-def-ghi abc--def--ghi abc--def--ghi abc=def=ghi abc==def==ghi !2005-06-10 Fri #include int main() { gchar str[256] = " \tabc \t"; g_print("%s\n", g_strstrip(str)); g_print("%s\n", str); return 0; } で、 abc abc !2005-06-09 Thu #include int main() { gchar str[256] = " \tabc \t"; g_print("%s\n", g_strchomp(str)); g_print("%s\n", str); return 0; } で、 abc abc !2005-06-08 Wed #include int main() { gchar str[256] = " \tabc \t"; g_print("%s\n", g_strchug(str)); g_print("%s\n", str); return 0; } で、 abc abc !2005-06-07 Tue #include int main() { g_print("%f\n", g_strtod("1.2345", NULL)); g_print("%f\n", g_strtod("1.0", NULL)); g_print("%f\n", g_strtod("0.5", NULL)); return 0; } で、 1.234500 1.000000 0.500000 !2005-06-06 Mon #include int main() { gchar str[256]; g_print("%s\n", g_ascii_formatd(str, 256, "%f", 1.2345)); g_print("%s\n", g_ascii_formatd(str, 256, "%e", 1.2345)); g_print("%s\n", g_ascii_formatd(str, 256, "%e", 1.0)); g_print("%s\n", g_ascii_formatd(str, 256, "%e", 0.5)); return 0; } で、 1.234500 1.234500e+00 1.000000e+00 5.000000e-01 !2005-06-05 Sun #include int main() { gchar str[256]; g_print("%s\n", g_ascii_dtostr(str, 256, 1.2345)); g_print("%s\n", str); g_print("%s\n", g_ascii_dtostr(str, 256, 1.0)); g_print("%s\n", g_ascii_dtostr(str, 256, 0.5)); return 0; } で、 1.2344999999999999 1.2344999999999999 1 0.5 !2005-06-04 Sat #include int main() { g_print("%f\n", g_ascii_strtod("1.2345", NULL)); g_print("%f\n", g_ascii_strtod("1.0", NULL)); g_print("%f\n", g_ascii_strtod("0.5", NULL)); g_print("%f\n", g_ascii_strtod("abc", NULL)); return 0; } で、 1.234500 1.000000 0.500000 0.000000 !2005-06-03 Fri #include int main() { g_print("%d\n", G_ASCII_DTOSTR_BUF_SIZE); return 0; } で、 39 !2005-06-02 Thu #include int main() { g_print("%lld\n", g_ascii_strtoull("12345", NULL, 10)); g_print("%lld\n", g_ascii_strtoull("01010", NULL, 2)); g_print("%lld\n", g_ascii_strtoull("ff", NULL, 16)); g_print("%lld\n", g_ascii_strtoull("a", NULL, 10)); return 0; } で、 12345 10 255 0 2.0 にはないや? !2005-06-01 Wed #include int main() { gchar str[256] = "abcdefg"; #if 0 /* Segmentation fault */ g_print("%s\n", g_strreverse("abcdefg")); #endif g_print("%s\n", g_strreverse(str)); return 0; } で、 gfedcba !2005-05-31 Tue #include int main() { GString *str; str = g_string_ascii_down(g_string_new("ABC")); g_print("%s\n", str->str); g_string_free(str, TRUE); str = g_string_ascii_down(g_string_new("abc")); g_print("%s\n", str->str); g_string_free(str, TRUE); return 0; } で、 abc abc !2005-05-30 Mon #include int main() { GString *str; str = g_string_ascii_up(g_string_new("abc")); g_print("%s\n", str->str); g_string_free(str, TRUE); str = g_string_ascii_up(g_string_new("ABC")); g_print("%s\n", str->str); g_string_free(str, TRUE); str = g_string_new("abc"); g_string_ascii_up(str); g_print("%s\n", str->str); g_string_free(str, TRUE); return 0; } で、 ABC ABC ABC !2005-05-29 Sun #include int main() { g_print("%c\n", g_ascii_toupper('a')); g_print("%c\n", g_ascii_toupper('A')); return 0; } で、 A A !2005-05-28 Sat #include int main() { g_print("%c\n", g_ascii_tolower('A')); g_print("%c\n", g_ascii_tolower('a')); return 0; } で、 a a !2005-05-27 Fri #include int main() { g_print("%s\n", g_ascii_strdown("abc", -1)); g_print("%s\n", g_ascii_strdown("ABC", -1)); g_print("%s\n", g_ascii_strdown("abc", 2)); g_print("%s\n", g_ascii_strdown("ABC", 2)); return 0; } で、 abc abc ab ab !2005-05-26 Thu #include int main() { g_print("%s\n", g_ascii_strup("abc", -1)); g_print("%s\n", g_ascii_strup("ABC", -1)); g_print("%s\n", g_ascii_strup("abc", 2)); g_print("%s\n", g_ascii_strup("ABC", 2)); return 0; } で、 ABC ABC AB AB !2005-05-25 Wed #include int main() { g_print("%d\n", g_ascii_strncasecmp("abc", "abc", 0)); g_print("%d\n", g_ascii_strncasecmp("abc", "abc", 5)); g_print("%d\n", g_ascii_strncasecmp("abc", "ABC", 3)); g_print("%d\n", g_ascii_strncasecmp("CCB", "CCD", 2)); g_print("%d\n", g_ascii_strncasecmp("CCB", "CCD", 3)); return 0; } で、 0 0 0 0 -2 !2005-05-24 Tue #include int main() { g_print("%d\n", g_ascii_strcasecmp("abc", "abc")); g_print("%d\n", g_ascii_strcasecmp("abc", "ABC")); g_print("%d\n", g_ascii_strcasecmp("CCB", "BBC")); g_print("%d\n", g_ascii_strcasecmp("abc", "CCB")); return 0; } で、 0 0 1 -2 !2005-05-23 Mon #include int main() { g_print("%d\n", g_ascii_xdigit_value('a')); g_print("%d\n", g_ascii_xdigit_value('A')); g_print("%d\n", g_ascii_xdigit_value('F')); g_print("%d\n", g_ascii_xdigit_value('G')); g_print("%d\n", g_ascii_xdigit_value('0')); g_print("%d\n", g_ascii_xdigit_value(' ')); g_print("%d\n", g_ascii_xdigit_value('\n')); return 0; } で、 10 10 15 -1 0 -1 -1 !2005-05-22 Sun #include int main() { g_print("%d\n", g_ascii_digit_value('a')); g_print("%d\n", g_ascii_digit_value('A')); g_print("%d\n", g_ascii_digit_value('F')); g_print("%d\n", g_ascii_digit_value('G')); g_print("%d\n", g_ascii_digit_value('0')); g_print("%d\n", g_ascii_digit_value(' ')); g_print("%d\n", g_ascii_digit_value('\n')); return 0; } で、 -1 -1 -1 -1 0 -1 -1 !2005-05-21 Sat #include int main() { g_print("%d\n", g_ascii_isxdigit('a')); g_print("%d\n", g_ascii_isxdigit('A')); g_print("%d\n", g_ascii_isxdigit('F')); g_print("%d\n", g_ascii_isxdigit('G')); g_print("%d\n", g_ascii_isxdigit('0')); g_print("%d\n", g_ascii_isxdigit(' ')); g_print("%d\n", g_ascii_isxdigit('\n')); return 0; } で、 1 1 1 0 1 0 0 !2005-05-20 Fri #include int main() { g_print("%d\n", g_ascii_isupper('a')); g_print("%d\n", g_ascii_isupper('A')); g_print("%d\n", g_ascii_isupper('0')); g_print("%d\n", g_ascii_isupper(' ')); g_print("%d\n", g_ascii_isupper('\n')); return 0; } で、 0 1 0 0 0 !2005-05-19 Thu #include int main() { g_print("%d\n", g_ascii_isspace('a')); g_print("%d\n", g_ascii_isspace('A')); g_print("%d\n", g_ascii_isspace('0')); g_print("%d\n", g_ascii_isspace(' ')); g_print("%d\n", g_ascii_isspace('\n')); return 0; } で、 0 0 0 1 1 !2005-05-18 Wed 文字が (スペースと英数字を除く) 句読点かどうか だと? #include int main() { g_print("%d\n", g_ascii_ispunct('a')); g_print("%d\n", g_ascii_ispunct('A')); g_print("%d\n", g_ascii_ispunct('0')); g_print("%d\n", g_ascii_ispunct(' ')); g_print("%d\n", g_ascii_ispunct('\n')); g_print("%d\n", g_ascii_ispunct(',')); g_print("%d\n", g_ascii_ispunct('.')); g_print("%d\n", g_ascii_ispunct('-')); g_print("%d\n", g_ascii_ispunct('=')); g_print("%d\n", g_ascii_ispunct('^')); return 0; } で、 0 0 0 0 0 1 1 1 1 1 !2005-05-17 Tue #include int main() { g_print("%d\n", g_ascii_isprint('a')); g_print("%d\n", g_ascii_isprint('A')); g_print("%d\n", g_ascii_isprint('0')); g_print("%d\n", g_ascii_isprint(' ')); g_print("%d\n", g_ascii_isprint('\n')); return 0; } で、 1 1 1 1 0 !2005-05-16 Mon #include int main() { g_print("%d\n", g_ascii_islower('a')); g_print("%d\n", g_ascii_islower('A')); g_print("%d\n", g_ascii_islower('0')); g_print("%d\n", g_ascii_islower(' ')); g_print("%d\n", g_ascii_islower('\n')); return 0; } で、 1 0 0 0 0 !2005-05-15 Sun #include int main() { g_print("%d\n", g_ascii_isgraph('a')); g_print("%d\n", g_ascii_isgraph('0')); g_print("%d\n", g_ascii_isgraph(' ')); g_print("%d\n", g_ascii_isgraph('\n')); return 0; } で、 1 1 0 0 !2005-05-14 Sat #include int main() { g_print("%d\n", g_ascii_isdigit('a')); g_print("%d\n", g_ascii_isdigit('0')); g_print("%d\n", g_ascii_isdigit(' ')); g_print("%d\n", g_ascii_isdigit('\n')); return 0; } で、 0 1 0 0 !2005-05-13 Fri #include int main() { g_print("%d\n", g_ascii_iscntrl('a')); g_print("%d\n", g_ascii_iscntrl('0')); g_print("%d\n", g_ascii_iscntrl(' ')); g_print("%d\n", g_ascii_iscntrl('\n')); return 0; } で、 0 0 0 1 !2005-05-12 Thu #include int main() { g_print("%d\n", g_ascii_isalpha('a')); g_print("%d\n", g_ascii_isalpha('0')); g_print("%d\n", g_ascii_isalpha(' ')); g_print("%d\n", g_ascii_isalpha('\n')); return 0; } で、 1 0 0 0 !2005-05-11 Wed #include int main() { g_print("%d\n", g_ascii_isalnum('a')); g_print("%d\n", g_ascii_isalnum('0')); g_print("%d\n", g_ascii_isalnum(' ')); g_print("%d\n", g_ascii_isalnum('\n')); return 0; } で、 1 1 0 0 !2005-05-10 Tue g_vsnprintf とばし #include static int get_size(const char *fmt, ...) { va_list args; int size; va_start(args, fmt); size = g_printf_string_upper_bound(fmt, args); va_end(args); return size; } int main() { gint i; i = get_size("%s%s", "0123456789", "0123456789"); g_print("%d\n", i); return 0; } で、 21 !2005-05-09 Mon g_printf, g_vprintf, g_fprintf, g_vfprintf, g_sprintf, g_vsprintf とばし #include int main() { gchar str[16]; g_snprintf(str, 16, "%s %d %f", "foo bar baz", 20, 1.2); g_print("%s\n", str); return 0; } で、 foo bar baz 20 !2005-05-08 Sun vprintf 自体、良く分かっていないのだが… #include static char* print(const char *fmt, ...) { gchar *p; va_list args; va_start(args, fmt); p = g_strdup_vprintf(fmt, args); va_end(args); return p; } int main() { gchar *str; str = print("%s %d %f", "foo", 20, 1.2); g_print("%s\n", str); g_free(str); return 0; } で、 foo 20 1.200000 !2005-05-07 Sat #include int main() { gchar *str; str = g_strdup_printf("%s %d %f", "foo", 20, 1.2); g_print("%s\n", str); g_free(str); return 0; } で、 foo 20 1.200000 !2005-05-06 Fri #include int main() { gchar str[16] = "foobarbaz"; gchar *str2 = "looooooooong"; g_strlcat(str, str2, 16); g_print("str %s\n", str); g_print("str2 %s\n", str2); return 0; } で、 str foobarbazlooooo str2 looooooooong !2005-05-05 Thu #include int main() { gchar str[256]; gchar *str2 = "foo"; g_strlcpy(str, str2, 256); g_print("%s %s\n", str, str2); return 0; } で、 foo foo l って何? limit か??? !2005-05-04 Wed #include int main() { g_print("%d\n", g_str_has_suffix("foo.c", ".c")); g_print("%d\n", g_str_has_suffix("foo.c", ".d")); return 0; } 2.0 にはないや。 で、 1 0 !2005-05-03 Tue #include int main() { g_print("%d\n", g_str_has_prefix("foo.c", "foo")); g_print("%d\n", g_str_has_prefix("foo.c", "bar")); return 0; } 2.0 にはないや。 あれ? Fedora Core 3 の /usr/lib/libglib-2.0.a には含まれているようだな で、 1 0 !2005-05-02 Mon #include int main() { gchar *p; p = g_strrstr_len("abcdefghiabcdefghi", 256, "de"); g_print("%s %p\n", p, p); return 0; } で、 defghi 0x80485b3 !2005-05-01 Sun #include int main() { gchar *p; p = g_strrstr("abcdefghiabcdefghi", "de"); g_print("%s %p\n", p, p); return 0; } で、 defghi 0x80485b3 !2005-04-30 Sat #include int main() { gchar *p; p = g_strstr_len("abcdefghiabcdefghi", 256, "de"); g_print("%s %p\n", p, p); return 0; } で、 defghiabcdefghi 0x80485aa 検出する上限を制限しない場合はどうしたら良いんだろうか? !2005-04-29 Fri #include int main() { gchar str[256] = "foo"; gchar *str2 = "bar"; gchar *p; p = g_stpcpy(str, str2); g_print("%s %p\n", str2, str2); g_print("%s %p\n", str, str); g_print("%s %p\n", p, p); return 0; } で、 bar 0x8048638 bar 0xbffffa5c 0xbffffa5f !2005-04-28 Thu #include int main() { gchar *str; str = g_strnfill(3, 't'); g_print("%s\n", str); g_free(str); return 0; } で、 ttt !2005-04-27 Wed #include int main(int argc, char **argv) { gchar **str_ary; str_ary = g_strdupv(argv); g_print("%s %s\n", argv[0], argv[1]); g_print("%s %s\n", str_ary[0], str_ary[1]); g_strfreev(str_ary); return 0; } で、 ./a.out (null) ./a.out (null) とか ./a.out foo ./a.out foo とか !2005-04-26 Tue #include int main() { gchar *str = "foo"; gchar *str2; str2 = g_strndup(str, 2); g_print("%s %p\n", str, str); g_print("%s %p\n", str2, str2); g_free(str2); return 0; } で、 foo 0x80485f4 fo 0x8049740 !2005-04-25 Mon #include int main() { gchar *str = "foo"; gchar *str2; str2 = g_strdup(str); g_print("%s %p\n", str, str); g_print("%s %p\n", str2, str2); g_free(str2); return 0; } で、 foo 0x80485f4 foo 0x8049740 これらの printf() 系の関数を使用する際は、 を明示的にインクルードして下さい。 変なの !2005-04-24 Sun #include int main() { GArray *array; gint i; array = g_array_new(FALSE, FALSE, sizeof(gint)); i = 0; g_array_append_val(array, i); i = 1; g_array_append_val(array, i); g_print("%d\n", array->len); g_array_set_size(array, 10); g_print("%d\n", array->len); g_array_free(array, FALSE); return 0; } で、 2 10 !2005-04-23 Sat #include static int i_cmp(gpointer p, gpointer q, gpointer ud) { g_print("%s\n", (char *)ud); if (*((gint *)p) > *((gint *)q)) { return 1; } else if (*((gint *)p) < *((gint *)q)) { return -1; } else { return 0; } } int main() { GArray *array; gint i; array = g_array_new(FALSE, FALSE, sizeof(gint)); i = 1; g_array_append_val(array, i); i = 3; g_array_append_val(array, i); i = 2; g_array_append_val(array, i); i = 0; g_array_append_val(array, i); g_array_sort_with_data(array, (GCompareDataFunc)i_cmp, (char *)"foo"); g_print("%d\n", g_array_index(array, gint, 0)); g_print("%d\n", g_array_index(array, gint, 1)); g_print("%d\n", g_array_index(array, gint, 2)); g_print("%d\n", g_array_index(array, gint, 3)); g_print("len: %d\n", array->len); g_array_free(array, FALSE); return 0; } で、 foo foo foo foo foo foo foo foo 0 1 2 3 len: 4 !2005-04-22 Fri #include static int i_cmp(gpointer p, gpointer q) { if (*((gint *)p) > *((gint *)q)) { return 1; } else if (*((gint *)p) < *((gint *)q)) { return -1; } else { return 0; } } int main() { GArray *array; gint i; array = g_array_new(FALSE, FALSE, sizeof(gint)); i = 1; g_array_append_val(array, i); i = 3; g_array_append_val(array, i); i = 2; g_array_append_val(array, i); i = 0; g_array_append_val(array, i); g_array_sort(array, (GCompareFunc)i_cmp); g_print("%d\n", g_array_index(array, gint, 0)); g_print("%d\n", g_array_index(array, gint, 1)); g_print("%d\n", g_array_index(array, gint, 2)); g_print("%d\n", g_array_index(array, gint, 3)); g_print("len: %d\n", array->len); g_array_free(array, FALSE); return 0; } で、 0 1 2 3 len: 4 !2005-04-21 Thu #include int main() { GArray *array; gint i; array = g_array_new(FALSE, FALSE, sizeof(gint)); i = 0; g_array_append_val(array, i); i = 1; g_array_append_val(array, i); i = 2; g_array_append_val(array, i); i = 3; g_array_append_val(array, i); i = 1; g_array_remove_index_fast(array, 1); g_print("%d\n", g_array_index(array, gint, 0)); g_print("%d\n", g_array_index(array, gint, 1)); g_print("%d\n", g_array_index(array, gint, 2)); g_print("%d\n", g_array_index(array, gint, 3)); g_print("len: %d\n", array->len); g_array_free(array, FALSE); return 0; } で、 0 3 2 3 len: 3 !2005-04-20 Wed #include int main() { GArray *array; gint i; array = g_array_new(FALSE, FALSE, sizeof(gint)); i = 0; g_array_append_val(array, i); i = 1; g_array_append_val(array, i); i = 2; g_array_append_val(array, i); i = 3; g_array_append_val(array, i); i = 1; g_array_remove_index(array, 1); g_print("%d\n", g_array_index(array, gint, 0)); g_print("%d\n", g_array_index(array, gint, 1)); g_print("%d\n", g_array_index(array, gint, 2)); g_print("%d\n", g_array_index(array, gint, 3)); g_print("len: %d\n", array->len); g_array_free(array, FALSE); return 0; } で、 0 2 3 3 len: 3 !2005-04-19 Tue #include int main() { GArray *array; gint i, i_ary[] = {10, 20, 30}; array = g_array_new(FALSE, FALSE, sizeof(gint)); i = 0; g_array_append_val(array, i); i = 2; g_array_append_val(array, i); g_array_insert_vals(array, 1, i_ary, 3); g_print("%d\n", g_array_index(array, gint, 0)); g_print("%d\n", g_array_index(array, gint, 1)); g_print("%d\n", g_array_index(array, gint, 2)); g_print("%d\n", g_array_index(array, gint, 3)); g_print("%d\n", g_array_index(array, gint, 4)); g_array_free(array, FALSE); return 0; } で、 0 10 20 30 2 !2005-04-18 Mon #include int main() { GArray *array; gint i; array = g_array_new(FALSE, FALSE, sizeof(gint)); i = 0; g_array_append_val(array, i); i = 2; g_array_append_val(array, i); i = 1; g_array_insert_val(array, 1, i); g_print("%d\n", g_array_index(array, gint, 0)); g_print("%d\n", g_array_index(array, gint, 1)); g_print("%d\n", g_array_index(array, gint, 2)); g_array_free(array, FALSE); return 0; } で、 0 1 2 !2005-04-17 Sun #include int main() { GArray *array; gint i; array = g_array_new(FALSE, FALSE, sizeof(gint)); i = 3; g_array_prepend_val(array, i); i = 2; g_array_prepend_val(array, i); i = 1; g_array_prepend_val(array, i); i = 10; g_array_prepend_vals(array, &i, 3); g_print("%d\n", g_array_index(array, gint, 0)); g_print("%d\n", g_array_index(array, gint, 1)); g_print("%d\n", g_array_index(array, gint, 2)); g_print("%d\n", g_array_index(array, gint, 3)); g_print("%d\n", g_array_index(array, gint, 4)); g_print("%d\n", g_array_index(array, gint, 5)); g_array_free(array, FALSE); return 0; } で、 10 -1073742952 1074389327 1 2 3 あれ? こう使うのかな? #include int main() { GArray *array; gint i, i_ary[] = {10, 20, 30}; array = g_array_new(FALSE, FALSE, sizeof(gint)); i = 3; g_array_prepend_val(array, i); i = 2; g_array_prepend_val(array, i); i = 1; g_array_prepend_val(array, i); i = 10; g_array_prepend_vals(array, i_ary, 3); g_print("%d\n", g_array_index(array, gint, 0)); g_print("%d\n", g_array_index(array, gint, 1)); g_print("%d\n", g_array_index(array, gint, 2)); g_print("%d\n", g_array_index(array, gint, 3)); g_print("%d\n", g_array_index(array, gint, 4)); g_print("%d\n", g_array_index(array, gint, 5)); g_array_free(array, FALSE); return 0; } で、 10 20 30 1 2 3 !2005-04-16 Sat #include int main() { GArray *array; gint i; array = g_array_new(FALSE, FALSE, sizeof(gint)); i = 3; g_array_prepend_val(array, i); i = 2; g_array_prepend_val(array, i); i = 1; g_array_prepend_val(array, i); g_print("%d\n", g_array_index(array, gint, 0)); g_print("%d\n", g_array_index(array, gint, 1)); g_print("%d\n", g_array_index(array, gint, 2)); g_array_free(array, FALSE); return 0; } で、 1 2 3 !2005-04-15 Fri #include int main() { GArray *array; gint i = 1; array = g_array_new(FALSE, FALSE, sizeof(gint)); g_array_append_vals(array, &i, 10); g_print("%d\n", array->len); g_print("%d\n", g_array_index(array, gint, 0)); g_array_free(array, FALSE); return 0; } で、 10 1 !2005-04-14 Thu #include int main() { GArray *array; gint i; array = g_array_sized_new(FALSE, FALSE, sizeof(gint), 10); i = 0; g_array_append_val(array, i); i = 1; g_array_append_val(array, i); g_print("%d\n", array->len); g_array_free(array, FALSE); array = g_array_sized_new(FALSE, TRUE, sizeof(gint), 10); i = 0; g_array_append_val(array, i); i = 1; g_array_append_val(array, i); g_print("%d\n", array->len); g_array_free(array, FALSE); return 0; } で、 2 2 !2005-04-13 Wed #include int main() { GArray *array; gint i; array = g_array_new(FALSE, FALSE, sizeof(gint)); i = 0; g_array_append_val(array, i); i = 1; g_array_append_val(array, i); g_print("%d\n", array->len); g_array_free(array, FALSE); return 0; } で、 2 g_array_new(TRUE, のときの挙動を確かめるコードが分からないや。 !2005-04-12 Tue #include int main() { GArray *array; gint i; array = g_array_new(FALSE, FALSE, sizeof(gint)); i = 0; g_array_append_val(array, i); i = 1; g_array_append_val(array, i); g_print("%d\n", g_array_index(array, gint, 0)); g_print("%d\n", g_array_index(array, gint, 1)); g_print("%d\n", g_array_index(array, gint, 2)); g_array_free(array, FALSE); return 0; } で、 0 1 0 g_array_append_val() で数値を直接入れたら、 invalid lvalue in unary `&' と怒られてしまった。 !2005-04-11 Mon #include static int del_func(gpointer k, gpointer v, gpointer ud) { return !g_ascii_strcasecmp((gchar*)k, "Perl"); } static void print_func(gpointer k, gpointer v, gpointer ud) { g_print("%s -> %s\n", (gchar*)k, (gchar*)v); } int main() { GHashTable *hash; hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash, "Ruby", "Matz"); g_hash_table_insert(hash, "Perl", "Larry"); g_hash_table_insert(hash, "Python", "Guido"); g_hash_table_foreach_steal(hash, del_func, NULL); g_hash_table_foreach(hash, print_func, NULL); g_hash_table_destroy(hash); return 0; } で、 Ruby -> Matz Python -> Guido そういえば、g_int_equal, g_int_hash はうまく使えなかった。理由不明。 !2005-04-10 Sun #include static int del_func(gpointer k, gpointer v, gpointer ud) { return !g_ascii_strcasecmp((gchar*)k, "Perl"); } static void print_func(gpointer k, gpointer v, gpointer ud) { g_print("%s -> %s\n", (gchar*)k, (gchar*)v); } int main() { GHashTable *hash; hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash, "Ruby", "Matz"); g_hash_table_insert(hash, "Perl", "Larry"); g_hash_table_insert(hash, "Python", "Guido"); g_hash_table_foreach_remove(hash, del_func, NULL); g_hash_table_foreach(hash, print_func, NULL); g_hash_table_destroy(hash); return 0; } で、 Ruby -> Matz Python -> Guido !2005-04-09 Sat #include static void print_free(gpointer data) { g_print("free %s\n", (gchar*)data); } int main() { GHashTable *hash; hash = g_hash_table_new_full (g_str_hash, g_str_equal, (GDestroyNotify) print_free, NULL); g_hash_table_insert(hash, "Ruby", "Matz"); g_hash_table_insert(hash, "Perl", "Larry"); g_hash_table_insert(hash, "Python", "Guido"); g_print("%d\n", g_hash_table_steal(hash, "Python")); g_print("%d\n", g_hash_table_steal(hash, "foo")); g_hash_table_destroy(hash); return 0; } で、 1 0 free Ruby free Perl !2005-04-08 Fri #include static void print_func(gpointer k, gpointer v, gpointer ud) { g_print("%d -> %s\n", GPOINTER_TO_INT(k), (gchar*)v); g_print("ud %s\n", (gchar*)ud); } int main() { GHashTable *hash; hash = g_hash_table_new(g_direct_hash, g_direct_equal); g_hash_table_insert(hash, GINT_TO_POINTER(1), "one"); g_hash_table_insert(hash, GINT_TO_POINTER(2), "two"); g_hash_table_insert(hash, GINT_TO_POINTER(3), "three"); g_hash_table_foreach(hash, print_func, "foo"); g_hash_table_destroy(hash); return 0; } で、 1 -> one ud foo 2 -> two ud foo 3 -> three ud foo !2005-04-07 Thu g_hash_table_lookup_extended とばし #include int main() { GHashTable *hash; hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash, "Ruby", "Matz"); g_hash_table_insert(hash, "Perl", "Larry"); g_hash_table_insert(hash, "Python", "Guido"); g_print("%d\n", g_hash_table_size(hash)); g_hash_table_destroy(hash); return 0; } で、 3 !2005-04-06 Wed #include static void print_free(gpointer data) { g_print("free %10s %p\n", (gchar*)data, data); } int main() { GHashTable *hash; hash = g_hash_table_new_full (g_str_hash, g_str_equal, (GDestroyNotify) print_free, NULL); g_hash_table_insert(hash, "Ruby", "Matz"); g_hash_table_insert(hash, "Perl", "Larry"); g_hash_table_insert(hash, "Python", "Guido"); g_hash_table_replace(hash, g_strdup("Python"), "Guido"); g_hash_table_destroy(hash); return 0; } で、 free Python 0x80487fd free Ruby 0x80487e7 free Python 0x8049de8 free Perl 0x80487f2 insert と replace の違いが分かっていない…。 !2005-04-05 Tue #include static void print_free(gpointer data) { g_print("free %s\n", (gchar*)data); } int main() { GHashTable *hash; hash = g_hash_table_new_full (g_str_hash, g_str_equal, (GDestroyNotify) print_free, NULL); g_hash_table_insert(hash, "Ruby", "Matz"); g_hash_table_insert(hash, "Perl", "Larry"); g_hash_table_insert(hash, "Python", "Guido"); g_hash_table_insert(hash, "Python", "Guido"); g_hash_table_destroy(hash); return 0; } で、 free Python free Ruby free Python free Perl !2005-04-04 Mon #include static void print_free(gpointer data) { g_print("free %s\n", (gchar*)data); } int main() { GHashTable *hash; hash = g_hash_table_new_full (g_str_hash, g_str_equal, (GDestroyNotify) print_free, NULL); g_hash_table_insert(hash, "Ruby", "Matz"); g_hash_table_insert(hash, "Perl", "Larry"); g_hash_table_insert(hash, "Python", "Guido"); g_hash_table_remove(hash, "Python"); g_hash_table_destroy(hash); return 0; } で、 free Python free Ruby free Perl !2005-04-03 Sun #include static void print_free(gpointer data) { g_print("free %s\n", (gchar*)data); } int main() { GHashTable *hash; hash = g_hash_table_new_full (g_str_hash, g_str_equal, (GDestroyNotify) print_free, NULL); g_hash_table_insert(hash, "Ruby", "Matz"); g_hash_table_insert(hash, "Perl", "Larry"); g_hash_table_insert(hash, "Python", "Guido"); g_hash_table_destroy(hash); return 0; } で、 free Ruby free Python free Perl !2005-04-02 Sat #include int main() { GHashTable *hash; gchar* ret; hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash, "Ruby", "Matz"); g_hash_table_insert(hash, "Perl", "Larry"); g_hash_table_insert(hash, "Python", "Guido"); ret = g_hash_table_lookup(hash, "Ruby"); g_print("%s -> %s\n", "Ruby", ret); g_hash_table_destroy(hash); hash = g_hash_table_new(g_str_hash, NULL); g_hash_table_insert(hash, "Ruby", "Matz"); ret = g_hash_table_lookup(hash, "Ruby"); g_print("%s -> %s\n", "Ruby", ret); g_hash_table_destroy(hash); hash = g_hash_table_new(g_str_hash, NULL); g_hash_table_insert(hash, "Ruby", "Matz"); ret = g_hash_table_lookup(hash, g_strdup("Ruby")); g_print("%s -> %s\n", "Ruby", ret); g_hash_table_destroy(hash); return 0; } で、 Ruby -> Matz Ruby -> Matz Ruby -> (null) !2005-04-01 Fri #include static void print_func(gpointer k, gpointer v, gpointer ud) { g_print("%d -> %s\n", GPOINTER_TO_INT(k), (gchar*)v); } int main() { GHashTable *hash; hash = g_hash_table_new(g_direct_hash, g_direct_equal); g_hash_table_insert(hash, GINT_TO_POINTER(1), "one"); g_hash_table_insert(hash, GINT_TO_POINTER(2), "two"); g_hash_table_insert(hash, GINT_TO_POINTER(3), "three"); g_hash_table_foreach(hash, print_func, NULL); g_hash_table_destroy(hash); return 0; } で、 1 -> one 2 -> two 3 -> three !2005-03-31 Thu #include static void print_func(gpointer k, gpointer v, gpointer ud) { g_print("%s -> %s\n", (gchar*)k, (gchar*)v); } int main() { GHashTable *hash; hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(hash, "Ruby", "Matz"); g_hash_table_insert(hash, "Perl", "Larry"); g_hash_table_insert(hash, "Python", "Guido"); g_hash_table_foreach(hash, print_func, NULL); g_hash_table_destroy(hash); return 0; } で、 Ruby -> Matz Python -> Guido Perl -> Larry !2005-03-30 Wed #include int main() { GSList *list = NULL; list = g_slist_append(list, "foo"); list = g_slist_append(list, "bar"); g_print("%d\n", g_slist_index(list, "foo")); g_print("%d\n", g_slist_index(list, "bar")); g_print("%d\n", g_slist_index(list, "baz")); g_slist_free(list); return 0; } で、 0 1 -1 g_slist_push_allocator, g_slist_pop_allocator とばし !2005-03-29 Tue #include int main() { GSList *list = NULL; list = g_slist_append(list, "foo"); list = g_slist_append(list, "bar"); g_print("%d\n", g_slist_position(list, g_slist_nth(list, 0))); g_print("%d\n", g_slist_position(list, g_slist_nth(list, 1))); g_slist_free(list); return 0; } で、 0 1 !2005-03-28 Mon #include static int find_func(gpointer data, gpointer ud) { if (g_ascii_strncasecmp((char *)data, (char *)ud, 1) == 0) { return 0; } else { return 1; } } static void print_item(gpointer data, gpointer ud) { g_print("%s\n", (char *)data); } int main() { GSList *list = NULL; list = g_slist_append(list, "foo"); list = g_slist_append(list, "bar"); g_slist_foreach(g_slist_find_custom(list, "f", (GCompareFunc)find_func), print_item, NULL); g_slist_free(list); return 0; } で、 foo bar !2005-03-27 Sun #include static void print_item(gpointer data, gpointer ud) { g_print("%s\n", (char *)data); } int main() { GSList *list = NULL; list = g_slist_append(list, "foo"); list = g_slist_append(list, "bar"); g_slist_foreach(g_slist_find(list, "foo"), print_item, NULL); g_print("***\n"); g_slist_foreach(g_slist_find(list, "bar"), print_item, NULL); g_print("***\n"); g_slist_foreach(g_slist_find(list, "baz"), print_item, NULL); g_slist_free(list); return 0; } で、 foo bar *** bar *** !2005-03-26 Sat #include int main() { GSList *list = NULL; list = g_slist_append(list, "foo"); list = g_slist_append(list, "bar"); g_print("%s\n", (char *)g_slist_nth_data(list, 0)); g_slist_free(list); return 0; } で、 foo !2005-03-25 Fri #include static void print_item(gpointer data, gpointer ud) { g_print("%s\n", (char *)data); } int main() { GSList *list = NULL; list = g_slist_append(list, "foo"); list = g_slist_append(list, "bar"); g_slist_foreach(g_slist_next(list), print_item, NULL); g_slist_free(list); return 0; } で、 bar !2005-03-24 Thu #include static void print_item(gpointer data, gpointer ud) { g_print("%s\n", (char *)data); } int main() { GSList *list = NULL, *list2 = NULL; list = g_slist_append(list, "foo"); list = g_slist_append(list, "bar"); list2 = g_slist_append(list2, "FOO"); list2 = g_slist_append(list2, "BAR"); list = g_slist_concat(list, list2); g_slist_foreach(list, print_item, NULL); g_slist_foreach(list2, print_item, NULL); g_slist_free(list); g_slist_free(list2); return 0; } で、 foo bar FOO BAR FOO BAR !2005-03-23 Wed #include static int i_cmp(gpointer p, gpointer q) { if (GPOINTER_TO_INT(p) > GPOINTER_TO_INT(q)) { return 1; } else if (GPOINTER_TO_INT(p) < GPOINTER_TO_INT(q)) { return -1; } else { return 0; } } static void print_item(gpointer data, gpointer ud) { g_print("%d\n", (int)data); } int main() { GSList *list = NULL; list = g_slist_append(list, GINT_TO_POINTER(3) ); list = g_slist_append(list, GINT_TO_POINTER(2) ); list = g_slist_append(list, GINT_TO_POINTER(1) ); list = g_slist_sort(list, (GCompareFunc)i_cmp); g_slist_foreach(list, print_item, NULL); g_slist_free(list); return 0; } で、 1 2 3 !2005-03-22 Tue #include static void print_item(gpointer data, gpointer ud) { g_print("%s\n", (char *)data); } int main() { GSList *list = NULL; list = g_slist_append(list, "foo"); list = g_slist_append(list, "bar"); list = g_slist_append(list, "AHA"); list = g_slist_sort(list, (GCompareFunc)g_ascii_strcasecmp); g_slist_foreach(list, print_item, NULL); g_slist_free(list); return 0; } で、 AHA bar foo !2005-03-21 Mon #include static void print_item(gpointer data, gpointer ud) { g_print("%s\n", (char *)data); } int main() { GSList *list = NULL; list = g_slist_append(list, "foo"); list = g_slist_append(list, "bar"); list = g_slist_reverse(list); g_slist_foreach(list, print_item, NULL); g_slist_free(list); return 0; } で、 bar foo !2005-03-20 Sun #include static void print_item(gpointer data, gpointer ud) { g_print("%s\n", (char *)data); } int main() { GSList *list = NULL, *list2; list = g_slist_append(list, "foo"); list = g_slist_append(list, "bar"); list2 = g_slist_copy(list); g_slist_foreach(list, print_item, NULL); g_slist_foreach(list2, print_item, NULL); g_slist_free(list); g_slist_free(list2); return 0; } で、 foo bar foo bar !2005-03-19 Sat g_slist_free_1 () とばし #include int main() { GSList *list = NULL; list = g_slist_append(list, "foo"); list = g_slist_append(list, "bar"); g_print("%d\n", g_slist_length(list)); g_slist_free(list); return 0; } で、 2 !2005-03-18 Fri #include static void print_item(gpointer data, gpointer ud) { g_print("%s\n", (char *)data); } int main() { GSList *list = NULL; list = g_slist_append(list, "foo"); list = g_slist_append(list, "bar"); list = g_slist_append(list, "foo"); list = g_slist_remove_all(list, "foo"); g_slist_foreach(list, print_item, NULL); g_slist_free(list); return 0; } で、 bar !2005-03-17 Thu 動作 理解できていない… #include static void print_item(gpointer data, gpointer ud) { g_print("%s\n", (char *)data); } int main() { GSList *list = NULL, *list2; list = g_slist_append(list, "foo"); list = g_slist_append(list, "bar"); list2 = g_slist_append(list, "baz"); g_slist_foreach(g_slist_delete_link(list, list2), print_item, NULL); g_slist_free(list); return 0; } で、 bar baz !2005-03-16 Wed 動作 理解できていない… #include static void print_item(gpointer data, gpointer ud) { g_print("%s\n", (char *)data); } int main() { GSList *list = NULL, *list2; list = g_slist_append(list, "foo"); list = g_slist_append(list, "bar"); list2 = g_slist_append(list, "baz"); list = g_slist_remove_link(list2, list); g_slist_foreach(list, print_item, NULL); g_slist_free(list); return 0; } で、 bar baz !2005-03-15 Tue #include static void print_item(gpointer data, gpointer ud) { g_print("%s\n", (char *)data); } int main() { GSList *list = NULL; list = g_slist_append(list, "foo"); list = g_slist_append(list, "bar"); list = g_slist_append(list, "foo"); list = g_slist_remove(list, "foo"); list = g_slist_remove(list, "hoge"); g_slist_foreach(list, print_item, NULL); g_slist_free(list); return 0; } で、 bar foo !2005-03-14 Mon #include static void print_item(gpointer data, gpointer ud) { g_print("%s\n", (char *)data); } int main() { GSList *list = NULL; list = g_slist_append(list, "bar"); list = g_slist_append(list, "hoge"); list = g_slist_insert_sorted(list, "foo", (GCompareFunc)g_ascii_strncasecmp); g_slist_foreach(list, print_item, NULL); g_slist_free(list); return 0; } で、 bar foo hoge !2005-03-13 Sun #include static void print_item(gpointer data, gpointer ud) { g_print("%s\n", (char *)data); } int main() { GSList *list = NULL; list = g_slist_append(list, "foo"); list = g_slist_append(list, "bar"); list = g_slist_insert_before(list, g_slist_nth(list, 1), "hoge 1"); list = g_slist_insert_before(list, g_slist_last(list), "hoge 2"); g_slist_foreach(list, print_item, NULL); g_slist_free(list); return 0; } で、 foo hoge 1 hoge 2 bar !2005-03-12 Sat #include static void print_item(gpointer data, gpointer ud) { g_print("%s\n", (char *)data); } int main() { GSList *list = NULL; list = g_slist_append(list, "foo"); list = g_slist_append(list, "bar"); list = g_slist_insert(list, "hoge", 1); g_slist_foreach(list, print_item, NULL); g_slist_free(list); return 0; } で、 foo hoge bar !2005-03-11 Fri #include static void print_item(gpointer data, gpointer ud) { g_print("%s\n", (char *)data); } int main() { GSList *list = NULL; list = g_slist_prepend(list, "foo"); list = g_slist_prepend(list, "bar"); g_slist_foreach(list, print_item, NULL); g_slist_free(list); return 0; } で、 bar foo !2005-03-10 Thu #include static void print_item(gpointer data, gpointer ud) { g_print("%d\n", (int)data); } int main() { GSList *list = NULL; list = g_slist_append(list, GINT_TO_POINTER (27)); list = g_slist_append(list, GINT_TO_POINTER (14)); g_slist_foreach(list, print_item, NULL); g_slist_free(list); return 0; } で、 27 14 !2005-03-09 Wed #include static void print_item(gpointer data, gpointer ud) { g_print("%s\n", (char *)data); } int main() { GSList *list = NULL; list = g_slist_append(list, "foo"); list = g_slist_append(list, "bar"); g_slist_foreach(list, print_item, NULL); g_slist_free(list); return 0; } で、 foo bar !2005-03-08 Tue #include int main() { GString *str1, *str2, *str3; str1 = g_string_new("foo"); str2 = g_string_new("foo"); str3 = g_string_new("bar"); g_print("%d\n", g_string_equal(str1, str2)); g_print("%d\n", g_string_equal(str1, str3)); g_string_free(str1, TRUE); g_string_free(str2, TRUE); return 0; } で、 1 0 !2005-03-07 Mon #include int main() { GString *str; str = g_string_new("foo"); g_string_set_size(str, 1); g_print("%s\n", str->str); g_print("%d\n", str->len); g_print("%d\n", str->allocated_len); g_string_free(str, TRUE); return 0; } で、 f 1 8 !2005-03-06 Sun #include int main() { GString *str; str = g_string_new("foo"); g_string_truncate(str, 1); g_print("%s\n", str->str); g_string_free(str, TRUE); return 0; } で、 f #include int main() { GString *str; str = g_string_new("foo"); g_string_truncate(str, 5); g_print("%s\n", str->str); g_string_free(str, TRUE); return 0; } で、 foo !2005-03-05 Sat int main() { GString *str; str = g_string_new("foo"); g_string_erase(str, 1, 2); g_print("%s\n", str->str); g_print("%d\n", str->len); g_print("%d\n", str->allocated_len); g_string_free(str, TRUE); return 0; } で、 f 1 8 いじめちゃう、 #include int main() { GString *str; str = g_string_new("foo"); g_string_erase(str, 1, 3); g_print("%s\n", str->str); g_print("%d\n", str->len); g_print("%d\n", str->allocated_len); g_string_free(str, TRUE); return 0; } で、 (process:3327): GLib-CRITICAL **: file gstring.c: line 599 (g_string_erase): assertion `pos + len <= string->len' failed foo 3 8 !2005-03-04 Fri #include int main() { GString *str; str = g_string_new("foo"); g_string_insert_len(str, 1, "bar", 10); g_print("%s\n", str->str); g_print("%d\n", str->len); g_print("%d\n", str->allocated_len); g_string_free(str, TRUE); return 0; } で、 fbar 13 16 !2005-03-03 Thu #include int main() { GString *str; str = g_string_new("foo"); g_string_insert_c(str, 3, 'o'); g_print("%s\n", str->str); g_string_free(str, TRUE); return 0; } で、 fooo !2005-03-02 Wed #include int main() { GString *str; str = g_string_new("foo"); g_string_insert(str, 1, "bar"); g_print("%s\n", str->str); g_string_free(str, TRUE); return 0; } で、 fbaroo こうしたら? #include int main() { GString *str; str = g_string_new("foo"); g_string_insert(str, 5, "bar"); g_print("%s\n", str->str); g_string_free(str, TRUE); return 0; } で、 (process:3836): GLib-CRITICAL **: file gstring.c: line 524 (g_string_insert): assertion `pos <= string->len' failed foo !2005-03-01 Tue g_string_prepend_unichar() はとばし #include int main() { GString *str; str = g_string_new("foo"); g_string_prepend_len(str, "bar", 10); g_print("%s\n", str->str); g_print("%d\n", str->len); g_print("%d\n", str->allocated_len); g_string_free(str, TRUE); return 0; } で、 bar 13 16 !2005-02-28 Mon #include int main() { GString *str; str = g_string_new("foo"); g_string_prepend_c(str, ' '); g_string_prepend_c(str, 'r'); g_string_prepend_c(str, 'a'); g_string_prepend_c(str, 'b'); g_print("%s\n", str->str); g_string_free(str, TRUE); return 0; } で、 bar foo !2005-02-27 Sun #include int main() { GString *str; str = g_string_new("foo"); g_string_prepend(str, "bar "); g_print("%s\n", str->str); g_string_free(str, TRUE); return 0; } で、 bar foo !2005-02-26 Sat #include int main() { GString *str; str = g_string_new("foo"); g_string_append_len(str, "bar", 10); g_print("%s\n", str->str); g_print("%d\n", str->len); g_print("%d\n", str->allocated_len); g_string_free(str, TRUE); return 0; } で、 foobar 13 16 !2005-02-25 Fri #include int main() { GString *str; str = g_string_new("foo"); g_string_append_unichar(str, ' '); g_string_append_unichar(str, 'a'); g_string_append_unichar(str, 'b'); g_print("%s\n", str->str); g_string_free(str, TRUE); return 0; } で、 foo ab (ユニコードじゃないじゃん?) !2005-02-24 Thu #include int main() { GString *str; str = g_string_new("foo"); g_string_append_c(str, ' '); g_string_append_c(str, 'b'); g_string_append_c(str, 'a'); g_string_append_c(str, 'r'); g_print("%s\n", str->str); g_string_free(str, TRUE); return 0; } で、 foo bar !2005-02-23 Wed #include int main() { GString *str; str = g_string_new("foo"); g_string_append(str, " bar"); g_print("%s\n", str->str); g_string_free(str, TRUE); return 0; } で、 foo bar !2005-02-22 Tue #include int main() { GString *str; str = g_string_new("foo"); g_string_append_printf(str, " %s %d", "bar", 16); g_print("%s\n", str->str); g_string_free(str, TRUE); return 0; } で、 foo bar 16 !2005-02-21 Mon g_string_sprintf, g_string_sprintfa は使うなということなので、とばし #include int main() { GString *str; str = g_string_new(""); g_string_printf(str, "%s %d", "foo", 16); g_print("%s\n", str->str); g_string_free(str, TRUE); return 0; } で、 foo 16 !2005-02-20 Sun #include int main() { GString *str; str = g_string_new(""); g_string_assign(str, "foo"); g_print("%s\n", str->str); g_print("%s\n", g_string_assign(str, "bar")->str); g_string_free(str, TRUE); return 0; } で、 foo bar !2005-02-19 Sat #include int main() { GString *str; str = g_string_sized_new (256); g_print("%s\n", str->str); g_print("%d\n", str->len); g_print("%d\n", str->allocated_len); g_string_free(str, TRUE); return 0; } で、 0 512 !2005-02-18 Fri #include int main() { GString *str; str = g_string_new_len("foo", 5); g_print("%s\n", str->str); g_print("%d\n", str->len); g_print("%d\n", str->allocated_len); g_string_free(str, TRUE); return 0; } で、 foo 5 8 !2005-02-17 Thu #include int main() { GString *str; str = g_string_new("foo"); g_print("%s\n", str->str); g_print("%d\n", str->len); g_print("%d\n", str->allocated_len); g_string_free(str, TRUE); return 0; } で、 foo 3 8 !2005-02-16 Wed #include int main() { g_print("G_IEEE754_FLOAT_BIAS %d\n", G_IEEE754_FLOAT_BIAS); g_print("G_IEEE754_DOUBLE_BIAS %d\n", G_IEEE754_DOUBLE_BIAS); g_print("G_E %e\n", G_E); g_print("G_LN2 %e\n", G_LN2); g_print("G_LN10 %e\n", G_LN10); g_print("G_PI %e\n", G_PI); g_print("G_PI_2 %e\n", G_PI_2); g_print("G_PI_4 %e\n", G_PI_4); g_print("G_SQRT2 %e\n", G_SQRT2); g_print("G_LOG_2_BASE_10 %e\n", G_LOG_2_BASE_10); return 0; } で、 G_IEEE754_FLOAT_BIAS 127 G_IEEE754_DOUBLE_BIAS 1023 G_E 2.718282e+00 G_LN2 6.931472e-01 G_LN10 2.302585e+00 G_PI 3.141593e+00 G_PI_2 1.570796e+00 G_PI_4 7.853982e-01 G_SQRT2 1.414214e+00 G_LOG_2_BASE_10 3.010300e-01 union GFloatIEEE754, union GDoubleIEEE754 とばし !2005-02-15 Tue #include int main() { g_print("GINT_FROM_BE(0xabcdef12) %x\n", GINT_FROM_BE(0xabcdef12)); g_print("GINT_FROM_LE(0xabcdef12) %x\n", GINT_FROM_LE(0xabcdef12)); g_print("GINT_TO_BE(0xabcdef12) %x\n", GINT_TO_BE(0xabcdef12)); g_print("GINT_TO_LE(0xabcdef12) %x\n", GINT_TO_LE(0xabcdef12)); return 0; } で、 GINT_FROM_BE(0xabcdef12) 12efcdab GINT_FROM_LE(0xabcdef12) abcdef12 GINT_TO_BE(0xabcdef12) 12efcdab GINT_TO_LE(0xabcdef12) abcdef12 他の、*_BE, *_LE *_SWAP* とばし !2005-02-14 Mon #include int main() { g_print("g_htons(0xabcd) %x\n", g_htons(0xabcd)); g_print("g_ntohl(0xabcdef01) %x\n", g_ntohl(0xabcdef01)); g_print("g_ntohs(0xabcd) %x\n", g_ntohs(0xabcd)); return 0; } で、 g_htons(0xabcd) cdab g_ntohl(0xabcdef01) 1efcdab g_ntohs(0xabcd) cdab !2005-02-13 Sun #include int main() { g_print("G_BYTE_ORDER %d\n", G_BYTE_ORDER); g_print("G_LITTLE_ENDIAN %d\n", G_LITTLE_ENDIAN); g_print("G_BIG_ENDIAN %d\n", G_BIG_ENDIAN); return 0; } で、 G_BYTE_ORDER 1234 G_LITTLE_ENDIAN 1234 G_BIG_ENDIAN 4321 !2005-02-12 Sat G_STRUCT_*, G_CONST_RETURN とばし #include int main() { g_print("GLIB_MAJOR_VERSION %d\n", GLIB_MAJOR_VERSION); g_print("GLIB_MINOR_VERSION %d\n", GLIB_MINOR_VERSION); g_print("GLIB_MICRO_VERSION %d\n", GLIB_MICRO_VERSION); #ifdef G_OS_WIN32 g_print("G_OS_WIN32\n"); #endif #ifdef G_OS_UNIX g_print("G_OS_UNIX\n"); #endif g_print("GLIB_CHECK_VERSION() %d\n", GLIB_CHECK_VERSION(2, 0, 0)); g_print("GLIB_CHECK_VERSION() %d\n", GLIB_CHECK_VERSION(2, 2, 0)); g_print("G_DIR_SEPARATOR %c\n", G_DIR_SEPARATOR); g_print("G_DIR_SEPARATOR_S %s\n", G_DIR_SEPARATOR_S); g_print("G_SEARCHPATH_SEPARATOR %c\n", G_SEARCHPATH_SEPARATOR); g_print("G_SEARCHPATH_SEPARATOR_S %s\n", G_SEARCHPATH_SEPARATOR_S); g_print("MIN(10, 20) %d\n", MIN(10, 20)); g_print("MAX(10, 20) %d\n", MAX(10, 20)); g_print("ABS(-1) %d\n", ABS(-1)); g_print("CLAMP(5, 10, 15) %d\n", CLAMP(5, 10, 15)); g_print("CLAMP(15, 5, 10) %d\n", CLAMP(15, 5, 10)); g_print("CLAMP(20, 15, 25) %d\n", CLAMP(20, 15, 25)); g_print("G_MEM_ALIGN %d\n", G_MEM_ALIGN); return 0; } で、 GLIB_MAJOR_VERSION 2 GLIB_MINOR_VERSION 0 GLIB_MICRO_VERSION 6 G_OS_UNIX GLIB_CHECK_VERSION() 1 GLIB_CHECK_VERSION() 0 G_DIR_SEPARATOR / G_DIR_SEPARATOR_S / G_SEARCHPATH_SEPARATOR : G_SEARCHPATH_SEPARATOR_S : MIN(10, 20) 10 MAX(10, 20) 20 ABS(-1) 1 CLAMP(5, 10, 15) 10 CLAMP(15, 5, 10) 10 CLAMP(20, 15, 25) 20 G_MEM_ALIGN 4 !2005-02-11 Fri 「基本型の限界」ってなんかなー。 #include int main() { g_print("G_MININT %d\n", G_MININT); g_print("G_MAXINT %d\n", G_MAXINT); g_print("G_MAXUINT %ud\n", G_MAXUINT); g_print("G_MINSHORT %d\n", G_MINSHORT); g_print("G_MAXSHORT %d\n", G_MAXSHORT); g_print("G_MAXUSHORT %d\n", G_MAXUSHORT); g_print("G_MINLONG %ld\n", G_MINLONG); g_print("G_MAXLONG %ld\n", G_MAXLONG); g_print("G_MAXULONG %lud\n", G_MAXULONG); g_print("G_MININT64 %lld\n", G_MININT64); g_print("G_MAXINT64 %lld\n", G_MAXINT64); g_print("G_MAXUINT64 %llud\n", G_MAXUINT64); g_print("G_MINFLOAT %e\n", G_MINFLOAT); g_print("G_MAXFLOAT %e\n", G_MAXFLOAT); g_print("G_MINDOUBLE %e\n", G_MINDOUBLE); g_print("G_MAXDOUBLE %e\n", G_MAXDOUBLE); return 0; } で、 G_MININT -2147483648 G_MAXINT 2147483647 G_MAXUINT 4294967295d G_MINSHORT -32768 G_MAXSHORT 32767 G_MAXUSHORT 65535 G_MINLONG -2147483648 G_MAXLONG 2147483647 G_MAXULONG 4294967295d G_MININT64 -9223372036854775808 G_MAXINT64 9223372036854775807 G_MAXUINT64 18446744073709551615d G_MINFLOAT 1.175494e-38 G_MAXFLOAT 3.402823e+38 G_MINDOUBLE 2.225074e-308 G_MAXDOUBLE 1.797693e+308 !2005-02-10 Thu #include int main() { gint64 i; g_print("G_GINT64_CONSTANT(%llx): %llx\n", G_GINT64_CONSTANT(0x1d636b02300a7aa7U), G_GINT64_CONSTANT(0x1d636b02300a7aa7U)); i = 0x1d636b02300a7aa7U; g_print("%llx: %llx\n", i, i); return 0; } で、 G_GINT64_CONSTANT(1d636b02300a7aa7): 1d636b02300a7aa7 1d636b02300a7aa7: 1d636b02300a7aa7 !2005-02-09 Wed gpointer, gconstpointer とばし。 gchar, guchar, gint, guint, gshort, gushort, glong, gulong, gint8, guint8, gint16, guint16, gint32, guint32 もいいっしょ。 #include int main() { g_print("G_HAVE_GINT64: %d\n", G_HAVE_GINT64); return 0; } で、 G_HAVE_GINT64: 1 !2005-02-08 Tue #include int main() { gboolean flag; flag = TRUE; g_print("gboolean: %d\n", flag); flag = FALSE; g_print("gboolean: %d\n", flag); return 0; } で、 gboolean: 1 gboolean: 0