Exemple #1
0
static void DNSSD_API
dnssd_service_resolve_reply(DNSServiceRef client, DNSServiceFlags flags,
    uint32_t interface, DNSServiceErrorType e, const char *name,
    const char *target, uint16_t port, uint16_t txt_len,
    const unsigned char *txt_rec, void *context) {
  VALUE service, reply, argv[7];

  dnssd_check_error_code(e);

  service = (VALUE)context;

  argv[0] = service;
  argv[1] = ULONG2NUM(flags);
  argv[2] = ULONG2NUM(interface);
  argv[3] = rb_str_new2(name);
  rb_enc_associate(argv[3], rb_utf8_encoding());
  argv[4] = rb_str_new2(target);
  rb_enc_associate(argv[4], rb_utf8_encoding());
  argv[5] = UINT2NUM(ntohs(port));
  argv[6] = rb_str_new((char *)txt_rec, txt_len);
  rb_enc_associate(argv[6], rb_utf8_encoding());

  reply = rb_class_new_instance(7, argv, cDNSSDReplyResolve);

  dnssd_service_callback(service, reply);
}
Exemple #2
0
static int yajl_found_hash_key(void * ctx, const unsigned char * stringVal, unsigned int stringLen) {
    yajl_parser_wrapper * wrapper;
    VALUE keyStr;
#ifdef HAVE_RUBY_ENCODING_H
    rb_encoding *default_internal_enc;
#endif
    GetParser((VALUE)ctx, wrapper);
#ifdef HAVE_RUBY_ENCODING_H
    default_internal_enc = rb_default_internal_encoding();
#endif

    if (wrapper->symbolizeKeys) {
        char buf[stringLen+1];
        memcpy(buf, stringVal, stringLen);
        buf[stringLen] = 0;
        VALUE stringEncoded = rb_str_new2(buf);
#ifdef HAVE_RUBY_ENCODING_H
        rb_enc_associate(stringEncoded, rb_utf8_encoding());
#endif

        yajl_set_static_value(ctx, ID2SYM(rb_to_id(stringEncoded)));
    } else {
        keyStr = rb_str_new((const char *)stringVal, stringLen);
#ifdef HAVE_RUBY_ENCODING_H
        rb_enc_associate(keyStr, utf8Encoding);
        if (default_internal_enc) {
          keyStr = rb_str_export_to_enc(keyStr, default_internal_enc);
        }
#endif
        yajl_set_static_value(ctx, keyStr);
    }
    yajl_check_and_fire_callback(ctx);
    return 1;
}
Exemple #3
0
void rb_raise_mysql2_stmt_error(mysql_stmt_wrapper *stmt_wrapper) {
  VALUE e;
  GET_CLIENT(stmt_wrapper->client);
  VALUE rb_error_msg = rb_str_new2(mysql_stmt_error(stmt_wrapper->stmt));
  VALUE rb_sql_state = rb_tainted_str_new2(mysql_stmt_sqlstate(stmt_wrapper->stmt));

#ifdef HAVE_RUBY_ENCODING_H
  rb_encoding *conn_enc;
  conn_enc = rb_to_encoding(wrapper->encoding);

  rb_encoding *default_internal_enc = rb_default_internal_encoding();

  rb_enc_associate(rb_error_msg, conn_enc);
  rb_enc_associate(rb_sql_state, conn_enc);
  if (default_internal_enc) {
    rb_error_msg = rb_str_export_to_enc(rb_error_msg, default_internal_enc);
    rb_sql_state = rb_str_export_to_enc(rb_sql_state, default_internal_enc);
  }
#endif

  e = rb_funcall(cMysql2Error, intern_new_with_args, 4,
                 rb_error_msg,
                 LONG2FIX(wrapper->server_version),
                 UINT2NUM(mysql_stmt_errno(stmt_wrapper->stmt)),
                 rb_sql_state);
  rb_exc_raise(e);
}
Exemple #4
0
// Reads data from the IO, according to the Rack specifications for `#read`.
static VALUE tfio_read(int argc, VALUE *argv, VALUE self) {
  int fd = get_tmpfile(self);
  size_t pos = get_pos(self);
  size_t end = get_end(self);
  VALUE buffer = Qnil;
  char ret_nil = 0;
  ssize_t len = 0;
  // get the buffer object if given
  if (argc == 2) {
    Check_Type(argv[1], T_STRING);
    buffer = argv[1];
  }
  // get the length object, if given
  if (argc > 0 && argv[0] != Qnil) {
    Check_Type(argv[0], T_FIXNUM);
    len = FIX2LONG(argv[0]);
    if (len < 0)
      rb_raise(rb_eRangeError, "length should be bigger then 0.");
    ret_nil = 1;
  }
  // return if we're at the EOF.
  if (pos == end)
    goto no_data;
  // calculate length if it wasn't specified.
  if (len == 0) {
    // make sure we're not reading more then we have
    len = end - pos;
    // set position for future reads
    set_pos(self, end);
    if (len == 0)
      goto no_data;
  } else {
    // set position for future reads
    set_pos(self, pos + len);
  }
  // limit read to what we have
  if (len + pos > end)
    len = end - pos;
  // create the buffer if we don't have one.
  if (buffer == Qnil) {
    buffer = rb_str_buf_new(len);
    // make sure the buffer is binary encoded.
    rb_enc_associate(buffer, BinaryEncoding);
  } else {
    // make sure the buffer is binary encoded.
    rb_enc_associate(buffer, BinaryEncoding);
    if (rb_str_capacity(buffer) < len)
      rb_str_resize(buffer, len);
  }
  // read the data.
  if (pread(fd, RSTRING_PTR(buffer), len, pos) <= 0)
    goto no_data;
  rb_str_set_len(buffer, len);
  return buffer;
no_data:
  if (ret_nil)
    return Qnil;
  else
    return rb_str_buf_new(0);
}
Exemple #5
0
static VALUE rb_raise_mysql2_error(mysql_client_wrapper *wrapper) {
  VALUE rb_error_msg = rb_str_new2(mysql_error(wrapper->client));
  VALUE rb_sql_state = rb_tainted_str_new2(mysql_sqlstate(wrapper->client));
  VALUE e;
#ifdef HAVE_RUBY_ENCODING_H
  if (wrapper->server_version < 50500) {
    /* MySQL < 5.5 uses mixed encoding, just call it binary. */
    int err_enc = rb_ascii8bit_encindex();
    rb_enc_associate_index(rb_error_msg, err_enc);
    rb_enc_associate_index(rb_sql_state, err_enc);
  } else {
    /* MySQL >= 5.5 uses UTF-8 errors internally and converts them to the connection encoding. */
    rb_encoding *default_internal_enc = rb_default_internal_encoding();
    rb_encoding *conn_enc = rb_to_encoding(wrapper->encoding);
    rb_enc_associate(rb_error_msg, conn_enc);
    rb_enc_associate(rb_sql_state, conn_enc);
    if (default_internal_enc) {
      rb_error_msg = rb_str_export_to_enc(rb_error_msg, default_internal_enc);
      rb_sql_state = rb_str_export_to_enc(rb_sql_state, default_internal_enc);
    }
  }
#endif

  e = rb_exc_new3(cMysql2Error, rb_error_msg);
  rb_funcall(e, intern_error_number_eql, 1, UINT2NUM(mysql_errno(wrapper->client)));
  rb_funcall(e, intern_sql_state_eql, 1, rb_sql_state);
  rb_exc_raise(e);
  return Qnil;
}
Exemple #6
0
static void DNSSD_API
dnssd_service_query_record_reply(DNSServiceRef client, DNSServiceFlags flags,
    uint32_t interface, DNSServiceErrorType e, const char *fullname,
    uint16_t rrtype, uint16_t rrclass, uint16_t rdlen, const void *rdata,
    uint32_t ttl, void *context) {
  VALUE service, reply, argv[8];

  dnssd_check_error_code(e);

  service = (VALUE)context;

  argv[0] = service;
  argv[1] = ULONG2NUM(flags);
  argv[2] = ULONG2NUM(interface);
  argv[3] = rb_str_new2(fullname);
  rb_enc_associate(argv[3], rb_utf8_encoding());
  argv[4] = UINT2NUM(rrtype);
  argv[5] = UINT2NUM(rrclass);
  argv[6] = rb_str_new((char *)rdata, rdlen);
  rb_enc_associate(argv[6], rb_utf8_encoding());
  argv[7] = ULONG2NUM(ttl);

  reply = rb_class_new_instance(8, argv, cDNSSDReplyQueryRecord);

  dnssd_service_callback(service, reply);
}
Exemple #7
0
static VALUE mysql2_set_field_string_encoding(VALUE val, MYSQL_FIELD field, rb_encoding *default_internal_enc, rb_encoding *conn_enc) {
  /* if binary flag is set, respect its wishes */
  if (field.flags & BINARY_FLAG && field.charsetnr == 63) {
    rb_enc_associate(val, binaryEncoding);
  } else if (!field.charsetnr) {
    /* MySQL 4.x may not provide an encoding, binary will get the bytes through */
    rb_enc_associate(val, binaryEncoding);
  } else {
    /* lookup the encoding configured on this field */
    const char *enc_name;
    int enc_index;

    enc_name = mysql2_mysql_enc_to_rb[field.charsetnr-1];
    if (enc_name != NULL) {
      /* use the field encoding we were able to match */
      enc_index = rb_enc_find_index(enc_name);
      rb_enc_set_index(val, enc_index);
    } else {
      /* otherwise fall-back to the connection's encoding */
      rb_enc_associate(val, conn_enc);
    }

    if (default_internal_enc) {
      val = rb_str_export_to_enc(val, default_internal_enc);
    }
  }
  return val;
}
Exemple #8
0
static VALUE
syserr_initialize(int argc, VALUE *argv, VALUE self)
{
#if !defined(_WIN32)
    char *strerror();
#endif
    const char *err;
    VALUE mesg, error, func;
    VALUE klass = rb_obj_class(self);

    if (klass == rb_eSystemCallError) {
	st_data_t data = (st_data_t)klass;
	rb_scan_args(argc, argv, "12", &mesg, &error, &func);
	if (argc == 1 && FIXNUM_P(mesg)) {
	    error = mesg; mesg = Qnil;
	}
	if (!NIL_P(error) && st_lookup(syserr_tbl, NUM2LONG(error), &data)) {
	    klass = (VALUE)data;
	    /* change class */
	    if (!RB_TYPE_P(self, T_OBJECT)) { /* insurance to avoid type crash */
		rb_raise(rb_eTypeError, "invalid instance type");
	    }
	    RBASIC_SET_CLASS(self, klass);
	}
    }
    else {
	rb_scan_args(argc, argv, "02", &mesg, &func);
	error = rb_const_get(klass, rb_intern("Errno"));
    }
    if (!NIL_P(error)) err = strerror(NUM2INT(error));
    else err = "unknown error";
    if (!NIL_P(mesg)) {
	rb_encoding *le = rb_locale_encoding();
	VALUE str = StringValue(mesg);
	rb_encoding *me = rb_enc_get(mesg);

	if (NIL_P(func))
	    mesg = rb_sprintf("%s - %"PRIsVALUE, err, mesg);
	else
	    mesg = rb_sprintf("%s @ %"PRIsVALUE" - %"PRIsVALUE, err, func, mesg);
	if (le != me && rb_enc_asciicompat(me)) {
	    le = me;
	}/* else assume err is non ASCII string. */
	OBJ_INFECT(mesg, str);
	rb_enc_associate(mesg, le);
    }
    else {
	mesg = rb_str_new2(err);
	rb_enc_associate(mesg, rb_locale_encoding());
    }
    rb_call_super(1, &mesg);
    rb_iv_set(self, "errno", error);
    return self;
}
Exemple #9
0
static VALUE rb_tinytds_escape(VALUE self, VALUE string) {
  Check_Type(string, T_STRING);
  GET_CLIENT_WRAPPER(self);
  VALUE new_string = rb_funcall(string, intern_gsub, 2, opt_escape_regex, opt_escape_dblquote);
  rb_enc_associate(new_string, cwrap->encoding);
  return new_string;
}
Exemple #10
0
static VALUE rb_raise_mysql2_error(mysql_client_wrapper *wrapper) {
  VALUE rb_error_msg = rb_str_new2(mysql_error(wrapper->client));
  VALUE rb_sql_state = rb_tainted_str_new2(mysql_sqlstate(wrapper->client));
  VALUE e;

#ifdef HAVE_RUBY_ENCODING_H
  rb_enc_associate(rb_error_msg, rb_utf8_encoding());
  rb_enc_associate(rb_sql_state, rb_usascii_encoding());
#endif

  e = rb_funcall(cMysql2Error, rb_intern("new"), 2, rb_error_msg, LONG2FIX(wrapper->server_version));
  rb_funcall(e, intern_error_number_eql, 1, UINT2NUM(mysql_errno(wrapper->client)));
  rb_funcall(e, intern_sql_state_eql, 1, rb_sql_state);
  rb_exc_raise(e);
  return Qnil;
}
Exemple #11
0
static char *
ruby__sfvextra(rb_printf_buffer *fp, size_t valsize, void *valp, long *sz, int sign)
{
    VALUE value, result = (VALUE)fp->_bf._base;
    rb_encoding *enc;
    char *cp;

    if (valsize != sizeof(VALUE)) return 0;
    value = *(VALUE *)valp;
    if (RBASIC(result)->klass) {
	rb_raise(rb_eRuntimeError, "rb_vsprintf reentered");
    }
    if (sign == '+') {
	value = rb_inspect(value);
    }
    else {
	value = rb_obj_as_string(value);
    }
    enc = rb_enc_compatible(result, value);
    if (enc) {
	rb_enc_associate(result, enc);
    }
    else {
	enc = rb_enc_get(result);
	value = rb_str_conv_enc_opts(value, rb_enc_get(value), enc,
				     ECONV_UNDEF_REPLACE|ECONV_INVALID_REPLACE,
				     Qnil);
	*(volatile VALUE *)valp = value;
    }
    StringValueCStr(value);
    RSTRING_GETMEM(value, cp, *sz);
    ((rb_printf_buffer_extra *)fp)->value = value;
    OBJ_INFECT(result, value);
    return cp;
}
Exemple #12
0
/*
 * Convert a Verse host id to a String with ASCII8BIT encoding.
 */
