/*==================================================================== * 函数名 : vGenPFMINFOCPUMsg * 功能 : 上报设备CPU性能消息 * 算法实现 : * 参数说明 : vpd 要上报的设备 * 返回值说明: 成功 生成的消息 * 失败 NULL * ---------------------------------------------------------------------- * 修改记录: * 日 期 版本 修改人 走读人 修改记录 * 2015/1/21 v1.0 YLI 创建 * ====================================================================*/ json_t * vGenPFMINFOCPUMsg(VPD vpd) { json_t *root; json_t *cpuinfo; json_t *coreinfo; char saLocalTime[256]; root = json_object(); //devid if (json_object_set(root,"devid", json_string(vpd.saDevId)) == FAILUER) { json_decref(root); vLogErr("devid set error!!!"); return NULL; } //devtype if (json_object_set(root,"devtype", json_string(vpd.saDevType)) == FAILUER) { json_decref(root); vLogErr("devtype set error!!!"); return NULL; } //eventid if (json_object_set(root,"eventid", json_string("EV_PFMINFO_CPU")) == FAILUER) { json_decref(root); vLogErr("eventid set error!!!"); return NULL; } //rpttime memset(saLocalTime,0x00,sizeof saLocalTime); GetLocalTime(saLocalTime); json_object_set(root,"rpttime",json_string(saLocalTime)); /*cpuinfo*/ cpuinfo = json_object(); //cpuusage json_object_set(cpuinfo,"cpuusage",json_integer(13)); //cpucorecount json_object_set(cpuinfo,"cpucorecount",json_integer(3)); //coreinfo coreinfo = json_array(); json_array_insert(coreinfo,0,json_pack("{s:i}","cpucore1",143)); json_array_insert(coreinfo,1,json_pack("{s:i}","cpucore2",143)); json_array_insert(coreinfo,2,json_pack("{s:i}","cpucore3",143)); json_object_set(cpuinfo,"coreinfo",coreinfo); json_object_set(root,"cpuinfo",cpuinfo); json_decref(coreinfo); json_decref(cpuinfo); return root; }
JS_EXPORT_API JSObjectRef installer_get_system_users() { GRAB_CTX (); JSObjectRef array = json_array_create (); struct passwd *user; gchar *username = NULL; int i = 0; while ((user = getpwent ()) != NULL){ if (user->pw_uid >= 1000 || g_strcmp0 ("deepin", user->pw_name) == 0) { continue; } username = g_strdup (user->pw_name); json_array_insert(array, i, jsvalue_from_cstr (get_global_context(), username)); i++; g_free (username); } endpwent (); UNGRAB_CTX (); return array; }
JS_EXPORT_API JSObjectRef launcher_get_items_by_category(double _id) { int id = _id; if (id == ALL_CATEGORY_ID) return _init_category_table(); JSObjectRef items = json_array_create(); GHashTable* l = (GHashTable*)g_hash_table_lookup(_category_table, GINT_TO_POINTER(id)); if (l == NULL) { return items; } JSContextRef cxt = get_global_context(); GHashTableIter iter; gpointer value = NULL; g_hash_table_iter_init(&iter, l); for (int i = 0; g_hash_table_iter_next(&iter, &value, NULL); ++i) { char const* path = (char const*)value; json_array_insert(items, i, jsvalue_from_cstr(cxt, path)); } return items; }
JS_EXPORT_API JSObjectRef greeter_get_users () { JSObjectRef array = json_array_create (); LightDMUser *user = NULL; guint i; if (users == NULL) { LightDMUserList *user_list = lightdm_user_list_get_instance (); if (user_list == NULL) { g_warning ("get users:user list is NULL\n"); return array; } users = lightdm_user_list_get_users (user_list); } for (i = 0; i < g_list_length (users); ++i) { gchar *username = NULL; user = (LightDMUser *) g_list_nth_data (users, i); username = g_strdup (lightdm_user_get_name (user)); json_array_insert (array, i, jsvalue_from_cstr (get_global_context (), g_strdup (username))); g_free (username); } return array; }
//native json_array_insert(Handle:hArray, iIndex, Handle:hValue); static cell_t Native_json_array_insert(IPluginContext *pContext, const cell_t *params) { HandleError err; HandleSecurity sec; sec.pOwner = NULL; sec.pIdentity = myself->GetIdentity(); // Param 1: hArray json_t *object; Handle_t hndlObject = static_cast<Handle_t>(params[1]); if ((err=g_pHandleSys->ReadHandle(hndlObject, htJanssonObject, &sec, (void **)&object)) != HandleError_None) { return pContext->ThrowNativeError("Invalid <Array> handle %x (error %d)", hndlObject, err); } // Param 2: iIndex int iIndex = params[2]; // Param 3: hValue json_t *value; Handle_t hndlValue = static_cast<Handle_t>(params[3]); if ((err=g_pHandleSys->ReadHandle(hndlValue, htJanssonObject, &sec, (void **)&value)) != HandleError_None) { return pContext->ThrowNativeError("Invalid JSON handle %x (error %d)", hndlObject, err); } return (json_array_insert(object, iIndex, value) == 0); }
void trans_to_js_array(char** strv, gsize length, JSObjectRef json) { JSContextRef ctx = get_global_context(); for (guint i = 0; strv[i] != NULL || i < length; ++i) { json_array_insert(json, i, jsvalue_from_cstr(ctx, strv[i])); } }
void scan_plugin_dir(char const* path, char const* app_name, JSObjectRef array) { if (!g_file_test(path, G_FILE_TEST_EXISTS)) { return; } JSContextRef ctx = get_global_context(); GDir* dir = g_dir_open(path, 0, NULL); if (dir != NULL) { const char* file_name = NULL; for (int i=0; NULL != (file_name = g_dir_read_name(dir)); ) { char* full_path = g_build_filename(path, file_name, NULL); if (IS_DIR(full_path) && is_plugin(full_path)) { char* js_name = g_strconcat(file_name, ".js", NULL); char* js_path = g_build_filename(full_path, js_name, NULL); g_free(js_name); char* key = g_strconcat(app_name, ":", file_name, NULL); if (g_hash_table_contains(enabled_plugins, key)) { JSValueRef v = jsvalue_from_cstr(ctx, js_path); json_array_insert(array, i++, v); } g_free(key); g_free(js_path); } g_free(full_path); } g_dir_close(dir); } }
JS_EXPORT_API JSObjectRef greeter_get_sessions () { JSObjectRef array = json_array_create (); guint i; if (sessions == NULL) { sessions = lightdm_get_sessions (); } for (i = 0; i < g_list_length (sessions); ++i) { gchar *key = NULL; LightDMSession *session = (LightDMSession *)g_list_nth_data(sessions, i); key = g_strdup (lightdm_session_get_key (session)); json_array_insert(array, i, jsvalue_from_cstr(get_global_context(), g_strdup(key))); g_free (key); } return array; }
PRIVATE void _insert_category(JSObjectRef categories, int array_index, int id, const char* name) { JSObjectRef item = json_create(); json_append_number(item, "ID", id); json_append_string(item, "Name", name); json_array_insert(categories, array_index, item); }
static void test_circular() { json_t *array1, *array2; /* the simple cases are checked */ array1 = json_array(); if(!array1) fail("unable to create array"); if(json_array_append(array1, array1) == 0) fail("able to append self"); if(json_array_insert(array1, 0, array1) == 0) fail("able to insert self"); if(json_array_append_new(array1, json_true())) fail("failed to append true"); if(json_array_set(array1, 0, array1) == 0) fail("able to set self"); json_decref(array1); /* create circular references */ array1 = json_array(); array2 = json_array(); if(!array1 || !array2) fail("unable to create array"); if(json_array_append(array1, array2) || json_array_append(array2, array1)) fail("unable to append"); /* circularity is detected when dumping */ if(json_dumps(array1, 0) != NULL) fail("able to dump circulars"); /* decref twice to deal with the circular references */ json_decref(array1); json_decref(array2); json_decref(array1); }
static void test_insert(void) { json_t *array, *five, *seven, *eleven, *value; int i; array = json_array(); five = json_integer(5); seven = json_integer(7); eleven = json_integer(11); if(!array) fail("unable to create array"); if(!five || !seven || !eleven) fail("unable to create integer"); if(!json_array_insert(array, 1, five)) fail("able to insert value out of bounds"); if(json_array_insert(array, 0, five)) fail("unable to insert value in an empty array"); if(json_array_get(array, 0) != five) fail("json_array_insert works incorrectly"); if(json_array_size(array) != 1) fail("array size is invalid after insertion"); if(json_array_insert(array, 1, seven)) fail("unable to insert value at the end of an array"); if(json_array_get(array, 0) != five) fail("json_array_insert works incorrectly"); if(json_array_get(array, 1) != seven) fail("json_array_insert works incorrectly"); if(json_array_size(array) != 2) fail("array size is invalid after insertion"); if(json_array_insert(array, 1, eleven)) fail("unable to insert value in the middle of an array"); if(json_array_get(array, 0) != five) fail("json_array_insert works incorrectly"); if(json_array_get(array, 1) != eleven) fail("json_array_insert works incorrectly"); if(json_array_get(array, 2) != seven) fail("json_array_insert works incorrectly"); if(json_array_size(array) != 3) fail("array size is invalid after insertion"); if(json_array_insert_new(array, 2, json_integer(123))) fail("unable to insert value in the middle of an array"); value = json_array_get(array, 2); if(!json_is_integer(value) || json_integer_value(value) != 123) fail("json_array_insert_new works incorrectly"); if(json_array_size(array) != 4) fail("array size is invalid after insertion"); for(i = 0; i < 20; i++) { if(json_array_insert(array, 0, seven)) fail("unable to insert value at the begining of an array"); } for(i = 0; i < 20; i++) { if(json_array_get(array, i) != seven) fail("json_aray_insert works incorrectly"); } if(json_array_size(array) != 24) fail("array size is invalid after loop insertion"); json_decref(five); json_decref(seven); json_decref(eleven); json_decref(array); }
JS_EXPORT_API JSObjectRef installer_get_timezone_list () { GRAB_CTX (); gsize index = 0; GError *error = NULL; GFile *file = NULL; GFileInputStream *input = NULL; GDataInputStream *data_input = NULL; JSObjectRef timezones = json_array_create (); file = g_file_new_for_path ("/usr/share/zoneinfo/zone.tab"); if (!g_file_query_exists (file, NULL)) { g_warning ("get timezone list:zone.tab not exists\n"); goto out; } input = g_file_read (file, NULL, &error); if (error != NULL){ g_warning ("get timezone list:read zone.tab error->%s", error->message); goto out; } data_input = g_data_input_stream_new ((GInputStream *) input); if (data_input == NULL) { g_warning ("get timezone list:get data input stream failed\n"); goto out; } char *data = (char *) 1; while (data) { data = g_data_input_stream_read_line (data_input, NULL, NULL, NULL); if (data == NULL) { break; } if (g_str_has_prefix (data, "#")){ g_debug ("get timezone list:comment line, just pass"); continue; } else { gchar **line = g_strsplit (data, "\t", -1); if (line == NULL) { g_warning ("get timezone list:split %s failed\n", data); } else { json_array_insert (timezones, index, jsvalue_from_cstr (get_global_context (), line[2])); index++; g_strfreev (line); } } } goto out; out: if (file != NULL) { g_object_unref (file); } if (data_input != NULL) { g_object_unref (data_input); } if (input != NULL) { g_object_unref (input); } if (error != NULL) { g_error_free (error); error = NULL; } UNGRAB_CTX (); return timezones; }
void MltRuntime::add_runtime_entry(const JsonPath& path, json_t* script_serialed, int give) throw(Exception) { if ( path.path.size() == 0) { throw_error_v(ErrorRuntimeUuidPathInvalid, "root path not allowed for add entry"); } JsonPath parentPath(path); parentPath.path.pop_back(); json_t* parent_je = json_serialize; JsonPath::PathCompIter it = parentPath.path.begin(); for (; it != parentPath.path.end(); it++) { json_t* je = json_object_get(parent_je, it->name.c_str()); if (!je) { throw_error_v(ErrorRuntimeUuidPathInvalid, "parent path invalid for add entry"); } if ( it->type == JsonPathComponent::PathObject ) { if ( !json_is_object(je) ) { throw_error_v(ErrorRuntimeUuidPathInvalid, "parent path invalid for add entry"); } parent_je = je; } else if (it->type == JsonPathComponent::PathArray ) { if (!json_is_array(je)) { throw_error_v(ErrorRuntimeUuidPathInvalid, "parent path invalid for add entry"); } int sz = json_array_size(je); if (sz == 0) { throw_error_v(ErrorRuntimeUuidPathInvalid, "parent path invalid for add entry"); } int idx = it->arr_idx; if (idx < 0) idx = sz + idx; if (idx < 0 || idx >= sz ) { throw_error_v(ErrorRuntimeUuidPathInvalid, "parent path invalid for add entry"); } parent_je = json_array_get(je, idx); } } if (!json_is_object(parent_je)) { throw_error_v(ErrorRuntimeUuidPathInvalid, "parent path invalid for add entry"); } const JsonPathComponent& lastPath = *(path.path.rbegin()); if (lastPath.type == JsonPathComponent::PathArray) { json_t* cur = json_object_get(parent_je, lastPath.name.c_str()); if ( !cur ) { cur = json_array(); json_object_set_new(parent_je, lastPath.name.c_str(), cur); } if (!json_is_array(cur)) { throw_error_v(ErrorRuntimeUuidPathInvalid, " path invalid for add entry"); } else { int idx = lastPath.arr_idx; int sz = json_array_size(cur); if (idx < 0) { idx = sz + idx + 1; } if (idx > sz) idx = sz; if (idx < 0) idx = 0; if ( idx == sz ) { JsonPath curPath(parentPath); curPath.push_back(lastPath.name.c_str(), idx); parse_struct(script_serialed, curPath, uuid_pathmap); if (give) json_array_append_new(cur, script_serialed); else json_array_append(cur, script_serialed); } else { JsonPath curPath(parentPath); curPath.push_back(lastPath.name.c_str(), idx); parse_struct(script_serialed, curPath, uuid_pathmap); if (give) json_array_insert_new(cur, idx, script_serialed); else json_array_insert(cur, idx, script_serialed); } } } else if (lastPath.type == JsonPathComponent::PathObject) { json_t* cur = json_object_get(parent_je, lastPath.name.c_str()); if (cur) { throw_error_v(ErrorRuntimeUuidPathInvalid, " path invalid for add entry"); } JsonPath curPath(parentPath); curPath.push_back(lastPath.name.c_str()); parse_struct(script_serialed, curPath, uuid_pathmap); if (give) { json_object_set_new(parent_je, lastPath.name.c_str(), script_serialed); } else { json_object_set(parent_je, lastPath.name.c_str(), script_serialed); } } json_version++; return; }
void net_json_convert_min3(json_t* r) { // ADC has changed i/o count from [2,2] to [3,2], // and likewise size of byte-arry of state data. // there is only one ADC operator, so this isn't too bad. /// magic number based on what 0.3.x scenes looked like. int lastAdcIn = 29; json_t* ins = json_object_get(r, "ins"); json_t* insData = json_object_get(ins, "data"); int insCount = json_integer_value(json_object_get(ins, "count")); json_integer_set(json_object_get(ins, "count"), insCount + 1); // need to f*x // all we should have to do for input nodes is: // - insert a new node // - fix everyone's index json_t* o = json_object(); json_object_set(o, "idx", json_integer(lastAdcIn)); json_object_set(o, "name", json_string("MODE ")); json_object_set(o, "opIdx", json_integer(10)); json_object_set(o, "opInIdx", json_integer(2)); json_object_set(o, "value", json_integer(0)); json_object_set(o, "play", json_integer(0)); int err = json_array_insert(insData, lastAdcIn + 1, o ); if(err != 0) { printf(" error inserting input node in ADC op conversion."); } // loop over input nodes and fix idx fields for(int i=0; i<NET_INS_MAX; i++) { json_integer_set(json_object_get(json_array_get(insData, i), "idx"), i); } // we also have to add some dummy values to the byte array of the ADC op's state. json_t* ops = json_object_get(r, "ops"); // 10 is the magic number of the ADC op in 0.3.x json_t* adc = json_array_get(json_object_get(ops, "data"), 10); // mode pickle: 2 bytes input value, zero is fine json_array_append(json_object_get(adc, "state"), json_integer(0)); json_array_append(json_object_get(adc, "state"), json_integer(0)); /* // copy all input nodes above this operator's... */ /* // count down from top. src has fewer input nodes; otherwise we would lose last value. */ /* for(int i = NET_INS_MAX - 1; i > lastAdcIn; i--) { */ /* // get json object at this index in node array */ /* json_t* dst = json_array_get(insData, i); */ /* json_t* src = json_array_get(insData, i - 1); */ /* if(src != NULL) { */ /* if(dst == NULL) { */ /* int err; */ /* dst = json_object(); */ /* json_object_set(dst, "idx", json_integer(i)); */ /* err = json_array_insert(insData, i, dst); */ /* if(err != 0) { printf(" error inserting input node in ADC op conversion."); } */ /* } */ /* // shallow copy ok? */ /* json_array_set(insData, src); */ /* // custom-copy, leaving idx */ /* /\* json_object_(dst, "name", json_object_get(src, "name")); *\/ */ /* /\* json_object_set(dst, "opIdx", json_object_get(src, "opIdx")); *\/ */ /* /\* json_object_set(dst, "opInIdx", json_object_get(src, "opInIdx")); *\/ */ /* /\* json_object_set(dst, "value", json_object_get(src, "value")); *\/ */ /* /\* json_object_set(dst, "play", json_object_get(src, "play")); *\/ */ /* } */ /* } */ /* // finally, we need to fix up the values for the new input ("mode"); */ /* ///... FIXME */ //---- -outs json_t* outs = json_object_get(r, "outs"); json_t* outsData = json_object_get(outs, "data"); // loop over all outputs and check targets for(int i = 0; i< NET_OUTS_MAX; i++) { json_t* o = json_array_get(outsData, i); int t = json_integer_value(json_object_get(o, "target")); if(t > lastAdcIn) { json_integer_set(json_object_get(o, "target"), t + 1); } } // i don't think anyone really used presets in this rev, so skipping those for now. // write the output of the converted json for a visual check. json_dump_file(r, "converted.json", JSON_INDENT(4) | JSON_PRESERVE_ORDER | JSON_ESCAPE_SLASH); }
/*==================================================================== * 函数名 : vGenPFMINFONETCARDMsg * 功能 : 主动上报设备NETCARD性能 * 算法实现 : * 参数说明 : vpd 要上报的设备 * 返回值说明: 成功 生成的消息 * 失败 NULL * ---------------------------------------------------------------------- * 修改记录: * 日 期 版本 修改人 走读人 修改记录 * 2015/1/21 v1.0 YLI 创建 * ====================================================================*/ json_t * vGenPFMINFONETCARDMsg(VPD vpd) { json_t *root; json_t *netcardinfo; json_t *netcards; char saLocalTime[256]; root = json_object(); //devid if (json_object_set(root,"devid", json_string(vpd.saDevId)) == FAILUER) { json_decref(root); vLogErr("devid set error!!!"); return NULL; } //devtype if (json_object_set(root,"devtype", json_string(vpd.saDevType)) == FAILUER) { json_decref(root); vLogErr("devtype set error!!!"); return NULL; } //eventid if (json_object_set(root,"eventid", json_string("EV_PFMINFO_NETCARD")) == FAILUER) { json_decref(root); vLogErr("eventid set error!!!"); return NULL; } //rpttime memset(saLocalTime,0x00,sizeof saLocalTime); GetLocalTime(saLocalTime); json_object_set(root,"rpttime",json_string(saLocalTime)); /*netcardinfo*/ netcardinfo = json_object(); //netcardcount json_object_set(netcardinfo,"netcardcount",json_integer(3)); //sendkbps json_object_set(netcardinfo,"sendkbps",json_integer(3000)); //recvkbps json_object_set(netcardinfo,"recvkbps",json_integer(3000)); //recvpktloserate json_object_set(netcardinfo,"recvpktloserate",json_integer(3)); //netcards netcards = json_array(); json_array_insert(netcards,0, json_pack("{s:{s:i,s:i,s:i}}", "netcard1", "sendkbps",234, "recvkbps",234, "recvpktloserate",23)); json_array_insert(netcards,1, json_pack("{s:{s:i,s:i,s:i}}", "netcard2", "sendkbps",234, "recvkbps",234, "recvpktloserate",23)); json_array_insert(netcards,2, json_pack("{s:{s:i,s:i,s:i}}", "netcard3", "sendkbps",234, "recvkbps",234, "recvpktloserate",23)); json_object_set(netcardinfo,"netcards",netcards); json_object_set(root,"netcardinfo",netcardinfo); json_decref(netcards); json_decref(netcardinfo); return root; }
/*==================================================================== * 函数名 : vGenCONNSRVCONNMsg * 功能 : 终端与某服务器连上时上报消息 * 算法实现 : * 参数说明 : vpd 要上报的设备 * 返回值说明: 成功 生成的消息 * 失败 NULL * ---------------------------------------------------------------------- * 修改记录: * 日 期 版本 修改人 走读人 修改记录 * 2015/1/26 v1.0 YLI 创建 * ====================================================================*/ json_t * vGenCONNSRVCONNMsg(VPD vpd) { json_t *root; json_t *conn_srv_state_info; json_t *jt; json_t *jp; char saCfgPath[L_PATH_MAX_LEN]; int n,m; root = json_object(); memset(saCfgPath,0x00,sizeof saCfgPath); //eventid if (json_object_set(root,"eventid", json_string("EV_CONNSRV_CONN_MSG")) == FAILUER) { json_decref(root); vLogErr("eventid set error!!!"); return NULL; } conn_srv_state_info = json_array(); if(getenv("APP_PROFILE_PATH") == NULL) { json_decref(root); vLogErr("devtypes config file path error,please check evn value!!!"); return NULL; } strcpy(saCfgPath,getenv("APP_PROFILE_PATH")); strcat(saCfgPath,"/devtypes.json"); /*conn_srv_state_info*/ jt = tPflGetJsonObj(saCfgPath,K_LOGICTYPE_KEY); if(jt == NULL) { json_decref(root); return NULL; } srand((unsigned)time(NULL)); if (json_is_array(jt)) { int it; m = json_array_size(jt); n = rand()%m; jp = json_pack("{s:s,s:s}", "type",json_string_value(json_array_get(jt,n)), "ip",_gstrpShm->rc.nSrvIP); if ((jp ==NULL) || (json_array_insert(conn_srv_state_info,0, jp)) == FAILUER) { json_decref(root); vLogErr("devtype set error!!!"); return NULL; } it = rand()%m; while(it == n) it = rand()%m; n = it; jp = json_pack("{s:s,s:s}", "type",json_string_value(json_array_get(jt,n)), "ip",_gstrpShm->rc.nSrvIP); if (json_array_insert(conn_srv_state_info,1, json_array_get(jt,n)) == FAILUER) { json_decref(root); vLogErr("devtype set error!!!"); return NULL; } } json_decref(jt); json_decref(jp); json_decref(conn_srv_state_info); json_object_set(root,"conn_srv_state_info",conn_srv_state_info); return root; }
int la_codec_array_insert(la_codec_value_t *array, size_t index, la_codec_value_t *value) { return json_array_insert((json_t*) array, index, (json_t*) value); }