static mrb_value mrb_skkdic_dict_data(mrb_state *mrb, mrb_value self) { mrb_value path, block, argv[4], table; char line[2048], *cstr, *p; int okuri_pos = 0; FILE *f; int i = 0; mrb_get_args(mrb, "&S", &block, &path); f = fopen(RSTRING_PTR(path), "r"); if (f == NULL) { puts("file open error\n"); } else { while(fgets(line, sizeof(line), f) != NULL) { int ai = mrb_gc_arena_save(mrb); if (strcmp(line, ";; okuri-ari entries.\n") == 0) { okuri_pos++; continue; } if (strcmp(line, ";; okuri-nasi entries.\n") == 0) { okuri_pos++; continue; } if (line[0] == ';') { continue; } cstr = strtok(line, " "); argv[1] = mrb_str_new_cstr(mrb, cstr); cstr = strtok(NULL, " "); p = strtok(cstr, "/"); table = mrb_ary_new(mrb); while (p != NULL) { int ai2 = mrb_gc_arena_save(mrb); mrb_ary_push(mrb, table, mrb_str_new_cstr(mrb, p)); p = strtok(NULL, "/"); mrb_gc_arena_restore(mrb, ai2); } mrb_ary_pop(mrb, table); argv[2] = table; if (okuri_pos == 1) { // okuri_ari argv[0] = mrb_fixnum_value(0); mrb_yield_argv(mrb, block, 3, argv); } else if(okuri_pos == 2) { // okuri_nasi argv[0] = mrb_fixnum_value(1); mrb_yield_argv(mrb, block, 3, argv); } mrb_gc_arena_restore(mrb, ai); } } return self; }
static mrb_value join_ary(mrb_state *mrb, mrb_value ary, mrb_value sep, mrb_value list) { mrb_int i; mrb_value result, val, tmp; /* check recursive */ for (i=0; i<RARRAY_LEN(list); i++) { if (mrb_obj_equal(mrb, ary, RARRAY_PTR(list)[i])) { mrb_raise(mrb, E_ARGUMENT_ERROR, "recursive array join"); } } mrb_ary_push(mrb, list, ary); result = mrb_str_buf_new(mrb, 64); for (i=0; i<RARRAY_LEN(ary); i++) { if (i > 0 && !mrb_nil_p(sep)) { mrb_str_cat_str(mrb, result, sep); } val = RARRAY_PTR(ary)[i]; switch (mrb_type(val)) { case MRB_TT_ARRAY: ary_join: val = join_ary(mrb, val, sep, list); /* fall through */ case MRB_TT_STRING: str_join: mrb_str_cat_str(mrb, result, val); break; default: if (!mrb_immediate_p(val)) { tmp = mrb_check_string_type(mrb, val); if (!mrb_nil_p(tmp)) { val = tmp; goto str_join; } tmp = mrb_check_convert_type(mrb, val, MRB_TT_ARRAY, "Array", "to_ary"); if (!mrb_nil_p(tmp)) { val = tmp; goto ary_join; } } val = mrb_obj_as_string(mrb, val); goto str_join; } } mrb_ary_pop(mrb, list); return result; }
static mrb_value mrb_vedis_append_hash(mrb_state *mrb, mrb_value self) { int ai; vedis *vstore = DATA_PTR(self); mrb_value hash, keys, key, val; mrb_get_args(mrb, "H", &hash); keys = mrb_hash_keys(mrb, hash); ai = mrb_gc_arena_save(mrb); while (!mrb_nil_p(key = mrb_ary_pop(mrb, keys))) { val = mrb_hash_get(mrb, hash, key); mrb_vedis_append_s(mrb, key, val, vstore); mrb_gc_arena_restore(mrb, ai); } return mrb_true_value(); }
static mrb_value mrb_queue_pop(mrb_state* mrb, mrb_value self) { mrb_value ret; mrb_queue_context* context = DATA_PTR(self); int len; mrb_queue_lock(mrb, self); len = RARRAY_LEN(context->queue); mrb_queue_unlock(mrb, self); if (len == 0) { if (pthread_mutex_lock(&context->queue_lock) != 0) { mrb_raise(mrb, E_RUNTIME_ERROR, "cannot lock"); } } mrb_queue_lock(mrb, self); ret = migrate_simple_value(context->mrb, mrb_ary_pop(context->mrb, context->queue), mrb); mrb_queue_unlock(mrb, self); return ret; }
static mrb_value inspect_ary(mrb_state *mrb, mrb_value ary, mrb_value list) { mrb_int i; mrb_value s, arystr; char head[] = { '[' }; char sep[] = { ',', ' ' }; char tail[] = { ']' }; /* check recursive */ for(i=0; i<RARRAY_LEN(list); i++) { if (mrb_obj_equal(mrb, ary, RARRAY_PTR(list)[i])) { return mrb_str_new(mrb, "[...]", 5); } } mrb_ary_push(mrb, list, ary); arystr = mrb_str_buf_new(mrb, 64); mrb_str_buf_cat(mrb, arystr, head, sizeof(head)); for(i=0; i<RARRAY_LEN(ary); i++) { int ai = mrb_gc_arena_save(mrb); if (i > 0) { mrb_str_buf_cat(mrb, arystr, sep, sizeof(sep)); } if (mrb_array_p(RARRAY_PTR(ary)[i])) { s = inspect_ary(mrb, RARRAY_PTR(ary)[i], list); } else { s = mrb_inspect(mrb, RARRAY_PTR(ary)[i]); } mrb_str_buf_cat(mrb, arystr, RSTRING_PTR(s), RSTRING_LEN(s)); mrb_gc_arena_restore(mrb, ai); } mrb_str_buf_cat(mrb, arystr, tail, sizeof(tail)); mrb_ary_pop(mrb, list); return arystr; }