int main (int argc, char *argv[]) { int res = 1; if (argc != 3) { fprintf(stderr, "Usage: %s < unix:<socket_path> / tcp:<address>:<port> > <request_payload>\n", (argc > 0 ? argv[0] : "")); goto fail0; } char *connect_address = argv[1]; char *request_payload_string = argv[2]; BLog_InitStderr(); BTime_Init(); NCDValMem_Init(&request_mem); if (!NCDValParser_Parse(request_payload_string, strlen(request_payload_string), &request_mem, &request_value)) { BLog(BLOG_ERROR, "BReactor_Init failed"); goto fail1; } if (!BNetwork_GlobalInit()) { BLog(BLOG_ERROR, "BNetwork_Init failed"); goto fail1; } if (!BReactor_Init(&reactor)) { BLog(BLOG_ERROR, "BReactor_Init failed"); goto fail1; } struct BConnection_addr addr; if (!make_connect_addr(connect_address, &addr)) { goto fail2; } if (!NCDRequestClient_Init(&client, addr, &reactor, NULL, client_handler_error, client_handler_connected)) { BLog(BLOG_ERROR, "NCDRequestClient_Init failed"); goto fail2; } have_request = 0; res = BReactor_Exec(&reactor); if (have_request) { NCDRequestClientRequest_Free(&request); } NCDRequestClient_Free(&client); fail2: BReactor_Free(&reactor); fail1: NCDValMem_Free(&request_mem); BLog_Free(); fail0: DebugObjectGlobal_Finish(); return res; }
static void decode_value_eval (NCDCall call) { if (NCDCall_ArgCount(&call) != 1) { return FunctionLog(&call, BLOG_ERROR, "decode_value: need one argument"); } // Evaluate the string to a temporary mem, not ResMem. // Otherwise the ResMem could get resized while we're // parsing a string within it, and boom. NCDValMem temp_mem; NCDValMem_Init(&temp_mem, NCDCall_Iparams(&call)->string_index); NCDValRef arg = NCDCall_EvalArg(&call, 0, &temp_mem); if (NCDVal_IsInvalid(arg)) { goto fail1; } if (!NCDVal_IsString(arg)) { FunctionLog(&call, BLOG_ERROR, "decode_value: argument not a string"); goto fail1; } NCDValRef value; int res = NCDValParser_Parse(NCDVal_StringMemRef(arg), NCDCall_ResMem(&call), &value); if (!res) { FunctionLog(&call, BLOG_ERROR, "decode_value: NCDValParser_Parse failed"); goto fail1; } NCDCall_SetResult(&call, value); fail1: NCDValMem_Free(&temp_mem); }
static void new_templ (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, parse_func pfunc) { struct instance *o = vo; o->i = i; // read arguments NCDValRef str_arg; if (!NCDVal_ListRead(params->args, 1, &str_arg)) { ModuleLog(i, BLOG_ERROR, "wrong arity"); goto fail0; } if (!NCDVal_IsString(str_arg)) { ModuleLog(o->i, BLOG_ERROR, "wrong type"); goto fail0; } // init mem NCDValMem_Init(&o->mem); // parse o->succeeded = pfunc(i, NCDVal_StringMemRef(str_arg), &o->mem, &o->value); // signal up NCDModuleInst_Backend_Up(i); return; fail0: NCDModuleInst_Backend_DeadError(i); }
int main () { int res; res = NCDStringIndex_Init(&string_index); ASSERT_FORCE(res) NCDValMem_Init(&mem, &string_index); res = NCDValCons_Init(&cons, &mem); ASSERT_FORCE(res) NCDValRef val1 = complete(list_prepend(list_prepend(list_prepend(make_list(), make_string("hello")), make_string("world")), make_list())); char *str1 = NCDValGenerator_Generate(val1); ASSERT_FORCE(str1) ASSERT_FORCE(!strcmp(str1, "{{}, \"world\", \"hello\"}")) free(str1); NCDValRef val2 = complete(map_insert(map_insert(map_insert(make_map(), make_list(), make_list()), make_string("A"), make_list()), make_string("B"), make_list())); char *str2 = NCDValGenerator_Generate(val2); ASSERT_FORCE(str2) printf("%s\n", str2); ASSERT_FORCE(!strcmp(str2, "[\"A\":{}, \"B\":{}, {}:{}]")) free(str2); NCDValCons_Free(&cons); NCDValMem_Free(&mem); NCDStringIndex_Free(&string_index); return 0; }