示例#1
0
文件: rodbtp.c 项目: cfiorini/rodbtp
VALUE rodbres_fields(VALUE klass)
{
	odbHANDLE *hQry;
	int n, numCols = 0;

	Data_Get_Struct(klass, odbHANDLE, hQry);

	numCols = odbGetTotalCols((odbHANDLE)hQry);

	VALUE ary = rb_ary_new2(numCols);
	for(n = 1; n <= numCols; n++) {
		rb_ary_push(ary, rb_tainted_str_new2(odbColName( (odbHANDLE)hQry, n) ));
	}
	return ary;
}
示例#2
0
/*
 * call-seq:
 *    res.error_field(fieldcode) -> String
 *
 * Returns the individual field of an error.
 *
 * +fieldcode+ is one of:
 * * +PG_DIAG_SEVERITY+
 * * +PG_DIAG_SQLSTATE+
 * * +PG_DIAG_MESSAGE_PRIMARY+
 * * +PG_DIAG_MESSAGE_DETAIL+
 * * +PG_DIAG_MESSAGE_HINT+
 * * +PG_DIAG_STATEMENT_POSITION+
 * * +PG_DIAG_INTERNAL_POSITION+
 * * +PG_DIAG_INTERNAL_QUERY+
 * * +PG_DIAG_CONTEXT+
 * * +PG_DIAG_SOURCE_FILE+
 * * +PG_DIAG_SOURCE_LINE+
 * * +PG_DIAG_SOURCE_FUNCTION+
 *
 * An example:
 * 
 *   begin
 *       conn.exec( "SELECT * FROM nonexistant_table" )
 *   rescue PG::Error => err
 *       p [
 *           result.error_field( PG::Result::PG_DIAG_SEVERITY ),
 *           result.error_field( PG::Result::PG_DIAG_SQLSTATE ),
 *           result.error_field( PG::Result::PG_DIAG_MESSAGE_PRIMARY ),
 *           result.error_field( PG::Result::PG_DIAG_MESSAGE_DETAIL ),
 *           result.error_field( PG::Result::PG_DIAG_MESSAGE_HINT ),
 *           result.error_field( PG::Result::PG_DIAG_STATEMENT_POSITION ),
 *           result.error_field( PG::Result::PG_DIAG_INTERNAL_POSITION ),
 *           result.error_field( PG::Result::PG_DIAG_INTERNAL_QUERY ),
 *           result.error_field( PG::Result::PG_DIAG_CONTEXT ),
 *           result.error_field( PG::Result::PG_DIAG_SOURCE_FILE ),
 *           result.error_field( PG::Result::PG_DIAG_SOURCE_LINE ),
 *           result.error_field( PG::Result::PG_DIAG_SOURCE_FUNCTION ),
 *       ]
 *   end
 * 
 * Outputs:
 * 
 *   ["ERROR", "42P01", "relation \"nonexistant_table\" does not exist", nil, nil, 
 *    "15", nil, nil, nil, "path/to/parse_relation.c", "857", "parserOpenTable"]
 */
static VALUE
pgresult_error_field(VALUE self, VALUE field)
{
	PGresult *result = pgresult_get( self );
	int fieldcode = NUM2INT( field );
	char * fieldstr = PQresultErrorField( result, fieldcode );
	VALUE ret = Qnil;

	if ( fieldstr ) {
		ret = rb_tainted_str_new2( fieldstr );
		ASSOCIATE_INDEX( ret, self );
	}

	return ret;
}
示例#3
0
/*
 * call-seq:
 *    res.fname( index ) -> String
 *
 * Returns the name of the column corresponding to _index_.
 */
static VALUE
pgresult_fname(VALUE self, VALUE index)
{
	VALUE fname;
	PGresult *result;
	int i = NUM2INT(index);

	result = pgresult_get(self);
	if (i < 0 || i >= PQnfields(result)) {
		rb_raise(rb_eArgError,"invalid field number %d", i);
	}
	fname = rb_tainted_str_new2(PQfname(result, i));
	ASSOCIATE_INDEX(fname, self);
	return fname;
}
示例#4
0
static VALUE
env_assoc(VALUE env, SEL sel, VALUE key)
{
    rb_secure(4);

    const char *s = StringValuePtr(key);
    if (strlen(s) != RSTRING_LEN(key)) {
	rb_raise(rb_eArgError, "bad environment variable name");
    }
    const char *e = getenv(s);
    if (e != NULL) {
	return rb_assoc_new(key, rb_tainted_str_new2(e));
    }
    return Qnil;
}
示例#5
0
static VALUE
rb_rcsfile_getlog(VALUE self, VALUE rev)
{
	struct rb_rcsfile *rb_rf = rcsfile_data(self);
	VALUE ret;
	char *data;

	StringValue(rev);
	data = rcsgetlog(rb_rf->rf, RSTRING_PTR(rev));
	if (data == NULL)
		return Qnil;
	ret = rb_tainted_str_new2(data);
	free(data);
	return ret;
}
示例#6
0
static VALUE
rb_shadow_getspent(VALUE self)
{
  struct spwd *entry;
  VALUE result;

  entry = getspent();

  if( entry == NULL )
    return Qnil;

  result = rb_struct_new(rb_sPasswdEntry,
		      rb_tainted_str_new2(entry->sp_namp),
		      rb_tainted_str_new2(entry->sp_pwdp),
		      INT2FIX(entry->sp_lstchg),
		      INT2FIX(entry->sp_min),
		      INT2FIX(entry->sp_max),
		      INT2FIX(entry->sp_warn),
		      INT2FIX(entry->sp_inact),
		      INT2FIX(entry->sp_expire),
		      INT2FIX(entry->sp_flag),
		      0);
  return result;
};
示例#7
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;
}
示例#8
0
/*
 * call-seq:
 *    res.fields() -> Array
 *
 * Returns an array of Strings representing the names of the fields in the result.
 */
static VALUE
pgresult_fields(VALUE self)
{
	PGresult *result = pgresult_get( self );
	int n = PQnfields( result );
	VALUE fields = rb_ary_new2( n );
	int i;

	for ( i = 0; i < n; i++ ) {
		VALUE val = rb_tainted_str_new2(PQfname(result, i));
		ASSOCIATE_INDEX(val, self);
		rb_ary_store( fields, i, val );
	}

	return fields;
}
示例#9
0
文件: etc.c 项目: Shopify/ruby
/* call-seq:
 *	getlogin	->  String
 *
 * Returns the short user name of the currently logged in user.
 * Unfortunately, it is often rather easy to fool ::getlogin.
 *
 * Avoid ::getlogin for security-related purposes.
 *
 * If ::getlogin fails, try ::getpwuid.
 *
 * See the unix manpage for <code>getpwuid(3)</code> for more detail.
 *
 * e.g.
 *   Etc.getlogin -> 'guest'
 */
