Module: TkUtil

Included in:
Tk::OptionObj, TkComm, TkComm, TkConfigMethod, TkItemConfigMethod, TkItemConfigOptkeys
Defined in:
ext/tk/tkutil/tkutil.c

Defined Under Namespace

Classes: CallbackSubst

Constant Summary collapse

RELEASE_DATE =

rb_obj_freeze(rb_str_new2(tkutil_release_date))
None =
TK_None

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._conv_args(*args) ⇒ Object

[0]:base_array, [1]:enc_mode, [2]..:args



899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
# File 'ext/tk/tkutil/tkutil.c', line 899

static VALUE
tk_conv_args(
    int   argc,
    VALUE *argv, /* [0]:base_array, [1]:enc_mode, [2]..[n]:args */
    VALUE self
)
{
    int idx;
    long size;
    volatile VALUE dst;
    int thr_crit_bup;
    VALUE old_gc;

    if (argc < 2) {
      rb_raise(rb_eArgError, "too few arguments");
    }

    thr_crit_bup = rb_thread_critical;
    rb_thread_critical = Qtrue;
    old_gc = rb_gc_disable();

    for(size = 0, idx = 2; idx < argc; idx++) {
        if (RB_TYPE_P(argv[idx], T_HASH)) {
            size += 2 * RHASH_SIZE(argv[idx]);
        } else {
            size++;
        }
    }
    /* dst = rb_ary_new2(argc - 2); */
    dst = rb_ary_new2(size);
    for(idx = 2; idx < argc; idx++) {
        if (RB_TYPE_P(argv[idx], T_HASH)) {
            if (RTEST(argv[1])) {
                hash2kv_enc(argv[idx], dst, self);
            } else {
                hash2kv(argv[idx], dst, self);
            }
        } else if (argv[idx] != TK_None) {
            rb_ary_push(dst, get_eval_string_core(argv[idx], argv[1], self));
        }
    }

    if (old_gc == Qfalse) rb_gc_enable();
    rb_thread_critical = thr_crit_bup;

    return rb_ary_plus(argv[0], dst);
}

._get_eval_enc_str(obj) ⇒ Object



889
890
891
892
893
894
895
896
897
# File 'ext/tk/tkutil/tkutil.c', line 889

static VALUE
tk_get_eval_enc_str(VALUE self, VALUE obj)
{
    if (obj == TK_None) {
        return obj;
    } else {
        return get_eval_string_core(obj, Qtrue, self);
    }
}

._get_eval_string(*args) ⇒ Object



877
878
879
880
881
882
883
884
885
886
887
# File 'ext/tk/tkutil/tkutil.c', line 877

static VALUE
tk_get_eval_string(int argc, VALUE *argv, VALUE self)
{
    VALUE obj, enc_flag;

    if (rb_scan_args(argc, argv, "11", &obj, &enc_flag) == 1) {
        enc_flag = Qnil;
    }

    return get_eval_string_core(obj, enc_flag, self);
}

._symbolkey2str(keys) ⇒ Object



289
290
291
292
293
294
295
296
297
298
# File 'ext/tk/tkutil/tkutil.c', line 289

static VALUE
tk_symbolkey2str(VALUE self, VALUE keys)
{
    volatile VALUE new_keys = rb_hash_new();

    if (NIL_P(keys)) return new_keys;
    keys = rb_convert_type(keys, T_HASH, "Hash", "to_hash");
    rb_hash_foreach(keys, to_strkey, new_keys);
    return new_keys;
}

.bool(value) ⇒ Object

**********************************



950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
# File 'ext/tk/tkutil/tkutil.c', line 950

static VALUE
tcl2rb_bool(VALUE self, VALUE value)
{
    if (RB_TYPE_P(value, T_FIXNUM)) {
        if (NUM2INT(value) == 0) {
            return Qfalse;
        } else {
            return Qtrue;
        }
    }

    if (value == Qtrue || value == Qfalse) {
        return value;
    }

    rb_check_type(value, T_STRING);

    value = rb_funcall(value, ID_downcase, 0);

    if (RSTRING_PTR(value) == (char*)NULL) return Qnil;

    if (RSTRING_PTR(value)[0] == '\0'
        || strcmp(RSTRING_PTR(value), "0") == 0
        || strcmp(RSTRING_PTR(value), "no") == 0
        || strcmp(RSTRING_PTR(value), "off") == 0
        || strcmp(RSTRING_PTR(value), "false") == 0) {
        return Qfalse;
    } else {
        return Qtrue;
    }
}

