Пример #1
0
/*	fetch_hash2 (internal)	*/
static VALUE fetch_hash2(VALUE obj, VALUE with_table)
{
    MYSQL_RES* res = GetMysqlRes(obj);
    unsigned int n = mysql_num_fields(res);
    MYSQL_ROW row = mysql_fetch_row(res);
    unsigned long* lengths = mysql_fetch_lengths(res);
    MYSQL_FIELD* fields = mysql_fetch_fields(res);
    unsigned int i;
    VALUE hash;
    if (row == NULL)
	return Qnil;
    hash = rb_hash_new();
    for (i=0; i<n; i++) {
	VALUE col;
	if (row[i] == NULL)
	    continue;
	if (with_table == Qnil || with_table == Qfalse)
	    col = rb_tainted_str_new2(fields[i].name);
	else {
	    col = rb_tainted_str_new(fields[i].table, strlen(fields[i].table)+strlen(fields[i].name)+1);
	    RSTRING(col)->ptr[strlen(fields[i].table)] = '.';
	    strcpy(RSTRING(col)->ptr+strlen(fields[i].table)+1, fields[i].name);
	}
	rb_hash_aset(hash, col, row[i]? rb_tainted_str_new(row[i], lengths[i]): Qnil);
    }
    return hash;
}
Пример #2
0
VALUE
rb_yp_next(VALUE self, VALUE domain, VALUE map, VALUE inkey)
{
    char *key, *val;
    int keylen, vallen;
    int res;
    VALUE vkey, vval;

    if( domain == Qnil ) {
        domain = rb_yp_get_default_domain(self);
    };

    res = yp_next(STR2CSTR(domain), STR2CSTR(map),
                  STR2CSTR(inkey), RSTRING(inkey)->len,
                  &key, &keylen, &val, &vallen);
    rb_yp_check_yperr(res);

    if( keylen > 0 ) {
        vkey = rb_tainted_str_new(key, keylen);
    }
    else {
        vkey = Qnil;
    };

    if( vallen > 0 ) {
        vval = rb_tainted_str_new(val, vallen);
    }
    else {
        vval = Qnil;
    };

    return rb_assoc_new(vkey, vval);
};
Пример #3
0
static int
rb_ypall_callback(int status, char *inkey, int inkeylen, char *inval,
                  int invallen, char *indata)
{
    VALUE cb = (VALUE)indata;
    VALUE res, key, val;

    if( inkeylen > 0 ) {
        key = rb_tainted_str_new(inkey, inkeylen);
    }
    else {
        key = Qnil;
    };

    if( invallen > 0 ) {
        val = rb_tainted_str_new(inval, invallen);
    }
    else {
        val = Qnil;
    };

    if( SYMBOL_P(cb) ) {
        res = rb_funcall(rb_mKernel, SYM2ID(cb), 3,
                         INT2NUM(status), key, val);
    }
    else if( cb == Qnil ) {
        res = rb_yield(rb_ary_new3(3, INT2NUM(status), key, val));
    }
    else {
        res = rb_funcall(cb, rb_intern("call"), 3,
                         INT2NUM(status), key, val);
    };

    /* return value:
     * 0: call again for additional key-value pair
     * nonzero: don't call again
     */
    if( TYPE(res) != T_FIXNUM ) {
        if( res == Qfalse || res == Qnil ) {
            return 0;
        }
        else {
            return 1;
        };
    }
    else {
        return FIX2INT(res);
    };
};
Пример #4
0
static VALUE
jpeg_buf_internal(VALUE obj, VALUE quality)
{
    char *buf = NULL;
    int length;

    GetImg(obj, data, im);

#if ATLEAST_VIPS( 7, 28 )
{
    size_t len;

    if (vips_jpegsave_buffer(im, &buf, &len,
        "Q", NUM2INT(quality),
	NULL))
        vips_lib_error();

    /* Argh.
     */
    length = len;
}
#else
    if (im_vips2bufjpeg(im, NULL, NUM2INT(quality), &buf, &length)) 
        vips_lib_error();
#endif

    return rb_tainted_str_new(buf, length);
}
Пример #5
0
static VALUE
env_str_new(const char *ptr, long len)
{
    VALUE str = rb_tainted_str_new(ptr, len);
    rb_obj_freeze(str);
    return str;
}
Пример #6
0
/*
 * Apache::MultiVal#initialize( *values )
 * --
 * Create a new MultiVal object with the specified +values+. The first of
 * the values given will be the value used for String operations.
 */
