xml::~xml(void) { clear(); delete root_; if (xml_) acl_xml_free(xml_); delete buf_; if (m_pTokenTree) acl_token_tree_destroy(m_pTokenTree); if (iter_) acl_myfree(iter_); }
int main(int argc, char* argv[]) { char buf[8192], filepath[256]; int ret, n; ACL_VSTREAM* fp; ACL_XML* xml; struct timeval begin, end; double spent; int ch, use_slice = 0, cache_count = 1000; filepath[0] = 0; while ((ch = getopt(argc, argv, "hmc:f:")) > 0) { switch (ch) { case 'h': usage(argv[0]); return 0; case 'm': use_slice = 1; break; case 'c': cache_count = atoi(optarg); if (cache_count <= 0) cache_count = 1000; break; case 'f': snprintf(filepath, sizeof(filepath), "%s", optarg); break; default: break; } } if (use_slice) acl_mem_slice_init(8, 1024, 100000, ACL_SLICE_FLAG_GC2 | ACL_SLICE_FLAG_RTGC_OFF | ACL_SLICE_FLAG_LP64_ALIGN); if (filepath[0] == 0) { usage(argv[0]); return 1; } xml = acl_xml_alloc(); if (cache_count > 0) acl_xml_cache(xml, cache_count); fp = acl_vstream_fopen(filepath, O_RDONLY, 0600, 8192); if (fp == NULL) { printf("open file %s error %s\r\n", filepath, acl_last_serror()); acl_xml_free(xml); return 1; } gettimeofday(&begin, NULL); n = 0; ACL_METER_TIME("------begin------"); while (1) { ret = acl_vstream_fgets(fp, buf, sizeof(buf) - 1); if (ret == ACL_VSTREAM_EOF) break; buf[ret] = 0; acl_xml_parse(xml, buf); if (++n % 10000 == 0) { printf("line: %d\r\n", n); ACL_METER_TIME("-------ok------"); } if (n % cache_count == 0) { printf("reset xml, line: %d\r\n", n); acl_xml_reset(xml); } } gettimeofday(&end, NULL); spent = stamp_sub(&end, &begin); printf("\r\ntotal spent: %0.2f ms\r\n", spent); acl_xml_free(xml); acl_vstream_fclose(fp); return 0; }
bool CMsnContactManager::GetContacts(const CMsnTicket& ticket, acl::http_client& client) { // 创建请求体 acl::string request_body; if (BuildGetContactRequest(ticket, request_body) == false) return (false); ////////////////////////////////////////////////////////////////////////// // 向 MSN CONTACT 服务器发送请求数据 // 创建 HTTP 请求头 acl::http_header header; header.set_method(acl::HTTP_METHOD_POST); header.set_url(MSN_GET_CONTACT_POST_URL); header.set_host(MSN_CONTACT_SERVER); header.set_content_length(request_body.length()); header.set_content_type("text/xml; charset=utf-8"); header.set_keep_alive(false); header.add_entry("SOAPAction", MSN_GET_CONTACT_SOAP_ACTION); header.add_entry("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); header.add_entry("Accept", "*/*"); header.add_entry("Cache-Control", "no-cache"); // 现在ACL_CPP的压缩库仅支持HTTP传输中的GZIP方式 header.accept_gzip(true); // 发送 HTTP 请求头 acl::string header_buf; header.build_request(header_buf); // 记日志 logger_format("send: %s\r\n", header_buf.c_str()); if (client.get_ostream().write(header_buf) == -1) { logger_error("write http header error"); return (false); } // 发送 HTTP 请求体 // 记录发送体 logger_format("send: %s\r\n", request_body.c_str()); if (client.get_ostream().write(request_body) == -1) { logger_error("write http body error"); return (false); } ////////////////////////////////////////////////////////////////////////// // 从 MSN CONTACT 服务器读取响应数据 if (client.read_head() == false) { logger_error("read http respond head error"); return (false); } const HTTP_HDR_RES* hdr_res = client.get_respond_head(NULL); acl_assert(hdr_res); // 记录响应头 if (debug_fpout_) http_hdr_fprint(debug_fpout_, &hdr_res->hdr, "GetContacts read hdr: "); // 如果没有数据体,则认为失败 if (hdr_res->hdr.content_length == 0 || (hdr_res->hdr.content_length == -1 && !hdr_res->hdr.chunked && hdr_res->reply_status > 300 && hdr_res->reply_status < 400)) { logger_error("http respond no body"); http_hdr_print(&hdr_res->hdr, "error"); return (false); } /* 读书 HTTP 响应体 */ ACL_XML* body = acl_xml_alloc(); int ret; acl::string plain; acl::ofstream fp; fp.open_write("zip_list.text"); int len = 0; while(true) { // 读 HTTP 响应数据体 ret = client.read_body(plain); if (ret < 0) break; else if (client.body_finish()) break; fp.write(plain); acl_xml_update(body, plain.c_str()); } //acl_xml_dump(body, ACL_VSTREAM_OUT); // 分析联系人列表 if (ParseContacts(body) == false) { acl_xml_free(body); return (false); } acl_xml_free(body); return (true); }