.callback(*args) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'ext/tk/tkutil/tkutil.c', line 183

static VALUE
tk_do_callback(int argc, VALUE *argv, VALUE self)
{
#if 0
    volatile VALUE id;
    volatile VALUE rest;

    rb_scan_args(argc, argv, "1*", &id, &rest);
    return rb_apply(rb_hash_aref(CALLBACK_TABLE, id), ID_call, rest);
#endif
    return rb_funcall2(rb_hash_aref(CALLBACK_TABLE, argv[0]),
                       ID_call, argc - 1, argv + 1);
}

.eval_cmd(*args) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
# File 'ext/tk/tkutil/tkutil.c', line 170

static VALUE
tk_eval_cmd(int argc, VALUE *argv, VALUE self)
{
    VALUE cmd, rest;

    rb_scan_args(argc, argv, "1*", &cmd, &rest);
#ifdef RB_PASS_KEYWORDS
    return rb_eval_cmd_kw(cmd, rest, 0);
#else
    return rb_eval_cmd(cmd, rest, 0);
#endif
}

.hash_kv(*args) ⇒ Object



728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
# File 'ext/tk/tkutil/tkutil.c', line 728

static VALUE
tk_hash_kv(int argc, VALUE *argv, VALUE self)
{
    volatile VALUE hash, enc_flag, ary;

    ary = Qnil;
    enc_flag = Qnil;
    switch(argc) {
    case 3:
        ary = argv[2];
  if (!NIL_P(ary)) Check_Type(ary, T_ARRAY);
    case 2:
        enc_flag = argv[1];
    case 1:
        hash = argv[0];
        break;
    case 0:
        rb_raise(rb_eArgError, "too few arguments");
    default: /* >= 3 */
        rb_raise(rb_eArgError, "too many arguments");
    }

    switch(TYPE(hash)) {
    case T_ARRAY:
        if (RTEST(enc_flag)) {
            return assoc2kv_enc(hash, ary, self);
        } else {
            return assoc2kv(hash, ary, self);
        }

    case T_HASH:
        if (RTEST(enc_flag)) {
            return hash2kv_enc(hash, ary, self);
        } else {
            return hash2kv(hash, ary, self);
        }

    case T_NIL:
        if (NIL_P(ary)) {
            return rb_ary_new();
        } else {
            return ary;
        }

    default:
        if (hash == TK_None) {
            if (NIL_P(ary)) {
                return rb_ary_new();
            } else {
                return ary;
            }
        }
        rb_raise(rb_eArgError, "Hash is expected for 1st argument");
    }

    UNREACHABLE;
}

.install_cmd(*args) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'ext/tk/tkutil/tkutil.c', line 212

static VALUE
tk_install_cmd(int argc, VALUE *argv, VALUE self)
{
    volatile VALUE cmd;

#if 0
    if (rb_scan_args(argc, argv, "01", &cmd) == 0) {
        cmd = rb_block_proc();
    }
    return tk_install_cmd_core(cmd);
#endif
    if (argc == 0) {
        cmd = rb_block_proc();
    } else {
        cmd = argv[0];
    }
    return tk_install_cmd_core(cmd);
}

.num_or_nil(value) ⇒ Object



1073
1074
1075
1076
1077
1078
1079
1080
1081
# File 'ext/tk/tkutil/tkutil.c', line 1073

static VALUE
tcl2rb_num_or_nil(VALUE self, VALUE value)
{
    rb_check_type(value, T_STRING);

    if (RSTRING_LEN(value) == 0) return Qnil;

    return tkstr_to_number(value);
}

.num_or_str(value) ⇒ Object



1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
# File 'ext/tk/tkutil/tkutil.c', line 1061

static VALUE
tcl2rb_num_or_str(VALUE self, VALUE value)
{
    rb_check_type(value, T_STRING);

    if (RSTRING_PTR(value) == (char*)NULL) return rb_str_new2("");

    return rb_rescue2(tkstr_to_number, value,
                      tkstr_to_str, value,
                      rb_eArgError, 0);
}

.number(value) ⇒ Object



1030
1031
1032
1033
1034
# File 'ext/tk/tkutil/tkutil.c', line 1030

static VALUE
tcl2rb_number(VALUE self, VALUE value)
{
    return tkstr_to_number(value);
}

.string(value) ⇒ Object



1051
1052
1053
1054
1055
1056
1057
1058
1059
# File 'ext/tk/tkutil/tkutil.c', line 1051

