/* Callback used to iterate over the HTTP headers and store them in an slist. */ static int each_http_header(VALUE header_key, VALUE header_value, VALUE self) { struct curl_state *state = get_curl_state(self); CURL* curl = state->handle; VALUE name = rb_obj_as_string(header_key); VALUE value = rb_obj_as_string(header_value); VALUE header_str = Qnil; // TODO: see how to combine this with automatic_content_encoding if (rb_str_cmp(name, rb_str_new2("Accept-Encoding")) == 0) { if (rb_funcall(value, rb_intern("include?"), 1, rb_str_new2("gzip"))) { #ifdef CURLOPT_ACCEPT_ENCODING curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "gzip"); #elif defined CURLOPT_ENCODING curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip"); #else rb_raise(rb_eArgError, "The libcurl version installed doesn't support 'gzip'."); #endif } } header_str = rb_str_plus(name, rb_str_new2(": ")); header_str = rb_str_plus(header_str, value); state->headers = curl_slist_append(state->headers, StringValuePtr(header_str)); return 0; }
// Callback used to iterate over the HTTP headers and store them in an slist. static VALUE each_http_header(VALUE header, VALUE self) { struct curl_state *state; Data_Get_Struct(self, struct curl_state, state); VALUE name = rb_obj_as_string(rb_ary_entry(header, 0)); VALUE value = rb_obj_as_string(rb_ary_entry(header, 1)); VALUE header_str = Qnil; header_str = rb_str_plus(name, rb_str_new2(": ")); header_str = rb_str_plus(header_str, value); state->headers = curl_slist_append(state->headers, StringValuePtr(header_str)); return Qnil; }
// Callback used to iterate over the HTTP headers and store them in an slist. static int each_http_header(VALUE header_key, VALUE header_value, VALUE self) { struct curl_state *state; Data_Get_Struct(self, struct curl_state, state); VALUE name = rb_obj_as_string(header_key); VALUE value = rb_obj_as_string(header_value); VALUE header_str = Qnil; header_str = rb_str_plus(name, rb_str_new2(": ")); header_str = rb_str_plus(header_str, value); state->headers = curl_slist_append(state->headers, StringValuePtr(header_str)); return 0; }
VALUE munge_xpath_namespace( VALUE orig_expr, xmlChar *root_ns ) { VALUE path_bits = rb_str_split( orig_expr, "/" ); VALUE ns_prefix = rb_str_new2( (const char*)root_ns ); VALUE ns_indic = rb_str_new2( ":" ); VALUE slash = rb_str_new2( "/" ); VALUE path_bit, str_idx; VALUE ret_ary = rb_ary_new(); long i; rb_str_append( ns_prefix, ns_indic ); for (i=0; i<RARRAY(path_bits)->len; i++) { path_bit = RARRAY(path_bits)->ptr[i]; if (RSTRING_LEN(path_bit) > 0) { str_idx = rb_funcall( path_bit, rb_intern( "index" ), 1, ns_indic ); if (str_idx == Qnil || str_idx == Qfalse) // didn't find the :, so it looks like we don't have a namespace path_bit = rb_str_plus( ns_prefix, path_bit ); } rb_ary_push( ret_ary, path_bit ); } return rb_ary_join( ret_ary, slash ); }
/* call-seq: stmt.bind_param(key, value) * * Binds value to the named (or positional) placeholder. If +param+ is a * Fixnum, it is treated as an index for a positional placeholder. * Otherwise it is used as the name of the placeholder to bind to. * * See also #bind_params. */ static VALUE bind_param(VALUE self, VALUE key, VALUE value) { sqlite3StmtRubyPtr ctx; int status; int index; Data_Get_Struct(self, sqlite3StmtRuby, ctx); REQUIRE_OPEN_STMT(ctx); switch(TYPE(key)) { case T_SYMBOL: key = rb_funcall(key, rb_intern("to_s"), 0); case T_STRING: if(RSTRING_PTR(key)[0] != ':') key = rb_str_plus(rb_str_new2(":"), key); index = sqlite3_bind_parameter_index(ctx->st, StringValuePtr(key)); break; default: index = (int)NUM2INT(key); } if(index == 0) rb_raise(rb_path2class("SQLite3::Exception"), "no such bind parameter"); switch(TYPE(value)) { case T_STRING: if(CLASS_OF(value) == cSqlite3Blob || rb_enc_get_index(value) == rb_ascii8bit_encindex() ) { status = sqlite3_bind_blob( ctx->st, index, (const char *)StringValuePtr(value), (int)RSTRING_LEN(value), SQLITE_TRANSIENT ); } else { if (UTF16_LE_P(value) || UTF16_BE_P(value)) { status = sqlite3_bind_text16( ctx->st, index, (const char *)StringValuePtr(value), (int)RSTRING_LEN(value), SQLITE_TRANSIENT ); } else { if (!UTF8_P(value) || !USASCII_P(value)) { value = rb_str_encode(value, rb_enc_from_encoding(rb_utf8_encoding()), 0, Qnil); } status = sqlite3_bind_text( ctx->st, index, (const char *)StringValuePtr(value), (int)RSTRING_LEN(value), SQLITE_TRANSIENT ); } } break; case T_BIGNUM: { sqlite3_int64 num64; if (bignum_to_int64(value, &num64)) { status = sqlite3_bind_int64(ctx->st, index, num64); break; } } case T_FLOAT: status = sqlite3_bind_double(ctx->st, index, NUM2DBL(value)); break; case T_FIXNUM: status = sqlite3_bind_int64(ctx->st, index, (sqlite3_int64)FIX2LONG(value)); break; case T_NIL: status = sqlite3_bind_null(ctx->st, index); break; default: rb_raise(rb_eRuntimeError, "can't prepare %s", rb_class2name(CLASS_OF(value))); break; } CHECK(sqlite3_db_handle(ctx->st), status); return self; }
VALUE string_spec_rb_str_plus(VALUE self, VALUE str1, VALUE str2) { return rb_str_plus(str1, str2); }
VALUE ss_str_plus(VALUE self, VALUE str1, VALUE str2) { return rb_str_plus(str1, str2); }
/* call-seq: stmt.bind_param(key, value) * * Binds value to the named (or positional) placeholder. If +param+ is a * Fixnum, it is treated as an index for a positional placeholder. * Otherwise it is used as the name of the placeholder to bind to. * * See also #bind_params. */ static VALUE bind_param(VALUE self, VALUE key, VALUE value) { sqlite3StmtRubyPtr ctx; int status; int index; Data_Get_Struct(self, sqlite3StmtRuby, ctx); REQUIRE_OPEN_STMT(ctx); switch(TYPE(key)) { case T_SYMBOL: key = rb_funcall(key, rb_intern("to_s"), 0); case T_STRING: if(RSTRING_PTR(key)[0] != ':') key = rb_str_plus(rb_str_new2(":"), key); index = sqlite3_bind_parameter_index(ctx->st, StringValuePtr(key)); break; default: index = (int)NUM2INT(key); } if(index == 0) rb_raise(rb_path2class("SQLite3::Exception"), "no such bind parameter"); switch(TYPE(value)) { case T_STRING: if(CLASS_OF(value) == cSqlite3Blob #ifdef HAVE_RUBY_ENCODING_H || rb_enc_get_index(value) == rb_ascii8bit_encindex() #endif ) { status = sqlite3_bind_blob( ctx->st, index, (const char *)StringValuePtr(value), (int)RSTRING_LEN(value), SQLITE_TRANSIENT ); } else { #ifdef HAVE_RUBY_ENCODING_H if(!UTF8_P(value)) { VALUE db = rb_iv_get(self, "@connection"); VALUE encoding = rb_funcall(db, rb_intern("encoding"), 0); rb_encoding * enc = rb_to_encoding(encoding); value = rb_str_export_to_enc(value, enc); } #endif status = sqlite3_bind_text( ctx->st, index, (const char *)StringValuePtr(value), (int)RSTRING_LEN(value), SQLITE_TRANSIENT ); } break; case T_BIGNUM: #if SIZEOF_LONG < 8 if (RBIGNUM_LEN(value) * SIZEOF_BDIGITS <= 8) { status = sqlite3_bind_int64(ctx->st, index, (sqlite3_int64)NUM2LL(value)); break; } #endif case T_FLOAT: status = sqlite3_bind_double(ctx->st, index, NUM2DBL(value)); break; case T_FIXNUM: status = sqlite3_bind_int64(ctx->st, index, (sqlite3_int64)FIX2LONG(value)); break; case T_NIL: status = sqlite3_bind_null(ctx->st, index); break; default: rb_raise(rb_eRuntimeError, "can't prepare %s", rb_class2name(CLASS_OF(value))); break; } CHECK(sqlite3_db_handle(ctx->st), status); return self; }
/* Return "Application Name - Window Title" string to identify a window */ VALUE MW_Window_to_string(VALUE self) { return rb_str_plus( MW_Window_app_name(self), rb_str_plus(rb_str_new2(" - "), MW_Window_title(self)) ); }