static VALUE
multival_init( VALUE self, VALUE args )
{
    VALUE collect;
    long len, i;

    /* Make sure there's at least one argument to work with */
    if ( RARRAY_LEN(args) == 0 )
        rb_ary_push( args, rb_tainted_str_new("", 0) );

    /* Stringify all arguments */
    len = RARRAY_LEN(args);
    collect = rb_ary_new2(len);
    for ( i = 0; i < len; i++ ) {
        VALUE str;
		
        str = rb_str_dup( rb_obj_as_string(RARRAY_PTR(args)[i]) );
        OBJ_INFECT( str, RARRAY_PTR(args)[i] );
		
        rb_ary_push( collect, str );
    }
	
    rb_iv_set( self, "@args", collect );
    return self;
}
Пример #7
0
static VALUE
fdbm_fetch(VALUE obj, VALUE keystr, VALUE ifnone)
{
    datum key, value;
    struct dbmdata *dbmp;
    DBM *dbm;
    long len;

    ExportStringValue(keystr);
    len = RSTRING_LEN(keystr);
    if (TOO_LONG(len)) goto not_found;
    key.dptr = RSTRING_PTR(keystr);
    key.dsize = (DSIZE_TYPE)len;

    GetDBM2(obj, dbmp, dbm);
    value = dbm_fetch(dbm, key);
    if (value.dptr == 0) {
      not_found:
	if (NIL_P(ifnone) && rb_block_given_p()) {
	    keystr = rb_str_dup(keystr);
	    OBJ_TAINT(keystr);
	    return rb_yield(keystr);
	}
	return ifnone;
    }
    return rb_tainted_str_new(value.dptr, value.dsize);
}
/*
 * Document-class: PG::TextDecoder::String < PG::SimpleDecoder
 *
 * This is a decoder class for conversion of PostgreSQL text output to
 * to Ruby String object. The output value will have the character encoding
 * set with PG::Connection#internal_encoding= .
 *
 */
VALUE
pg_text_dec_string(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
{
    VALUE ret = rb_tainted_str_new( val, len );
    PG_ENCODING_SET_NOCHECK( ret, enc_idx );
    return ret;
}
Пример #9
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;
}
Пример #10
0
/* Gobal Helper Functions {{{1 */
static VALUE
str_from_tok(struct rcstoken *tok)
{
	if (tok == NULL)
		rb_raise(rb_eRuntimeError, "Token is NULL");
	return rb_tainted_str_new(tok->str, tok->len);
}
Пример #11
0
static VALUE
str_from_tok2(struct rcstoken *tok)
{
	if (tok == NULL)
		return Qnil;
	return rb_tainted_str_new(tok->str, tok->len);
}
Пример #12
0
static VALUE
bdb_env_log_get(VALUE obj, VALUE a)
{
    bdb_ENV *envst;
    DBT data;
    struct dblsnst *lsnst;
    VALUE res, lsn;
    int ret, flag;

    GetEnvDB(obj, envst);
    flag = NUM2INT(a);
    MEMZERO(&data, DBT, 1);
    data.flags |= DB_DBT_MALLOC;
    lsn = bdb_makelsn(obj);
    Data_Get_Struct(lsn, struct dblsnst, lsnst);
#if HAVE_ST_DB_ENV_LG_INFO
    if (!envst->envp->lg_info) {
	rb_raise(bdb_eFatal, "log region not open");
    }
    ret = bdb_test_error(log_get(envst->envp->lg_info, lsnst->lsn, &data, flag));
#else
    ret = bdb_test_error(log_get(envst->envp, lsnst->lsn, &data, flag));
#endif
    if (ret == DB_NOTFOUND) {
	return Qnil;
    }
    res = rb_tainted_str_new(data.data, data.size);
    free(data.data);
    return rb_assoc_new(res, lsn);
}
Пример #13
0
static VALUE
png_buf_internal(VALUE obj, VALUE compression, VALUE interlace)
{
#if IM_MAJOR_VERSION > 7 || IM_MINOR_VERSION >= 23
    VipsImage *im_out;
    char *buf;
    int length;
    GetImg(obj, data, im);

    if (!(im_out = im_open("writer_png_buf", "p")))
        vips_lib_error();

    if (im_vips2bufpng(im, im_out, NUM2INT(compression), NUM2INT(interlace),
        &buf, &length)) {
		im_close(im_out);
        vips_lib_error();
	}

    im_close(im_out);

    return rb_tainted_str_new(buf, length);
#else
    rb_raise(eVIPSError, "This method is not implemented in your version of VIPS");
#endif
}
Пример #14
0
/*
 * call-seq:
 * entry.get_values(attr)  => Array of String
 * entry.vals(attr)        => Array of String
 * entry[attr]             => Array of String
 *
 * Return an array of all the values belonging to the attribute, +attr+, of
 * the entry.
 */