static VALUE
tcl2rb_string(VALUE self, VALUE value)
{
    rb_check_type(value, T_STRING);

    if (RSTRING_PTR(value) == (char*)NULL) return rb_str_new2("");

    return tkstr_to_str(value, Qnil);
}

.uninstall_cmd(cmd_id) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'ext/tk/tkutil/tkutil.c', line 231

static VALUE
tk_uninstall_cmd(VALUE self, VALUE cmd_id)
{
    size_t head_len = strlen(cmd_id_head);
    size_t prefix_len = strlen(cmd_id_prefix);

    StringValue(cmd_id);
    if (strncmp(cmd_id_head, RSTRING_PTR(cmd_id), head_len) != 0) {
        return Qnil;
    }
    if (strncmp(cmd_id_prefix,
                RSTRING_PTR(cmd_id) + head_len, prefix_len) != 0) {
        return Qnil;
    }

    return rb_hash_delete(CALLBACK_TABLE,
                          rb_str_new2(RSTRING_PTR(cmd_id) + head_len));
}

.untrust(obj) ⇒ Object

**********************************



164
165
166
167
168
# File 'ext/tk/tkutil/tkutil.c', line 164

static VALUE
tk_obj_untrust(VALUE self, VALUE obj)
{
  return obj;
}

Instance Method Details

#_conv_args(*args) ⇒ Object

[0]:base_array, [1]:enc_mode, [2]..:args



899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
# File 'ext/tk/tkutil/tkutil.c', line 899

static VALUE
tk_conv_args(
    int   argc,
    VALUE *argv, /* [0]:base_array, [1]:enc_mode, [2]..[n]:args */
    VALUE self
)
{
    int idx;
    long size;
    volatile VALUE dst;
    int thr_crit_bup;
    VALUE old_gc;

    if (argc < 2) {
      rb_raise(rb_eArgError, "too few arguments");
    }

    thr_crit_bup = rb_thread_critical;
    rb_thread_critical = Qtrue;
    old_gc = rb_gc_disable();

    for(size = 0, idx = 2; idx < argc; idx++) {
        if (RB_TYPE_P(argv[idx], T_HASH)) {
            size += 2 * RHASH_SIZE(argv[idx]);
        } else {
            size++;
        }
    }
    /* dst = rb_ary_new2(argc - 2); */
    dst = rb_ary_new2(size);
    for(idx = 2; idx < argc; idx++) {
        if (RB_TYPE_P(argv[idx], T_HASH)) {
            if (RTEST(argv[1])) {
                hash2kv_enc(argv[idx], dst, self);
            } else {
                hash2kv(argv[idx], dst, self);
            }
        } else if (argv[idx] != TK_None) {
            rb_ary_push(dst, get_eval_string_core(argv[idx], argv[1], self));
        }
    }

    if (old_gc == Qfalse) rb_gc_enable();
    rb_thread_critical = thr_crit_bup;

    return rb_ary_plus(argv[0], dst);
}

#_fromUTF8(*args) ⇒ Object



256
257
258
259
260
# File 'ext/tk/tkutil/tkutil.c', line 256

static VALUE
tk_fromUTF8(int argc, VALUE *argv, VALUE self)
{
    return rb_funcall2(cTclTkLib, ID_fromUTF8, argc, argv);
}

#_get_eval_enc_str(obj) ⇒ Object



889
890
891
892
893
894
895
896
897
# File 'ext/tk/tkutil/tkutil.c', line 889

static VALUE
tk_get_eval_enc_str(VALUE self, VALUE obj)
{
    if (obj == TK_None) {
        return obj;
    } else {
        return get_eval_string_core(obj, Qtrue, self);
    }
}

#_get_eval_string(*args) ⇒ Object



877
878
879
880
881
882
883
884
885
886
887
# File 'ext/tk/tkutil/tkutil.c', line 877

static VALUE
tk_get_eval_string(int argc, VALUE *argv, VALUE self)
{
    VALUE obj, enc_flag;

    if (rb_scan_args(argc, argv, "11", &obj, &enc_flag) == 1) {
        enc_flag = Qnil;
    }

    return get_eval_string_core(obj, enc_flag, self);
}

#_symbolkey2str(keys) ⇒ Object



289
290
291
292
293
294
295
296
297
298
# File 'ext/tk/tkutil/tkutil.c', line 289

static VALUE
tk_symbolkey2str(VALUE self, VALUE keys)
{
    volatile VALUE new_keys = rb_hash_new();

    if (NIL_P(keys)) return new_keys;
    keys = rb_convert_type(keys, T_HASH, "Hash", "to_hash");
    rb_hash_foreach(keys, to_strkey, new_keys);
    return new_keys;
}