inline VALUE
rbverse_host_id2str( const uint8 *hostid ) {
	VALUE idstring = rb_str_new( (const char *)hostid, V_HOST_ID_SIZE );

	rb_enc_associate( idstring, rb_ascii8bit_encoding() );
	return idstring;
}
Exemple #13
0
/* call-seq: as_s()
 *
 * *return* value as an String.
 */
static VALUE
sax_value_as_s(VALUE self) {
    SaxDrive	dr = DATA_PTR(self);
    VALUE	rs;

    if ('\0' == *dr->buf.str) {
	return Qnil;
    }
    if (dr->options.convert_special) {
	ox_sax_collapse_special(dr, dr->buf.str, dr->buf.pos, dr->buf.line, dr->buf.col);
    }
    switch (dr->options.skip) {
    case CrSkip:
	buf_collapse_return(dr->buf.str);
	break;
    case SpcSkip:
	buf_collapse_white(dr->buf.str);
	break;
    default:
	break;
    }
    rs = rb_str_new2(dr->buf.str);
#if HAS_ENCODING_SUPPORT
    if (0 != dr->encoding) {
	rb_enc_associate(rs, dr->encoding);
    }
#elif HAS_PRIVATE_ENCODING
    if (Qnil != dr->encoding) {
	rb_funcall(rs, ox_force_encoding_id, 1, dr->encoding);
    }
#endif
    return rs;
}
Exemple #14
0
static inline void
preserve_original_state(VALUE orig, VALUE dest)
{
    rb_enc_associate(dest, rb_enc_get(orig));

    RB_OBJ_INFECT_RAW(dest, orig);
}
Exemple #15
0
int map_key_callback(void *ctx, const unsigned char *stringVal, size_t stringLen) {
  VALUE key;
#ifdef HAVE_RUBY_ENCODING_H
  rb_encoding *default_internal_enc;
#endif

  if ( ((CTX *)ctx)->symbolizeKeys ) {
#ifdef HAVE_RUBY_ENCODING_H
    ID id = rb_intern3((const char *)stringVal, stringLen, utf8Encoding);
    key = ID2SYM(id);
#else
    VALUE str = rb_str_new((const char *)stringVal, stringLen);
    key = rb_str_intern(str);
#endif
  } else {
    key = rb_str_new((const char *)stringVal, stringLen);
#ifdef HAVE_RUBY_ENCODING_H
    default_internal_enc = rb_default_internal_encoding();
    rb_enc_associate(key, utf8Encoding);
    if (default_internal_enc) {
      key = rb_str_export_to_enc(key, default_internal_enc);
    }
#endif
  }
  set_key(ctx, key);
  return 1;
}
Exemple #16
0
static VALUE rb_mysql_result_fetch_field(VALUE self, unsigned int idx, int symbolize_keys) {
  VALUE rb_field;
  GET_RESULT(self);

  if (wrapper->fields == Qnil) {
    wrapper->numberOfFields = mysql_num_fields(wrapper->result);
    wrapper->fields = rb_ary_new2(wrapper->numberOfFields);
  }

  rb_field = rb_ary_entry(wrapper->fields, idx);
  if (rb_field == Qnil) {
    MYSQL_FIELD *field = NULL;
    rb_encoding *default_internal_enc = rb_default_internal_encoding();
    rb_encoding *conn_enc = rb_to_encoding(wrapper->encoding);

    field = mysql_fetch_field_direct(wrapper->result, idx);
    if (symbolize_keys) {
      rb_field = rb_intern3(field->name, field->name_length, rb_utf8_encoding());
      rb_field = ID2SYM(rb_field);
    } else {
      rb_field = rb_str_new(field->name, field->name_length);
      rb_enc_associate(rb_field, conn_enc);
      if (default_internal_enc) {
        rb_field = rb_str_export_to_enc(rb_field, default_internal_enc);
      }
    }
    rb_ary_store(wrapper->fields, idx, rb_field);
  }

  return rb_field;
}
Exemple #17
0
static VALUE rb_mysql_client_escape(VALUE self, VALUE str) {
    VALUE newStr;
    unsigned long newLen, oldLen;
    GET_CLIENT(self);

    REQUIRE_OPEN_DB(wrapper);
    Check_Type(str, T_STRING);
#ifdef HAVE_RUBY_ENCODING_H
    rb_encoding *default_internal_enc = rb_default_internal_encoding();
    rb_encoding *conn_enc = rb_to_encoding(wrapper->encoding);
    // ensure the string is in the encoding the connection is expecting
    str = rb_str_export_to_enc(str, conn_enc);
#endif

    oldLen = RSTRING_LEN(str);
    newStr = rb_str_new(0, oldLen*2+1);

    newLen = mysql_real_escape_string(wrapper->client, RSTRING_PTR(newStr), StringValuePtr(str), oldLen);
    if (newLen == oldLen) {
        // no need to return a new ruby string if nothing changed
        return str;
    } else {
        rb_str_resize(newStr, newLen);
#ifdef HAVE_RUBY_ENCODING_H
        rb_enc_associate(newStr, conn_enc);
        if (default_internal_enc) {
            newStr = rb_str_export_to_enc(newStr, default_internal_enc);
        }
#endif
        return newStr;
    }
}
Exemple #18
0
/*
 * call-seq:
 *    res.getvalue( tup_num, field_num )
 *
 * Returns the value in tuple number _tup_num_, field _field_num_,
 * or +nil+ if the field is +NULL+.
 */