VALUE
rb_ldap_entry_get_values (VALUE self, VALUE attr)
{
  RB_LDAPENTRY_DATA *edata;
  char *c_attr;
  struct berval **c_vals;
  int i;
  int count;
  VALUE vals;

  GET_LDAPENTRY_DATA (self, edata);
  c_attr = StringValueCStr (attr);

  c_vals = ldap_get_values_len (edata->ldap, edata->msg, c_attr);
  if (c_vals)
    {
      vals = rb_ary_new ();
      count = ldap_count_values_len (c_vals);
      for (i = 0; i < count; i++)
	{
	  VALUE str;
	  str = rb_tainted_str_new (c_vals[i]->bv_val, c_vals[i]->bv_len);
	  rb_ary_push (vals, str);
	}
      ldap_value_free_len (c_vals);
    }
  else
    {
      vals = Qnil;
    }

  return vals;
}
Пример #15
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;
}
Пример #16
0
static
VALUE w_hook_data_to_string(const char *data, size_t len)
{
   if(!data)
      return Qnil;
   
   return rb_tainted_str_new(data, len);
}
Пример #17
0
VALUE
pg_text_dec_string(t_pg_type *conv, char *val, int len, int tuple, int field, int enc_idx)
{
	VALUE ret = rb_tainted_str_new( val, len );
#ifdef M17N_SUPPORTED
	ENCODING_SET_INLINED( ret, enc_idx );
#endif
	return ret;
}
Пример #18
0
static VALUE
fdbm_fetch(VALUE obj, VALUE keystr, VALUE ifnone)
{
    datum key, value;
    struct dbmdata *dbmp;
    DBM *dbm;

    ExportStringValue(keystr);
    key.dptr = RSTRING_PTR(keystr);
    key.dsize = RSTRING_LEN(keystr);

    GetDBM2(obj, dbmp, dbm);
    value = dbm_fetch(dbm, key);
    if (value.dptr == 0) {
	if (ifnone == Qnil && rb_block_given_p())
	    return rb_yield(rb_tainted_str_new(key.dptr, key.dsize));
	return ifnone;
    }
    return rb_tainted_str_new(value.dptr, value.dsize);
}
Пример #19
0
static VALUE
jpeg_buf_internal(VALUE obj, VALUE quality)
{
    char *buf = NULL;
    int length;

    GetImg(obj, data, im);

    if (im_vips2bufjpeg(im, NULL, NUM2INT(quality), &buf, &length)) 
        vips_lib_error();

    return rb_tainted_str_new(buf, length);
}
Пример #20
0
/*
 * call-seq:
 *   attr_get_binary(attr_type) -> string
 *
 * <b>(new in 2.0.4)</b>
 *
 * Gets the value of an attribute as `ub1 *' datatype.
 * The return value is tagged with ASCII-8BIT when the ruby version is 1.9.
 *
 * <b>Caution:</b> If the specified attr_type's datatype is not a
 * pointer type, it causes a segmentation fault.
 */
