static mrb_value mrb_file_s_chmod(mrb_state *mrb, mrb_value klass) { mrb_int mode; mrb_int argc, i; mrb_value *filenames; int ai = mrb_gc_arena_save(mrb); mrb_get_args(mrb, "i*", &mode, &filenames, &argc); for (i = 0; i < argc; i++) { const char *utf8_path = mrb_str_to_cstr(mrb, filenames[i]); char *path = mrb_locale_from_utf8(utf8_path, -1); if (CHMOD(path, mode) == -1) { mrb_locale_free(path); mrb_sys_fail(mrb, utf8_path); } mrb_locale_free(path); } mrb_gc_arena_restore(mrb, ai); return mrb_fixnum_value(argc); }
static mrb_value mrb_file_realpath(mrb_state *mrb, mrb_value klass) { mrb_value pathname, dir_string, s, result; int argc; char *cpath; argc = mrb_get_args(mrb, "S|S", &pathname, &dir_string); if (argc == 2) { s = mrb_str_dup(mrb, dir_string); s = mrb_str_append(mrb, s, mrb_str_new_cstr(mrb, FILE_SEPARATOR)); s = mrb_str_append(mrb, s, pathname); pathname = s; } cpath = mrb_str_to_cstr(mrb, pathname); result = mrb_str_buf_new(mrb, PATH_MAX); if (realpath(cpath, RSTRING_PTR(result)) == NULL) mrb_sys_fail(mrb, cpath); mrb_str_resize(mrb, result, strlen(RSTRING_PTR(result))); return result; }
mrb_value mrb_rmd160_s__digest(mrb_state *mrb, mrb_value self) { byte *hashcode; char *content; mrb_value content_mrb; char result[100]; char *hexcode = result; int i; mrb_get_args(mrb, "S", &content_mrb); content = mrb_str_to_cstr(mrb, content_mrb); hashcode = RMD((byte *)content); for (i=0; i<RMDsize/8; i++) { hexcode += sprintf(hexcode, "%02x", hashcode[i]); } return mrb_str_new(mrb, (char *)hashcode, 20); }
mrb_value mrb_qr_s__generate(mrb_state *mrb, mrb_value self) { QRCMD_PTR_TYPE *qr; mrb_int version, mode, eclevel, masktype, fmt, mag; mrb_value content_mrb, result_mrb; char *content; int buf_size; qr_byte_t *buf; int sep = QR_DIM_SEP; int errcode = QR_ERR_NONE; mrb_get_args(mrb, "Siiiiii", &content_mrb, &version, &mode, &eclevel, &masktype, &fmt, &mag); content = mrb_str_to_cstr(mrb, content_mrb); qr = qrInit(version, mode, eclevel, masktype, QRCMD_MAX_NUM_B &errcode); if (qr == NULL) { mrb_raisef(mrb, E_QR_ERROR, "generating the QRCode failed with error code: %d", (mrb_int)errcode); return mrb_nil_value(); } qr_addData(mrb, qr, content, mode); if (!qrCmdFinalize(qr)) { mrb_raisef(mrb, E_QR_ERROR, "QR ERROR: %s", qrCmdGetErrorInfo(qr)); qrCmdDestroy(qr); return mrb_nil_value(); } buf = qrGetSymbol(qr, fmt, sep, mag, &buf_size); result_mrb = mrb_str_new(mrb, buf, (size_t)buf_size); free(buf); return result_mrb; }
static mrb_value ngx_mrb_errlogger(mrb_state *mrb, mrb_value self) { mrb_value *argv; mrb_value msg; mrb_int argc; mrb_int log_level; ngx_http_request_t *r = ngx_mrb_get_request(); if (r == NULL) { mrb_raise(mrb, E_RUNTIME_ERROR, "can't use logger at this phase. only use at request phase"); } mrb_get_args(mrb, "*", &argv, &argc); if (argc != 2) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "%s ERROR %s: argument is not 2", MODULE_NAME, __func__); return self; } if (mrb_type(argv[0]) != MRB_TT_FIXNUM) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "%s ERROR %s: argv[0] is not integer", MODULE_NAME, __func__); return self; } log_level = mrb_fixnum(argv[0]); if (log_level < 0) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "%s ERROR %s: log level is not positive number", MODULE_NAME, __func__); return self; } if (mrb_type(argv[1]) != MRB_TT_STRING) { msg = mrb_funcall(mrb, argv[1], "to_s", 0, NULL); } else { msg = mrb_str_dup(mrb, argv[1]); } ngx_log_error((ngx_uint_t)log_level, r->connection->log, 0, "%s", mrb_str_to_cstr(mrb, msg)); return self; }
mrb_value mrb_redis_smembers(mrb_state *mrb, mrb_value self) { int i; mrb_value array, key; redisContext *rc = DATA_PTR(self); mrb_get_args(mrb, "o", &key); redisReply *rr = redisCommand(rc, "SMEMBERS %s", mrb_str_to_cstr(mrb, key)); if (rr->type == REDIS_REPLY_ARRAY) { array = mrb_ary_new(mrb); for (i = 0; i < rr->elements; i++) { mrb_ary_push(mrb, array, mrb_str_new(mrb, rr->element[i]->str, rr->element[i]->len)); } } else { freeReplyObject(rr); return mrb_nil_value(); } freeReplyObject(rr); return array; }
static mrb_value ngx_stream_mrb_errlogger(mrb_state *mrb, mrb_value self) { mrb_value msg; mrb_int log_level; ngx_stream_mruby_internal_ctx_t *ictx = mrb->ud; ngx_stream_session_t *s = ictx->s; if (s == NULL) { mrb_raise(mrb, E_RUNTIME_ERROR, "can't use logger at this phase. only use at session stream phase"); } mrb_get_args(mrb, "io", &log_level, &msg); if (log_level < 0) { ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, "%s ERROR %s: log level is not positive number", MODULE_NAME, __func__); return self; } msg = mrb_obj_as_string(mrb, msg); ngx_log_error((ngx_uint_t)log_level, s->connection->log, 0, "%s", mrb_str_to_cstr(mrb, msg)); return self; }
std::string MRuby::echo(const std::string &s) const { mrb_value a = mrb_str_new_cstr(mMrb, s.c_str()); mrb_value r = mrb_funcall(mMrb, mrb_top_self(mMrb), "echo", 1, a); return mrb_str_to_cstr(mMrb, r); }
char * nstringhash_get(const nstringhash *hash, const char *key) { return mrb_str_to_cstr(__LIBN_R, mrb_hash_get(__LIBN_R, hash->_hash, mrb_str_new_cstr(__LIBN_R, key))); }
char * nhash_inspect(const nhash *hash) { return mrb_str_to_cstr(__LIBN_R, mrb_inspect(__LIBN_R, hash->_hash)); }
static void mrb_cache_str_to_iovec(mrb_state *mrb, mrb_value str, mrb_cache_iovec_t *io) { io->base = mrb_str_to_cstr(mrb, str); io->len = RSTRING_LEN(str); }
bool rubyval_to_ccvalue(mrb_state* mrb, mrb_value arg, cocos2d::Value* ret, const char* funcName) { switch (arg.tt) { case mrb_vtype::MRB_TT_ARRAY: { ValueVector arrVal; if (! rubyval_to_ccvaluevector(mrb, arg, &arrVal)) { return false; } *ret = Value(arrVal); break; } case mrb_vtype::MRB_TT_HASH: { ValueMap dictVal; if (! rubyval_to_ccvaluemap(mrb, arg, &dictVal)) { return false; } *ret = Value(dictVal); break; } case mrb_vtype::MRB_TT_RANGE: { // TODO return false; break; } case mrb_vtype::MRB_TT_FIXNUM: { mrb_int val = mrb_fixnum(arg); *ret = Value((int)val); break; } case mrb_vtype::MRB_TT_FLOAT: { mrb_float val = mrb_float(arg); *ret = Value((float)val); break; } case mrb_vtype::MRB_TT_TRUE: case mrb_vtype::MRB_TT_FALSE: { bool val = mrb_bool(arg); *ret = Value(val); break; } case mrb_vtype::MRB_TT_STRING: { char* val = mrb_str_to_cstr(mrb, arg); *ret = Value(val); break; } case mrb_vtype::MRB_TT_SYMBOL: { mrb_sym sym = mrb_symbol(arg); const char* val = mrb_sym2name(mrb, sym); *ret = Value(val); break; } default: return false; } return true; }
static void set_config_port(mrb_state *mrb, mrb_value args, mrb_http2_config_t *config, mrb_value val) { config->service = mrb_str_to_cstr(mrb, mrb_fixnum_to_str(mrb, val, 10)); }
static mrb_value mrb_addrinfo_getaddrinfo(mrb_state *mrb, mrb_value klass) { struct addrinfo hints, *res0, *res; mrb_value ai, ary, family, lastai, nodename, protocol, sa, service, socktype; mrb_int flags; int arena_idx, error; const char *hostname = NULL, *servname = NULL; ary = mrb_ary_new(mrb); arena_idx = mrb_gc_arena_save(mrb); /* ary must be on arena! */ family = socktype = protocol = mrb_nil_value(); flags = 0; mrb_get_args(mrb, "oo|oooi", &nodename, &service, &family, &socktype, &protocol, &flags); if (mrb_string_p(nodename)) { hostname = mrb_str_to_cstr(mrb, nodename); } else if (mrb_nil_p(nodename)) { hostname = NULL; } else { mrb_raise(mrb, E_TYPE_ERROR, "nodename must be String or nil"); } if (mrb_string_p(service)) { servname = mrb_str_to_cstr(mrb, service); } else if (mrb_fixnum_p(service)) { servname = mrb_str_to_cstr(mrb, mrb_funcall(mrb, service, "to_s", 0)); } else if (mrb_nil_p(service)) { servname = NULL; } else { mrb_raise(mrb, E_TYPE_ERROR, "service must be String, Fixnum, or nil"); } memset(&hints, 0, sizeof(hints)); hints.ai_flags = flags; if (mrb_fixnum_p(family)) { hints.ai_family = mrb_fixnum(family); } if (mrb_fixnum_p(socktype)) { hints.ai_socktype = mrb_fixnum(socktype); } lastai = mrb_cv_get(mrb, klass, mrb_intern_lit(mrb, "_lastai")); if (mrb_cptr_p(lastai)) { freeaddrinfo(mrb_cptr(lastai)); mrb_cv_set(mrb, klass, mrb_intern_lit(mrb, "_lastai"), mrb_nil_value()); } error = getaddrinfo(hostname, servname, &hints, &res0); if (error) { mrb_raisef(mrb, E_SOCKET_ERROR, "getaddrinfo: %S", mrb_str_new_cstr(mrb, gai_strerror(error))); } mrb_cv_set(mrb, klass, mrb_intern_lit(mrb, "_lastai"), mrb_cptr_value(mrb, res0)); for (res = res0; res != NULL; res = res->ai_next) { sa = mrb_str_new(mrb, (void *)res->ai_addr, res->ai_addrlen); ai = mrb_funcall(mrb, klass, "new", 4, sa, mrb_fixnum_value(res->ai_family), mrb_fixnum_value(res->ai_socktype), mrb_fixnum_value(res->ai_protocol)); mrb_ary_push(mrb, ary, ai); mrb_gc_arena_restore(mrb, arena_idx); } freeaddrinfo(res0); mrb_cv_set(mrb, klass, mrb_intern_lit(mrb, "_lastai"), mrb_nil_value()); return ary; }