static VALUE
pgresult_getvalue(VALUE self, VALUE tup_num, VALUE field_num)
{
	VALUE val;
	PGresult *result;
	int i = NUM2INT(tup_num);
	int j = NUM2INT(field_num);

	result = pgresult_get(self);
	if(i < 0 || i >= PQntuples(result)) {
		rb_raise(rb_eArgError,"invalid tuple number %d", i);
	}
	if(j < 0 || j >= PQnfields(result)) {
		rb_raise(rb_eArgError,"invalid field number %d", j);
	}
	if(PQgetisnull(result, i, j))
		return Qnil;
	val = rb_tainted_str_new(PQgetvalue(result, i, j),
				PQgetlength(result, i, j));

#ifdef M17N_SUPPORTED
	/* associate client encoding for text format only */
	if ( 0 == PQfformat(result, j) ) {
		ASSOCIATE_INDEX( val, self );
	} else {
		rb_enc_associate( val, rb_ascii8bit_encoding() );
	}
#endif

	return val;
}
Exemple #19
0
static void
require_libraries(VALUE *req_list)
{
    VALUE list = *req_list;
    VALUE self = rb_vm_top_self();
    ID require;
    rb_thread_t *th = GET_THREAD();
    rb_block_t *prev_base_block = th->base_block;
    rb_encoding *extenc = rb_default_external_encoding();
    int prev_parse_in_eval = th->parse_in_eval;
    th->base_block = 0;
    th->parse_in_eval = 0;

    Init_ext();		/* should be called here for some reason :-( */
    CONST_ID(require, "require");
    while (list && RARRAY_LEN(list) > 0) {
	VALUE feature = rb_ary_shift(list);
	rb_enc_associate(feature, extenc);
	RBASIC(feature)->klass = rb_cString;
	OBJ_FREEZE(feature);
	rb_funcall2(self, require, 1, &feature);
    }
    *req_list = 0;

    th->parse_in_eval = prev_parse_in_eval;
    th->base_block = prev_base_block;
}
Exemple #20
0
static VALUE rb_mysql_client_server_info(VALUE self) {
  VALUE version, server_info;
#ifdef HAVE_RUBY_ENCODING_H
  rb_encoding *default_internal_enc;
  rb_encoding *conn_enc;
#endif
  GET_CLIENT(self);

  REQUIRE_OPEN_DB(wrapper);
#ifdef HAVE_RUBY_ENCODING_H
  default_internal_enc = rb_default_internal_encoding();
  conn_enc = rb_to_encoding(wrapper->encoding);
#endif

  version = rb_hash_new();
  rb_hash_aset(version, sym_id, LONG2FIX(mysql_get_server_version(wrapper->client)));
  server_info = rb_str_new2(mysql_get_server_info(wrapper->client));
#ifdef HAVE_RUBY_ENCODING_H
  rb_enc_associate(server_info, conn_enc);
  if (default_internal_enc) {
    server_info = rb_str_export_to_enc(server_info, default_internal_enc);
  }
#endif
  rb_hash_aset(version, sym_version, server_info);
  return version;
}
Exemple #21
0
/**
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 tfio_gets(VALUE self) {
  int fd = get_tmpfile(self);
  size_t pos = get_pos(self);
  size_t end = get_end(self);
  if (pos == end)
    return Qnil;
  size_t pos_e = pos;
  char c;
  int ret;
  VALUE buffer;

  do {
    ret = pread(fd, &c, 1, pos_e);
  } while (ret > 0 && c != '\n' && (++pos_e < end));
  set_pos(self, pos_e + 1);
  if (pos > pos_e) {
    buffer = rb_str_buf_new(pos_e - pos);
    // make sure the buffer is binary encoded.
    rb_enc_associate(buffer, BinaryEncoding);
    if (pread(fd, RSTRING_PTR(buffer), pos_e - pos, pos) < 0)
      return Qnil;
    rb_str_set_len(buffer, pos_e - pos);
    return buffer;
  }
  return Qnil;
}
Exemple #22
0
static VALUE rb_mysql_client_escape(VALUE self, VALUE str) {
  MYSQL * client;
  VALUE newStr;
  unsigned long newLen, oldLen;

  Check_Type(str, T_STRING);
#ifdef HAVE_RUBY_ENCODING_H
  rb_encoding *default_internal_enc = rb_default_internal_encoding();
  rb_encoding *conn_enc = rb_to_encoding(rb_iv_get(self, "@encoding"));
  // ensure the string is in the encoding the connection is expecting
  str = rb_str_export_to_enc(str, conn_enc);
#endif

  oldLen = RSTRING_LEN(str);
  char escaped[(oldLen*2)+1];

  Data_Get_Struct(self, MYSQL, client);

  REQUIRE_OPEN_DB(client);
  newLen = mysql_real_escape_string(client, escaped, StringValuePtr(str), RSTRING_LEN(str));
  if (newLen == oldLen) {
    // no need to return a new ruby string if nothing changed
    return str;
  } else {
    newStr = rb_str_new(escaped, newLen);
#ifdef HAVE_RUBY_ENCODING_H
    rb_enc_associate(newStr, conn_enc);
    if (default_internal_enc) {
      newStr = rb_str_export_to_enc(newStr, default_internal_enc);
    }
#endif
    return newStr;
  }
}
Exemple #23
0
static VALUE rb_mysql_client_info(VALUE self) {
  VALUE version, client_info;
#ifdef HAVE_RUBY_ENCODING_H
  rb_encoding *default_internal_enc;
  rb_encoding *conn_enc;
#endif
  GET_CLIENT(self);
  version = rb_hash_new();

#ifdef HAVE_RUBY_ENCODING_H
  default_internal_enc = rb_default_internal_encoding();
  conn_enc = rb_to_encoding(wrapper->encoding);
#endif

  rb_hash_aset(version, sym_id, LONG2NUM(mysql_get_client_version()));
  client_info = rb_str_new2(mysql_get_client_info());
#ifdef HAVE_RUBY_ENCODING_H
  rb_enc_associate(client_info, conn_enc);
  if (default_internal_enc) {
    client_info = rb_str_export_to_enc(client_info, default_internal_enc);
  }
#endif
  rb_hash_aset(version, sym_version, client_info);
  return version;
}
Exemple #24
0
VALUE
rb_enc_vsprintf(rb_encoding *enc, const char *fmt, va_list ap)
{
    rb_printf_buffer_extra buffer;
#define f buffer.base
    VALUE result;

    f._flags = __SWR | __SSTR;
    f._bf._size = 0;
    f._w = 120;
    result = rb_str_buf_new(f._w);
    if (enc) {
	if (rb_enc_mbminlen(enc) > 1) {
	    /* the implementation deeply depends on plain char */
	    rb_raise(rb_eArgError, "cannot construct wchar_t based encoding string: %s",
		     rb_enc_name(enc));
	}
	rb_enc_associate(result, enc);
    }
    f._bf._base = (unsigned char *)result;
    f._p = (unsigned char *)RSTRING_PTR(result);
    RBASIC_CLEAR_CLASS(result);
    f.vwrite = ruby__sfvwrite;
    f.vextra = ruby__sfvextra;
    buffer.value = 0;
    BSD_vfprintf(&f, fmt, ap);
    RBASIC_SET_CLASS_RAW(result, rb_cString);
    rb_str_resize(result, (char *)f._p - RSTRING_PTR(result));
