/* * call-seq: * marshal_load(string) -> self * * loads a string into an UDim. */ VALUE _marshal_load(VALUE self,VALUE load) { VALUE result = rb_funcall(load,rb_intern("unpack"),1,rb_str_new2("dd")); _set_d_offset(self,rb_ary_pop(result)); _set_d_scale(self,rb_ary_pop(result)); return self; }
void end_object(CTX *ctx) { VALUE key_stack = rb_ivar_get(ctx->self, rb_intern("key_stack")); VALUE stack = rb_ivar_get(ctx->self, rb_intern("stack")); rb_ivar_set(ctx->self, rb_intern("key"), rb_ary_pop(key_stack)); if ( RARRAY_LEN(stack) > 1 ) { set_value(ctx, rb_ary_pop(stack)); } }
/* * call-seq: * marshal_load(string) -> self * * loads a string into an Size. */ VALUE _marshal_load(VALUE self,VALUE load) { VALUE result = rb_funcall(load,rb_intern("unpack"),1,rb_str_new2("d*")); _self->d_height.d_offset = NUM2DBL(rb_ary_pop(result)); _self->d_height.d_scale = NUM2DBL(rb_ary_pop(result)); _self->d_width.d_offset = NUM2DBL(rb_ary_pop(result)); _self->d_width.d_scale = NUM2DBL(rb_ary_pop(result)); return self; }
void test_array(){ VALUE ary = rb_ary_new(); rb_ary_push( ary, INT2NUM(1) ); rb_ary_push( ary, INT2NUM(2) ); rb_ary_push( ary, INT2NUM(3) ); //=> test_array 3 2 1 printf("test_array %d %d %d\n", NUM2INT(rb_ary_pop(ary)), NUM2INT(rb_ary_pop(ary)), NUM2INT(rb_ary_pop(ary)) ); }
VALUE kernel_spec_call_proc_with_raised_exc(VALUE arg_array, VALUE raised_exc) { VALUE argv[2]; int argc; VALUE arg = rb_ary_pop(arg_array); VALUE proc = rb_ary_pop(arg_array); argv[0] = arg; argv[1] = raised_exc; argc = 2; return rb_funcall2(proc, rb_intern("call"), argc, argv); }
/** * Convert a Ruby Hash to a maps * * @param t the Ruby Hash to convert * @return a new maps */ maps* mapsFromRubyHash(VALUE t){ maps* res=NULL; maps* cursor=res; VALUE list; list = rb_ary_new(); typedef int (*HOOK)(...); rb_hash_foreach(t, reinterpret_cast<HOOK>(keys_i), list); int nb=rb_hash_size(t); int i; for(i=0;i<FIX2INT(nb);i++){ VALUE key=rb_ary_pop(list); VALUE value=rb_hash_aref(t,key); #ifdef DEBUG fprintf(stderr,">> DEBUG VALUES : %s => %s\n", StringValueCStr(key),StringValueCStr(value)); #endif cursor=createMaps(StringValueCStr(key)); cursor->content=mapFromRubyHash(value); if(res==NULL) res=dupMaps(&cursor); else addMapsToMaps(&res,cursor); freeMaps(&cursor); free(cursor); } return res; }
/** * This method provides the close method for the Connection class. * * @param self A reference to the object that the call is being made on. * * @return A reference to the closed Connection on success, nil otherwise or * if the method is called on a closed Connection. * */ static VALUE closeConnection(VALUE self) { VALUE result = Qnil; ConnectionHandle *connection = NULL; Data_Get_Struct(self, ConnectionHandle, connection); if(connection->handle != 0) { VALUE transactions = rb_iv_get(self, "@transactions"), transaction = Qnil; ISC_STATUS status[ISC_STATUS_LENGTH]; /* Roll back an outstanding transactions. */ while((transaction = rb_ary_pop(transactions)) != Qnil) { VALUE active = rb_funcall(transaction, rb_intern("active?"), 0); if(active == Qtrue) { rb_funcall(transaction, rb_intern("rollback"), 0); } } /* Detach from the database. */ if(isc_detach_database(status, &connection->handle) == 0) { connection->handle = 0; result = self; } else { /* Generate an error. */ rb_fireruby_raise(status, "Error closing connection."); } } return(result); }
static void cut_stack_tail(VALUE stack, long len) { while (len > 0) { rb_ary_pop(stack); len--; } }
static int yajl_found_end_array(void * ctx) { yajl_parser_wrapper * wrapper; GetParser((VALUE)ctx, wrapper); wrapper->nestedArrayLevel--; if (RARRAY_LEN(wrapper->builderStack) > 1) { rb_ary_pop(wrapper->builderStack); } yajl_check_and_fire_callback(ctx); return 1; }
/* * Document-method: parse * * call-seq: * parse(input, buffer_size=8092) * parse(input, buffer_size=8092) { |obj| ... } * * +input+ can either be a string or an IO to parse JSON from * * +buffer_size+ is the size of chunk that will be parsed off the input (if it's an IO) for each loop of the parsing process. * 8092 is a good balance between the different types of streams (off disk, off a socket, etc...), but this option * is here so the caller can better tune their parsing depending on the type of stream being passed. * A larger read buffer will perform better for files off disk, where as a smaller size may be more efficient for * reading off of a socket directly. * * If a block was passed, it's called when an object has been parsed off the stream. This is especially * usefull when parsing a stream of multiple JSON objects. * * NOTE: you can optionally assign the +on_parse_complete+ callback, and it will be called the same way the optional * block is for this method. */ static VALUE rb_yajl_parser_parse(int argc, VALUE * argv, VALUE self) { yajl_status stat; yajl_parser_wrapper * wrapper; VALUE rbufsize, input, blk; unsigned int len; const char * cptr; GetParser(self, wrapper); /* setup our parameters */ rb_scan_args(argc, argv, "11&", &input, &rbufsize, &blk); if (NIL_P(rbufsize)) { rbufsize = INT2FIX(READ_BUFSIZE); } else { Check_Type(rbufsize, T_FIXNUM); } if (!NIL_P(blk)) { rb_yajl_parser_set_complete_cb(self, blk); } if (TYPE(input) == T_STRING) { cptr = RSTRING_PTR(input); len = RSTRING_LEN(input); yajl_parse_chunk((const unsigned char*)cptr, len, wrapper->parser); } else if (rb_respond_to(input, intern_io_read)) { VALUE parsed = rb_str_new(0, FIX2LONG(rbufsize)); while (rb_funcall(input, intern_io_read, 2, rbufsize, parsed) != Qnil) { cptr = RSTRING_PTR(parsed); len = RSTRING_LEN(parsed); yajl_parse_chunk((const unsigned char*)cptr, len, wrapper->parser); } } else { rb_raise(cParseError, "input must be a string or IO"); } /* parse any remaining buffered data */ stat = yajl_parse_complete(wrapper->parser); if (wrapper->parse_complete_callback != Qnil) { yajl_check_and_fire_callback((void *)self); return Qnil; } // Because we've overridden the yajl_parse_complete state of the parser to allow // us to parse multiple objects out of one stream; we can't use it to determine // whether we've parsed a single object. Instead we'll check whether we've successfully // parsed a single token. if (!RARRAY_LEN(wrapper->builderStack) || wrapper->nestedHashLevel || wrapper->nestedArrayLevel) { rb_raise(cParseError, "unexpected end of JSON string"); } return rb_ary_pop(wrapper->builderStack); }
static void set_search_scope(MDQueryRef query, VALUE scopeDirectories) { int i; int len = RARRAY_LEN(scopeDirectories); CFMutableArrayRef scopesRef = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); for (i=0; i<len; i++) { CFArrayAppendValue(scopesRef, rbstr2cfstring(rb_ary_pop(scopeDirectories))); } MDQuerySetSearchScope(query, scopesRef, 0); RELEASE_IF_NOT_NULL(scopesRef); }
static void ruby_vm_run_at_exit_hooks(rb_vm_t *vm) { VALUE hook = (VALUE)&vm->at_exit; while (RARRAY_LEN(hook) > 0) { typedef void rb_vm_at_exit_func(rb_vm_t*); rb_vm_at_exit_func *func = (rb_vm_at_exit_func*)rb_ary_pop(hook); (*func)(vm); } rb_ary_free(hook); }
static VALUE mParser_do_yajl_parse(VALUE self, VALUE str, VALUE yajl_opts) { yajl_handle hand; yajl_status stat; unsigned char *err; volatile CTX ctx; rb_ivar_set(self, rb_intern("key"), Qnil); rb_ivar_set(self, rb_intern("stack"), rb_ary_new()); rb_ivar_set(self, rb_intern("key_stack"), rb_ary_new()); ctx.self = self; ctx.symbolizeKeys = get_opts_key(self, "symbolize_keys"); ctx.uniqueKeyChecking = get_opts_key(self, "unique_key_checking"); hand = yajl_alloc(&callbacks, NULL, (void *)&ctx); if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_comments"))) == Qtrue) { yajl_config(hand, yajl_allow_comments, 1); } if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_dont_validate_strings"))) == Qtrue) { yajl_config(hand, yajl_dont_validate_strings, 1); } if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_trailing_garbage"))) == Qtrue) { yajl_config(hand, yajl_allow_trailing_garbage, 1); } if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_multiple_values"))) == Qtrue) { yajl_config(hand, yajl_allow_multiple_values, 1); } if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_partial_values"))) == Qtrue) { yajl_config(hand, yajl_allow_partial_values, 1); } if ((stat = yajl_parse(hand, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str))) != yajl_status_ok) { err = yajl_get_error(hand, 1, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str)); goto raise; } if ((stat = yajl_complete_parse(hand)) != yajl_status_ok) { err = yajl_get_error(hand, 1, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str)); goto raise; } yajl_free(hand); return rb_ary_pop(rb_ivar_get(self, rb_intern("stack"))); raise: if (hand) { yajl_free(hand); } rb_raise(cParseError, "%s", err); }
static int yajl_found_end_array(void * ctx) { yajl_parser_wrapper * wrapper; GetParser((VALUE)ctx, wrapper); wrapper->nestedArrayLevel--; if (RARRAY_LEN(wrapper->builderStack) > 1) { VALUE popped = rb_ary_pop(wrapper->builderStack); if (wrapper->processNestedCallback && (wrapper->nestedArrayLevel + wrapper->nestedHashLevel <= wrapper->nestedCallbackDepth || wrapper->nestedCallbackDepth == 0)) { if ( wrapper->parse_nested_callback != Qnil) { rb_funcall(wrapper->parse_nested_callback, intern_call, 2, popped, INT2NUM(wrapper->nestedArrayLevel + wrapper->nestedHashLevel)); } } } yajl_check_and_fire_callback(ctx); return 1; }
static DWORD extract_flags_from_rb_array(VALUE flags_array) { VALUE flag_symbol; DWORD flags; flags = 0; while ( RARRAY_LEN(flags_array) != 0 ) { flag_symbol = rb_ary_pop(flags_array); Check_Type(flag_symbol, T_SYMBOL); flags |= id_to_flag( SYM2ID(flag_symbol) ); } return flags; }
/* * Document-method: []= * * call-seq: * [index]= value * [range]= value-array * * Element Assignment - Sets the element at _index_ or replaces the subset * specified by _range_. Values can be only be positive numerics. Even * values will be set to 0 (n % 2 = 0) and uneven values will be set to 1 * (n % 2 = 1). */ static VALUE bf_bit_set(VALUE self, VALUE position, VALUE value) { BitField *ptr; Data_Get_Struct(self, BitField, ptr); if(rb_obj_is_kind_of(position, rb_cRange) && rb_obj_is_kind_of(value, rb_cArray)) { long beg, len; VALUE tmp; switch(rb_range_beg_len(position, &beg, &len, (long int)ptr->data.size()-1, 2)) { case Qfalse: case Qnil: return Qfalse; default: for(long i = beg+len-1; i >= beg; i--) { tmp = rb_ary_pop(value); if(tmp != Qnil) { /* Is the array value a number? */ Check_Type(tmp, T_FIXNUM); ptr->data[i] = FIX2INT(tmp) % 2; } else { rb_raise(rb_eRangeError, "Array is smaller than given range."); } } } } else { /* Sanity checks for position and value */ Check_Type(position, T_FIXNUM); Check_Type(value, T_FIXNUM); int pos = FIX2INT(position); if(pos < 0 || pos >= ptr->data.size()) { rb_raise(rb_eRangeError, "BitField out of range with value %d.", pos); } ptr->data[pos] = FIX2INT(value) % 2; } return Qnil; }
/* * Document-method: parse * * call-seq: * parse(input, buffer_size=8092) * parse(input, buffer_size=8092) { |obj| ... } * * +input+ can either be a string or an IO to parse JSON from * * +buffer_size+ is the size of chunk that will be parsed off the input (if it's an IO) for each loop of the parsing process. * 8092 is a good balance between the different types of streams (off disk, off a socket, etc...), but this option * is here so the caller can better tune their parsing depending on the type of stream being passed. * A larger read buffer will perform better for files off disk, where as a smaller size may be more efficient for * reading off of a socket directly. * * If a block was passed, it's called when an object has been parsed off the stream. This is especially * usefull when parsing a stream of multiple JSON objects. * * NOTE: you can optionally assign the +on_parse_complete+ callback, and it will be called the same way the optional * block is for this method. */ static VALUE rb_yajl_parser_parse(int argc, VALUE * argv, VALUE self) { yajl_status stat; yajl_parser_wrapper * wrapper; VALUE rbufsize, input, blk; unsigned int len; const char * cptr; GetParser(self, wrapper); /* setup our parameters */ rb_scan_args(argc, argv, "11&", &input, &rbufsize, &blk); if (NIL_P(rbufsize)) { rbufsize = INT2FIX(READ_BUFSIZE); } else { Check_Type(rbufsize, T_FIXNUM); } if (!NIL_P(blk)) { rb_yajl_parser_set_complete_cb(self, blk); } if (TYPE(input) == T_STRING) { cptr = RSTRING_PTR(input); len = RSTRING_LEN(input); yajl_parse_chunk((const unsigned char*)cptr, len, wrapper->parser); } else if (rb_respond_to(input, intern_io_read)) { VALUE parsed = rb_str_new(0, FIX2LONG(rbufsize)); while (rb_funcall(input, intern_io_read, 2, rbufsize, parsed) != Qnil) { cptr = RSTRING_PTR(parsed); len = RSTRING_LEN(parsed); yajl_parse_chunk((const unsigned char*)cptr, len, wrapper->parser); } } else { rb_raise(cParseError, "input must be a string or IO"); } /* parse any remaining buffered data */ stat = yajl_parse_complete(wrapper->parser); if (wrapper->parse_complete_callback != Qnil) { yajl_check_and_fire_callback((void *)self); return Qnil; } return rb_ary_pop(wrapper->builderStack); }
/** * Convert a Ruby Hash to a map * * @param t the Ruby Hash to convert * @return a new map */ map* mapFromRubyHash(VALUE t){ map* res=NULL; VALUE list; list = rb_ary_new(); typedef int (*HOOK)(...); rb_hash_foreach(t,reinterpret_cast<HOOK>(keys_i), list); int nb=RHASH_TBL(t)->num_entries; int i; for(i=0;i<nb;i++){ VALUE key=rb_ary_pop(list); VALUE value=rb_hash_aref(t,key); #ifdef DEBUG fprintf(stderr,">> DEBUG VALUES : %s => %s\n", StringValueCStr(key),StringValueCStr(value)); #endif if(strcmp(StringValueCStr(key),"value")==0){ char *buffer=NULL; int size=RSTRING_LEN(value); buffer=StringValueCStr(value); if(res!=NULL){ addToMap(res,StringValueCStr(key),""); }else{ res=createMap(StringValueCStr(key),""); } map* tmpR=getMap(res,"value"); free(tmpR->value); tmpR->value=(char*)malloc((size+1)*sizeof(char)); memmove(tmpR->value,buffer,size*sizeof(char)); tmpR->value[size]=0; char sin[1024]; sprintf(sin,"%d",size); addToMap(res,"size",sin); }else{ if(res!=NULL){ addToMap(res,StringValueCStr(key),StringValueCStr(value)); } else{ res= createMap(StringValueCStr(key),StringValueCStr(value)); } } } return res; }
static VALUE args_pop_keyword_hash(struct args_info *args, VALUE *kw_hash_ptr, rb_thread_t *th) { VALUE rest_hash; if (args->rest == Qfalse) { from_argv: VM_ASSERT(args->argc > 0); *kw_hash_ptr = args->argv[args->argc-1]; if (keyword_hash_p(kw_hash_ptr, &rest_hash, th)) { if (rest_hash) { args->argv[args->argc-1] = rest_hash; } else { args->argc--; return TRUE; } } } else { long len = RARRAY_LEN(args->rest); if (len > 0) { *kw_hash_ptr = RARRAY_AREF(args->rest, len - 1); if (keyword_hash_p(kw_hash_ptr, &rest_hash, th)) { if (rest_hash) { RARRAY_ASET(args->rest, len - 1, rest_hash); } else { args->rest = rb_ary_dup(args->rest); rb_ary_pop(args->rest); return TRUE; } } } else { goto from_argv; } } return FALSE; }
VALUE _marshal_load(VALUE self,VALUE load) { VALUE result = rb_funcall(load,rb_intern("unpack"),1,rb_str_new2("d*")); Ogre::Real x,y,z; z=NUM2DBL(rb_ary_pop(result)); y=NUM2DBL(rb_ary_pop(result)); x=NUM2DBL(rb_ary_pop(result)); _self->setMinimum(x,y,z); z=NUM2DBL(rb_ary_pop(result)); y=NUM2DBL(rb_ary_pop(result)); x=NUM2DBL(rb_ary_pop(result)); _self->setMaximum(x,y,z); return self; }
/* Helpers for building objects */ inline void yajl_check_and_fire_callback(void * ctx) { yajl_parser_wrapper * wrapper; GetParser((VALUE)ctx, wrapper); /* No need to do any of this if the callback isn't even setup */ if (wrapper->parse_complete_callback != Qnil) { int len = RARRAY_LEN(wrapper->builderStack); if (len == 1 && wrapper->nestedArrayLevel == 0 && wrapper->nestedHashLevel == 0) { rb_funcall(wrapper->parse_complete_callback, intern_call, 1, rb_ary_pop(wrapper->builderStack)); } } else { int len = RARRAY_LEN(wrapper->builderStack); if (len == 1 && wrapper->nestedArrayLevel == 0 && wrapper->nestedHashLevel == 0) { wrapper->objectsFound++; if (wrapper->objectsFound > 1) { rb_raise(cParseError, "%s", "Found multiple JSON objects in the stream but no block or the on_parse_complete callback was assigned to handle them."); } } } }
inline void yajl_set_static_value(void * ctx, VALUE val) { yajl_parser_wrapper * wrapper; VALUE lastEntry, hash; int len; GetParser((VALUE)ctx, wrapper); len = RARRAY_LEN(wrapper->builderStack); if (len > 0) { lastEntry = rb_ary_entry(wrapper->builderStack, len-1); switch (TYPE(lastEntry)) { case T_ARRAY: rb_ary_push(lastEntry, val); if (TYPE(val) == T_HASH || TYPE(val) == T_ARRAY) { rb_ary_push(wrapper->builderStack, val); } break; case T_HASH: rb_hash_aset(lastEntry, val, Qnil); rb_ary_push(wrapper->builderStack, val); break; case T_STRING: case T_SYMBOL: hash = rb_ary_entry(wrapper->builderStack, len-2); if (TYPE(hash) == T_HASH) { rb_hash_aset(hash, lastEntry, val); rb_ary_pop(wrapper->builderStack); if (TYPE(val) == T_HASH || TYPE(val) == T_ARRAY) { rb_ary_push(wrapper->builderStack, val); } } break; } } else { rb_ary_push(wrapper->builderStack, val); } }
static VALUE call_ruby_from_js_invoke(VALUE args) { VALUE self = rb_ary_pop(args); VALUE id = rb_ary_pop(args); return rb_apply(self, SYM2ID(id), args); }
static void exception_print(FILE *out, int cgi) { VALUE errat; VALUE eclass; VALUE einfo; if (NIL_P(ruby_errinfo)) return; errat = rb_funcall(ruby_errinfo, rb_intern("backtrace"), 0); if (!NIL_P(errat)) { VALUE mesg = RARRAY_PTR(errat)[0]; if (NIL_P(mesg)) { error_pos(out, cgi); } else { if (cgi) write_escaping_html(out, RSTRING_PTR(mesg), RSTRING_LEN(mesg)); else fwrite(RSTRING_PTR(mesg), 1, RSTRING_LEN(mesg), out); } } eclass = CLASS_OF(ruby_errinfo); einfo = rb_obj_as_string(ruby_errinfo); if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) { fprintf(out, ": unhandled exception\n"); } else { VALUE epath; epath = rb_class_path(eclass); if (RSTRING_LEN(einfo) == 0) { fprintf(out, ": "); if (cgi) write_escaping_html(out, RSTRING_PTR(epath), RSTRING_LEN(epath)); else fwrite(RSTRING_PTR(epath), 1, RSTRING_LEN(epath), out); if (cgi) fprintf(out, "<br>\n"); else fprintf(out, "\n"); } else { char *tail = 0; int len = RSTRING_LEN(einfo); if (RSTRING_PTR(epath)[0] == '#') epath = 0; if ((tail = strchr(RSTRING_PTR(einfo), '\n')) != NULL) { len = tail - RSTRING_PTR(einfo); tail++; /* skip newline */ } fprintf(out, ": "); if (cgi) write_escaping_html(out, RSTRING_PTR(einfo), len); else fwrite(RSTRING_PTR(einfo), 1, len, out); if (epath) { fprintf(out, " ("); if (cgi) write_escaping_html(out, RSTRING_PTR(epath), RSTRING_LEN(epath)); else fwrite(RSTRING_PTR(epath), 1, RSTRING_LEN(epath), out); if (cgi) fprintf(out, ")<br>\n"); else fprintf(out, ")\n"); } if (tail) { if (cgi) write_escaping_html(out, tail, RSTRING_LEN(einfo) - len - 1); else fwrite(tail, 1, RSTRING_LEN(einfo) - len - 1, out); if (cgi) fprintf(out, "<br>\n"); else fprintf(out, "\n"); } } } if (!NIL_P(errat)) { int i; struct RArray *ep = RARRAY(errat); #define TRACE_MAX (TRACE_HEAD+TRACE_TAIL+5) #define TRACE_HEAD 8 #define TRACE_TAIL 5 rb_ary_pop(errat); ep = RARRAY(errat); for (i=1; i<RARRAY_LEN(ep); i++) { if (TYPE(RARRAY_PTR(ep)[i]) == T_STRING) { if (cgi) { fprintf(out, "<div class=\"backtrace\">from "); write_escaping_html(out, RSTRING_PTR(RARRAY_PTR(ep)[i]), RSTRING_LEN(RARRAY_PTR(ep)[i])); } else { fprintf(out, " from "); fwrite(RSTRING_PTR(RARRAY_PTR(ep)[i]), 1, RSTRING_LEN(RARRAY_PTR(ep)[i]), out); } if (cgi) fprintf(out, "<br></div>\n"); else fprintf(out, "\n"); } if (i == TRACE_HEAD && RARRAY_LEN(ep) > TRACE_MAX) { char buff[BUFSIZ]; if (cgi) snprintf(buff, BUFSIZ, "<div class=\"backtrace\">... %ld levels...\n", RARRAY_LEN(ep) - TRACE_HEAD - TRACE_TAIL); else snprintf(buff, BUFSIZ, " ... %ld levels...<br></div>\n", RARRAY_LEN(ep) - TRACE_HEAD - TRACE_TAIL); if (cgi) write_escaping_html(out, buff, strlen(buff)); else fputs(buff, out); i = RARRAY_LEN(ep) - TRACE_TAIL; } } } }
VALUE thread_spec_call_proc(VALUE arg_array) { VALUE arg = rb_ary_pop(arg_array); VALUE proc = rb_ary_pop(arg_array); return rb_funcall(proc, rb_intern("call"), 1, arg); }
static VALUE do_params_build(VALUE ptr) { VALUE opts; /* unpack arguments */ struct build_params_st *p = (struct build_params_st *)ptr; struct params_st *params = p->params; int argc = p->argc; VALUE argv = p->argv; /* extract options */ if (argc > 1 && TYPE(RARRAY_PTR(argv)[argc-1]) == T_HASH) { opts = rb_ary_pop(argv); --argc; } else { opts = Qnil; } params->npayload = PACKET_HEADER_SIZE; /* size of packet header */ switch (params->type) { case cmd_touch: params->cmd.touch.quiet = params->bucket->quiet; params->cmd.touch.ttl = params->bucket->default_ttl; cb_params_touch_parse_options(params, opts); cb_params_touch_parse_arguments(params, argc, argv); break; case cmd_remove: params->cmd.remove.quiet = params->bucket->quiet; if (argc == 2) { int type = TYPE(RARRAY_PTR(argv)[1]); if (type == T_FIXNUM || type == T_BIGNUM) { /* allow form delete("foo", 0xdeadbeef) */ --argc; params->cmd.remove.cas = NUM2ULL(rb_ary_pop(argv)); } } cb_params_remove_parse_options(params, opts); cb_params_remove_parse_arguments(params, argc, argv); break; case cmd_store: if (argc == 1 && opts != Qnil) { /* put last hash back because it is the value */ rb_ary_push(argv, opts); opts = Qnil; ++argc; } params->cmd.store.datatype = 0x00; params->cmd.store.ttl = params->bucket->default_ttl; params->cmd.store.flags = flags_set_format(params->bucket->default_flags, params->bucket->default_format); params->cmd.store.observe = Qnil; cb_params_store_parse_options(params, opts); cb_params_store_parse_arguments(params, argc, argv); break; case cmd_get: params->cmd.get.quiet = params->bucket->quiet; cb_params_get_parse_options(params, opts); cb_params_get_parse_arguments(params, argc, argv); break; case cmd_arith: params->cmd.arith.delta = 1; params->cmd.arith.format = params->bucket->default_format; params->cmd.arith.ttl = params->bucket->default_ttl; if (argc == 2 && TYPE(RARRAY_PTR(argv)[1]) == T_FIXNUM) { /* allow form incr("foo", 1) */ --argc; params->cmd.arith.delta = NUM2ULL(rb_ary_pop(argv)) & INT64_MAX; } cb_params_arith_parse_options(params, opts); cb_params_arith_parse_arguments(params, argc, argv); break; case cmd_stats: cb_params_stats_parse_arguments(params, argc, argv); break; case cmd_version: cb_params_version_alloc(params); break; case cmd_observe: cb_params_observe_parse_arguments(params, argc, argv); break; case cmd_unlock: params->cmd.unlock.quiet = params->bucket->quiet; if (argc == 2) { int type = TYPE(RARRAY_PTR(argv)[1]); if (type == T_FIXNUM || type == T_BIGNUM) { /* allow form unlock("foo", 0xdeadbeef) */ --argc; params->cmd.unlock.cas = NUM2ULL(rb_ary_pop(argv)); } } cb_params_unlock_parse_options(params, opts); cb_params_unlock_parse_arguments(params, argc, argv); break; } return Qnil; }
/* * call-seq: * Module.load(String) => Module * * Load a module from a string. */ static VALUE module_load(VALUE klass, VALUE str) { VALUE arr, class_name, metaclass_str, metaclass, superclass_name, included_modules, class_variables_str, class_variables, instance_methods_str, instance_methods, flags, module; if( rb_safe_level() >= 4 || (rb_safe_level() >= 1 && OBJ_TAINTED(str))) { /* no playing with knives in the sandbox */ rb_raise(rb_eSecurityError, "Insecure: can't load module"); } arr = marshal_load(str); class_name = rb_ary_pop(arr); metaclass_str = rb_ary_pop(arr); superclass_name = rb_ary_pop(arr); included_modules = rb_ary_pop(arr); class_variables_str = rb_ary_pop(arr); instance_methods_str = rb_ary_pop(arr); flags = rb_ary_pop(arr); if(RTEST(superclass_name)) { VALUE superclass; rb_check_type(superclass_name, T_STRING); superclass = rb_funcall( lookup_module_proc, rb_intern("call"), 1, superclass_name); #if RUBY_VERSION_CODE >= 180 /* Can't make subclass of Class on 1.8.x */ module = rb_class_boot(superclass); rb_define_alloc_func(module, module_instance_allocate); #else module = rb_class_new(superclass); #endif } else { module = rb_module_new(); } if(!NIL_P(class_name)) { VALUE outer_module = rb_funcall(outer_module_proc, rb_intern("call"), 1, class_name); VALUE module_name = rb_funcall(module_name_proc, rb_intern("call"), 1, class_name); rb_const_set(outer_module, SYM2ID(module_name), module); } RBASIC(module)->flags = NUM2INT(flags); include_modules(module, included_modules); class_variables = marshal_load(class_variables_str); add_class_variables(module, class_variables); instance_methods = marshal_load(instance_methods_str); add_methods(module, instance_methods); metaclass = marshal_load(metaclass_str); if(RTEST(metaclass)) { rb_singleton_class_attached(metaclass, module); RBASIC(module)->klass = metaclass; } return module; }
static VALUE array_spec_rb_ary_pop(VALUE self, VALUE array) { return rb_ary_pop(array); }
/* The main method that handles parsing */ static VALUE rb_rcsv_raw_parse(int argc, VALUE * argv, VALUE self) { struct rcsv_metadata meta; VALUE csvio, options, option; VALUE ensure_container = rb_ary_new(); /* [] */ struct csv_parser cp; unsigned char csv_options = CSV_STRICT_FINI | CSV_APPEND_NULL; /* Setting up some sane defaults */ meta.row_as_hash = false; meta.empty_field_is_nil = false; meta.skip_current_row = false; meta.encoding_index = -1; meta.num_columns = 0; meta.current_col = 0; meta.current_row = 0; meta.offset_rows = 0; meta.num_only_rows = 0; meta.num_except_rows = 0; meta.num_row_defaults = 0; meta.num_row_conversions = 0; meta.only_rows = NULL; meta.except_rows = NULL; meta.row_defaults = NULL; meta.row_conversions = NULL; meta.column_names = NULL; meta.result = (VALUE[]){rb_ary_new()}; /* [] */ /* csvio is required, options is optional (pun intended) */ rb_scan_args(argc, argv, "11", &csvio, &options); /* options ||= {} */ if (NIL_P(options)) { options = rb_hash_new(); } /* First of all, we parse libcsv-related params so that it fails early if something is wrong with them */ /* By default, parsing is strict */ option = rb_hash_aref(options, ID2SYM(rb_intern("nostrict"))); if (!option || (option == Qnil)) { csv_options |= CSV_STRICT; } /* By default, empty strings are treated as Nils and quoted empty strings are treated as empty Ruby strings */ option = rb_hash_aref(options, ID2SYM(rb_intern("parse_empty_fields_as"))); if ((option == Qnil) || (option == ID2SYM(rb_intern("nil_or_string")))) { csv_options |= CSV_EMPTY_IS_NULL; } else if (option == ID2SYM(rb_intern("nil"))) { meta.empty_field_is_nil = true; } else if (option == ID2SYM(rb_intern("string"))) { meta.empty_field_is_nil = false; } else { rb_raise(rcsv_parse_error, "The only valid options for :parse_empty_fields_as are :nil, :string and :nil_or_string, but %s was supplied.", RSTRING_PTR(rb_inspect(option))); } /* rb_ensure() only expects callback functions to accept and return VALUEs */ /* This ugly hack converts C pointers into Ruby Fixnums in order to pass them in Array */ rb_ary_push(ensure_container, options); /* [options] */ rb_ary_push(ensure_container, csvio); /* [options, csvio] */ rb_ary_push(ensure_container, LONG2NUM((long)&meta)); /* [options, csvio, &meta] */ rb_ary_push(ensure_container, LONG2NUM((long)&cp)); /* [options, csvio, &meta, &cp] */ /* Try to initialize libcsv */ if (csv_init(&cp, csv_options) == -1) { rb_raise(rcsv_parse_error, "Couldn't initialize libcsv"); } /* From now on, cp handles allocated data and should be free'd on exit or exception */ rb_ensure(rcsv_raw_parse, ensure_container, rcsv_free_memory, ensure_container); /* Remove the last row if it's empty. That happens if CSV file ends with a newline. */ if (RARRAY_LEN(*(meta.result)) && /* meta.result.size != 0 */ RARRAY_LEN(rb_ary_entry(*(meta.result), -1)) == 0) { rb_ary_pop(*(meta.result)); } if (rb_block_given_p()) { return Qnil; /* STREAMING */ } else { return *(meta.result); /* Return accumulated result */ } }