static VALUE attr_get_binary(VALUE self, VALUE attr_type)
{
    oci8_base_t *base = DATA_PTR(self);
    union {
        char *value;
        ub8 dummy; /* padding for incorrect attrtype to protect the stack */
    } v;
    ub4 size = 0;

    v.dummy = 0;
    Check_Type(attr_type, T_FIXNUM);
    oci_lc(OCIAttrGet(base->hp.ptr, base->type, &v.value, &size, FIX2INT(attr_type), oci8_errhp));
    return rb_tainted_str_new(v.value, size);
}
/*
 * Document-class: PG::TextDecoder::Bytea < PG::SimpleDecoder
 *
 * This is a decoder class for conversion of PostgreSQL bytea type
 * to binary String objects.
 *
 */
static VALUE
pg_text_dec_bytea(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
{
    unsigned char *to;
    size_t to_len;
    VALUE ret;

    to = PQunescapeBytea( (unsigned char *)val, &to_len);

    ret = rb_tainted_str_new((char*)to, to_len);
    PQfreemem(to);

    return ret;
}
Пример #22
0
static VALUE
minidb_get(VALUE self, VALUE key)
{
    DEPOT *db;
    char *p;
    int len;
    VALUE str;
    Data_Get_Struct(self, DEPOT, db);
    StringValue(key);
    p = dpget(db, RSTRING_PTR(key), RSTRING_LEN(key), 0, -1, &len);
    str = rb_tainted_str_new(p, len);
    free(p);
    return str;
}
Пример #23
0
static VALUE
memory_get_bytes(VALUE self, VALUE offset, VALUE length)
{
    AbstractMemory* ptr = MEMORY(self);
    long off, len;
    
    off = NUM2LONG(offset);
    len = NUM2LONG(length);

    checkRead(ptr);
    checkBounds(ptr, off, len);
    
    return rb_tainted_str_new((char *) ptr->address + off, len);
}
Пример #24
0
/*
 *  call-seq:
 *     dir.each { |filename| block }  => dir
 *
 *  Calls the block once for each entry in this directory, passing the
 *  filename of each entry as a parameter to the block.
 *
 *     d = Dir.new("testdir")
 *     d.each  {|x| puts "Got #{x}" }
 *
 *  <em>produces:</em>
 *
 *     Got .
 *     Got ..
 *     Got config.h
 *     Got main.rb
 */
static VALUE
dir_each(VALUE dir)
{
    struct dir_data *dirp;
    struct dirent *dp;

    RETURN_ENUMERATOR(dir, 0, 0);
    GetDIR(dir, dirp);
    rewinddir(dirp->dir);
    for (dp = readdir(dirp->dir); dp != NULL; dp = readdir(dirp->dir)) {
	rb_yield(dir_enc_str(rb_tainted_str_new(dp->d_name, NAMLEN(dp)), dirp));
	if (dirp->dir == NULL) dir_closed();
    }
    return dir;
}
Пример #25
0
/*
 *  Initiates a *blocking* read
 */
static VALUE 
rb_aio_read(aiocb_t *cb)
{
    int ret;    
    TRAP_BEG;
    ret = aio_read(cb);
    TRAP_END;
    if (ret != 0) rb_aio_read_error();
    while ( aio_error(cb) == EINPROGRESS );
    if ((ret = aio_return(cb)) > 0) {
      return rb_tainted_str_new( (char *)cb->aio_buf, cb->aio_nbytes );
    }else{
      return INT2NUM(errno);
    }
}
Пример #26
0
static VALUE sqlite3val2rb(sqlite3_value * val)
{
  switch(sqlite3_value_type(val)) {
    case SQLITE_INTEGER:
      return LL2NUM(sqlite3_value_int64(val));
      break;
    case SQLITE_FLOAT:
      return rb_float_new(sqlite3_value_double(val));
      break;
    case SQLITE_TEXT:
      return rb_tainted_str_new2((const char *)sqlite3_value_text(val));
      break;
    case SQLITE_BLOB: {
      /* Sqlite warns calling sqlite3_value_bytes may invalidate pointer from sqlite3_value_blob,
         so we explicitly get the length before getting blob pointer.
         Note that rb_str_new and rb_tainted_str_new apparently create string with ASCII-8BIT (BINARY) encoding,
         which is what we want, as blobs are binary
       */
      int len = sqlite3_value_bytes(val);
#ifdef HAVE_RUBY_ENCODING_H
      return rb_tainted_str_new((const char *)sqlite3_value_blob(val), len);
#else
      /* When encoding is not available, make it class SQLite3::Blob. */
      VALUE strargv[1];
      strargv[0] = rb_tainted_str_new((const char *)sqlite3_value_blob(val), len);
      return rb_class_new_instance(1, strargv, cSqlite3Blob);
#endif
      break;
    }
    case SQLITE_NULL:
      return Qnil;
      break;
    default:
      rb_raise(rb_eRuntimeError, "bad type"); /* FIXME */
  }
}
Пример #27
0
/*
 *  Blocking lio_listio
 */
static VALUE
rb_aio_lio_listio_blocking(VALUE *cbs)
{
    aiocb_t *list[AIO_MAX_LIST];
    int op;
    int ops = rb_aio_lio_listio( LIO_WAIT, cbs, list );
    VALUE results = rb_ary_new2( ops );
    for (op=0; op < ops; op++) {
      if (list[op]->aio_lio_opcode == LIO_READ){ 
         rb_ary_push( results, rb_tainted_str_new( (char *)list[op]->aio_buf, list[op]->aio_nbytes ) );
      }else{
         rb_ary_push( results, INT2FIX(list[op]->aio_nbytes) );
      }
    } 
    return results;
}
Пример #28
0
// read from buffer and return a ruby string
// params: start=0, length=self.length, encoding=Encoding.default_internal_encoding
// return: ruby string
VALUE ruv_buffer_read(int argc, VALUE* argv, VALUE rb_buffer) {
    VALUE rb_start, rb_length, rb_intern_enc, rb_str;
    size_t start, length;
    rb_encoding  *intern_enc;
    ruv_buffer_t *buffer;
    char *str_p;

    Data_Get_Struct(rb_buffer, ruv_buffer_t, buffer);

    rb_scan_args(argc, argv, "03", &rb_start, &rb_length, &rb_intern_enc);

    if(!NIL_P(rb_start)) {
        Check_Type(rb_start, T_FIXNUM);
        start = NUM2SIZET(rb_start);
    } else {
        start = 0;
    }
    if(start >= buffer->length) {
        rb_raise(rb_eArgError, "start is out of bounds.");
    }

    if(!NIL_P(rb_length)) {
        Check_Type(rb_length, T_FIXNUM);
        length = NUM2SIZET(length);
    } else {
        length = buffer->length - start;
    }

    // make sure it's not out of bound
    length = MIN(length, buffer->length - start);

    if(!NIL_P(rb_intern_enc)) {
        intern_enc = rb_enc_get(rb_intern_enc);
    } else {
        intern_enc = rb_default_internal_encoding();
    }

    str_p = buffer->data + start;

    rb_str = rb_tainted_str_new(str_p, length);

    if(intern_enc) {
        rb_str = rb_str_export_to_enc(rb_str, intern_enc);
    }

    return rb_str;
}
Пример #29
0
static VALUE
png_buf_internal(VALUE obj, VALUE compression, VALUE interlace)
{
#if IM_MAJOR_VERSION > 7 || IM_MINOR_VERSION >= 23
    char *buf;
    size_t length;
    GetImg(obj, data, im);

    if (im_vips2bufpng(im, NULL, NUM2INT(compression), NUM2INT(interlace),
        &buf, &length)) 
        vips_lib_error();

    return rb_tainted_str_new(buf, length);
#else
    rb_raise(eVIPSError, "This method is not implemented in your version of VIPS");
#endif
}
Пример #30
0
static VALUE
minidb_each(VALUE self)
{
    DEPOT * db;
    char *p;
    int len;
    VALUE key;
    Data_Get_Struct(self, DEPOT, db);
    dpiterinit(db);
    for(;;) {
        p = dpiternext(db, &len);
        if (!p) break;
        key = rb_tainted_str_new(p, len);
        rb_yield_values(2, key, minidb_get(self, key));
    }
    return self;
}