#undef f

    return result;
}
Exemple #25
0
/*
 * Make a Ruby array out of the encoded values from the specified
 * column in the given result.
 */
static VALUE
make_column_result_array( VALUE self, int col )
{
	PGresult *result = pgresult_get( self );
	int rows = PQntuples( result );
	int i;
	VALUE val = Qnil;
	VALUE results = rb_ary_new2( rows );

	if ( col >= PQnfields(result) )
		rb_raise( rb_eIndexError, "no column %d in result", col );

	for ( i=0; i < rows; i++ ) {
		val = rb_tainted_str_new( PQgetvalue(result, i, col),
		                          PQgetlength(result, i, col) );

#ifdef M17N_SUPPORTED
		/* associate client encoding for text format only */
		if ( 0 == PQfformat(result, col) ) {
			ASSOCIATE_INDEX( val, self );
		} else {
			rb_enc_associate( val, rb_ascii8bit_encoding() );
		}
#endif

		rb_ary_store( results, i, val );
	}

	return results;
}
Exemple #26
0
VALUE layout_get_default(const upb_fielddef *field) {
  switch (upb_fielddef_type(field)) {
    case UPB_TYPE_FLOAT:   return DBL2NUM(upb_fielddef_defaultfloat(field));
    case UPB_TYPE_DOUBLE:  return DBL2NUM(upb_fielddef_defaultdouble(field));
    case UPB_TYPE_BOOL:
      return upb_fielddef_defaultbool(field) ? Qtrue : Qfalse;
    case UPB_TYPE_MESSAGE: return Qnil;
    case UPB_TYPE_ENUM: {
      const upb_enumdef *enumdef = upb_fielddef_enumsubdef(field);
      int32_t num = upb_fielddef_defaultint32(field);
      const char *label = upb_enumdef_iton(enumdef, num);
      if (label) {
        return ID2SYM(rb_intern(label));
      } else {
        return INT2NUM(num);
      }
    }
    case UPB_TYPE_INT32:   return INT2NUM(upb_fielddef_defaultint32(field));
    case UPB_TYPE_INT64:   return LL2NUM(upb_fielddef_defaultint64(field));;
    case UPB_TYPE_UINT32:  return UINT2NUM(upb_fielddef_defaultuint32(field));
    case UPB_TYPE_UINT64:  return ULL2NUM(upb_fielddef_defaultuint64(field));
    case UPB_TYPE_STRING:
    case UPB_TYPE_BYTES: {
      size_t size;
      const char *str = upb_fielddef_defaultstr(field, &size);
      VALUE str_rb = rb_str_new(str, size);

      rb_enc_associate(str_rb, (upb_fielddef_type(field) == UPB_TYPE_BYTES) ?
                 kRubyString8bitEncoding : kRubyStringUtf8Encoding);
      rb_obj_freeze(str_rb);
      return str_rb;
    }
    default: return Qnil;
  }
}
Exemple #27
0
static void
add_element(PInfo pi, const char *ename, Attr attrs, int hasChildren) {
    VALUE       e;
    VALUE       s = rb_str_new2(ename);

#ifdef HAVE_RUBY_ENCODING_H
    if (0 != pi->encoding) {
        rb_enc_associate(s, pi->encoding);
    }
#endif
    e = rb_obj_alloc(ox_element_clas);
    rb_ivar_set(e, value_id, s);
    if (0 != attrs->name) {
        VALUE   ah = rb_hash_new();
        
        for (; 0 != attrs->name; attrs++) {
            VALUE   sym;
            VALUE   *slot;

            if (Qundef == (sym = ox_cache_get(symbol_cache, attrs->name, &slot))) {
                sym = ID2SYM(rb_intern(attrs->name));
                *slot = sym;
            }
            s = rb_str_new2(attrs->value);
#ifdef HAVE_RUBY_ENCODING_H
            if (0 != pi->encoding) {
                rb_enc_associate(s, pi->encoding);
            }
#endif
            rb_hash_aset(ah, sym, s);
        }
        rb_ivar_set(e, attributes_id, ah);
    }
    if (0 == pi->h) { // top level object
        pi->h = pi->helpers;
        pi->obj = e;
    } else {
        rb_ary_push(pi->h->obj, e);
        pi->h++;
    }
    if (hasChildren) {
        VALUE   nodes = rb_ary_new();

        rb_ivar_set(e, nodes_id, nodes);
        pi->h->obj = nodes;
    }
}
Exemple #28
0
static VALUE rb_raise_mysql2_error(mysql_client_wrapper *wrapper) {
  VALUE rb_error_msg = rb_str_new2(mysql_error(wrapper->client));
  VALUE rb_sql_state = rb_tainted_str_new2(mysql_sqlstate(wrapper->client));
  VALUE e;

#ifdef HAVE_RUBY_ENCODING_H
  rb_enc_associate(rb_error_msg, rb_utf8_encoding());
  rb_enc_associate(rb_sql_state, rb_usascii_encoding());
#endif

  e = rb_funcall(cMysql2Error, intern_new_with_args, 4,
                 rb_error_msg,
                 LONG2FIX(wrapper->server_version),
                 UINT2NUM(mysql_errno(wrapper->client)),
                 rb_sql_state);
  rb_exc_raise(e);
}
Exemple #29
0
// Appends a 'bytes' string to a repeated field.
static void* appendbytes_handler(void *closure,
                                 const void *hd,
                                 size_t size_hint) {
    VALUE ary = (VALUE)closure;
    VALUE str = rb_str_new2("");
    rb_enc_associate(str, kRubyString8bitEncoding);
    RepeatedField_push(ary, str);
    return (void*)str;
}
Exemple #30
0
/* call-seq:
 *    client.info
 *
 * Returns a string that represents the client library version.
 */
static VALUE rb_mysql_client_info(RB_MYSQL_UNUSED VALUE klass) {
  VALUE version_info, version, header_version;
  version_info = rb_hash_new();

  version = rb_str_new2(mysql_get_client_info());
  header_version = rb_str_new2(MYSQL_LINK_VERSION);

#ifdef HAVE_RUBY_ENCODING_H
  rb_enc_associate(version, rb_usascii_encoding());
  rb_enc_associate(header_version, rb_usascii_encoding());
#endif

  rb_hash_aset(version_info, sym_id, LONG2NUM(mysql_get_client_version()));
  rb_hash_aset(version_info, sym_version, version);
  rb_hash_aset(version_info, sym_header_version, header_version);

  return version_info;
}