#_toUTF8(*args) ⇒ Object



250
251
252
253
254
# File 'ext/tk/tkutil/tkutil.c', line 250

static VALUE
tk_toUTF8(int argc, VALUE *argv, VALUE self)
{
    return rb_funcall2(cTclTkLib, ID_toUTF8, argc, argv);
}

#bool(value) ⇒ Object

**********************************



950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
# File 'ext/tk/tkutil/tkutil.c', line 950

static VALUE
tcl2rb_bool(VALUE self, VALUE value)
{
    if (RB_TYPE_P(value, T_FIXNUM)) {
        if (NUM2INT(value) == 0) {
            return Qfalse;
        } else {
            return Qtrue;
        }
    }

    if (value == Qtrue || value == Qfalse) {
        return value;
    }

    rb_check_type(value, T_STRING);

    value = rb_funcall(value, ID_downcase, 0);

    if (RSTRING_PTR(value) == (char*)NULL) return Qnil;

    if (RSTRING_PTR(value)[0] == '\0'
        || strcmp(RSTRING_PTR(value), "0") == 0
        || strcmp(RSTRING_PTR(value), "no") == 0
        || strcmp(RSTRING_PTR(value), "off") == 0
        || strcmp(RSTRING_PTR(value), "false") == 0) {
        return Qfalse;
    } else {
        return Qtrue;
    }
}

#hash_kv(*args) ⇒ Object



728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
# File 'ext/tk/tkutil/tkutil.c', line 728

static VALUE
tk_hash_kv(int argc, VALUE *argv, VALUE self)
{
    volatile VALUE hash, enc_flag, ary;

    ary = Qnil;
    enc_flag = Qnil;
    switch(argc) {
    case 3:
        ary = argv[2];
  if (!NIL_P(ary)) Check_Type(ary, T_ARRAY);
    case 2:
        enc_flag = argv[1];
    case 1:
        hash = argv[0];
        break;
    case 0:
        rb_raise(rb_eArgError, "too few arguments");
    default: /* >= 3 */
        rb_raise(rb_eArgError, "too many arguments");
    }

    switch(TYPE(hash)) {
    case T_ARRAY:
        if (RTEST(enc_flag)) {
            return assoc2kv_enc(hash, ary, self);
        } else {
            return assoc2kv(hash, ary, self);
        }

    case T_HASH:
        if (RTEST(enc_flag)) {
            return hash2kv_enc(hash, ary, self);
        } else {
            return hash2kv(hash, ary, self);
        }

    case T_NIL:
        if (NIL_P(ary)) {
            return rb_ary_new();
        } else {
            return ary;
        }

    default:
        if (hash == TK_None) {
            if (NIL_P(ary)) {
                return rb_ary_new();
            } else {
                return ary;
            }
        }
        rb_raise(rb_eArgError, "Hash is expected for 1st argument");
    }

    UNREACHABLE;
}

#num_or_nil(value) ⇒ Object



1073
1074
1075
1076
1077
1078
1079
1080
1081
# File 'ext/tk/tkutil/tkutil.c', line 1073

static VALUE
tcl2rb_num_or_nil(VALUE self, VALUE value)
{
    rb_check_type(value, T_STRING);

    if (RSTRING_LEN(value) == 0) return Qnil;

    return tkstr_to_number(value);
}

#num_or_str(value) ⇒ Object



1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
# File 'ext/tk/tkutil/tkutil.c', line 1061

static VALUE
tcl2rb_num_or_str(VALUE self, VALUE value)
{
    rb_check_type(value, T_STRING);

    if (RSTRING_PTR(value) == (char*)NULL) return rb_str_new2("");

    return rb_rescue2(tkstr_to_number, value,
                      tkstr_to_str, value,
                      rb_eArgError, 0);
}

#number(value) ⇒ Object



1030
1031
1032
1033
1034
# File 'ext/tk/tkutil/tkutil.c', line 1030

static VALUE
tcl2rb_number(VALUE self, VALUE value)
{
    return tkstr_to_number(value);
}

#string(value) ⇒ Object



1051
1052
1053
1054
1055
1056
1057
1058
1059
# File 'ext/tk/tkutil/tkutil.c', line 1051

static VALUE
tcl2rb_string(VALUE self, VALUE value)
{
    rb_check_type(value, T_STRING);

    if (RSTRING_PTR(value) == (char*)NULL) return rb_str_new2("");

    return tkstr_to_str(value, Qnil);
}