static VALUE
etc_getlogin(VALUE obj)
{
    char *login;

#ifdef HAVE_GETLOGIN
    login = getlogin();
    if (!login) login = getenv("USER");
#else
    login = getenv("USER");
#endif

    if (login)
	return rb_tainted_str_new2(login);
    return Qnil;
}
示例#10
0
文件: client.c 项目: jaylane/mysql2
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);
}
示例#11
0
文件: ngraph.c 项目: htrb/ngraph-gtk
static int
load_script(int argc, char **argv)
{
  VALUE r_argv, fname;
  int state, i;

  if (argc < 1) {
    return 0;
  }

  r_argv = rb_const_get(rb_mKernel, rb_intern("ARGV"));
  rb_ary_clear(r_argv);
  for (i = 1; i < argc; i++) {
    rb_ary_push(r_argv, rb_tainted_str_new2(argv[i]));
  }

  fname = rb_funcall(rb_cFile, ExpandPath, 1, rb_str_new2(argv[0]));
  rb_load_protect(fname, 1, &state);
  if (state) {
    VALUE errinfo, errstr, errat;
    int n, i;
    const char *cstr;

    errinfo = rb_errinfo();
    errstr = rb_obj_as_string(errinfo);
    cstr = StringValueCStr(errstr);
    if (strcmp(cstr, "exit")) {
      ngraph_err_puts(cstr);
      errat = rb_funcall(errinfo, rb_intern("backtrace"), 0);
      if (! NIL_P(errat)) {
	n = RARRAY_LEN(errat);
	for (i = 0; i < n; i ++) {
	  errstr = rb_str_new2("\tfrom ");
	  rb_str_append(errstr, rb_ary_entry(errat, i));
	  ngraph_err_puts(StringValueCStr(errstr));
	}
      }
    }
  }
  rb_gc_start();

  return 0;
}
示例#12
0
文件: yp.c 项目: flavorjones/nis
VALUE
rb_yp_master(VALUE self, VALUE domain, VALUE map)
{
    int res;
    char *master;
    VALUE obj;

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

    res = yp_master(STR2CSTR(domain), STR2CSTR(map), &master);
    rb_yp_check_yperr(res);

    obj = rb_tainted_str_new2(master);
    free(master);

    return obj;
};
示例#13
0
/*	list_dbs(db=nil)	*/
static VALUE list_dbs(int argc, VALUE* argv, VALUE obj)
{
    unsigned int i, n;
    VALUE db, ret;
    MYSQL* m = GetHandler(obj);
    MYSQL_RES* res;

    rb_scan_args(argc, argv, "01", &db);
    res = mysql_list_dbs(m, NILorSTRING(db));
    if (res == NULL)
	mysql_raise(m);

    n = mysql_num_rows(res);
    ret = rb_ary_new2(n);
    for (i=0; i<n; i++)
	rb_ary_store(ret, i, rb_tainted_str_new2(mysql_fetch_row(res)[0]));
    mysql_free_result(res);
    return ret;
}
示例#14
0
static VALUE
rb_rcsfile_resolve_sym(int argc, VALUE *argv, VALUE self)
{
	struct rb_rcsfile *rb_rf = rcsfile_data(self);
	VALUE sym;
	VALUE ret;
	const char *symstr = "HEAD";
	char *rev;

	if (rb_scan_args(argc, argv, "01", &sym) == 1) {
		StringValue(sym);
		symstr = RSTRING_PTR(sym);
	}
	rev = rcsrevfromsym(rb_rf->rf, symstr);
	if (rev == NULL)
		return Qnil;
	ret = rb_tainted_str_new2(rev);
	free(rev);
	return ret;
}
示例#15
0
/*
 * call-seq:
 * entry.to_hash  => Hash
 *
 * Convert the entry to a hash.
 */
VALUE
rb_ldap_entry_to_hash (VALUE self)
{
  VALUE attrs = rb_ldap_entry_get_attributes (self);
  VALUE hash = rb_hash_new ();
  VALUE attr, vals;
  int i;

  Check_Type (attrs, T_ARRAY);
  rb_hash_aset (hash, rb_tainted_str_new2 ("dn"),
		rb_ary_new3 (1, rb_ldap_entry_get_dn (self)));
  for (i = 0; i < RARRAY_LEN (attrs); i++)
    {
      attr = rb_ary_entry (attrs, i);
      vals = rb_ldap_entry_get_values (self, attr);
      rb_hash_aset (hash, attr, vals);
    }

  return hash;
}
示例#16
0
文件: log.c 项目: mallowlabs/ruby-bdb
static VALUE
bdb_lsn_log_file(VALUE obj)
{
    struct dblsnst *lsnst;
    bdb_ENV *envst;
    char name[2048];

    GetLsn(obj, lsnst, envst);
#if HAVE_ST_DB_ENV_LG_INFO
    if (!envst->envp->lg_info) {
	rb_raise(bdb_eFatal, "log region not open");
    }
    bdb_test_error(log_file(envst->envp->lg_info, lsnst->lsn, name, 2048));
#elif HAVE_ST_DB_ENV_LOG_FILE
    bdb_test_error(envst->envp->log_file(envst->envp, lsnst->lsn, name, 2048));
#else
    bdb_test_error(log_file(envst->envp, lsnst->lsn, name, 2048));
#endif
    return rb_tainted_str_new2(name);
}
示例#17
0
/*
 * call-seq:
 *    res[ n ] -> Hash
 *
 * Returns tuple _n_ as a hash.
 */
static VALUE
pgresult_aref(VALUE self, VALUE index)
{
	PGresult *result = pgresult_get(self);
	int tuple_num = NUM2INT(index);
	int field_num;
	VALUE fname;
	VALUE tuple;

	if ( tuple_num < 0 || tuple_num >= PQntuples(result) )
		rb_raise( rb_eIndexError, "Index %d is out of range", tuple_num );

	tuple = rb_hash_new();
	for ( field_num = 0; field_num < PQnfields(result); field_num++ ) {
		fname = rb_tainted_str_new2( PQfname(result,field_num) );
		ASSOCIATE_INDEX(fname, self);
		rb_hash_aset( tuple, fname, pgresult_value(self, result, tuple_num, field_num) );
	}
	return tuple;
}
示例#18
0
文件: client.c 项目: 0xCCD/mysql2
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));
#ifdef HAVE_RUBY_ENCODING_H
  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

  VALUE 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;
}
示例#19
0
文件: ldap.c 项目: afbroman/ruby-ldap
/*
 * call-seq:
 * LDAP.dn2ufn(dn)  => String or nil
 *
 * Translate the DN, +dn+, to a more User-Friendly Name (UFN).
 *
 * For example:
 *
 * <code>LDAP.dn2ufn('uid=ianmacd,ou=People,dc=google,dc=com')</code>
 *
 * produces:
 *
 * ianmacd, People, google.com
 *
 * The UFN format is described in
 * RFC1781[http://www.faqs.org/rfcs/rfc1781.html].
 */
VALUE
rb_ldap_dn2ufn (VALUE self, VALUE dn)
{
  char *c_dn;
  char *c_ufn;

  if (dn == Qnil)
    {
      return Qnil;
    }

  c_dn = StringValueCStr (dn);
  if ((c_ufn = ldap_dn2ufn (c_dn)))
    {
      return rb_tainted_str_new2 (c_ufn);
    }
  else
    {
      return Qnil;
    }
}
示例#20
0
/*
 * call-seq:
 * entry.get_dn  => String
 * entry.dn      => String
 */
