static void counters_save(running_machine &machine, int config_type, xml_data_node *parentnode) { generic_machine_private *state = machine.generic_machine_data; int i; /* only care about game-specific data */ if (config_type != CONFIG_TYPE_GAME) return; /* iterate over coin counters */ for (i = 0; i < COIN_COUNTERS; i++) if (state->coin_count[i] != 0) { xml_data_node *coinnode = xml_add_child(parentnode, "coins", NULL); if (coinnode != NULL) { xml_set_attribute_int(coinnode, "index", i); xml_set_attribute_int(coinnode, "number", state->coin_count[i]); } } /* output tickets */ if (state->dispensed_tickets != 0) { xml_data_node *tickets = xml_add_child(parentnode, "tickets", NULL); if (tickets != NULL) xml_set_attribute_int(tickets, "number", state->dispensed_tickets); } }
void crosshair_manager::config_save(config_type cfg_type, xml_data_node *parentnode) { /* Note: crosshair_save() is only registered if crosshairs are used */ xml_data_node *crosshairnode; int player; /* we only care about game files */ if (cfg_type != config_type::CONFIG_TYPE_GAME) return; for (player = 0; player < MAX_PLAYERS; player++) { const render_crosshair &crosshair = *m_crosshair[player]; if (crosshair.is_used()) { /* create a node */ crosshairnode = xml_add_child(parentnode, "crosshair", nullptr); if (crosshairnode != nullptr) { bool changed = false; xml_set_attribute_int(crosshairnode, "player", player); if (crosshair.mode() != CROSSHAIR_VISIBILITY_DEFAULT) { xml_set_attribute_int(crosshairnode, "mode", crosshair.mode()); changed = true; } // only save graphic name if not the default if (*crosshair.bitmap_name() != '\0') { xml_set_attribute(crosshairnode, "pic", crosshair.bitmap_name()); changed = true; } /* if nothing changed, kill the node */ if (!changed) xml_delete_node(crosshairnode); } } } /* always store autotime so that it stays at the user value if it is needed */ if (m_auto_time != CROSSHAIR_VISIBILITY_AUTOTIME_DEFAULT) { /* create a node */ crosshairnode = xml_add_child(parentnode, "autotime", nullptr); if (crosshairnode != nullptr) xml_set_attribute_int(crosshairnode, "val", m_auto_time); } }
static void crosshair_save(running_machine &machine, int config_type, xml_data_node *parentnode) { /* Note: crosshair_save() is only registered if crosshairs are used */ xml_data_node *crosshairnode; int player; /* we only care about game files */ if (config_type != CONFIG_TYPE_GAME) return; for (player = 0; player < MAX_PLAYERS; player++) { if (global.used[player]) { /* create a node */ crosshairnode = xml_add_child(parentnode, "crosshair", NULL); if (crosshairnode != NULL) { int changed = FALSE; xml_set_attribute_int(crosshairnode, "player", player); if (global.visible[player] != CROSSHAIR_VISIBILITY_DEFAULT) { xml_set_attribute_int(crosshairnode, "mode", global.mode[player]); changed = TRUE; } /* the default graphic name is "", so only save if not */ if (*(global.name[player]) != 0) { xml_set_attribute(crosshairnode, "pic", global.name[player]); changed = TRUE; } /* if nothing changed, kill the node */ if (!changed) xml_delete_node(crosshairnode); } } } /* always store autotime so that it stays at the user value if it is needed */ if (global.auto_time != CROSSHAIR_VISIBILITY_AUTOTIME_DEFAULT) { /* create a node */ crosshairnode = xml_add_child(parentnode, "autotime", NULL); if (crosshairnode != NULL) xml_set_attribute_int(crosshairnode, "val", global.auto_time); } }
void laserdisc_device::config_save(config_type cfg_type, xml_data_node *parentnode) { // we only care about game files if (cfg_type != config_type::CONFIG_TYPE_GAME) return; // create a node xml_data_node *ldnode = xml_add_child(parentnode, "device", nullptr); if (ldnode != nullptr) { // output the basics xml_set_attribute(ldnode, "tag", tag().c_str()); // add an overlay node xml_data_node *overnode = xml_add_child(ldnode, "overlay", nullptr); bool changed = false; if (overnode != nullptr) { // output the positioning controls if (m_overposx != m_orig_config.m_overposx) { xml_set_attribute_float(overnode, "hoffset", m_overposx); changed = true; } if (m_overscalex != m_orig_config.m_overscalex) { xml_set_attribute_float(overnode, "hstretch", m_overscalex); changed = true; } if (m_overposy != m_orig_config.m_overposy) { xml_set_attribute_float(overnode, "voffset", m_overposy); changed = true; } if (m_overscaley != m_orig_config.m_overscaley) { xml_set_attribute_float(overnode, "vstretch", m_overscaley); changed = true; } } // if nothing changed, kill the node if (!changed) xml_delete_node(ldnode); } }
static void sound_save(int config_type, xml_data_node *parentnode) { int mixernum; /* we only care about game files */ if (config_type != CONFIG_TYPE_GAME) return; /* iterate over mixer channels */ if (parentnode != NULL) for (mixernum = 0; mixernum < MAX_MIXER_CHANNELS; mixernum++) { float defvol = sound_get_default_gain(mixernum); float newvol = sound_get_user_gain(mixernum); if (defvol != newvol) { xml_data_node *channelnode = xml_add_child(parentnode, "channel", NULL); if (channelnode != NULL) { xml_set_attribute_int(channelnode, "index", mixernum); xml_set_attribute_float(channelnode, "defvol", defvol); xml_set_attribute_float(channelnode, "newvol", newvol); } } } }
static void configuration_save(running_machine &machine, int config_type, xml_data_node *parentnode) { /* we only care about game files */ if (config_type != CONFIG_TYPE_GAME) return; // Loop over all the nodes for(win_i *p = win_list; p != NULL; p = p->next) { /* create a node */ xml_data_node *debugger_node; debugger_node = xml_add_child(parentnode, "window", NULL); xml_set_attribute_int(debugger_node, "type", p->type); if (debugger_node != NULL) { int x, y; gtk_window_get_position(GTK_WINDOW(p->win), &x, &y); xml_set_attribute_int(debugger_node, "position_x", x); xml_set_attribute_int(debugger_node, "position_y", y); gtk_window_get_size(GTK_WINDOW(p->win), &x, &y); xml_set_attribute_int(debugger_node, "size_x", x); xml_set_attribute_int(debugger_node, "size_y", y); } } }
static int config_save_xml(running_machine &machine, emu_file &file, int which_type) { xml_data_node *root = xml_file_create(); xml_data_node *confignode, *systemnode; config_type *type; /* if we don't have a root, bail */ if (!root) return 0; /* create a config node */ confignode = xml_add_child(root, "mameconfig", NULL); if (!confignode) goto error; xml_set_attribute_int(confignode, "version", CONFIG_VERSION); /* create a system node */ systemnode = xml_add_child(confignode, "system", NULL); if (!systemnode) goto error; xml_set_attribute(systemnode, "name", (which_type == CONFIG_TYPE_DEFAULT) ? "default" : machine.system().name); /* create the input node and write it out */ /* loop over all registrants and call their save function */ for (type = typelist; type; type = type->next) { xml_data_node *curnode = xml_add_child(systemnode, type->name, NULL); if (!curnode) goto error; type->save(which_type, curnode); /* if nothing was added, just nuke the node */ if (!curnode->value && !curnode->child) xml_delete_node(curnode); } /* flush the file */ xml_file_write(root, file); /* free and get out of here */ xml_file_free(root); return 1; error: xml_file_free(root); return 0; }
int api_board_all(void) { db_res_t *res = db_query(BOARD_SELECT_QUERY_BASE); if (!res) return WEB_ERROR_INTERNAL; xml_node_t *root = set_response_root("bbs-board-all", XML_NODE_ANONYMOUS_JSON, XML_ENCODING_UTF8); xml_node_t *boards = xml_new_node("boards", XML_NODE_CHILD_ARRAY); xml_add_child(root, boards); for (int i = db_res_rows(res) - 1; i >= 0; --i) { board_t board; res_to_board(res, i, &board); if (!has_read_perm(&board)) continue; xml_node_t *node = xml_new_node("board", XML_NODE_ANONYMOUS_JSON); board_to_node(&board, node); xml_add_child(boards, node); } db_clear(res); return WEB_OK; }
/****************************************************************************** **函数名称: invtd_search_list_trav **功 能: 构建搜索应答项 **输入参数: ** doc: 搜索列表 ** xml: 应答结果树 **输出参数: NONE **返 回: 0:Succ !0:Fail **实现描述: **注意事项: **作 者: # Qifeng.zou # 2016.05.04 01:33:38 # ******************************************************************************/ static int invtd_search_list_trav(invt_word_doc_t *doc, xml_tree_t *xml) { xml_node_t *root, *item; char freq[SRCH_SEG_FREQ_LEN]; root = xml->root->child; snprintf(freq, sizeof(freq), "%d", doc->freq); item = xml_add_child(xml, root, "ITEM", NULL); if (NULL == item) { log_error(xml->log, "Add child failed! url:%s freq:%d", doc->url.str, doc->freq); return -1; } xml_add_attr(xml, item, "URL", doc->url.str); xml_add_attr(xml, item, "FREQ", freq); return 0; }
/****************************************************************************** **函数名称: xml_add_node **功 能: 给节点添加属性或孩子节点 **输入参数: ** node: 父节点 ** name: 新节点名 ** value: 新节点值 ** type: 新节点类型. 其取值范围xml_node_type_t **输出参数: **返 回: 新增节点地址 **实现描述: **注意事项: **作 者: # Qifeng.zou # 2013.06.12 # ******************************************************************************/ xml_node_t *xml_add_node(xml_tree_t *xml, xml_node_t *node, const char *name, const char *value, int type) { switch(type) { case XML_NODE_ATTR: { return xml_add_attr(xml, node, name, value); } case XML_NODE_ROOT: case XML_NODE_CHILD: { return xml_add_child(xml, node, name, value); } } return NULL; }
/****************************************************************************** **函数名称: invtd_search_no_data_hdl **功 能: 无数据的应答处理 **输入参数: ** xml: 应答结果树 **输出参数: NONE **返 回: 0:Succ !0:Fail **实现描述: **注意事项: **作 者: # Qifeng.zou # 2016.05.01 01:49:01 # ******************************************************************************/ static int invtd_search_no_data_hdl(xml_tree_t *xml) { xml_node_t *root, *item; char freq[SRCH_SEG_FREQ_LEN]; xml_add_attr(xml, xml->root, "CODE", SRCH_CODE_NO_DATA); /* 无数据 */ snprintf(freq, sizeof(freq), "%d", 0); root = xml->root->child; item = xml_add_child(xml, root, "ITEM", NULL); if (NULL == item) { xml_destroy(xml); return -1; } xml_add_attr(xml, item, "URL", "Sorry, Didn't search anything!"); xml_add_attr(xml, item, "FREQ", freq); return 0; }
static void xml_configuration_save(running_machine &machine, int config_type, xml_data_node *parentnode) { // We only write to game configurations if (config_type != CONFIG_TYPE_GAME) return; for (int i = 0; i < xmlConfigurations.size(); i++) { WindowQtConfig* config = xmlConfigurations[i]; // Create an xml node xml_data_node *debugger_node; debugger_node = xml_add_child(parentnode, "window", NULL); if (debugger_node == NULL) continue; // Insert the appropriate information config->addToXmlDataNode(debugger_node); } }
bool_t saveenv(char * file) { struct environ_t * environ = &(runtime_get()->__environ); struct environ_t * p; struct xml * root, * env; char * str; int fd; root = xml_new("environment"); if(!root) return FALSE; for(p = environ->next; p != environ; p = p->next) { env = xml_add_child(root, "env", 0); xml_set_txt(env, p->content); } str = xml_toxml(root); if(!str) { xml_free(root); return FALSE; } fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)); if(fd < 0) { free(str); xml_free(root); return FALSE; } write(fd, str, strlen(str)); close(fd); free(str); xml_free(root); return TRUE; }
void image_manager::config_save(config_type cfg_type, xml_data_node *parentnode) { xml_data_node *node; const char *dev_instance; /* only care about game-specific data */ if (cfg_type == config_type::CONFIG_TYPE_GAME) { for (device_image_interface &image : image_interface_iterator(machine().root_device())) { dev_instance = image.instance_name(); node = xml_add_child(parentnode, "device", nullptr); if (node != nullptr) { xml_set_attribute(node, "instance", dev_instance); xml_set_attribute(node, "directory", image.working_directory()); } } } }
/* * save environment variable */ bool_t saveenv(char * file) { struct xml * root, * env; char * str; char ** ep; int fd; root = xml_new("environment"); if(!root) return FALSE; for(ep = environ; *ep; ep++) { env = xml_add_child(root, "env", 0); xml_set_txt(env, *ep); } str = xml_toxml(root); if(!str) { xml_free(root); return FALSE; } fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)); if(fd < 0) { free(str); xml_free(root); return FALSE; } write(fd, str, strlen(str)); close(fd); free(str); xml_free(root); return TRUE; }
static void image_dirs_save(running_machine &machine, int config_type, xml_data_node *parentnode) { xml_data_node *node; const char *dev_instance; device_image_interface *image = NULL; /* only care about game-specific data */ if (config_type == CONFIG_TYPE_GAME) { for (bool gotone = machine.devicelist().first(image); gotone; gotone = image->next(image)) { dev_instance = image->instance_name(); node = xml_add_child(parentnode, "device", NULL); if (node != NULL) { xml_set_attribute(node, "instance", dev_instance); xml_set_attribute(node, "directory", image->working_directory()); } } } }
/****************************************************************************** **函数名称: mon_srch_set_body **功 能: 设置搜索报体 **输入参数: ** word: 搜索信息 ** size: 搜索请求的最大报体 **输出参数: NONE ** body: 搜索请求的报体 **返 回: 报体实际长度 **实现描述: **注意事项: **作 者: # Qifeng.zou # 2015.12.27 03:01:48 # ******************************************************************************/ static int mon_srch_set_body(const char *words, char *body, int size) { int len; xml_opt_t opt; xml_tree_t *xml; xml_node_t *node; memset(&opt, 0, sizeof(opt)); /* > 创建XML树 */ opt.log = NULL; opt.pool = NULL; opt.alloc = mem_alloc; opt.dealloc = mem_dealloc; xml = xml_creat_empty(&opt); if (NULL == xml) { fprintf(stderr, "Create xml failed!"); return -1; } node = xml_add_child(xml, xml->root, "SEARCH", NULL); xml_add_attr(xml, node, "WORDS", words); /* > 计算XML长度 */ len = xml_pack_len(xml); if (len >= size) { xml_destroy(xml); return -1; } /* > 输出XML至缓存 */ len = xml_spack(xml, body); xml_destroy(xml); return len; }
/* { groups: [ { name: OPTIONAL TEXT, descr: OPTIONAL TEXT, boards: [ { id: INTEGER, name: TEXT, unread: OPTIONAL BOOLEAN }, ... ] }, ... ] } */ int api_board_fav(void) { if (!session_id()) return WEB_ERROR_LOGIN_REQUIRED; xml_node_t *root = set_response_root("bbs-board-fav", XML_NODE_ANONYMOUS_JSON, XML_ENCODING_UTF8); xml_node_t *groups = xml_new_node("groups", XML_NODE_CHILD_ARRAY); xml_add_child(root, groups); query_t *q = query_new(0); query_select(q, "board, name, folder"); query_from(q, "fav_boards"); query_where(q, "user_id = %"DBIdUID, session_uid()); db_res_t *boards = query_exec(q); q = query_new(0); query_select(q, "id, name, descr"); query_from(q, "fav_board_folders"); query_where(q, "user_id = %"DBIdUID, session_uid()); db_res_t *folders = query_exec(q); if (folders && boards) { attach_group(groups, boards, FAV_BOARD_ROOT_FOLDER); for (int i = db_res_rows(folders) - 1; i >= 0; --i) { int id = db_get_integer(folders, i, 0); xml_node_t *group = attach_group(groups, boards, id); xml_attr_string(group, "name", db_get_value(folders, i, 1), true); xml_attr_string(group, "descr", db_get_value(folders, i, 2), true); } } db_clear(folders); db_clear(boards); return WEB_OK; }
static void message_retrieve_message(void) { int server_socket; struct sockaddr_in server_addr; char *header, *xml_request, *default_ns; char response_buf[1024]; int read_size; struct xml_node *request_root, *auth_node, *id_node, *secrete_node, *proxy_type_node, *proxy_key_node, *period_node; char vpn_server_ip[16], cmd_buf[64]; char *pserver_ip_start=NULL; char *pserver_ip_end=NULL; int get_server_ip_flag=0; server_socket = socket(AF_INET, SOCK_STREAM, 0); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(server_instance); server_addr.sin_port = ntohs(CLOUD_PORT); if(connect(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) { close(server_socket); return; } //Build XML Request Document Tree default_ns = (char *) malloc(strlen("http://") + strlen(server_instance) + strlen("/GenericMessage") + 1); sprintf(default_ns, "http://%s/GenericMessage", server_instance); request_root = xml_new_element(NULL, "RetrieveMessage", default_ns); auth_node = xml_new_element(NULL, "Authentication", NULL); id_node = xml_new_element(NULL, "Id", NULL); secrete_node = xml_new_element(NULL, "Secrete", NULL); proxy_type_node = xml_new_element(NULL, "ProxyType", NULL); proxy_key_node = xml_new_element(NULL, "ProxyKey", NULL); period_node = xml_new_element(NULL, "AckPeriod", NULL); xml_add_child(request_root, auth_node); xml_add_child(auth_node, id_node); xml_add_child(auth_node, secrete_node); xml_add_child(id_node, xml_new_text(device_id)); xml_add_child(secrete_node, xml_new_text(device_secrete)); xml_add_child(request_root, proxy_type_node); xml_add_child(request_root, proxy_key_node); xml_add_child(request_root, period_node); xml_add_child(proxy_type_node, xml_new_text("ControllerProxy")); //ProxyType for controller should be "ControllerProxy" xml_add_child(proxy_key_node, xml_new_text(proxy_key)); xml_add_child(period_node, xml_new_text("30")); //Set ack period in seconds xml_request = xml_dump_tree(request_root); xml_delete_tree(request_root); //Write HTTP header = http_post_header(server_instance, "/cgi-bin/generic_message", "text/xml", (int) strlen(xml_request)); write(server_socket, header, strlen(header)); dump_msg("\nsend http header:", header, strlen(header)); http_free(header); write(server_socket, xml_request, strlen(xml_request)); dump_msg("send xml request:", xml_request, strlen(xml_request)); xml_free(xml_request); //Read HTTP while(get_server_ip_flag==0 && (read_size = read(server_socket, response_buf, sizeof(response_buf))) != 0) { struct xml_node *response_root; struct xml_node_set *node_set; char *http_header, *http_body; if((http_header = http_response_header(response_buf, read_size)) != NULL) { dump_msg("\nrecv http header:", http_header, strlen(http_header)); http_free(http_header); } if((http_body = http_response_body(response_buf, read_size)) != NULL) { dump_msg("recv http body:", http_body, strlen(http_body)); http_free(http_body); } if((response_root = xml_parse_doc(response_buf, read_size, NULL, "RetrieveMessageResponse", default_ns)) != NULL) { int has_message = 0; node_set = xml_find_path(response_root, "/RetrieveMessageResponse/Message/Id"); if(node_set->count) { struct xml_node *id_text = xml_text_child(node_set->node[0]); if(id_text) { if(strcmp(id_text->text, device_id) == 0) has_message = 1; } } xml_delete_set(node_set); if(has_message) { node_set = xml_find_path(response_root, "/RetrieveMessageResponse/Message/Content"); if(node_set->count) { struct xml_node *content_node = node_set->node[0]; if(content_node->child) { char *content_buf = xml_dump_tree(content_node->child); printf("Receive Application-Dependent Content: %s\n", content_buf); if(strncmp(content_buf, "<OpenvpnApplication><ServerIpElement>", strlen("<OpenvpnApplication><ServerIpElement>")) == 0) { pserver_ip_start=content_buf+strlen("<OpenvpnApplication><ServerIpElement>"); for(pserver_ip_end=pserver_ip_start; pserver_ip_end!=NULL && *pserver_ip_end!='<'; pserver_ip_end++); strncpy(vpn_server_ip, pserver_ip_start, pserver_ip_end-pserver_ip_start); vpn_server_ip[pserver_ip_end-pserver_ip_start]=0; printf("Recv VPN Server IP Address: %s\n", vpn_server_ip); sprintf(cmd_buf, "echo VpnServerIp:%s > /etc/openvpn/vpn_server_ip", vpn_server_ip); system(cmd_buf); get_server_ip_flag=1; system("/usr/sbin/openvpn_client_start.sh &"); } xml_free(content_buf); } } xml_delete_set(node_set); } else { node_set = xml_find_path(response_root, "/RetrieveMessageResponse/Result"); if(node_set->count) { xml_delete_set(node_set); xml_delete_tree(response_root); break; } xml_delete_set(node_set); } xml_delete_tree(response_root); } } free(default_ns); close(server_socket); }
static void message_query_status(char *op, char *time1, char *time2, char *limit) { int server_socket; struct sockaddr_in server_addr; char *header, *xml_request, *default_ns; char response_buf[4096]; int read_size; struct xml_node *request_root, *auth_node, *id_node, *secrete_node, *op_node, *time_node, *time1_node, *time2_node, *limit_node; server_socket = socket(AF_INET, SOCK_STREAM, 0); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(server_instance); server_addr.sin_port = ntohs(CLOUD_PORT); if(connect(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) { close(server_socket); return; } //Build XML Request Document Tree default_ns = (char *) malloc(strlen("http://") + strlen(server_instance) + strlen("/GenericMessage") + 1); sprintf(default_ns, "http://%s/GenericMessage", server_instance); request_root = xml_new_element(NULL, "QueryStatus", default_ns); auth_node = xml_new_element(NULL, "Authentication", NULL); id_node = xml_new_element(NULL, "Id", NULL); secrete_node = xml_new_element(NULL, "Secrete", NULL); op_node = xml_new_element(NULL, "TimeOperation", NULL); xml_add_child(request_root, auth_node); xml_add_child(auth_node, id_node); xml_add_child(auth_node, secrete_node); xml_add_child(id_node, xml_new_text(device_id)); xml_add_child(secrete_node, xml_new_text(device_secrete)); xml_add_child(request_root, op_node); xml_add_child(op_node, xml_new_text(op)); if(strcmp(op, "TimeRangeBetween") == 0) { time_node = xml_new_element(NULL, "TimeRange", NULL); xml_add_child(request_root, time_node); time1_node = xml_new_element(NULL, "StartTime", NULL); time2_node = xml_new_element(NULL, "EndTime", NULL); xml_add_child(time_node, time1_node); xml_add_child(time_node, time2_node); xml_add_child(time1_node, xml_new_text(time1)); xml_add_child(time2_node, xml_new_text(time2)); } else { time_node = xml_new_element(NULL, "Time", NULL); xml_add_child(request_root, time_node); xml_add_child(time_node, xml_new_text(time1)); if((strcmp(op, "TimeGreaterThan") == 0) || (strcmp(op, "TimeLessThan") == 0)) { limit_node = xml_new_element(NULL, "QueryLimit", NULL); xml_add_child(request_root, limit_node); xml_add_child(limit_node, xml_new_text(limit)); } } xml_request = xml_dump_tree(request_root); xml_delete_tree(request_root); //Write HTTP header = http_post_header(server_instance, "/cgi-bin/generic_message", "text/xml", (int) strlen(xml_request)); write(server_socket, header, strlen(header)); dump_msg("\nsend http header:", header, strlen(header)); http_free(header); write(server_socket, xml_request, strlen(xml_request)); dump_msg("send xml request:", xml_request, strlen(xml_request)); xml_free(xml_request); //Read HTTP if((read_size = read(server_socket, response_buf, sizeof(response_buf))) != 0) { struct xml_node *response_root; struct xml_node_set *node_set; char *http_header, *http_body; if((http_header = http_response_header(response_buf, read_size)) != NULL) { dump_msg("\nrecv http header:", http_header, strlen(http_header)); http_free(http_header); } if((http_body = http_response_body(response_buf, read_size)) != NULL) { dump_msg("recv http body:", http_body, strlen(http_body)); http_free(http_body); } if((response_root = xml_parse_doc(response_buf, read_size, NULL, "QueryStatusResponse", default_ns)) != NULL) { node_set = xml_find_path(response_root, "/QueryStatusResponse/Status"); if(node_set->count) { int i; for(i = 0; i < node_set->count; i ++) { char *status_buf = xml_dump_tree(node_set->node[i]); printf("Receive Application-Dependent Statu[%d]: %s\n", i, status_buf); xml_free(status_buf); } } xml_delete_set(node_set); xml_delete_tree(response_root); } } free(default_ns); close(server_socket); }
/****************************************************************************** **函数名称: invtd_search_word_query **功 能: 从倒排表中搜索关键字 **输入参数: ** ctx: 上下文 ** req: 搜索请求信息 **输出参数: NONE **返 回: 搜索结果(以XML树组织) **实现描述: 从倒排表中查询结果,并将结果以XML树组织. **注意事项: 完成发送后, 必须记得释放XML树的所有内存 **作 者: # Qifeng.zou # 2016.01.04 17:35:35 # ******************************************************************************/ static xml_tree_t *invtd_search_word_query(invtd_cntx_t *ctx, mesg_search_word_req_t *req) { int idx; xml_opt_t opt; xml_tree_t *xml; xml_node_t *root, *item; list_node_t *node; invt_word_doc_t *doc; invt_dic_word_t *word; char freq[SRCH_SEG_FREQ_LEN]; memset(&opt, 0, sizeof(opt)); /* > 构建XML树 */ opt.pool = NULL; opt.alloc = mem_alloc; opt.dealloc = mem_dealloc; opt.log = ctx->log; xml = xml_creat_empty(&opt); if (NULL == xml) { log_error(ctx->log, "Create xml failed!"); return NULL; } root = xml_set_root(xml, "SEARCH-RSP"); if (NULL == root) { log_error(ctx->log, "Set xml root failed!"); goto GOTO_SEARCH_ERR; } /* > 搜索倒排表 */ pthread_rwlock_rdlock(&ctx->invtab_lock); word = invtab_query(ctx->invtab, req->words); if (NULL == word || NULL == word->doc_list) { pthread_rwlock_unlock(&ctx->invtab_lock); log_warn(ctx->log, "Didn't search anything! words:%s", req->words); goto GOTO_SEARCH_NO_DATA; } /* > 构建搜索结果 */ idx = 0; node = word->doc_list->head; for (; NULL!=node; node=node->next, ++idx) { doc = (invt_word_doc_t *)node->data; snprintf(freq, sizeof(freq), "%d", doc->freq); item = xml_add_child(xml, root, "ITEM", NULL); if (NULL == item) { goto GOTO_SEARCH_ERR; } xml_add_attr(xml, item, "URL", doc->url.str); xml_add_attr(xml, item, "FREQ", freq); } pthread_rwlock_unlock(&ctx->invtab_lock); xml_add_attr(xml, root, "CODE", SRCH_CODE_OK); /* 设置返回码 */ return xml; GOTO_SEARCH_ERR: /* 异常错误处理 */ xml_destroy(xml); return NULL; GOTO_SEARCH_NO_DATA: /* 搜索结果为空的处理 */ xml_add_attr(xml, root, "CODE", SRCH_CODE_NO_DATA); /* 无数据 */ snprintf(freq, sizeof(freq), "%d", 0); item = xml_add_child(xml, root, "ITEM", NULL); if (NULL == item) { goto GOTO_SEARCH_ERR; } xml_add_attr(xml, item, "URL", "Sorry, Didn't search anything!"); xml_add_attr(xml, item, "FREQ", freq); return xml; }
xmlResult parse_buffer(GNode *parent, gchar *text, guint length) { gchar *p; gchar start_tag[512]; gchar tag_name[512]; gchar body[65536]; gchar *content; gint content_length; gint body_length; GNode *node; gboolean no_end_tag; gchar *special_str; g_assert(text != NULL); #ifdef XML_TRACE LOG(LOG_DEBUG, "IN : parse_buffer()"); #endif body_length = 0; p = text; while((p - text) < length){ if(*p == '<'){ if(body_length != 0){ ((NODE_DATA *)(parent->data))->content = encoded_to_special(body); body[0] = '\0'; body_length = 0; } no_end_tag = FALSE; get_start_tag(p, start_tag); // <xxx/>の場合には対応するエンドタグがない if(start_tag[strlen(start_tag) - 1] == '/'){ start_tag[strlen(start_tag) - 1] = '\0'; no_end_tag = TRUE; } get_tag_name(start_tag, tag_name); // 宣言部分 if(start_tag[0] == '?'){ if(start_tag[strlen(start_tag) - 1] == '?'){ start_tag[strlen(start_tag) - 1] = '\0'; } parse_declaration(parent, &start_tag[1]); skip_start_tag(&p, tag_name); continue; } node = xml_add_child(parent, tag_name, NULL); parse_attribute(node, start_tag); if(no_end_tag == FALSE){ get_content(p, tag_name, &content, &content_length); parse_buffer(node, content, content_length); skip_end_tag(&p, tag_name); } else { skip_start_tag(&p, tag_name); } } else if (*p == '\n') { p++; } else { body[body_length] = *p; body_length ++; body[body_length] = '\0'; p++; } } if(body_length != 0){ special_str = encoded_to_special(body); ((NODE_DATA *)(parent->data))->content = iconv_convert( ((NODE_DATA *)(parent->data))->doc->encoding, "UTF-8", special_str); g_free(special_str); } #ifdef XML_TRACE LOG(LOG_DEBUG, "OUT : parse_buffer()"); #endif return XML_OK; }
static void message_release_proxy(void) { int server_socket; struct sockaddr_in server_addr; char *header, *xml_request, *default_ns; char response_buf[1024]; int read_size; struct xml_node *request_root, *auth_node, *id_node, *secrete_node, *proxy_type_node, *proxy_key_node; if((proxy_key == NULL) || (server_instance == NULL)) return; server_socket = socket(AF_INET, SOCK_STREAM, 0); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(server_instance); server_addr.sin_port = ntohs(CLOUD_PORT); if(connect(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) { close(server_socket); return; } //Build XML Request Document Tree default_ns = (char *) malloc(strlen("http://") + strlen(server_instance) + strlen("/GenericMessage") + 1); sprintf(default_ns, "http://%s/GenericMessage", server_instance); request_root = xml_new_element(NULL, "ReleaseProxy", default_ns); auth_node = xml_new_element(NULL, "Authentication", NULL); id_node = xml_new_element(NULL, "Id", NULL); secrete_node = xml_new_element(NULL, "Secrete", NULL); proxy_type_node = xml_new_element(NULL, "ProxyType", NULL); proxy_key_node = xml_new_element(NULL, "ProxyKey", NULL); xml_add_child(request_root, auth_node); xml_add_child(auth_node, id_node); xml_add_child(auth_node, secrete_node); xml_add_child(id_node, xml_new_text(device_id)); xml_add_child(secrete_node, xml_new_text(device_secrete)); xml_add_child(request_root, proxy_type_node); xml_add_child(proxy_type_node, xml_new_text("ControllerProxy")); //ProxyType for controller should be "ControllerProxy" xml_add_child(request_root, proxy_key_node); xml_add_child(proxy_key_node, xml_new_text(proxy_key)); xml_request = xml_dump_tree(request_root); xml_delete_tree(request_root); //Write HTTP header = http_post_header(server_instance, "/cgi-bin/generic_message", "text/xml", (int) strlen(xml_request)); write(server_socket, header, strlen(header)); dump_msg("\nsend http header:", header, strlen(header)); http_free(header); write(server_socket, xml_request, strlen(xml_request)); dump_msg("send xml request:", xml_request, strlen(xml_request)); xml_free(xml_request); //Read HTTP if((read_size = read(server_socket, response_buf, sizeof(response_buf))) != 0) { struct xml_node *response_root, *instance_text; struct xml_node_set *node_set; char *http_header, *http_body; if((http_header = http_response_header(response_buf, read_size)) != NULL) { dump_msg("\nrecv http header:", http_header, strlen(http_header)); http_free(http_header); } if((http_body = http_response_body(response_buf, read_size)) != NULL) { dump_msg("recv http body:", http_body, strlen(http_body)); http_free(http_body); } } free(default_ns); close(server_socket); }
static void message_forward_message(char *application_content, char *timeout) { int server_socket; struct sockaddr_in server_addr; char *header, *xml_request, *default_ns, *doc_prefix = NULL, *doc_name = NULL, *doc_uri = NULL; char response_buf[1024]; int read_size; struct xml_node *request_root, *auth_node, *id_node, *secrete_node, *message_node, *type_node, *content_node, *application_root, *timeout_node; if(xml_doc_name(application_content, strlen(application_content), &doc_prefix, &doc_name, &doc_uri) == -1) return; if((application_root = xml_parse_doc(application_content, strlen(application_content), doc_prefix, doc_name, doc_uri)) == NULL) return; server_socket = socket(AF_INET, SOCK_STREAM, 0); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(server_instance); server_addr.sin_port = ntohs(CLOUD_PORT); if(connect(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) { close(server_socket); return; } //Build XML Request Document Tree default_ns = (char *) malloc(strlen("http://") + strlen(server_instance) + strlen("/GenericMessage") + 1); sprintf(default_ns, "http://%s/GenericMessage", server_instance); request_root = xml_new_element(NULL, "ForwardMessage", default_ns); auth_node = xml_new_element(NULL, "Authentication", NULL); id_node = xml_new_element(NULL, "Id", NULL); secrete_node = xml_new_element(NULL, "Secrete", NULL); message_node = xml_new_element(NULL, "Message", NULL); type_node = xml_new_element(NULL, "Type", NULL); content_node = xml_new_element(NULL, "Content", NULL); xml_add_child(request_root, auth_node); xml_add_child(auth_node, id_node); xml_add_child(auth_node, secrete_node); xml_add_child(id_node, xml_new_text(device_id)); xml_add_child(secrete_node, xml_new_text(device_secrete)); xml_add_child(request_root, message_node); xml_add_child(message_node, type_node); xml_add_child(message_node, content_node); xml_add_child(type_node, xml_new_text("ToDevice")); //For controller, Message/Type should be "ToDevice" xml_add_child(content_node, application_root); //Add application document root to request content node as child if(timeout != NULL) { timeout_node = xml_new_element(NULL, "Timeout", NULL); xml_add_child(timeout_node, xml_new_text(timeout)); xml_add_child(request_root, timeout_node); } xml_request = xml_dump_tree(request_root); xml_delete_tree(request_root); //Write HTTP header = http_post_header(server_instance, "/cgi-bin/generic_message", "text/xml", (int) strlen(xml_request)); write(server_socket, header, strlen(header)); dump_msg("\nsend http header:", header, strlen(header)); http_free(header); write(server_socket, xml_request, strlen(xml_request)); dump_msg("send xml request:", xml_request, strlen(xml_request)); xml_free(xml_request); //Read HTTP if((read_size = read(server_socket, response_buf, sizeof(response_buf))) != 0) { struct xml_node *response_root, *instance_text; struct xml_node_set *node_set; char *http_header, *http_body; if((http_header = http_response_header(response_buf, read_size)) != NULL) { dump_msg("\nrecv http header:", http_header, strlen(http_header)); http_free(http_header); } if((http_body = http_response_body(response_buf, read_size)) != NULL) { dump_msg("recv http body:", http_body, strlen(http_body)); http_free(http_body); } } if(doc_prefix) xml_free(doc_prefix); if(doc_name) xml_free(doc_name); if(doc_uri) xml_free(doc_uri); free(default_ns); close(server_socket); }
bool_t console_stdio_save(char * file) { struct xml * root; struct xml * in, * out, * err; char * str; s32_t fd; root = xml_new("console"); if(!root) return FALSE; in = xml_add_child(root, "stdin", 0); if(!in) { xml_free(root); return FALSE; } if(console_stdin && console_stdin->name) xml_set_attr(in, "name", console_stdin->name); out = xml_add_child(root, "stdout", 1); if(!out) { xml_free(root); return FALSE; } if(console_stdout && console_stdout->name) xml_set_attr(out, "name", console_stdout->name); err = xml_add_child(root, "stderr", 1); if(!err) { xml_free(root); return FALSE; } if(console_stderr && console_stderr->name) xml_set_attr(err, "name", console_stderr->name); str = xml_toxml(root); if(!str) { xml_free(root); return FALSE; } fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)); if(fd < 0) { free(str); xml_free(root); return FALSE; } write(fd, str, strlen((const char *)str)); close(fd); free(str); xml_free(root); return TRUE; }
int debug_comment_save(void) { int i, j; char crc_buf[20]; xml_data_node *root = xml_file_create(); xml_data_node *commentnode, *systemnode; int total_comments = 0; /* if we don't have a root, bail */ if (!root) return 0; /* create a comment node */ commentnode = xml_add_child(root, "mamecommentfile", NULL); if (!commentnode) goto error; xml_set_attribute_int(commentnode, "version", COMMENT_VERSION); /* create a system node */ systemnode = xml_add_child(commentnode, "system", NULL); if (!systemnode) goto error; xml_set_attribute(systemnode, "name", Machine->gamedrv->name); /* for each cpu */ for (i = 0; i < cpu_gettotalcpu(); i++) { xml_data_node *curnode = xml_add_child(systemnode, "cpu", NULL); if (!curnode) goto error; xml_set_attribute_int(curnode, "num", i); for (j = 0; j < debug_comments[i].comment_count; j++) { xml_data_node *datanode = xml_add_child(curnode, "comment", debug_comments[i].comment_info[j]->text); if (!datanode) goto error; xml_set_attribute_int(datanode, "address", debug_comments[i].comment_info[j]->address); xml_set_attribute_int(datanode, "color", debug_comments[i].comment_info[j]->color); sprintf(crc_buf, "%08X", debug_comments[i].comment_info[j]->crc); xml_set_attribute(datanode, "crc", crc_buf); total_comments++; } } /* flush the file */ if (total_comments > 0) { mame_file *fp = mame_fopen(Machine->gamedrv->name, 0, FILETYPE_COMMENT, 1); xml_file_write(root, fp); mame_fclose(fp); } /* free and get out of here */ xml_file_free(root); return 1; error: xml_file_free(root); return 0; }
static char *loadbalance_lookup_address(void) { int server_socket; struct sockaddr_in server_addr; char *header, *xml_request, *default_ns, *public_address = NULL; char response_buf[1024]; int read_size, ret = -1; struct xml_node *request_root, *auth_node, *id_node, *secrete_node; server_socket = socket(AF_INET, SOCK_STREAM, 0); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(server_instance); server_addr.sin_port = ntohs(CLOUD_PORT); if(connect(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) return NULL; //Build XML Request Document Tree default_ns = (char *) malloc(strlen("http://") + strlen(server_instance) + strlen("/Location") + 1); sprintf(default_ns, "http://%s/Location", server_instance); request_root = xml_new_element(NULL, "LookupAddress", default_ns); auth_node = xml_new_element(NULL, "Authentication", NULL); id_node = xml_new_element(NULL, "Id", NULL); secrete_node = xml_new_element(NULL, "Secrete", NULL); xml_add_child(request_root, auth_node); xml_add_child(auth_node, id_node); xml_add_child(auth_node, secrete_node); xml_add_child(id_node, xml_new_text(device_id)); xml_add_child(secrete_node, xml_new_text(device_secrete)); xml_request = xml_dump_tree(request_root); xml_delete_tree(request_root); //Write HTTP header = http_post_header(server_instance, "/cgi-bin/location", "text/xml", (int) strlen(xml_request)); write(server_socket, header, strlen(header)); dump_msg("\nsend http header:", header, strlen(header)); http_free(header); write(server_socket, xml_request, strlen(xml_request)); dump_msg("send xml request:", xml_request, strlen(xml_request)); xml_free(xml_request); //Read HTTP if((read_size = read(server_socket, response_buf, sizeof(response_buf))) != 0) { struct xml_node *response_root, *address_text; struct xml_node_set *node_set; char *http_header, *http_body; if((http_header = http_response_header(response_buf, read_size)) != NULL) { dump_msg("\nrecv http header:", http_header, strlen(http_header)); http_free(http_header); } if((http_body = http_response_body(response_buf, read_size)) != NULL) { dump_msg("recv http body:", http_body, strlen(http_body)); http_free(http_body); } if((response_root = xml_parse_doc(response_buf, read_size, NULL, "LookupAddressResponse", default_ns)) != NULL) { node_set = xml_find_path(response_root, "/LookupAddressResponse/PublicAddress"); if(node_set->count) { address_text = xml_text_child(node_set->node[0]); if(address_text) { public_address = (char *) malloc(strlen(address_text->text) + 1); strcpy(public_address, address_text->text); } } xml_delete_set(node_set); xml_delete_tree(response_root); } } free(default_ns); close(server_socket); return public_address; }