VALUE rugged_signature_new(const git_signature *sig, const char *encoding_name) { VALUE rb_sig, rb_time; rb_encoding *encoding = rb_utf8_encoding(); if (encoding_name != NULL) encoding = rb_enc_find(encoding_name); rb_sig = rb_hash_new(); /* Allocate the time with a the given timezone */ rb_time = rb_funcall( rb_time_new(sig->when.time, 0), rb_intern("getlocal"), 1, INT2FIX(sig->when.offset * 60) ); rb_hash_aset(rb_sig, CSTR2SYM("name"), rb_enc_str_new(sig->name, strlen(sig->name), encoding)); rb_hash_aset(rb_sig, CSTR2SYM("email"), rb_enc_str_new(sig->email, strlen(sig->email), encoding)); rb_hash_aset(rb_sig, CSTR2SYM("time"), rb_time); return rb_sig; }
static inline VALUE get_user_from_path(wchar_t **wpath, int offset, UINT cp, UINT path_cp, rb_encoding *path_encoding) { VALUE result, tmp; wchar_t *wuser = *wpath + offset; wchar_t *pos = wuser; char *user; size_t size; while (!IS_DIR_SEPARATOR_P(*pos) && *pos != '\0') pos++; *pos = '\0'; convert_wchar_to_mb(wuser, &user, &size, cp); /* convert to VALUE and set the path encoding */ if (path_cp == INVALID_CODE_PAGE) { tmp = rb_enc_str_new(user, size, rb_utf8_encoding()); result = rb_str_encode(tmp, rb_enc_from_encoding(path_encoding), 0, Qnil); rb_str_resize(tmp, 0); } else { result = rb_enc_str_new(user, size, path_encoding); } if (user) xfree(user); return result; }
void Init_request_parse(VALUE nyara) { str_accept = rb_enc_str_new("Accept", strlen("Accept"), u8_encoding); rb_gc_register_mark_object(str_accept); method_override_key = rb_enc_str_new("_method", strlen("_method"), u8_encoding); OBJ_FREEZE(method_override_key); rb_const_set(nyara, rb_intern("METHOD_OVERRIDE_KEY"), method_override_key); nyara_http_methods = rb_const_get(nyara, rb_intern("HTTP_METHODS")); }
static int cmp_1(const void *ap, const void *bp, void *dummy) { struct sort_data *d = dummy; VALUE a = rb_enc_str_new(ap, d->elsize, d->enc); VALUE b = rb_enc_str_new(bp, d->elsize, d->enc); VALUE retval = rb_yield_values(2, a, b); return rb_cmpint(retval, a, b); }
static VALUE to_ruby_value(plruby_context_t *ctx, char type, void *arg, int pos) { OCIString *str; OCINumber *num; double *d; char *cstr; ub4 size; double dbl; switch (type) { case ' ': trace(ctx, 1, "arg[%d]: nil", pos); return Qnil; case 'v': str = (OCIString *)arg; cstr = (char*)OCIStringPtr(ctx->envhp, str); size = OCIStringSize(ctx->envhp, str); trace(ctx, 1, "arg[%d]: %.*s", pos, size, cstr); return rb_enc_str_new(cstr, size, oracle_encoding); case 'n': num = (OCINumber *)arg; chk(OCINumberToReal(ctx->errhp, num, sizeof(dbl), &dbl)); trace(ctx, 1, "arg[%d]: %f", pos, dbl); return rb_float_new(dbl); case 'd': d = (double *)arg; trace(ctx, 1, "arg[%d]: %f", pos, *d); return rb_float_new(*d); } rb_raise(rb_eRuntimeError, "Unsupported type %d", type); }
static void err_append(const char *s, rb_encoding *enc) { rb_thread_t *th = GET_THREAD(); VALUE err = th->errinfo; if (th->mild_compile_error) { if (!RTEST(err)) { err = rb_exc_new3(rb_eSyntaxError, rb_enc_str_new(s, strlen(s), enc)); th->errinfo = err; } else { VALUE str = rb_obj_as_string(err); rb_str_cat2(str, "\n"); rb_str_cat2(str, s); th->errinfo = rb_exc_new3(rb_eSyntaxError, str); } } else { if (!RTEST(err)) { err = rb_exc_new2(rb_eSyntaxError, "compile error"); th->errinfo = err; } rb_write_error(s); rb_write_error("\n"); } }
VALUE typecast_detect(const char *data, size_t size, int type) { VALUE value; char *bytea; size_t bytea_len; switch (type) { case SWIFT_TYPE_INT: return rb_cstr2inum(data, 10); case SWIFT_TYPE_FLOAT: return rb_float_new(atof(data)); case SWIFT_TYPE_NUMERIC: return rb_funcall(cBigDecimal, fnew, 1, rb_str_new(data, size)); case SWIFT_TYPE_BOOLEAN: return (data && (data[0] =='t' || data[0] == '1')) ? Qtrue : Qfalse; case SWIFT_TYPE_BLOB: bytea = PQunescapeBytea(data, &bytea_len); value = rb_str_new(bytea, bytea_len); PQfreemem(bytea); return rb_funcall(cStringIO, fnew, 1, value); case SWIFT_TYPE_TIMESTAMP: return datetime_parse(cSwiftDateTime, data, size); case SWIFT_TYPE_DATE: return date_parse(cSwiftDateTime, data, size); default: return rb_enc_str_new(data, size, rb_utf8_encoding()); } }
/* * call-seq: * commit.header_field(field_name) -> str * * Returns +commit+'s header field value. */ static VALUE rb_git_commit_header_field(VALUE self, VALUE rb_field) { git_buf header_field = { 0 }; git_commit *commit = NULL; const char *encoding_name; rb_encoding *encoding = rb_utf8_encoding(); VALUE rb_result; int error; Check_Type(rb_field, T_STRING); Data_Get_Struct(self, git_commit, commit); error = git_commit_header_field(&header_field, commit, StringValueCStr(rb_field)); if (error < 0) { git_buf_free(&header_field); if (error == GIT_ENOTFOUND) return Qnil; rugged_exception_check(error); } encoding_name = git_commit_message_encoding(commit); if (encoding_name != NULL) encoding = rb_enc_find(encoding_name); rb_result = rb_enc_str_new(header_field.ptr, header_field.size, encoding); git_buf_free(&header_field); return rb_result; }
static void CheckOSStatusOrRaise(OSStatus err){ if(err != 0){ CFStringRef description = SecCopyErrorMessageString(err, NULL); CFIndex bufferSize = CFStringGetMaximumSizeForEncoding(CFStringGetLength(description), kCFStringEncodingUTF8); char *buffer = malloc(bufferSize + 1); CFStringGetCString(description, buffer, bufferSize + 1, kCFStringEncodingUTF8); CFRelease(description); VALUE exceptionString = rb_enc_str_new(buffer, strlen(buffer), rb_utf8_encoding()); free(buffer); VALUE exception = Qnil; switch(err){ case errSecAuthFailed: exception = rb_obj_alloc(rb_eKeychainAuthFailedError); break; case errSecNoSuchKeychain: exception = rb_obj_alloc(rb_eKeychainNoSuchKeychainError); break; case errSecDuplicateItem: exception = rb_obj_alloc(rb_eKeychainDuplicateItemError); break; default: exception = rb_obj_alloc(rb_eKeychainError); } rb_funcall(exception, rb_intern("initialize"), 2,exceptionString, INT2FIX(err)); rb_exc_raise(exception); } }
static VALUE decode(VALUE self, VALUE str) { int rc; punycode_uint *ustr; size_t len; char *buf = NULL; VALUE retv; str = rb_check_convert_type(str, T_STRING, "String", "to_s"); len = RSTRING_LEN(str); ustr = malloc(len * sizeof(punycode_uint)); if (ustr == NULL) { rb_raise(rb_eNoMemError, "cannot allocate memory (%d bytes)", (uint32_t)len); return Qnil; } rc = punycode_decode(RSTRING_LEN(str), RSTRING_PTR(str), &len, ustr, NULL); if (rc != PUNYCODE_SUCCESS) { xfree(ustr); rb_raise(ePunycodeError, "%s (%d)", punycode_strerror(rc), rc); return Qnil; } buf = stringprep_ucs4_to_utf8(ustr, len, NULL, &len); retv = rb_enc_str_new(buf, len, rb_utf8_encoding()); xfree(ustr); xfree(buf); return retv; }
VALUE rb_readlink(VALUE path) { ssize_t len; WCHAR *wpath, wbuf[MAX_PATH]; rb_encoding *enc; UINT cp, path_cp; FilePathValue(path); enc = rb_enc_get(path); cp = path_cp = code_page(enc); if (cp == INVALID_CODE_PAGE) { path = fix_string_encoding(path, enc); cp = CP_UTF8; } wpath = mbstr_to_wstr(cp, RSTRING_PTR(path), RSTRING_LEN(path)+rb_enc_mbminlen(enc), NULL); if (!wpath) rb_memerror(); len = rb_w32_wreadlink(wpath, wbuf, numberof(wbuf)); free(wpath); if (len < 0) rb_sys_fail_path(path); enc = rb_filesystem_encoding(); cp = path_cp = code_page(enc); if (cp == INVALID_CODE_PAGE) cp = CP_UTF8; return append_wstr(rb_enc_str_new(0, 0, enc), wbuf, len, cp, path_cp, enc); }
static OCIAnyData * call_ruby(plruby_context_t *ctx) { VALUE obj; trace(ctx, 1, "before encode to ('%s')", rb_enc_name(oracle_encoding)); obj = rb_enc_str_new(ctx->obj, strlen(ctx->obj), oracle_encoding); trace(ctx, 1, "after encode to ('%s')", rb_enc_name(oracle_encoding)); trace(ctx, 1, "before eval('%s')", ctx->obj); obj = rb_funcall(rb_cObject, rb_intern("eval"), 1, obj); trace(ctx, 1, "after eval('%s')", ctx->obj); if (ctx->meth != NULL) { VALUE args[MAX_ARG]; int idx; for (idx = 0; idx < MAX_ARG && ctx->args[idx] != EMPTY; idx++) { trace(ctx, 2, "before to_ruby_value(ctx, ctx->args[%d], %d)", idx, idx); args[idx] = to_ruby_value(ctx, ctx->argtype[idx], ctx->args[idx], idx); trace(ctx, 2, "after to_ruby_value(ctx, ctx->args[%d], %d) => %p", idx, idx, args[idx]); } trace(ctx, 1, "before calling %s method", ctx->meth); obj = rb_funcall2(obj, rb_intern(ctx->meth), idx, args); trace(ctx, 1, "after calling %s method", ctx->meth); } return to_return_value(ctx, obj); }
static VALUE to_utf8(UChar *ustr, int32_t ulen) { char buffer[BUF_SIZE]; int32_t len = 0; UErrorCode status = U_ZERO_ERROR; /* Figure out the size of the buffer we need to allocate: */ u_strToUTF8(buffer, 0, &len, ustr, ulen, &status); if (status == U_INVALID_CHAR_FOUND) len = 0; else if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR) return Qnil; /* Allocate the buffer and encode into it: */ status = U_ZERO_ERROR; char *ptr = ALLOC_N(char, len); u_strToUTF8(ptr, len, &len, ustr, ulen, &status); if (U_FAILURE(status)) { xfree(ptr); return Qnil; } VALUE str = rb_enc_str_new(ptr, len, rb_utf8_encoding()); xfree(ptr); return str; ; }
VALUE wrap< wxString >(const wxString &st ) { #ifdef HAVE_RUBY_ENCODING_H return rb_enc_str_new(st.c_str(),strlen(st.c_str()),rb_utf8_encoding()); #else return rb_str_new2(st.c_str()); #endif }
VALUE rb_utf8_str_new(const char *str, long len) { #ifdef RUBY_ENCODING_H return rb_enc_str_new(str, len, rb_enc_find("UTF-8")); #else return rb_str_new(str, len); #endif }
static VALUE cfstring_to_rb_string(CFStringRef s){ if(CFStringGetTypeID() != CFGetTypeID(s)){ rb_raise(rb_eTypeError, "Non cfstring passed to cfstring_to_rb_string"); } const char * fastBuffer = CFStringGetCStringPtr(s, kCFStringEncodingUTF8); if(fastBuffer){ return rb_enc_str_new(fastBuffer, strlen(fastBuffer), rb_utf8_encoding()); }else{ CFIndex bufferLength = CFStringGetMaximumSizeForEncoding(CFStringGetLength(s),kCFStringEncodingUTF8); char * buffer = malloc(bufferLength); CFIndex used = 0; CFStringGetBytes(s,CFRangeMake(0, CFStringGetLength(s)), kCFStringEncodingUTF8, 0, false, (UInt8*)buffer, bufferLength, &used); VALUE rb_string = rb_enc_str_new(buffer, used, rb_utf8_encoding()); free(buffer); return rb_string; } }
/* * call-seq: * commit.to_mbox(options = {}) -> str * * Returns +commit+'s contents formatted to resemble UNIX mailbox format. * * Does not (yet) support merge commits. * * The following options can be passed in the +options+ Hash: * * :patch_no :: * Number for this patch in the series. Defaults to +1+. * * :total_patches :: * Total number of patches in the series. Defaults to +1+. * * :exclude_subject_patch_marker :: * If set to true, no "[PATCH]" marker will be * added to the beginning of the subject line. * * Additionally, you can also pass the same options as for Rugged::Tree#diff. */ static VALUE rb_git_commit_to_mbox(int argc, VALUE *argv, VALUE self) { git_buf email_patch = { NULL }; git_repository *repo; git_commit *commit; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_diff_format_email_flags_t flags = GIT_DIFF_FORMAT_EMAIL_NONE; VALUE rb_repo = rugged_owner(self), rb_email_patch = Qnil, rb_val, rb_options; int error; size_t patch_no = 1, total_patches = 1; rb_scan_args(argc, argv, ":", &rb_options); rugged_check_repo(rb_repo); Data_Get_Struct(rb_repo, git_repository, repo); Data_Get_Struct(self, git_commit, commit); if (!NIL_P(rb_options)) { Check_Type(rb_options, T_HASH); rb_val = rb_hash_aref(rb_options, CSTR2SYM("patch_no")); if (!NIL_P(rb_val)) patch_no = NUM2INT(rb_val); rb_val = rb_hash_aref(rb_options, CSTR2SYM("total_patches")); if (!NIL_P(rb_val)) total_patches = NUM2INT(rb_val); if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("exclude_subject_patch_marker")))) flags |= GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER; rugged_parse_diff_options(&opts, rb_options); } error = git_diff_commit_as_email( &email_patch, repo, commit, patch_no, total_patches, flags, &opts); if (error) goto cleanup; rb_email_patch = rb_enc_str_new(email_patch.ptr, email_patch.size, rb_utf8_encoding()); cleanup: xfree(opts.pathspec.strings); git_buf_free(&email_patch); rugged_exception_check(error); return rb_email_patch; }
// s, len is the raw kv string static void _cookie_kv(VALUE output, const char* s, long len) { // strip for (; len > 0; len--, s++) { if (!isspace(*s)) { break; } } for (; len > 0; len--) { if (!isspace(s[len - 1])) { break; } } if (len > 0) { volatile VALUE key = rb_enc_str_new("", 0, u8_encoding); volatile VALUE value = rb_enc_str_new("", 0, u8_encoding); nyara_decode_uri_kv(key, value, s, len); rb_hash_aset(output, key, value); } }
static VALUE rb_keychain_path(VALUE self){ SecKeychainRef keychain=NULL; Data_Get_Struct(self, struct OpaqueSecKeychainRef, keychain); UInt32 pathLength = PATH_MAX; char path[PATH_MAX]; OSStatus result = SecKeychainGetPath(keychain, &pathLength, path); CheckOSStatusOrRaise(result); return rb_enc_str_new(path, pathLength, rb_utf8_encoding()); }
static int on_header_field(http_parser* parser, const char* s, size_t len) { Request* p = (Request*)parser; if (p->last_field == Qnil) { p->last_field = rb_enc_str_new(s, len, u8_encoding); p->last_value = Qnil; } else { rb_str_cat(p->last_field, s, len); } return 0; }
static inline VALUE fix_string_encoding(VALUE str, rb_encoding *encoding) { VALUE result, tmp; tmp = rb_enc_str_new(RSTRING_PTR(str), RSTRING_LEN(str), encoding); result = rb_str_encode(tmp, rb_enc_from_encoding(rb_utf8_encoding()), 0, Qnil); return result; }
VALUE wrap< sf::String >(const sf::String &cstr ) { std::string str(cstr.begin(),cstr.end()); #ifdef HAVE_RUBY_ENCODING_H return rb_enc_str_new(str.c_str(),strlen(str.c_str()),rb_utf8_encoding()); #else return rb_str_new2(str.c_str()); #endif }
static VALUE header_hash_tidy_key(VALUE key) { if (TYPE(key) == T_SYMBOL) { key = rb_sym_to_s(key); } else { Check_Type(key, T_STRING); key = rb_enc_str_new(RSTRING_PTR(key), RSTRING_LEN(key), u8_encoding); } nyara_headerlize(key); return key; }
static VALUE component_error_string(VALUE self) { qmlbind_component component = rbqml_get_component(self); qmlbind_string error = qmlbind_component_get_error_string(component); if (error) { VALUE str = rb_enc_str_new(qmlbind_string_get_chars(error), qmlbind_string_get_length(error), rb_utf8_encoding()); qmlbind_string_release(error); return str; } else { return Qnil; } }
static int on_url(http_parser* parser, const char* s, size_t len) { Request* p = (Request*)parser; p->method = parser->method; if (p->path_with_query == Qnil) { p->path_with_query = rb_enc_str_new(s, len, u8_encoding); } else { rb_str_cat(p->path_with_query, s, len); } return 0; }
VALUE rb_grn_context_rb_string_new (grn_ctx *context, const char *string, long length) { if (length < 0) length = strlen(string); #ifdef HAVE_RUBY_ENCODING_H return rb_enc_str_new(string, length, rb_grn_encoding_to_ruby_encoding(context->encoding)); #else return rb_str_new(string, length); #endif }
// s, len is the raw kv string static void _param_kv(VALUE output, const char* s, long len) { // strip for (; len > 0; len--, s++) { if (!isspace(*s)) { break; } } for (; len > 0; len--) { if (!isspace(s[len - 1])) { break; } } if (len <= 0) { return; } volatile VALUE name = rb_enc_str_new("", 0, u8_encoding); volatile VALUE value = rb_enc_str_new("", 0, u8_encoding); nyara_decode_uri_kv(name, value, s, len); _nested_aset(output, _split_name(name), value); }
/** Gets returns a line. this is okay for small lines, but shouldn't really be used. Limited to ~ 1Mb of a line length. */ static VALUE strio_gets(VALUE self) { char *str = get_str(self); size_t pos = get_pos(self); size_t end = get_end(self); if (str == NULL || pos == end) return Qnil; size_t pos_e = pos; while ((pos_e < end) && str[pos_e] != '\n') pos_e++; set_pos(self, pos_e + 1); return rb_enc_str_new(str + pos, pos_e - pos, BinaryEncoding); }
/* * call-seq: * commit.header -> str * * Returns +commit+'s entire raw header. */ static VALUE rb_git_commit_header(VALUE self) { VALUE rb_result; git_commit *commit; const char *raw_header; Data_Get_Struct(self, git_commit, commit); raw_header = git_commit_raw_header(commit); rb_result = rb_enc_str_new(raw_header, strlen(raw_header), rb_utf8_encoding()); return rb_result; }
/** * Get a cstring from the buffer. */ VALUE rb_bson_byte_buffer_get_cstring(VALUE self) { byte_buffer_t *b; VALUE string; int length; TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b); length = (int)strlen(READ_PTR(b)); ENSURE_BSON_READ(b, length); string = rb_enc_str_new(READ_PTR(b), length, rb_utf8_encoding()); b->read_position += length + 1; return string; }