VALUE
rb_ldap_entry_get_dn (VALUE self)
{
  RB_LDAPENTRY_DATA *edata;
  char *cdn;
  VALUE dn;

  GET_LDAPENTRY_DATA (self, edata);

  cdn = ldap_get_dn (edata->ldap, edata->msg);
  if (cdn)
    {
      dn = rb_tainted_str_new2 (cdn);
      ldap_memfree (cdn);
    }
  else
    {
      dn = Qnil;
    }

  return dn;
}
示例#21
0
/*
 * load libldap's attributes data structure into ruby hash
 */
static VALUE
rb_ldap_entry_load_attr(LDAP *ldap, LDAPMessage *msg)
{
  VALUE hash = rb_hash_new();
  BerElement *ber = NULL;
  char *c_attr;

  for (c_attr = ldap_first_attribute(ldap, msg, &ber);
    c_attr != NULL;
    c_attr = ldap_next_attribute(ldap, msg, ber)) {
    VALUE attr = rb_tainted_str_new2(c_attr);
    VALUE vals = rb_ldap_entry_load_val(ldap, msg, c_attr);

    rb_hash_aset(hash, attr, vals);
    ldap_memfree(c_attr);
  }

#if !defined(USE_OPENLDAP1)
  ber_free(ber, 0);
#endif

  return hash;
}
示例#22
0
/*
 * call-seq:
 *    res[ n ] -> Hash
 *
 * Returns tuple _n_ as a hash. 
 */
static VALUE
pgresult_aref(VALUE self, VALUE index)
{
	PGresult *result = pgresult_get(self);
	int tuple_num = NUM2INT(index);
	int field_num;
	VALUE fname,val;
	VALUE tuple;

	if ( tuple_num < 0 || tuple_num >= PQntuples(result) )
		rb_raise( rb_eIndexError, "Index %d is out of range", tuple_num );

	tuple = rb_hash_new();
	for ( field_num = 0; field_num < PQnfields(result); field_num++ ) {
		fname = rb_tainted_str_new2( PQfname(result,field_num) );
		ASSOCIATE_INDEX(fname, self);
		if ( PQgetisnull(result, tuple_num, field_num) ) {
			rb_hash_aset( tuple, fname, Qnil );
		}
		else {
			val = rb_tainted_str_new( PQgetvalue(result, tuple_num, field_num ),
			                          PQgetlength(result, tuple_num, field_num) );

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

			rb_hash_aset( tuple, fname, val );
		}
	}
	return tuple;
}
示例#23
0
/*
 *  call-seq:
 *     Dir.chdir( [ string] ) => 0
 *     Dir.chdir( [ string] ) {| path | block }  => anObject
 *
 *  Changes the current working directory of the process to the given
 *  string. When called without an argument, changes the directory to
 *  the value of the environment variable <code>HOME</code>, or
 *  <code>LOGDIR</code>. <code>SystemCallError</code> (probably
 *  <code>Errno::ENOENT</code>) if the target directory does not exist.
 *
 *  If a block is given, it is passed the name of the new current
 *  directory, and the block is executed with that as the current
 *  directory. The original working directory is restored when the block
 *  exits. The return value of <code>chdir</code> is the value of the
 *  block. <code>chdir</code> blocks can be nested, but in a
 *  multi-threaded program an error will be raised if a thread attempts
 *  to open a <code>chdir</code> block while another thread has one
 *  open.
 *
 *     Dir.chdir("/var/spool/mail")
 *     puts Dir.pwd
 *     Dir.chdir("/tmp") do
 *       puts Dir.pwd
 *       Dir.chdir("/usr") do
 *         puts Dir.pwd
 *       end
 *       puts Dir.pwd
 *     end
 *     puts Dir.pwd
 *
 *  <em>produces:</em>
 *
 *     /var/spool/mail
 *     /tmp
 *     /usr
 *     /tmp
 *     /var/spool/mail
 */
static VALUE
dir_s_chdir(int argc, VALUE *argv, VALUE obj)
{
    VALUE path = Qnil;

    rb_secure(2);
    if (rb_scan_args(argc, argv, "01", &path) == 1) {
	FilePathValue(path);
    }
    else {
	const char *dist = getenv("HOME");
	if (!dist) {
	    dist = getenv("LOGDIR");
	    if (!dist) rb_raise(rb_eArgError, "HOME/LOGDIR not set");
	}
	path = rb_str_new2(dist);
    }

    if (chdir_blocking > 0) {
	if (!rb_block_given_p() || rb_thread_current() != chdir_thread)
	    rb_warn("conflicting chdir during another chdir block");
    }

    if (rb_block_given_p()) {
	struct chdir_data args;
	char *cwd = my_getcwd();

	args.old_path = rb_tainted_str_new2(cwd); xfree(cwd);
	args.new_path = path;
	args.done = Qfalse;
	return rb_ensure(chdir_yield, (VALUE)&args, chdir_restore, (VALUE)&args);
    }
    dir_chdir(path);

    return INT2FIX(0);
}
示例#24
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 */
  }
}
示例#25
0
static int
rb_dl_scan_callback_args(long stack[], const char *proto,
			 int *argc, VALUE argv[])
{
  int i;
  long *sp;
  VALUE val;

  sp = stack;
  for (i=1; proto[i]; i++) {
    switch (proto[i]) {
    case 'C':
      {
	char v;
	v = (char)(*sp);
	sp++;
	val = INT2NUM(v);
      }
      break;
    case 'H':
      {
	short v;
	v = (short)(*sp);
	sp++;
	val = INT2NUM(v);
      }
      break;
    case 'I':
      {
	int v;
	v = (int)(*sp);
	sp++;
	val = INT2NUM(v);
      }
      break;
    case 'L':
      {
	long v;
	v = (long)(*sp);
	sp++;
	val = INT2NUM(v);
      }
      break;
    case 'F':
      {
	float v;
	memcpy(&v, sp, sizeof(float));
	sp += sizeof(float)/sizeof(long);
	val = rb_float_new(v);
      }
      break;
    case 'D':
      {
	double v;
	memcpy(&v, sp, sizeof(double));
	sp += sizeof(double)/sizeof(long);
	val = rb_float_new(v);
      }
      break;
    case 'P':
      {
	void *v;
	memcpy(&v, sp, sizeof(void*));
	sp++;
	val = rb_dlptr_new(v, 0, 0);
      }
      break;
    case 'S':
      {
	char *v;
	memcpy(&v, sp, sizeof(void*));
	sp++;
	val = rb_tainted_str_new2(v);
      }
      break;
    default:
      rb_raise(rb_eDLTypeError, "unsupported type `%c'", proto[i]);
      break;
    }
    argv[i-1] = val;
  }
  *argc = (i - 1);

  return (*argc);
}
示例#26
0
文件: Call.c 项目: albanpeignier/ffi
static VALUE
returnL(FunctionType* fnInfo, L* result)
{
    if (unlikely(!fnInfo->ignoreErrno)) {
        rbffi_save_errno();
    }

    /*
     * This needs to do custom boxing of the return value, since a function
     * may only fill out the lower 8, 16 or 32 bits of %al, %ah, %eax, %rax, and
     * the upper part will be garbage.  This will truncate the value again, then
     * sign extend it.
     */
    switch (fnInfo->returnType->nativeType) {
    case NATIVE_VOID:
        return Qnil;

    case NATIVE_INT8:
        return INT2NUM(*(signed char *) result);

    case NATIVE_INT16:
        return INT2NUM(*(signed short *) result);

    case NATIVE_INT32:
        return INT2NUM(*(signed int *) result);

    case NATIVE_LONG:
        return LONG2NUM(*(signed long *) result);

    case NATIVE_UINT8:
        return UINT2NUM(*(unsigned char *) result);

    case NATIVE_UINT16:
        return UINT2NUM(*(unsigned short *) result);

    case NATIVE_UINT32:
        return UINT2NUM(*(unsigned int *) result);

    case NATIVE_ULONG:
        return ULONG2NUM(*(unsigned long *) result);

#ifdef __x86_64__
    case NATIVE_INT64:
        return LL2NUM(*(signed long long *) result);

    case NATIVE_UINT64:
        return ULL2NUM(*(unsigned long long *) result);
#endif /* __x86_64__ */

    case NATIVE_STRING:
        return *(void **) result != 0 ? rb_tainted_str_new2(*(char **) result) : Qnil;

    case NATIVE_POINTER:
        return rbffi_Pointer_NewInstance(*(void **) result);

    case NATIVE_BOOL:
        return *(char *) result != 0 ? Qtrue : Qfalse;

    default:
        rb_raise(rb_eRuntimeError, "invalid return type: %d", fnInfo->returnType->nativeType);
        return Qnil;
    }
}
示例#27
0
static VALUE
safe_setup_str(const char *str)
{
    if (str == 0) str = "";
    return rb_tainted_str_new2(str);
}
示例#28
0
文件: ldap.c 项目: afbroman/ruby-ldap
/* Document-class: LDAP
 *
 * Container module for LDAP-related classes.
 */
