Created
July 31, 2017 21:32
-
-
Save november-kilo/27d9f2308d6ce8a05e446db82c0ff94c to your computer and use it in GitHub Desktop.
dgd kfun extension mapping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#include <curl/curl.h> | |
#include <json-c/json.h> | |
#include "lpc_ext.h" | |
static void random_map(LPC_frame f, int nargs, LPC_value retval) | |
{ | |
LPC_dataspace data; | |
LPC_mapping mapping; | |
LPC_value mapping_key, mapping_value; | |
LPC_string key_str, val_str; | |
char key[100]; | |
char val[5]; | |
data = lpc_frame_dataspace(f); | |
mapping_key = lpc_value_temp(data); | |
mapping_value = lpc_value_temp(data); | |
sprintf(key, "key_%d", 1); | |
sprintf(val, "val_%d", rand()); | |
key_str = lpc_string_new(data, key, strlen(key)); | |
val_str = lpc_string_new(data, val, strlen(val)); | |
lpc_string_putval(mapping_key, key_str); | |
lpc_string_putval(mapping_value, val_str); | |
mapping = lpc_mapping_new(data); | |
lpc_mapping_assign(data, mapping, mapping_key, mapping_value); | |
lpc_mapping_putval(retval, mapping); | |
/* expected, ([ "key_1":"val_1234" ]) */ | |
/* actual, ([ "val_730934857":"val_730934857" ]) */ | |
} | |
static char random_map_proto[] = | |
{ LPC_TYPE_ARRAY_OF(LPC_TYPE_MAPPING), LPC_TYPE_STRING, 0 }; | |
static LPC_ext_kfun kf[1] = { | |
"random_map", | |
random_map_proto, | |
&random_map | |
}; | |
int lpc_ext_init(int major, int minor, char *config) | |
{ | |
time_t t; | |
srand((unsigned)time(&t)); | |
lpc_ext_kfun(kf, 1); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment