static int cmp_hash (fz_context *ctx, pdfout_data *x, pdfout_data *y) { data_hash *a = to_hash (ctx, x); data_hash *b = to_hash (ctx, y); if (a->len != b->len) return 1; for (int i = 0; i < a->len; ++i) { if (pdfout_data_cmp (ctx, a->list[i].key, b->list[i].key) || pdfout_data_cmp (ctx, a->list[i].value, b->list[i].value)) return 1; } return 0; }
static mrb_value mrb_hash_replace(mrb_state *mrb, mrb_value hash) { mrb_value hash2, ifnone; khash_t(ht) *h2; khiter_t k; mrb_get_args(mrb, "o", &hash2); hash2 = to_hash(mrb, hash2); if (mrb_obj_equal(mrb, hash, hash2)) return hash; mrb_hash_clear(mrb, hash); h2 = RHASH_TBL(hash2); if (h2) { for (k = kh_begin(h2); k != kh_end(h2); k++) { if (kh_exist(h2, k)) mrb_hash_set(mrb, hash, kh_key(h2, k), kh_value(h2, k)); } } if (MRB_RHASH_PROCDEFAULT_P(hash2)) { RHASH(hash)->flags |= MRB_HASH_PROC_DEFAULT; ifnone = RHASH_PROCDEFAULT(hash2); } else { ifnone = RHASH_IFNONE(hash2); } mrb_iv_set(mrb, hash, mrb_intern_lit(mrb, "ifnone"), ifnone); return hash; }
int pdfout_data_hash_len (fz_context *ctx, pdfout_data *hash) { data_hash *h = to_hash (ctx, hash); return h->len; }
//static std::string KVFlickrRequest::getSignatureForCall(const LLSD& parameters, bool encoded) { std::vector<std::string> keys; for(LLSD::map_const_iterator itr = parameters.beginMap(); itr != parameters.endMap(); ++itr) { keys.push_back(itr->first); } std::sort(keys.begin(), keys.end()); std::string to_hash(KV_FLICKR_API_SECRET); for(std::vector<std::string>::const_iterator itr = keys.begin(); itr != keys.end(); ++itr) { to_hash += *itr; if(encoded) { to_hash += LLURI::escapeQueryValue(parameters[*itr].asString()); } else { to_hash += parameters[*itr].asString(); } } LLMD5 hashed((const unsigned char*)to_hash.c_str()); char hex_hash[MD5HEX_STR_SIZE]; hashed.hex_digest(hex_hash); return std::string(hex_hash); }
pdfout_data * pdfout_data_hash_get_value (fz_context *ctx, pdfout_data *hash, int pos) { data_hash *h = to_hash (ctx, hash); assert (pos < h->len); return h->list[pos].value; }
static VALUE env_update(VALUE env, SEL sel, VALUE hash) { rb_secure(4); if (env == hash) { return env; } hash = to_hash(hash); rb_hash_foreach(hash, env_update_i, 0); return env; }
void pdfout_data_drop (fz_context *ctx, pdfout_data *data) { switch (data->type) { case SCALAR: drop_scalar (ctx, to_scalar (ctx, data)); break; case ARRAY: drop_array (ctx, to_array (ctx, data)); break; case HASH: drop_hash (ctx, to_hash (ctx, data)); break; default: abort (); } }
static VALUE rhash_update(VALUE hash1, SEL sel, VALUE hash2) { rhash_modify(hash1); hash2 = to_hash(hash2); if (rb_block_given_p()) { rb_hash_foreach(hash2, update_block_i, hash1); RETURN_IF_BROKEN(); } else { rb_hash_foreach(hash2, update_i, hash1); } return hash1; }
static VALUE env_replace(VALUE env, SEL sel, VALUE hash) { VALUE keys = env_keys(Qnil, 0); /* rb_secure(4); */ if (env == hash) { return env; } hash = to_hash(hash); rb_hash_foreach(hash, env_replace_i, keys); for (long i = 0, count = RARRAY_LEN(keys); i < count; i++) { env_delete(env, RARRAY_AT(keys, i)); } return env; }
static VALUE rhash_replace(VALUE hash, SEL sel, VALUE hash2) { rhash_modify(hash); hash2 = to_hash(hash2); if (hash == hash2) { return hash; } // Copy RubyHash properties. if (IS_RHASH(hash2)) { if (RHASH(hash2)->tbl->type == &identhash) { RHASH(hash)->tbl->type = &identhash; } GC_WB(&RHASH(hash)->ifnone, RHASH(hash2)->ifnone); RHASH(hash)->has_proc_default = RHASH(hash2)->has_proc_default; } rhash_clear(hash, 0); rb_hash_foreach(hash2, replace_i, hash); return hash; }
void pdfout_data_hash_push (fz_context *ctx, pdfout_data *hash, pdfout_data *key, pdfout_data *value) { data_hash *h = to_hash (ctx, hash); data_scalar *k = to_scalar (ctx, key); /* Is the key already there? */ for (int i = 0; i < h->len; ++i) { data_scalar *k_i = to_scalar (ctx, h->list[i].key); if (k_i->len == k->len && memcmp (k_i->value, k->value, k->len) == 0) pdfout_throw (ctx, "key '%.*s' is already present in hash", k->len, k->value); } if (h->cap == h->len) h->list = pdfout_x2nrealloc (ctx, h->list, &h->cap, struct keyval); h->list[h->len].key = key; h->list[h->len].value = value; ++h->len; }
/* retrieve arguments from mrb_state. mrb_get_args(mrb, format, ...) returns number of arguments parsed. fortmat specifiers: o: Object [mrb_value] S: String [mrb_value] A: Array [mrb_value] H: Hash [mrb_value] s: String [char*,int] z: String [char*] a: Array [mrb_value*,int] f: Float [mrb_float] i: Integer [mrb_int] n: Symbol [mrb_sym] &: Block [mrb_value] *: rest argument [mrb_value*,int] |: optional */ int mrb_get_args(mrb_state *mrb, const char *format, ...) { char c; int i = 0; mrb_value *sp = mrb->stack + 1; va_list ap; int argc = mrb->ci->argc; int opt = 0; va_start(ap, format); if (argc < 0) { struct RArray *a = mrb_ary_ptr(mrb->stack[1]); argc = a->len; sp = a->ptr; } while ((c = *format++)) { switch (c) { case '|': case '*': case '&': break; default: if (argc <= i && !opt) { mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments"); } } switch (c) { case 'o': { mrb_value *p; p = va_arg(ap, mrb_value*); if (i < argc) { *p = *sp++; i++; } } break; case 'S': { mrb_value *p; p = va_arg(ap, mrb_value*); if (i < argc) { *p = to_str(mrb, *sp++); i++; } } break; case 'A': { mrb_value *p; p = va_arg(ap, mrb_value*); if (i < argc) { *p = to_ary(mrb, *sp++); i++; } } break; case 'H': { mrb_value *p; p = va_arg(ap, mrb_value*); if (i < argc) { *p = to_hash(mrb, *sp++); i++; } } break; case 's': { mrb_value ss; struct RString *s; char **ps = 0; int *pl = 0; ps = va_arg(ap, char**); pl = va_arg(ap, int*); if (i < argc) { ss = to_str(mrb, *sp++); s = mrb_str_ptr(ss); *ps = s->ptr; *pl = s->len; i++; } } break; case 'z': { mrb_value ss; struct RString *s; char **ps; ps = va_arg(ap, char**); if (i < argc) { ss = to_str(mrb, *sp++); s = mrb_str_ptr(ss); if (strlen(s->ptr) != s->len) { mrb_raise(mrb, E_ARGUMENT_ERROR, "String contains NUL"); } *ps = s->ptr; i++; } } break; case 'a': { mrb_value aa; struct RArray *a; mrb_value **pb; int *pl; pb = va_arg(ap, mrb_value**); pl = va_arg(ap, int*); if (i < argc) { aa = to_ary(mrb, *sp++); a = mrb_ary_ptr(aa); *pb = a->ptr; *pl = a->len; i++; } } break; case 'f': { mrb_float *p; p = va_arg(ap, mrb_float*); if (i < argc) { switch (mrb_type(*sp)) { case MRB_TT_FLOAT: *p = mrb_float(*sp); break; case MRB_TT_FIXNUM: *p = (mrb_float)mrb_fixnum(*sp); break; case MRB_TT_STRING: mrb_raise(mrb, E_TYPE_ERROR, "String can't be coerced into Float"); break; default: { mrb_value tmp; tmp = mrb_convert_type(mrb, *sp, MRB_TT_FLOAT, "Float", "to_f"); *p = mrb_float(tmp); } break; } sp++; i++; } } break; case 'i': { mrb_int *p; p = va_arg(ap, mrb_int*); if (i < argc) { switch (mrb_type(*sp)) { case MRB_TT_FIXNUM: *p = mrb_fixnum(*sp); break; case MRB_TT_FLOAT: { mrb_float f = mrb_float(*sp); if (!FIXABLE(f)) { mrb_raise(mrb, E_RANGE_ERROR, "float too big for int"); } *p = (mrb_int)f; } break; case MRB_TT_FALSE: *p = 0; break; default: { mrb_value tmp; tmp = mrb_convert_type(mrb, *sp, MRB_TT_FIXNUM, "Integer", "to_int"); *p = mrb_fixnum(tmp); } break; } sp++; i++; } } break; case 'n': { mrb_sym *symp; symp = va_arg(ap, mrb_sym*); if (i < argc) { mrb_value ss; ss = *sp++; if (mrb_type(ss) == MRB_TT_SYMBOL) { *symp = mrb_symbol(ss); } else { *symp = mrb_intern_str(mrb, to_str(mrb, ss)); } i++; } } break; case '&': { mrb_value *p, *bp; p = va_arg(ap, mrb_value*); if (mrb->ci->argc < 0) { bp = mrb->stack + 2; } else { bp = mrb->stack + mrb->ci->argc + 1; } *p = *bp; } break; case '|': opt = 1; break; case '*': { mrb_value **var; int *pl; var = va_arg(ap, mrb_value**); pl = va_arg(ap, int*); if (argc > i) { *pl = argc-i; if (*pl > 0) { *var = sp; i = argc; } i = argc; sp += *pl; } else { *pl = 0; *var = NULL; } } break; default: mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid argument specifier %c", c); break; } } if (!c && argc > i) { mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments"); } va_end(ap); return i; }
/* retrieve arguments from mrb_state. mrb_get_args(mrb, format, ...) returns number of arguments parsed. fortmat specifiers: o: Object [mrb_value] S: String [mrb_value] A: Array [mrb_value] H: Hash [mrb_value] s: String [char*,int] z: String [char*] a: Array [mrb_value*,int] f: Float [mrb_float] i: Integer [mrb_int] &: Block [mrb_value] *: rest argument [mrb_value*,int] |: optional */ int mrb_get_args(mrb_state *mrb, const char *format, ...) { char c; int i = 0; mrb_value *sp = mrb->stack + 1; va_list ap; int argc = mrb->ci->argc; int opt = 0; va_start(ap, format); if (argc < 0) { struct RArray *a = mrb_ary_ptr(mrb->stack[1]); argc = a->len; sp = a->buf; } while ((c = *format++)) { switch (c) { case '|': case '*': case '&': break; default: if (argc <= i) { if (opt) continue; mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments"); } } switch (c) { case 'o': { mrb_value *p; p = va_arg(ap, mrb_value*); *p = *sp; i++; sp++; } break; case 'S': { mrb_value *p; p = va_arg(ap, mrb_value*); *p = to_str(mrb, *sp); i++; sp++; } break; case 'A': { mrb_value *p; p = va_arg(ap, mrb_value*); *p = to_ary(mrb, *sp); i++; sp++; } break; case 'H': { mrb_value *p; p = va_arg(ap, mrb_value*); *p = to_hash(mrb, *sp); i++; sp++; } break; case 's': { mrb_value ss; struct RString *s; char **ps = 0; int *pl = 0; ss = to_str(mrb, *sp); s = mrb_str_ptr(ss); ps = va_arg(ap, char**); *ps = s->buf; pl = va_arg(ap, int*); *pl = s->len; i++; sp++; } break; case 'z': { mrb_value ss; struct RString *s; char **ps; ss = to_str(mrb, *sp); s = mrb_str_ptr(ss); if (strlen(s->buf) != s->len) { mrb_raise(mrb, E_ARGUMENT_ERROR, "String contains NUL"); } ps = va_arg(ap, char**); *ps = s->buf; i++; sp++; } break; case 'a': { mrb_value aa; struct RArray *a; mrb_value **pb; int *pl; aa = to_ary(mrb, *sp); a = mrb_ary_ptr(aa); pb = va_arg(ap, mrb_value**); *pb = a->buf; pl = va_arg(ap, int*); *pl = a->len; i++; sp++; } break; case 'f': { mrb_float *p; p = va_arg(ap, mrb_float*); switch (sp->tt) { case MRB_TT_FLOAT: *p = mrb_float(*sp); break; case MRB_TT_FIXNUM: *p = (mrb_float)mrb_fixnum(*sp); break; case MRB_TT_FALSE: *p = 0.0; break; default: { mrb_value tmp; tmp = mrb_convert_type(mrb, *sp, MRB_TT_FLOAT, "Float", "to_f"); *p = mrb_float(tmp); } break; } i++; sp++; } break; case 'i': { mrb_int *p; p = va_arg(ap, mrb_int*); switch (sp->tt) { case MRB_TT_FIXNUM: *p = mrb_fixnum(*sp); break; case MRB_TT_FLOAT: *p = (mrb_int)mrb_float(*sp); break; case MRB_TT_FALSE: *p = 0; break; default: { mrb_value tmp; tmp = mrb_convert_type(mrb, *sp, MRB_TT_FIXNUM, "Integer", "to_int"); *p = mrb_fixnum(tmp); } break; } i++; sp++; } break; case '&': { mrb_value *p, *bp = mrb->stack + 1; p = va_arg(ap, mrb_value*); if (mrb->ci->argc > 0) { bp += mrb->ci->argc; } *p = *bp; } break; case '|': opt = 1; break; case '*': { mrb_value **var; int *pl; var = va_arg(ap, mrb_value**); pl = va_arg(ap, int*); if (argc > i) { *pl = argc-i; if (*pl > 0) { *var = sp; i = argc; } i = argc; sp += *pl; } else { *pl = 0; *var = NULL; } } break; } } if (!c && argc > i) { mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments"); } va_end(ap); return i; }