void
Init_ldap ()
{
  rb_mLDAP = rb_define_module ("LDAP");

  rb_define_const (rb_mLDAP, "LDAP_VERSION", INT2NUM (LDAP_VERSION));

#ifdef LDAP_VERSION1
  rb_define_const (rb_mLDAP, "LDAP_VERSION1", INT2NUM (LDAP_VERSION1));
#endif

#ifdef LDAP_VERSION2
  rb_define_const (rb_mLDAP, "LDAP_VERSION2", INT2NUM (LDAP_VERSION2));
#endif

#ifdef LDAP_VERSION3
  rb_define_const (rb_mLDAP, "LDAP_VERSION3", INT2NUM (LDAP_VERSION3));
#endif

#ifdef LDAP_VERSION_MAX
  rb_define_const (rb_mLDAP, "LDAP_VERSION_MAX", INT2NUM (LDAP_VERSION_MAX));
#else
  rb_define_const (rb_mLDAP, "LDAP_VERSION_MAX", INT2NUM (LDAP_VERSION));
#endif

  rb_define_const (rb_mLDAP, "VERSION",
		   rb_tainted_str_new2 (RB_LDAP_VERSION));
  rb_define_const (rb_mLDAP, "MAJOR_VERSION",
		   INT2NUM (RB_LDAP_MAJOR_VERSION));
  rb_define_const (rb_mLDAP, "MINOR_VERSION",
		   INT2NUM (RB_LDAP_MINOR_VERSION));
  rb_define_const (rb_mLDAP, "PATCH_VERSION",
		   INT2NUM (RB_LDAP_PATCH_VERSION));

#ifdef LDAP_API_INFO_VERSION
  rb_define_const (rb_mLDAP, "LDAP_API_INFO_VERSION",
		   INT2NUM (LDAP_API_INFO_VERSION));
#else
  rb_define_const (rb_mLDAP, "LDAP_API_INFO_VERSION", Qnil);
#endif

#ifdef LDAP_VENDOR_VERSION
  rb_define_const (rb_mLDAP, "LDAP_VENDOR_VERSION",
		   INT2NUM (LDAP_VENDOR_VERSION));
#else
  rb_define_const (rb_mLDAP, "LDAP_VENDOR_VERSION", Qnil);
#endif
#ifdef LDAP_VENDOR_NAME
  rb_define_const (rb_mLDAP, "LDAP_VENDOR_NAME",
		   rb_tainted_str_new2 (LDAP_VENDOR_NAME));
#else
  rb_define_const (rb_mLDAP, "LDAP_VENDOR_NAME", Qnil);
#endif

#ifdef LDAP_API_VERSION
  rb_define_const (rb_mLDAP, "LDAP_API_VERSION", INT2NUM (LDAP_API_VERSION));
#else
  rb_define_const (rb_mLDAP, "LDAP_API_VERSION", Qnil);
#endif

  rb_define_const (rb_mLDAP, "LDAP_PORT", INT2NUM (389));
  rb_define_const (rb_mLDAP, "LDAPS_PORT", INT2NUM (636));
  rb_eLDAP_Error =
    rb_define_class_under (rb_mLDAP, "Error", rb_eStandardError);
  rb_eLDAP_ResultError =
    rb_define_class_under (rb_mLDAP, "ResultError", rb_eLDAP_Error);
  rb_eLDAP_InvalidDataError =
    rb_define_class_under (rb_mLDAP, "InvalidDataError", rb_eLDAP_Error);
  rb_eLDAP_InvalidEntryError =
    rb_define_class_under (rb_mLDAP, "InvalidEntryError",
			   rb_eLDAP_InvalidDataError);


  rb_define_module_function (rb_mLDAP, "err2string", rb_ldap_err2string, 1);
  rb_define_module_function (rb_mLDAP, "explode_dn", rb_ldap_explode_dn, 2);
  rb_define_module_function (rb_mLDAP, "explode_rdn", rb_ldap_explode_rdn, 2);
  rb_define_module_function (rb_mLDAP, "dn2ufn", rb_ldap_dn2ufn, 1);
  rb_define_module_function (rb_mLDAP, "mod", rb_ldap_mod_s_new, -1);
  rb_define_module_function (rb_mLDAP, "hash2mods", rb_ldap_hash2mods, 2);
  rb_define_module_function (rb_mLDAP, "entry2hash", rb_ldap_entry2hash, 1);

  /* the following error code must be defined in ldap.h */
#define rb_ldap_define_err_code(code) rb_define_const(rb_mLDAP,#code,INT2NUM(code))
  rb_ldap_define_err_code (LDAP_SUCCESS);
  rb_ldap_define_err_code (LDAP_OPERATIONS_ERROR);
  rb_ldap_define_err_code (LDAP_PROTOCOL_ERROR);
  rb_ldap_define_err_code (LDAP_TIMELIMIT_EXCEEDED);
  rb_ldap_define_err_code (LDAP_SIZELIMIT_EXCEEDED);
  rb_ldap_define_err_code (LDAP_COMPARE_FALSE);
  rb_ldap_define_err_code (LDAP_COMPARE_TRUE);
#ifdef LDAP_STRONG_AUTH_NOT_SUPPORTED
  rb_ldap_define_err_code (LDAP_STRONG_AUTH_NOT_SUPPORTED);
#endif
#ifdef LDAP_AUTH_METHOD_NOT_SUPPORTED
  rb_ldap_define_err_code (LDAP_AUTH_METHOD_NOT_SUPPORTED);
#endif
  rb_ldap_define_err_code (LDAP_STRONG_AUTH_REQUIRED);
#ifdef LDAP_REFERRAL
  rb_ldap_define_err_code (LDAP_REFERRAL);
#endif
#ifdef LDAP_ADMINLIMIT_EXCEEDED
  rb_ldap_define_err_code (LDAP_ADMINLIMIT_EXCEEDED);
#endif
#ifdef LDAP_UNAVAILABLE_CRITICAL_EXTENSION
  rb_ldap_define_err_code (LDAP_UNAVAILABLE_CRITICAL_EXTENSION);
#endif
#ifdef LDAP_CONFIDENTIALITY_REQUIRED
  rb_ldap_define_err_code (LDAP_CONFIDENTIALITY_REQUIRED);
#endif
#ifdef LDAP_SASL_BIND_IN_PROGRESS
  rb_ldap_define_err_code (LDAP_SASL_BIND_IN_PROGRESS);
#endif
#ifdef LDAP_PARTIAL_RESULTS
  rb_ldap_define_err_code (LDAP_PARTIAL_RESULTS);
#endif
  rb_ldap_define_err_code (LDAP_NO_SUCH_ATTRIBUTE);
  rb_ldap_define_err_code (LDAP_UNDEFINED_TYPE);
  rb_ldap_define_err_code (LDAP_INAPPROPRIATE_MATCHING);
  rb_ldap_define_err_code (LDAP_CONSTRAINT_VIOLATION);
  rb_ldap_define_err_code (LDAP_TYPE_OR_VALUE_EXISTS);
  rb_ldap_define_err_code (LDAP_INVALID_SYNTAX);
  rb_ldap_define_err_code (LDAP_NO_SUCH_OBJECT);
  rb_ldap_define_err_code (LDAP_ALIAS_PROBLEM);
  rb_ldap_define_err_code (LDAP_INVALID_DN_SYNTAX);
  rb_ldap_define_err_code (LDAP_IS_LEAF);
  rb_ldap_define_err_code (LDAP_ALIAS_DEREF_PROBLEM);
  rb_ldap_define_err_code (LDAP_INAPPROPRIATE_AUTH);
  rb_ldap_define_err_code (LDAP_INVALID_CREDENTIALS);
  rb_ldap_define_err_code (LDAP_INSUFFICIENT_ACCESS);
  rb_ldap_define_err_code (LDAP_BUSY);
  rb_ldap_define_err_code (LDAP_UNAVAILABLE);
  rb_ldap_define_err_code (LDAP_UNWILLING_TO_PERFORM);
  rb_ldap_define_err_code (LDAP_LOOP_DETECT);
  rb_ldap_define_err_code (LDAP_NAMING_VIOLATION);
  rb_ldap_define_err_code (LDAP_OBJECT_CLASS_VIOLATION);
  rb_ldap_define_err_code (LDAP_NOT_ALLOWED_ON_NONLEAF);
  rb_ldap_define_err_code (LDAP_NOT_ALLOWED_ON_RDN);
  rb_ldap_define_err_code (LDAP_ALREADY_EXISTS);
  rb_ldap_define_err_code (LDAP_NO_OBJECT_CLASS_MODS);
  rb_ldap_define_err_code (LDAP_RESULTS_TOO_LARGE);
  rb_ldap_define_err_code (LDAP_OTHER);
  rb_ldap_define_err_code (LDAP_SERVER_DOWN);
  rb_ldap_define_err_code (LDAP_LOCAL_ERROR);
  rb_ldap_define_err_code (LDAP_ENCODING_ERROR);
  rb_ldap_define_err_code (LDAP_DECODING_ERROR);
  rb_ldap_define_err_code (LDAP_TIMEOUT);
  rb_ldap_define_err_code (LDAP_AUTH_UNKNOWN);
  rb_ldap_define_err_code (LDAP_FILTER_ERROR);
  rb_ldap_define_err_code (LDAP_USER_CANCELLED);
  rb_ldap_define_err_code (LDAP_PARAM_ERROR);
  rb_ldap_define_err_code (LDAP_NO_MEMORY);
  /* rb_ldap_define_err_code(LDAP_CONNECT_ERROR); */
#undef rb_ldap_define_err_code

#define rb_ldap_define_opt(code) rb_define_const(rb_mLDAP,#code,INT2NUM((int)code))
#ifdef LDAP_OPT_ON
  rb_ldap_define_opt (LDAP_OPT_ON);
#endif
#ifdef LDAP_OPT_OFF
  rb_ldap_define_opt (LDAP_OPT_OFF);
#endif
#ifdef LDAP_OPT_DESC
  rb_ldap_define_opt (LDAP_OPT_DESC);
#endif
#ifdef LDAP_OPT_DEREF
  rb_ldap_define_opt (LDAP_OPT_DEREF);
#endif
#ifdef LDAP_OPT_SIZELIMIT
  rb_ldap_define_opt (LDAP_OPT_SIZELIMIT);
#endif
#ifdef LDAP_OPT_TIMELIMIT
  rb_ldap_define_opt (LDAP_OPT_TIMELIMIT);
#endif
#ifdef LDAP_OPT_THREAD_FN_PTRS
  rb_ldap_define_opt (LDAP_OPT_THREAD_FN_PTRS);
#endif
#ifdef LDAP_OPT_REBIND_FN
  rb_ldap_define_opt (LDAP_OPT_REBIND_FN);
#endif
#ifdef LDAP_OPT_REBIND_ARG
  rb_ldap_define_opt (LDAP_OPT_REBIND_ARG);
#endif
#ifdef LDAP_OPT_REFERRALS
  rb_ldap_define_opt (LDAP_OPT_REFERRALS);
#endif
#ifdef LDAP_OPT_RESTART
  rb_ldap_define_opt (LDAP_OPT_RESTART);
#endif
#ifdef LDAP_OPT_SSL
  rb_ldap_define_opt (LDAP_OPT_SSL);
#endif
#ifdef LDAP_OPT_IO_FN_PTRS
  rb_ldap_define_opt (LDAP_OPT_IO_FN_PTRS);
#endif
#ifdef LDAP_OPT_CACHE_FN_PTRS
  rb_ldap_define_opt (LDAP_OPT_CACHE_FN_PTRS);
#endif
#ifdef LDAP_OPT_CACHE_STRATEGY
  rb_ldap_define_opt (LDAP_OPT_CACHE_STRATEGY);
#endif
#ifdef LDAP_OPT_CACHE_ENABLE
  rb_ldap_define_opt (LDAP_OPT_CACHE_ENABLE);
#endif
#ifdef LDAP_OPT_REFERRAL_HOP_LIMIT
  rb_ldap_define_opt (LDAP_OPT_REFERRAL_HOP_LIMIT);
#endif
#ifdef LDAP_OPT_PROTOCOL_VERSION
  rb_ldap_define_opt (LDAP_OPT_PROTOCOL_VERSION);
#endif
#ifdef LDAP_OPT_SERVER_CONTROLS
  rb_ldap_define_opt (LDAP_OPT_SERVER_CONTROLS);
#endif
#ifdef LDAP_OPT_CLIENT_CONTROLS
  rb_ldap_define_opt (LDAP_OPT_CLIENT_CONTROLS);
#endif
#ifdef LDAP_OPT_PREFERRED_LANGUAGE
  rb_ldap_define_opt (LDAP_OPT_PREFERRED_LANGUAGE);
#endif
#ifdef LDAP_OPT_API_INFO
  rb_ldap_define_opt (LDAP_OPT_API_INFO);
#endif
#ifdef LDAP_OPT_API_FEATURE_INFO
  rb_ldap_define_opt (LDAP_OPT_API_FEATURE_INFO);
#endif
#ifdef LDAP_OPT_HOST_NAME
  rb_ldap_define_opt (LDAP_OPT_HOST_NAME);
#endif

#ifdef USE_OPENLDAP2		/* OpenLDAP TLS,SASL options */
#ifdef LDAP_OPT_X_TLS_CACERTFILE
  rb_ldap_define_opt (LDAP_OPT_X_TLS_CACERTFILE);
#endif
#ifdef LDAP_OPT_X_TLS_CACERTDIR
  rb_ldap_define_opt (LDAP_OPT_X_TLS_CACERTDIR);
#endif
#ifdef LDAP_OPT_X_TLS_CERT
  rb_ldap_define_opt (LDAP_OPT_X_TLS_CERT);
#endif
#ifdef LDAP_OPT_X_TLS_CERTFILE
  rb_ldap_define_opt (LDAP_OPT_X_TLS_CERTFILE);
#endif
#ifdef LDAP_OPT_X_TLS_KEYFILE
  rb_ldap_define_opt (LDAP_OPT_X_TLS_KEYFILE);
#endif
#ifdef LDAP_OPT_X_TLS_REQUIRE_CERT
  rb_ldap_define_opt (LDAP_OPT_X_TLS_REQUIRE_CERT);
#endif
#ifdef LDAP_OPT_X_TLS
  rb_ldap_define_opt (LDAP_OPT_X_TLS);
#endif
#ifdef LDAP_OPT_X_TLS_PROTOCOL_MIN
  rb_ldap_define_opt (LDAP_OPT_X_TLS_PROTOCOL_MIN);
#endif
#ifdef LDAP_OPT_X_TLS_CIPHER_SUITE
  rb_ldap_define_opt (LDAP_OPT_X_TLS_CIPHER_SUITE);
#endif
#ifdef LDAP_OPT_X_TLS_RANDOM_FILE
  rb_ldap_define_opt (LDAP_OPT_X_TLS_RANDOM_FILE);
#endif
#ifdef LDAP_OPT_X_TLS_NEVER
  rb_ldap_define_opt (LDAP_OPT_X_TLS_NEVER);
#endif
#ifdef LDAP_OPT_X_TLS_HARD
  rb_ldap_define_opt (LDAP_OPT_X_TLS_HARD);
#endif
#ifdef LDAP_OPT_X_TLS_DEMAND
  rb_ldap_define_opt (LDAP_OPT_X_TLS_DEMAND);
#endif
#ifdef LDAP_OPT_X_TLS_ALLOW
  rb_ldap_define_opt (LDAP_OPT_X_TLS_ALLOW);
#endif
#ifdef LDAP_OPT_X_TLS_TRY
  rb_ldap_define_opt (LDAP_OPT_X_TLS_TRY);
#endif
#ifdef LDAP_OPT_X_SASL_MECH
  rb_ldap_define_opt (LDAP_OPT_X_SASL_MECH);
#endif
#ifdef LDAP_OPT_X_SASL_REALM
  rb_ldap_define_opt (LDAP_OPT_X_SASL_REALM);
#endif
#ifdef LDAP_OPT_X_SASL_AUTHCID
  rb_ldap_define_opt (LDAP_OPT_X_SASL_AUTHCID);
#endif
#ifdef LDAP_OPT_X_SASL_AUTHZID
  rb_ldap_define_opt (LDAP_OPT_X_SASL_AUTHZID);
#endif
#ifdef LDAP_OPT_X_SASL_SSF
  rb_ldap_define_opt (LDAP_OPT_X_SASL_SSF);
#endif
#ifdef LDAP_OPT_X_SASL_SSF_EXTERNAL
  rb_ldap_define_opt (LDAP_OPT_X_SASL_SSF_EXTERNAL);
#endif
#ifdef LDAP_OPT_X_SASL_SECPROPS
  rb_ldap_define_opt (LDAP_OPT_X_SASL_SECPROPS);
#endif
#ifdef LDAP_OPT_X_SASL_SSF_MIN
  rb_ldap_define_opt (LDAP_OPT_X_SASL_SSF_MIN);
#endif
#ifdef LDAP_OPT_X_SASL_SSF_MAX
  rb_ldap_define_opt (LDAP_OPT_X_SASL_SSF_MAX);
#endif
#ifdef LDAP_OPT_X_SASL_MAXBUFSIZE
  rb_ldap_define_opt (LDAP_OPT_X_SASL_MAXBUFSIZE);
#endif
#endif /* USE_OPENLDAP2 */


#undef rb_ldap_define_opt

  /* these constants indicate search scopes */
#define rb_ldap_define_scope(scope) rb_define_const(rb_mLDAP,#scope,INT2NUM(scope))
  rb_ldap_define_scope (LDAP_SCOPE_BASE);
  rb_ldap_define_scope (LDAP_SCOPE_SUBTREE);
  rb_ldap_define_scope (LDAP_SCOPE_ONELEVEL);
#undef rb_ldap_define_scope

#define rb_ldap_define_deref(x) rb_define_const(rb_mLDAP,#x,INT2NUM(x))
#ifdef LDAP_DEREF_NEVER
  rb_ldap_define_deref (LDAP_DEREF_NEVER);
#endif
#ifdef LDAP_DEREF_SEARCHING
  rb_ldap_define_deref (LDAP_DEREF_SEARCHING);
#endif
#ifdef LDAP_DEREF_FINDING
  rb_ldap_define_deref (LDAP_DEREF_FINDING);
#endif
#ifdef LDAP_DEREF_ALWAYS
  rb_ldap_define_deref (LDAP_DEREF_ALWAYS);
#endif
#undef rb_ldap_define_deref

#define rb_ldap_define_sasl_mech(c) \
        (c ? rb_define_const(rb_mLDAP,#c,rb_str_new2(c)) : rb_define_const(rb_mLDAP,#c,Qnil))
#ifdef LDAP_SASL_SIMPLE
  rb_ldap_define_sasl_mech (LDAP_SASL_SIMPLE);
#endif
#undef rb_ldap_define_sasl_mech

#define rb_ldap_define_auth_method(c) rb_define_const(rb_mLDAP,#c,INT2NUM(c))
  rb_ldap_define_auth_method (LDAP_AUTH_NONE);
  rb_ldap_define_auth_method (LDAP_AUTH_SIMPLE);
#ifdef LDAP_AUTH_KRBV41
  rb_ldap_define_auth_method (LDAP_AUTH_KRBV41);
#endif
#ifdef LDAP_AUTH_KRBV42
  rb_ldap_define_auth_method (LDAP_AUTH_KRBV42);
#endif
#ifdef LDAP_AUTH_SASL
  rb_ldap_define_auth_method (LDAP_AUTH_SASL);
#endif
#ifdef LDAP_KRBV4
  rb_ldap_define_auth_method (LDAP_KRBV4);
#endif
  /* wldap32.h */
#ifdef LDAP_AUTH_OTHERKIND
  rb_ldap_define_auth_method (LDAP_AUTH_OTHERKIND);
#endif
#ifdef LDAP_AUTH_DPA
  rb_ldap_define_auth_method (LDAP_AUTH_DPA);
#endif
#ifdef LDAP_AUTH_MSN
  rb_ldap_define_auth_method (LDAP_AUTH_MSN);
#endif
#ifdef LDAP_AUTH_NEGOTIATE
  rb_ldap_define_auth_method (LDAP_AUTH_NEGOTIATE);
#endif
#ifdef LDAP_AUTH_NTLM
  rb_ldap_define_auth_method (LDAP_AUTH_NTLM);
#endif
#ifdef LDAP_AUTH_SICILY
  rb_ldap_define_auth_method (LDAP_AUTH_SICILY);
#endif
#ifdef LDAP_AUTH_SSPI
  rb_ldap_define_auth_method (LDAP_AUTH_SSPI);
#endif
#undef rb_ldap_define_auth_method

#ifdef LDAP_CONTROL_PAGEDRESULTS
  rb_define_const (rb_mLDAP, "LDAP_CONTROL_PAGEDRESULTS",
		   rb_str_new2 (LDAP_CONTROL_PAGEDRESULTS));
#endif

#define rb_ldap_define_const(c) rb_define_const(rb_mLDAP,#c,INT2NUM(c))
  rb_ldap_define_const (LDAP_MOD_ADD);
  rb_ldap_define_const (LDAP_MOD_DELETE);
  rb_ldap_define_const (LDAP_MOD_REPLACE);
  rb_ldap_define_const (LDAP_MOD_BVALUES);
#ifdef LDAP_MOD_INCREMENT
  /*
   * See http://www.ietf.org/internet-drafts/draft-zeilenga-ldap-incr-00.txt
   */
  rb_ldap_define_const (LDAP_MOD_INCREMENT);
#endif
#ifdef LDAP_MOD_OP
  rb_ldap_define_const (LDAP_MOD_OP);
#endif
#undef rb_ldap_define_const

  Init_ldap_conn ();
  Init_ldap_sslconn ();
  Init_ldap_saslconn ();
#ifdef USE_SSL_CLIENTAUTH
  Init_ldap_clientauth();
#endif
  Init_ldap_entry ();
  Init_ldap_mod ();
  Init_ldap_misc ();
}
示例#29
0
static void
callback_invoke(ffi_cif* cif, void* retval, void** parameters, void* user_data)
{
    Closure* closure = (Closure *) user_data;
    Function* fn = (Function *) closure->info;
    FunctionType *cbInfo = fn->info;
    VALUE* rbParams;
    VALUE rbReturnValue;
    int i;

    rbParams = ALLOCA_N(VALUE, cbInfo->parameterCount);
    for (i = 0; i < cbInfo->parameterCount; ++i) {
        VALUE param;
        switch (cbInfo->parameterTypes[i]->nativeType) {
            case NATIVE_INT8:
                param = INT2NUM(*(int8_t *) parameters[i]);
                break;
            case NATIVE_UINT8:
                param = UINT2NUM(*(uint8_t *) parameters[i]);
                break;
            case NATIVE_INT16:
                param = INT2NUM(*(int16_t *) parameters[i]);
                break;
            case NATIVE_UINT16:
                param = UINT2NUM(*(uint16_t *) parameters[i]);
                break;
            case NATIVE_INT32:
                param = INT2NUM(*(int32_t *) parameters[i]);
                break;
            case NATIVE_UINT32:
                param = UINT2NUM(*(uint32_t *) parameters[i]);
                break;
            case NATIVE_INT64:
                param = LL2NUM(*(int64_t *) parameters[i]);
                break;
            case NATIVE_UINT64:
                param = ULL2NUM(*(uint64_t *) parameters[i]);
                break;
            case NATIVE_LONG:
                param = LONG2NUM(*(long *) parameters[i]);
                break;
            case NATIVE_ULONG:
                param = ULONG2NUM(*(unsigned long *) parameters[i]);
                break;
            case NATIVE_FLOAT32:
                param = rb_float_new(*(float *) parameters[i]);
                break;
            case NATIVE_FLOAT64:
                param = rb_float_new(*(double *) parameters[i]);
                break;
            case NATIVE_STRING:
                param = (*(void **) parameters[i] != NULL) ? rb_tainted_str_new2(*(char **) parameters[i]) : Qnil;
                break;
            case NATIVE_POINTER:
                param = rbffi_Pointer_NewInstance(*(void **) parameters[i]);
                break;
            case NATIVE_BOOL:
                param = (*(uint8_t *) parameters[i]) ? Qtrue : Qfalse;
                break;

            case NATIVE_FUNCTION:
            case NATIVE_CALLBACK:
                param = rbffi_NativeValue_ToRuby(cbInfo->parameterTypes[i],
                     rb_ary_entry(cbInfo->rbParameterTypes, i), parameters[i], Qnil);
                break;
            default:
                param = Qnil;
                break;
        }
        rbParams[i] = param;
    }
    rbReturnValue = rb_funcall2(fn->rbProc, id_call, cbInfo->parameterCount, rbParams);
    if (rbReturnValue == Qnil || TYPE(rbReturnValue) == T_NIL) {
        memset(retval, 0, cbInfo->ffiReturnType->size);
    } else switch (cbInfo->returnType->nativeType) {
        case NATIVE_INT8:
        case NATIVE_INT16:
        case NATIVE_INT32:
            *((ffi_sarg *) retval) = NUM2INT(rbReturnValue);
            break;
        case NATIVE_UINT8:
        case NATIVE_UINT16:
        case NATIVE_UINT32:
            *((ffi_arg *) retval) = NUM2UINT(rbReturnValue);
            break;
        case NATIVE_INT64:
            *((int64_t *) retval) = NUM2LL(rbReturnValue);
            break;
        case NATIVE_UINT64:
            *((uint64_t *) retval) = NUM2ULL(rbReturnValue);
            break;
        case NATIVE_LONG:
            *((ffi_sarg *) retval) = NUM2LONG(rbReturnValue);
            break;
        case NATIVE_ULONG:
            *((ffi_arg *) retval) = NUM2ULONG(rbReturnValue);
            break;
        case NATIVE_FLOAT32:
            *((float *) retval) = (float) NUM2DBL(rbReturnValue);
            break;
        case NATIVE_FLOAT64:
            *((double *) retval) = NUM2DBL(rbReturnValue);
            break;
        case NATIVE_POINTER:
            if (TYPE(rbReturnValue) == T_DATA && rb_obj_is_kind_of(rbReturnValue, rbffi_PointerClass)) {
                *((void **) retval) = ((AbstractMemory *) DATA_PTR(rbReturnValue))->address;
            } else {
                // Default to returning NULL if not a value pointer object.  handles nil case as well
                *((void **) retval) = NULL;
            }
            break;

        case NATIVE_BOOL:
            *((ffi_arg *) retval) = rbReturnValue == Qtrue;
            break;

        case NATIVE_FUNCTION:
        case NATIVE_CALLBACK:
            if (TYPE(rbReturnValue) == T_DATA && rb_obj_is_kind_of(rbReturnValue, rbffi_PointerClass)) {

                *((void **) retval) = ((AbstractMemory *) DATA_PTR(rbReturnValue))->address;

            } else if (rb_obj_is_kind_of(rbReturnValue, rb_cProc) || rb_respond_to(rbReturnValue, id_call)) {
                VALUE function;

                function = rbffi_Function_ForProc(cbInfo->rbReturnType, rbReturnValue);

                *((void **) retval) = ((AbstractMemory *) DATA_PTR(function))->address;
            } else {
                *((void **) retval) = NULL;
            }
            break;

        default:
            *((ffi_arg *) retval) = 0;
            break;
    }
}
示例#30
0
文件: log.c 项目: mallowlabs/ruby-bdb
static VALUE
bdb_env_log_stat(int argc, VALUE *argv, VALUE obj)
{
    DB_LOG_STAT *bdb_stat;
    bdb_ENV *envst;
    VALUE res, b;
    int flags;

    GetEnvDB(obj, envst);
#if HAVE_ST_DB_ENV_LG_INFO
    if (!envst->envp->lg_info) {
	rb_raise(bdb_eFatal, "log region not open");
    }
    if (argc != 0) {
	rb_raise(rb_eArgError, "invalid number of arguments (%d for 0)", argc);
    }
    bdb_test_error(log_stat(envst->envp->lg_info, &bdb_stat, 0));
#elif HAVE_ST_DB_ENV_LOG_STAT
    flags = 0;
    if (rb_scan_args(argc, argv, "01", &b) == 1) {
	flags = NUM2INT(b);
    }
    bdb_test_error(envst->envp->log_stat(envst->envp, &bdb_stat, flags));
#else
    if (argc != 0) {
	rb_raise(rb_eArgError, "invalid number of arguments (%d for 0)", argc);
    }
#if HAVE_DB_LOG_STAT_3
    bdb_test_error(log_stat(envst->envp, &bdb_stat, 0));
#else
    bdb_test_error(log_stat(envst->envp, &bdb_stat));
#endif
#endif
    res = rb_hash_new();
    rb_hash_aset(res, rb_tainted_str_new2("st_magic"), INT2NUM(bdb_stat->st_magic));
    rb_hash_aset(res, rb_tainted_str_new2("st_version"), INT2NUM(bdb_stat->st_version));
    rb_hash_aset(res, rb_tainted_str_new2("st_regsize"), INT2NUM(bdb_stat->st_regsize));
    rb_hash_aset(res, rb_tainted_str_new2("st_mode"), INT2NUM(bdb_stat->st_mode));
#if HAVE_ST_DB_LOG_STAT_ST_REFCNT
    rb_hash_aset(res, rb_tainted_str_new2("st_refcnt"), INT2NUM(bdb_stat->st_refcnt));
#endif
#if HAVE_ST_DB_LOG_STAT_ST_LG_BSIZE
    rb_hash_aset(res, rb_tainted_str_new2("st_lg_bsize"), INT2NUM(bdb_stat->st_lg_bsize));
#endif
#if HAVE_ST_DB_LOG_STAT_ST_LG_SIZE
    rb_hash_aset(res, rb_tainted_str_new2("st_lg_size"), INT2NUM(bdb_stat->st_lg_size));
    rb_hash_aset(res, rb_tainted_str_new2("st_lg_max"), INT2NUM(bdb_stat->st_lg_size));
#else
    rb_hash_aset(res, rb_tainted_str_new2("st_lg_max"), INT2NUM(bdb_stat->st_lg_max));
#endif
    rb_hash_aset(res, rb_tainted_str_new2("st_w_mbytes"), INT2NUM(bdb_stat->st_w_mbytes));
    rb_hash_aset(res, rb_tainted_str_new2("st_w_bytes"), INT2NUM(bdb_stat->st_w_bytes));
    rb_hash_aset(res, rb_tainted_str_new2("st_wc_mbytes"), INT2NUM(bdb_stat->st_wc_mbytes));
    rb_hash_aset(res, rb_tainted_str_new2("st_wc_bytes"), INT2NUM(bdb_stat->st_wc_bytes));
    rb_hash_aset(res, rb_tainted_str_new2("st_wcount"), INT2NUM(bdb_stat->st_wcount));
#if HAVE_ST_DB_LOG_STAT_ST_WCOUNT_FILL
    rb_hash_aset(res, rb_tainted_str_new2("st_wcount_fill"), INT2NUM(bdb_stat->st_wcount_fill));
#endif
    rb_hash_aset(res, rb_tainted_str_new2("st_scount"), INT2NUM(bdb_stat->st_scount));
    rb_hash_aset(res, rb_tainted_str_new2("st_cur_file"), INT2NUM(bdb_stat->st_cur_file));
    rb_hash_aset(res, rb_tainted_str_new2("st_cur_offset"), INT2NUM(bdb_stat->st_cur_offset));
    rb_hash_aset(res, rb_tainted_str_new2("st_region_wait"), INT2NUM(bdb_stat->st_region_wait));
    rb_hash_aset(res, rb_tainted_str_new2("st_region_nowait"), INT2NUM(bdb_stat->st_region_nowait));
#if HAVE_ST_DB_LOG_STAT_ST_DISK_FILE
    rb_hash_aset(res, rb_tainted_str_new2("st_disk_file"), INT2NUM(bdb_stat->st_disk_file));
#endif
#if HAVE_ST_DB_LOG_STAT_ST_DISK_OFFSET
    rb_hash_aset(res, rb_tainted_str_new2("st_disk_offset"), INT2NUM(bdb_stat->st_disk_offset));
#if HAVE_ST_DB_LOG_STAT_ST_FLUSHCOMMIT
    rb_hash_aset(res, rb_tainted_str_new2("st_flushcommit"), INT2NUM(bdb_stat->st_flushcommit));
#endif
#if HAVE_ST_DB_LOG_STAT_ST_MAXCOMMITPERFLUSH
    rb_hash_aset(res, rb_tainted_str_new2("st_maxcommitperflush"), INT2NUM(bdb_stat->st_maxcommitperflush));
#endif
#if HAVE_ST_DB_LOG_STAT_ST_MINCOMMITPERFLUSH
    rb_hash_aset(res, rb_tainted_str_new2("st_mincommitperflush"), INT2NUM(bdb_stat->st_mincommitperflush));
#endif
#endif
    free(bdb_stat);
    return res;
}