Skip to content

Instantly share code, notes, and snippets.

@dams
Last active December 19, 2015 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dams/5876003 to your computer and use it in GitHub Desktop.
Save dams/5876003 to your computer and use it in GitHub Desktop.
void mapping_convert_inline_c(SV* hash_ref, SV* dict_ref) {
HV* hash;
HE* hash_entry;
int num_keys, i;
SV* sv_key;
SV* sv_val;
SV* sv_val2;
HV* dict;
HE* he_dict;
SV* sv_dict_val;
char* dict_val;
if (! SvROK(hash_ref))
croak("hash_ref is not a reference");
hash = (HV*)SvRV(hash_ref);
if (! SvROK(dict_ref))
croak("dict_ref is not a reference");
dict = (HV*)SvRV(dict_ref);
num_keys = hv_iterinit(hash);
for (i = 0; i < num_keys; i++) {
hash_entry = hv_iternext(hash);
sv_key = hv_iterkeysv(hash_entry);
sv_val = hv_iterval(hash, hash_entry);
printf("%s => %s\n", SvPV(sv_key, PL_na), SvPV(sv_val, PL_na));
he_dict = hv_fetch_ent(dict, sv_key, 0, 0);
if (he_dict == NULL) {
printf("%s => null\n", SvPV(sv_key, PL_na));
} else {
sv_dict_val = HeVAL(he_dict);
dict_val = SvPV(sv_dict_val, PL_na);
printf("%s => %s\n", SvPV(sv_key, PL_na), dict_val);
sv_val2 = hv_delete_ent(hash, sv_key, 0, 0);
if ( sv_val2 == NULL ) {
printf("deleting %s failed\n", SvPV(sv_key, PL_na));
} else {
hv_store_ent(hash, sv_key, SvREFCNT_inc(sv_val2), 0);
}
}
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment