/***************************************************************************** 函 数 名 : subdesc_create_hashtable 功能描述 : 根据doc中包括的有name的元素的数量创建hash表 输入参数 : doc sub文档 root 结构指针 输出参数 : 无 返 回 值 : ERR_SUCCESS成功 其他失败 调用函数 : 被调函数 : ============================================================================ 修改历史 : 1.日 期 : 2008年8月26日 修改内容 : 新生成函数 *****************************************************************************/ static int subdesc_create_hashtable(xmlDocPtr doc, SUB_ROOT * root) { xmlXPathContextPtr xpathCtx; int ret = ERROR_SUCCESS; /* Create xpath evaluation context */ xpathCtx = xmlXPathNewContext(doc); if(xpathCtx == NULL) { return ERROR_SYSTEM; } xmlXPathObjectPtr xpathObj; /* Evaluate xpath expression */ xpathObj = xmlXPathEvalExpression((unsigned char*)"//*[@name]", xpathCtx); if(xpathObj != NULL) { if(NULL == xpathObj->nodesetval || xpathObj->nodesetval->nodeNr ==0 || 0 == hcreate_r(xpathObj->nodesetval->nodeNr, (struct hsearch_data *)(root->_hashtable))) { ret = ERROR_SYSTEM; } xmlXPathFreeObject(xpathObj); } else { ret = ERROR_SYSTEM; } xmlXPathFreeContext(xpathCtx); return ret; }
static xmlSecXPathDataPtr xmlSecXPathDataCreate(xmlSecXPathDataType type) { xmlSecXPathDataPtr data; data = (xmlSecXPathDataPtr) xmlMalloc(sizeof(xmlSecXPathData)); if(data == NULL) { xmlSecMallocError(sizeof(xmlSecXPathData), NULL); return(NULL); } memset(data, 0, sizeof(xmlSecXPathData)); data->type = type; data->nodeSetType = xmlSecNodeSetTree; /* create xpath or xpointer context */ switch(data->type) { case xmlSecXPathDataTypeXPath: case xmlSecXPathDataTypeXPath2: data->ctx = xmlXPathNewContext(NULL); /* we'll set doc in the context later */ if(data->ctx == NULL) { xmlSecXmlError("xmlXPathNewContext", NULL); xmlSecXPathDataDestroy(data); return(NULL); } break; case xmlSecXPathDataTypeXPointer: data->ctx = xmlXPtrNewContext(NULL, NULL, NULL); /* we'll set doc in the context later */ if(data->ctx == NULL) { xmlSecXmlError("xmlXPtrNewContext", NULL); xmlSecXPathDataDestroy(data); return(NULL); } break; } return(data); }
xmlChar* get_java_home(char *current_path) { char path_to_bbwp_properties[PATH_MAX+1]; strncpy(path_to_bbwp_properties, current_path, PATH_MAX+1); strncat(path_to_bbwp_properties, "/bin/bbwp.properties", PATH_MAX+1); xmlDocPtr doc = xmlParseFile(path_to_bbwp_properties); if (doc == NULL) { printf("error: could not parse file %s\n", path_to_bbwp_properties); } xmlXPathContextPtr context = xmlXPathNewContext(doc); xmlChar * xpath = "/wcp/java"; xmlNodeSetPtr nodeset; xmlXPathObjectPtr result = xmlXPathEvalExpression(xpath, context); xmlChar *java_home; if(xmlXPathNodeSetIsEmpty(result->nodesetval)){ printf("No result\n"); } else { nodeset = result->nodesetval; java_home = xmlNodeListGetString(doc, nodeset->nodeTab[0]->xmlChildrenNode, 1); } xmlFreeDoc(doc); xmlXPathFreeContext(context); xmlXPathFreeObject(result); /* * Free the global variables that may * have been allocated by the parser. */ xmlCleanupParser(); return java_home; }
static int parallelsDiskDescParseNode(xmlDocPtr xml, xmlNodePtr root, virStorageVolDefPtr def) { xmlXPathContextPtr ctxt = NULL; int ret = -1; if (STRNEQ((const char *)root->name, "Parallels_disk_image")) { virReportError(VIR_ERR_XML_ERROR, "%s", _("unknown root element for storage pool")); goto cleanup; } ctxt = xmlXPathNewContext(xml); if (ctxt == NULL) { virReportOOMError(); goto cleanup; } ctxt->node = root; if (virXPathULongLong("string(./Disk_Parameters/Disk_size)", ctxt, &def->target.capacity) < 0) { virReportError(VIR_ERR_XML_ERROR, "%s", _("failed to get disk size from " "the disk descriptor xml")); goto cleanup; } def->target.capacity <<= 9; def->target.allocation = def->target.capacity; ret = 0; cleanup: xmlXPathFreeContext(ctxt); return ret; }
SEXP RS_XML_xpathNodeEval(SEXP s_node, SEXP path, SEXP namespaces, SEXP fun) { xmlXPathContextPtr ctxt = NULL; xmlXPathObjectPtr result; SEXP ans = NULL_USER_OBJECT; xmlDocPtr doc; if(TYPEOF(s_node) != EXTPTRSXP || R_ExternalPtrTag(s_node) != Rf_install("XMLInternalNode")) { PROBLEM "xpathEval must be given an internal XML document object, 'XMLInternalNode'" ERROR; } ctxt = xmlXPathNewContext(doc); if(GET_LENGTH(namespaces)) { ctxt->namespaces = R_namespaceArray(namespaces, ctxt); /* xmlCopyNamespaceList(doc); */ ctxt->nsNr = GET_LENGTH(namespaces); } result = xmlXPathEvalExpression(CHAR_TO_XMLCHAR(CHAR_DEREF(STRING_ELT(path, 0))), ctxt); if(result) ans = convertXPathObjectToR(result, fun); xmlXPathFreeObject(result); xmlXPathFreeContext(ctxt); if(!result) { PROBLEM "error evaluating xpath expression %s", CHAR_DEREF(STRING_ELT(path, 0)) ERROR; } return(ans); }
void new_child_node (const char* filename, const xmlChar* xpathExpr, const xmlChar *name, const xmlChar *content) { xmlDocPtr doc; xmlXPathContextPtr xpathCtx; xmlXPathObjectPtr xpathObj; xmlKeepBlanksDefault(0); /* Load XML document */ doc = xmlParseFile(filename); if (doc == NULL) { fprintf(stderr, "Error: unable to parse file \"%s\"\n", filename); return; } /* Create xpath evaluation context */ xpathCtx = xmlXPathNewContext(doc); if(xpathCtx == NULL) { fprintf(stderr,"Error: unable to create new XPath context\n"); xmlFreeDoc(doc); return; } /* Evaluate xpath expression */ xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx); if(xpathObj == NULL) { fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpathExpr); xmlXPathFreeContext(xpathCtx); xmlFreeDoc(doc); return; } xmlNewTextChild (xpathObj->nodesetval->nodeTab[0], NULL, name, content); xmlXPathFreeObject(xpathObj); xmlXPathFreeContext(xpathCtx); xmlSaveFormatFile (filename, doc, 1); xmlFreeDoc(doc); }
/** * flickcurl_auth_checkToken: * @fc: flickcurl context * @token: token string * * Get the credentials attached to an authentication token. * * Implements flickr.auth.checkToken (0.9) * Must be signed. * * FIXME: Cannot confirm this works, get intermittent results. * * Return value: permissions string or NULL on failure **/ char* flickcurl_auth_checkToken(flickcurl* fc, const char* token) { const char * parameters[6][2]; int count = 0; char *perms = NULL; xmlDocPtr doc = NULL; xmlXPathContextPtr xpathCtx = NULL; if(!token) return NULL; parameters[count][0] = "auth_token"; parameters[count++][1] = (char*)token; parameters[count][0] = NULL; flickcurl_set_sign(fc); if(flickcurl_prepare(fc, "flickr.auth.checkToken", parameters, count)) goto tidy; doc = flickcurl_invoke(fc); if(!doc) goto tidy; xpathCtx = xmlXPathNewContext(doc); if(xpathCtx) { perms = flickcurl_xpath_eval(fc, xpathCtx, (const xmlChar*)"/rsp/auth/perms"); xmlXPathFreeContext(xpathCtx); } tidy: return perms; }
/** * flickcurl_people_getUploadStatus: * @fc: flickcurl context * * Returns information for the calling user related to photo uploads. * * Implements flickr.people.getUploadStatus (0.13) * * Return value: non-0 on failure **/ flickcurl_user_upload_status* flickcurl_people_getUploadStatus(flickcurl* fc) { const char* parameters[7][2]; int count=0; xmlDocPtr doc=NULL; xmlXPathContextPtr xpathCtx=NULL; flickcurl_user_upload_status* status=NULL; parameters[count][0] = NULL; if(flickcurl_prepare(fc, "flickr.people.getUploadStatus", parameters, count)) goto tidy; doc=flickcurl_invoke(fc); if(!doc) goto tidy; xpathCtx = xmlXPathNewContext(doc); if(!xpathCtx) { flickcurl_error(fc, "Failed to create XPath context for document"); fc->failed=1; goto tidy; } status=flickcurl_build_user_upload_status(fc, xpathCtx, (const xmlChar*)"/rsp/user/*"); tidy: if(xpathCtx) xmlXPathFreeContext(xpathCtx); if(fc->failed) status=NULL; return status; }
/** * flickcurl_people_getInfo: * @fc: flickcurl context * @user_id: user NSID * * Get information about a person * * Implements flickr.people.getInfo (0.6) * * NSID can be found by flickcurl_people_findByEmail() or * flickcurl_people_findByUsername(). * * Return value: #flickcurl_person object or NULL on failure **/ flickcurl_person* flickcurl_people_getInfo(flickcurl* fc, const char* user_id) { xmlDocPtr doc = NULL; xmlXPathContextPtr xpathCtx = NULL; flickcurl_person* person = NULL; flickcurl_init_params(fc, 0); flickcurl_add_param(fc, "user_id", user_id); flickcurl_end_params(fc); if(flickcurl_prepare(fc, "flickr.people.getInfo")) goto tidy; doc = flickcurl_invoke(fc); if(!doc) goto tidy; xpathCtx = xmlXPathNewContext(doc); if(!xpathCtx) { flickcurl_error(fc, "Failed to create XPath context for document"); fc->failed = 1; goto tidy; } person = flickcurl_build_person(fc, xpathCtx, (const xmlChar*)"/rsp/person"); tidy: if(xpathCtx) xmlXPathFreeContext(xpathCtx); if(fc->failed) person = NULL; return person; }
std::string OSConfigure::readonestring ( std::string path ) { xmlChar * xpath = ( xmlChar * ) path.c_str (); xmlChar *keyword; std::string retstring; xmlNodeSetPtr nodeset; xmlXPathContextPtr context; xmlXPathObjectPtr result; context = xmlXPathNewContext ( doc ); if ( context == NULL ) return ""; result = xmlXPathEvalExpression ( xpath, context ); xmlXPathFreeContext ( context ); if ( result == NULL ) return ""; if ( xmlXPathNodeSetIsEmpty ( result->nodesetval ) ) { return ""; } nodeset = result->nodesetval; keyword = xmlNodeListGetString ( doc, nodeset->nodeTab[ 0 ] ->xmlChildrenNode, 1 ); if ( keyword != NULL ) retstring = std::string ( ( char * ) keyword ); xmlFree ( keyword ); xmlXPathFreeObject ( result ); return retstring; }
// Evaluate an XPath expression and return matching Nodes. NodeList Document::findXPath(const std::string& path) const { std::lock_guard<std::mutex> lock(_lock); // Set up the XPath context xmlXPathContextPtr context = xmlXPathNewContext(_xmlDoc); if (context == NULL) { rConsoleError() << "ERROR: xml::findPath() failed to create XPath context " << "when searching for " << path << std::endl; throw XPathException("Failed to create XPath context"); } // Evaluate the expression const xmlChar* xpath = reinterpret_cast<const xmlChar*>(path.c_str()); xmlXPathObjectPtr result = xmlXPathEvalExpression(xpath, context); xmlXPathFreeContext(context); if (result == NULL) { rConsoleError() << "ERROR: xml::findPath() failed to evaluate expression " << path << std::endl; throw XPathException("Failed to evaluate XPath expression"); } // Construct the return vector. This may be empty if the provided XPath // expression does not identify any nodes. NodeList retval; xmlNodeSetPtr nodeset = result->nodesetval; if (nodeset != NULL) { for (int i = 0; i < nodeset->nodeNr; i++) { retval.push_back(Node(nodeset->nodeTab[i])); } } xmlXPathFreeObject(result); return retval; }
xmlXPathObjectPtr get_nodes_with_name_xpath( XMLDoc *tree, FeriteString *str ) { char *xpath, *x = "descendant-or-self::node()"; xmlXPathCompExprPtr comp = NULL; int length = strlen( x ) + str->length + 4; xpath = malloc(length); memset(xpath, '\0', length ); sprintf( xpath, "%s/%s", x, str->data ); comp = xmlXPathCompile( BAD_CAST xpath ); free(xpath); if( comp != NULL ) { xmlXPathObjectPtr res = NULL; xmlXPathContextPtr ctxt = xmlXPathNewContext( tree->doc ); ctxt->node = tree->node; res = xmlXPathCompiledEval( comp, ctxt ); xmlXPathFreeContext( ctxt ); xmlXPathFreeCompExpr( comp ); return res; } return NULL; }
int process_recursive(xmlDocPtr doc, char *xpath, int level, char *fn) { int i, num; xmlXPathContextPtr context; xmlXPathObjectPtr op; xmlNodeSetPtr nodeset; context = xmlXPathNewContext(doc); if (context == NULL) { DPRINTF("Error in xmlXPathNewContext\n"); return 0; } DPRINTF("Trying to access xPath node %s\n", xpath); op = xmlXPathEvalExpression( (xmlChar *)xpath, context); xmlXPathFreeContext(context); if (op == NULL) { DPRINTF("Error in xmlXPathEvalExpression\n"); return -EIO; } if(xmlXPathNodeSetIsEmpty(op->nodesetval)) { xmlXPathFreeObject(op); DPRINTF("No result\n"); return -EINVAL; } nodeset = op->nodesetval; num = nodeset->nodeNr; for (i = 0; i < num; i++) process_data(doc, nodeset->nodeTab[i], xpath, level, fn); xmlXPathFreeObject(op); return 0; }
xmlXPathObjectPtr getnodeset (xmlDocPtr doc, xmlChar *xpath) { xmlXPathContextPtr context; xmlXPathObjectPtr result; context = xmlXPathNewContext(doc); if (context == NULL) { printf("[ getnodeset ] : [ ERROR ] : xmlXPathNewContext\n"); return NULL; } result = xmlXPathEvalExpression(xpath, context); xmlXPathFreeContext(context); if (result == NULL) { printf("[ getnodeset ] : [ ERROR ] : xmlXPathEvalExpression\n"); return NULL; } if(xmlXPathNodeSetIsEmpty(result->nodesetval)){ xmlXPathFreeObject(result); if(DEBUG>2) printf("[ getnodeset ] : [ WARNING ] : no xmlXPath result found\n"); return NULL; } return result; }
static struct ldap_object_node * parse_ref_tag(xmlNodePtr curr_node, struct ldap_object_node **objs, struct ldap_attr_node **attrs, struct idinfo *ids) { xmlXPathObjectPtr xobj; xmlXPathContextPtr xctx; char query[1024]; char *n; dbg_printf("Trying to parse ref tag\n"); n = (char *)xmlGetProp(curr_node, (xmlChar *)"name"); snprintf(query, sizeof(query), "//define[@name=\"%s\"]", n); xctx = xmlXPathNewContext(curr_node->doc); assert(xctx); xobj = xmlXPathEvalExpression((xmlChar *)query, xctx); printf("%d nodes match %s\n", xobj->nodesetval->nodeNr, query); assert(0); return NULL; }
static void testXPath(const char *str) { xmlXPathObjectPtr res; xmlXPathContextPtr ctxt; #if defined(LIBXML_XPTR_ENABLED) if (xptr) { ctxt = xmlXPtrNewContext(document, NULL, NULL); res = xmlXPtrEval(BAD_CAST str, ctxt); } else { #endif ctxt = xmlXPathNewContext(document); ctxt->node = xmlDocGetRootElement(document); if (expr) res = xmlXPathEvalExpression(BAD_CAST str, ctxt); else { /* res = xmlXPathEval(BAD_CAST str, ctxt); */ xmlXPathCompExprPtr comp; comp = xmlXPathCompile(BAD_CAST str); if (comp != NULL) { if (tree) xmlXPathDebugDumpCompExpr(stdout, comp, 0); res = xmlXPathCompiledEval(comp, ctxt); xmlXPathFreeCompExpr(comp); } else res = NULL; } #if defined(LIBXML_XPTR_ENABLED) } #endif xmlXPathDebugDumpObject(stdout, res, 0); xmlXPathFreeObject(res); xmlXPathFreeContext(ctxt); }
void XmlConfig::subKeys(const std::string &key, std::vector<std::string> &v) const { addForbiddenKey(key + "/*"); XmlXPathContextHelper xctx(xmlXPathNewContext(data_->doc_.get())); XmlUtils::throwUnless(NULL != xctx.get()); XmlXPathObjectHelper object(xmlXPathEvalExpression((const xmlChar*) key.c_str(), xctx.get())); XmlUtils::throwUnless(NULL != object.get()); if (NULL != object->nodesetval && 0 != object->nodesetval->nodeNr) { xmlNodeSetPtr ns = object->nodesetval; v.reserve(ns->nodeNr); for (int i = 0; i < ns->nodeNr; ++i) { XmlUtils::throwUnless(NULL != ns->nodeTab[i]); std::stringstream stream; stream << key << "[" << (i + 1) << "]"; v.push_back(stream.str()); } } }
config_t * config_load(const char *config_file) { config_t *c; c = malloc(sizeof(*c)); if (!c) return NULL; /* Analyse du fichier */ c->doc = xmlParseFile(config_file); if (!c->doc) { free(c); return NULL; } /* Recherche du noeud racine */ c->root = xmlDocGetRootElement(c->doc); xmlXPathInit(); c->context = xmlXPathNewContext(c->doc); return c; }
static int cx_parse_xml(cx_t *db, char *xml) /* {{{ */ { /* Load the XML */ xmlDocPtr doc = xmlParseDoc(BAD_CAST xml); if (doc == NULL) { ERROR("curl_xml plugin: Failed to parse the xml document - %s", xml); return -1; } xmlXPathContextPtr xpath_ctx = xmlXPathNewContext(doc); if (xpath_ctx == NULL) { ERROR("curl_xml plugin: Failed to create the xml context"); xmlFreeDoc(doc); return -1; } for (size_t i = 0; i < db->namespaces_num; i++) { cx_namespace_t const *ns = db->namespaces + i; int status = xmlXPathRegisterNs(xpath_ctx, BAD_CAST ns->prefix, BAD_CAST ns->url); if (status != 0) { ERROR("curl_xml plugin: " "unable to register NS with prefix=\"%s\" and href=\"%s\"\n", ns->prefix, ns->url); xmlXPathFreeContext(xpath_ctx); xmlFreeDoc(doc); return status; } } int status = cx_handle_parsed_xml(db, doc, xpath_ctx); /* Cleanup */ xmlXPathFreeContext(xpath_ctx); xmlFreeDoc(doc); return status; } /* }}} cx_parse_xml */
xmlXPathObjectPtr TrackerConfig::findConfigNodes(const string& sXPathExpr) const { string sFullPath = string("/trackerconfig"+sXPathExpr); xmlXPathContextPtr xpCtx; xmlXPathObjectPtr xpElement; xpCtx = xmlXPathNewContext(m_Doc); if(!xpCtx) { AVG_LOG_ERROR("Unable to create new XPath context"); return NULL; } xpElement = xmlXPathEvalExpression(BAD_CAST sFullPath.c_str(), xpCtx); if(!xpElement) { AVG_LOG_ERROR("Unable to evaluate XPath expression '" << sFullPath << "'"); xmlXPathFreeContext(xpCtx); return NULL; } xmlXPathFreeContext(xpCtx); return xpElement; }
bool parse_rss_item(xmlNodePtr node, wxString &url, wxString &version, wxString &md5, wxDateTime &datetime) { xmlXPathContextPtr xp_ctx = xmlXPathNewContext(node->doc); xp_ctx->node = node; int ret; ret = xmlXPathRegisterNs(xp_ctx, BAD_CAST "media", BAD_CAST "http://video.search.yahoo.com/mrss/"); assert(ret == 0); wxString datetime_string; wxString* str_array[] = { &md5, &url, &datetime_string }; const char * xpath_array[] = { "./media:content/media:hash[@algo='md5']", "./link", "./pubDate" }; for (size_t i = 0; i < sizeof(str_array) / sizeof(str_array[0]); i++) { xmlNodePtr _node = uxmlXPathGetNode(BAD_CAST xpath_array[i], xp_ctx); if (_node == NULL || _node->xmlChildrenNode == NULL) { xmlXPathFreeContext(xp_ctx); return false; } xmlChar* _str = xmlNodeListGetString(_node->doc, _node->xmlChildrenNode, 1); *str_array[i] = SC::U2S(_str); xmlFree(_str); } if (datetime.ParseRfc822Date(datetime_string.c_str()) == NULL) { xmlXPathFreeContext(xp_ctx); return false; } version = url2version(url); return true; }
xmlXPathObjectPtr static xml_GetNodeSet(xmlDocPtr doc, const xmlChar *pXpath) { xmlXPathContextPtr context = NULL; xmlXPathObjectPtr xpRet = NULL; if(doc == NULL || pXpath == NULL) return xpRet; { context = xmlXPathNewContext(doc); if (context != NULL) { xpRet = xmlXPathEvalExpression(pXpath, context); xmlXPathFreeContext(context); if (xpRet != NULL) { if (xmlXPathNodeSetIsEmpty(xpRet->nodesetval)) { xmlXPathFreeObject(xpRet); xpRet = NULL; } } } } return xpRet; }
/* * Use XPath for the search. Find all elements with the name specified by user. * Return - pointer to the list of elements or NULL if an error occurred. */ xmlXPathObjectPtr retrieve_all_elements(xmlDocPtr doc, char *element) { xmlXPathContextPtr context; xmlXPathObjectPtr result; context = xmlXPathNewContext(doc); if (context == NULL){ printf("Error in xmlXPathNewContext\n"); return NULL; } result = xmlXPathEvalExpression((xmlChar *)element, context); xmlXPathFreeContext(context); if (result == NULL){ printf("Error in xmlXPathEvalExpression\n"); return NULL; } if (xmlXPathNodeSetIsEmpty(result->nodesetval)){ xmlXPathFreeObject(result); printf("No result.\n"); return NULL; } return result; }
int parse_and_write_xml_data(const char *station_id, htmlDocPtr doc, const char *result_file){ char buff[256], buffer[buff_size]; char temp_buffer[buff_size]; int size; int i,j; xmlXPathContextPtr xpathCtx; xmlXPathObjectPtr xpathObj = NULL; xmlXPathObjectPtr xpathObj2 = NULL; xmlXPathObjectPtr xpathObj3 = NULL; xmlXPathObjectPtr xpathObj4 = NULL; xmlXPathObjectPtr xpathObj5 = NULL; xmlXPathObjectPtr xpathObj6 = NULL; xmlXPathObjectPtr xpathObj7 = NULL; xmlNodeSetPtr nodes; char *temp_char; struct tm tmp_tm = {0}; struct tm *tm; time_t t_start = 0, t_end = 0, current_time = 0; FILE *file_out; file_out = fopen(result_file, "w"); if (!file_out) return -1; fprintf(file_out,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<station name=\"Station name\" id=\"%s\" xmlns=\"http://omweather.garage.maemo.org/schemas\">\n", station_id); fprintf(file_out," <units>\n <t>C</t>\n <ws>m/s</ws>\n <wg>m/s</wg>\n <d>km</d>\n"); fprintf(file_out," <h>%%</h> \n <p>mmHg</p>\n </units>\n"); /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ hash_for_icons = hash_icons_forecacom_table_create(); /* Create xpath evaluation context */ xpathCtx = xmlXPathNewContext(doc); if(xpathCtx == NULL) { fprintf(stderr,"Error: unable to create new XPath context\n"); return(-1); } /* Register namespaces from list (if any) */ xmlXPathRegisterNs(xpathCtx, (const xmlChar*)"html", (const xmlChar*)"http://www.w3.org/1999/xhtml"); /* Day weather forecast */ /* Evaluate xpath expression */ xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/*/div[(@data-ts)]/@data-ts", xpathCtx); // xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div/div/table//tr/th[@colspan='3']", xpathCtx); if(xpathObj == NULL) { fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", "/html/body/*/div[(@data-ts)]/@data-ts"); xmlXPathFreeContext(xpathCtx); return(-1); } nodes = xpathObj->nodesetval; size = (nodes) ? nodes->nodeNr : 0; if (size > 14) size = 14; /* fprintf(stderr, "SIZE!!!!!!!!!!!!!!: %i\n", size); */ xpathObj2 = xmlXPathEvalExpression((const xmlChar*)"/html/body/*/div[(@data-ts)]/div/span[(@class='temp_max temp_warm') or (@class='temp_max temp_cold')]/text()", xpathCtx); xpathObj3 = xmlXPathEvalExpression((const xmlChar*)"/html/body/*/div[(@data-ts)]/div/span[(@class='temp_min')]/text()", xpathCtx); xpathObj4 = xmlXPathEvalExpression((const xmlChar*)"/html/body/*/div[(@data-ts)]/div/div/@class", xpathCtx); xpathObj5 = xmlXPathEvalExpression((const xmlChar*)"/html/body/*/div[(@data-ts)]/div/strong/text()", xpathCtx); xpathObj6 = xmlXPathEvalExpression((const xmlChar*)"/html/body/*/div[(@data-ts)]/div/img/@src", xpathCtx); xpathObj7 = xmlXPathEvalExpression((const xmlChar*)"/html/body/*/div[(@data-ts)]/div/img/@title", xpathCtx); /* fprintf(stderr, "Result (%d nodes):\n", size); */ for(i = 0; i < size; ++i) { /* Take time: */ if (!nodes->nodeTab[i]->children->content){ fprintf(stderr,"ERRROR"); continue; } /* fprintf(stderr," TEXT %s\n", nodes->nodeTab[i]->children->content); */ current_time = time(NULL); tm = localtime(¤t_time); setlocale(LC_TIME, "POSIX"); strptime((const char*)nodes->nodeTab[i]->children->content, "%Y%m%d", &tmp_tm); setlocale(LC_TIME, ""); /* set begin of day in localtime */ tmp_tm.tm_year = tm->tm_year; tmp_tm.tm_hour = 0; tmp_tm.tm_min = 0; tmp_tm.tm_sec = 0; t_start = mktime(&tmp_tm); fprintf(file_out," <period start=\"%li\"", t_start); /* set end of day in localtime */ t_end = t_start + 3600*24 - 1; fprintf(file_out," end=\"%li\">\n", t_end); /* added hi temperature */ if (xpathObj2 && !xmlXPathNodeSetIsEmpty(xpathObj2->nodesetval) && xpathObj2->nodesetval->nodeTab[i] && xpathObj2->nodesetval->nodeTab[i]->content){ /* fprintf (stderr, "temperature %s\n", xpathObj2->nodesetval->nodeTab[i]->content); */ snprintf(buffer, sizeof(buffer)-1,"%s", xpathObj2->nodesetval->nodeTab[i]->content); memset(temp_buffer, 0, sizeof(temp_buffer)); for (j = 0 ; (j<(strlen(buffer)) && j < buff_size); j++ ){ if (buffer[j] == '&') break; if ((uint)buffer[j] == 226 || buffer[j] == '-' || (buffer[j]>='0' && buffer[j]<='9')){ if ((uint)buffer[j] == 226) sprintf(temp_buffer,"%s-",temp_buffer); else sprintf(temp_buffer,"%s%c",temp_buffer, buffer[j]); } } /* fprintf(stderr, " <temperature>%s</temperature>\n", temp_buffer); */ fprintf(file_out," <temperature_hi>%s</temperature_hi>\n", temp_buffer); } /* added lo temperature */ if (xpathObj3 && !xmlXPathNodeSetIsEmpty(xpathObj3->nodesetval) && xpathObj3->nodesetval->nodeTab[i] && xpathObj3->nodesetval->nodeTab[i]->content){ /* fprintf (stderr, "temperature %s\n", xpathObj3->nodesetval->nodeTab[i]->content); */ snprintf(buffer, sizeof(buffer)-1,"%s", xpathObj3->nodesetval->nodeTab[i]->content); memset(temp_buffer, 0, sizeof(temp_buffer)); for (j = 0 ; (j<(strlen(buffer)) && j < buff_size); j++ ){ if (buffer[j] == '&') break; if ((uint)buffer[j] == 226 || buffer[j] == '-' || (buffer[j]>='0' && buffer[j]<='9')){ if ((uint)buffer[j] == 226) sprintf(temp_buffer,"%s-",temp_buffer); else sprintf(temp_buffer,"%s%c",temp_buffer, buffer[j]); } } /* fprintf(stderr, " <temperature>%s</temperature>\n", temp_buffer); */ fprintf(file_out," <temperature_low>%s</temperature_low>\n", temp_buffer); } /* added wind direction */ if (xpathObj4 && !xmlXPathNodeSetIsEmpty(xpathObj4->nodesetval) && xpathObj4->nodesetval->nodeTab[i] && xpathObj4->nodesetval->nodeTab[i]->children->content){ temp_char = strstr((char*)xpathObj4->nodesetval->nodeTab[i]->children->content, " "); temp_char++; temp_char++; switch (atoi(temp_char)){ case 0: snprintf(buff, sizeof(buff)-1,"N"); break; case 45: snprintf(buff, sizeof(buff)-1,"NE"); break; case 90: snprintf(buff, sizeof(buff)-1,"E"); break; case 135: snprintf(buff, sizeof(buff)-1,"SE"); break; case 180: snprintf(buff, sizeof(buff)-1,"S"); break; case 225: snprintf(buff, sizeof(buff)-1,"SW"); break; case 270: snprintf(buff, sizeof(buff)-1,"W"); break; case 315: snprintf(buff, sizeof(buff)-1,"NW"); break; case 360: snprintf(buff, sizeof(buff)-1,"N"); break; default: snprintf(buff, sizeof(buff)-1,""); break; } /* fprintf(stderr, "Wind direction %s %s \n",temp_char, buff); */ fprintf(file_out," <wind_direction>%s</wind_direction>\n", buff); } /* added wind speed */ if (xpathObj5 && !xmlXPathNodeSetIsEmpty(xpathObj5->nodesetval) && xpathObj5->nodesetval->nodeTab[i] && xpathObj5->nodesetval->nodeTab[i]->content){ /* fprintf(stderr, "Wind direction %s \n", xpathObj4->nodesetval->nodeTab[i]->children->content); */ fprintf(file_out," <wind_speed>%s</wind_speed>\n", xpathObj5->nodesetval->nodeTab[i]->content); } /* added icon */ if (xpathObj6 && !xmlXPathNodeSetIsEmpty(xpathObj6->nodesetval) && xpathObj6->nodesetval->nodeTab[i] && xpathObj6->nodesetval->nodeTab[i]->children->content){ temp_char = strrchr((char*)xpathObj6->nodesetval->nodeTab[i]->children->content, '/'); temp_char ++; /* fprintf (stderr, "icon %s %s \n", xpathObj6->nodesetval->nodeTab[i]->children->content, choose_hour_weather_icon(hash_for_icons, temp_char)); */ //fprintf(file_out," <icon>%s</icon>\n", // choose_hour_weather_icon(temp_char).toStdString().c_str()); if ((char*)xmlHashLookup(hash_for_icons, (const xmlChar*)temp_char)){ fprintf(file_out," <icon>%s</icon>\n", (char*)xmlHashLookup(hash_for_icons, (const xmlChar*)temp_char)); }else fprintf(file_out," <icon>49</icon>\n"); } /* added text */ if (xpathObj7 && !xmlXPathNodeSetIsEmpty(xpathObj7->nodesetval) && xpathObj7->nodesetval->nodeTab[i] && xpathObj7->nodesetval->nodeTab[i]->children->content){ snprintf(buffer, sizeof(buffer)-1,"%s", xpathObj7->nodesetval->nodeTab[i]->children->content); memset(temp_buffer, 0, sizeof(temp_buffer)); for (j = 0 ; (j<(strlen(buffer)) && j < buff_size); j++ ){ if (buffer[j] == 13 || buffer[j] == 10) continue; sprintf(temp_buffer,"%s%c",temp_buffer, buffer[j]); } fprintf(file_out," <description>%s</description>\n", temp_buffer); } fprintf(file_out," </period>\n"); } /* Cleanup */ fclose(file_out); if (xpathObj) xmlXPathFreeObject(xpathObj); if (xpathObj2) xmlXPathFreeObject(xpathObj2); if (xpathObj3) xmlXPathFreeObject(xpathObj3); if (xpathObj4) xmlXPathFreeObject(xpathObj4); if (xpathObj5) xmlXPathFreeObject(xpathObj5); if (xpathObj6) xmlXPathFreeObject(xpathObj6); if (xpathObj7) xmlXPathFreeObject(xpathObj7); if (xpathCtx) xmlXPathFreeContext(xpathCtx); xmlHashFree(hash_for_icons, NULL); return size; }
int parse_and_write_detail_data(const char *station_id, htmlDocPtr doc, const char *result_file){ char buff[256]; char buffer[buff_size]; char temp_buffer[buff_size]; int j, k, l; xmlXPathContextPtr xpathCtx; xmlXPathObjectPtr xpathObj = NULL; xmlXPathObjectPtr xpathObj2 = NULL; xmlNodeSetPtr nodes; char *temp_char; char *temp_char2; int pressure; int speed; int ppcp; char *image = NULL; double time_diff = 0; time_t loc_time; time_t utc_time; int location_timezone = 0; int flag; struct tm tmp_tm = {0}; struct tm *tm; time_t t_start = 0, t_end = 0, t_sunrise = 0, t_sunset = 0, current_time = 0; FILE *file_out; int index = 1; struct tm time_tm1; struct tm time_tm2; int localtimezone = 0; int remotetimezone = 0; int hour = 0; int day = 0; int month = 0; char buff_day[256]; /* fprintf(stderr, "parse_and_write_detail_data()\n"); */ file_out = fopen(result_file, "a"); if (!file_out) return -1; /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ hash_for_icons = hash_icons_forecacom_table_create(); /* Create xpath evaluation context */ xpathCtx = xmlXPathNewContext(doc); if(xpathCtx == NULL) { fprintf(stderr,"Error: unable to create new XPath context\n"); return(-1); } /* Register namespaces from list (if any) */ xmlXPathRegisterNs(xpathCtx, (const xmlChar*)"html", (const xmlChar*)"http://www.w3.org/1999/xhtml"); /* Current weather forecast */ /* Evaluate xpath expression */ xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div[@id='cc']/div[@class='cctext']/p/text()[3]", xpathCtx); if(xpathObj == NULL) { fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", " /html/body/div[@id='cc']/div[@class='cctext']/p/text()[3]"); xmlXPathFreeContext(xpathCtx); return(-1); } if (xpathObj->nodesetval == NULL || xpathObj->nodesetval->nodeTab == NULL || xpathObj->nodesetval->nodeTab[0] ==NULL || xpathObj->nodesetval->nodeTab[0]->content == NULL){ xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div[@id='cc']/div[@class='cctext']/p/text()[2]", xpathCtx); if (xpathObj->nodesetval == NULL || xpathObj->nodesetval->nodeTab == NULL || xpathObj->nodesetval->nodeTab[0] ==NULL || xpathObj->nodesetval->nodeTab[0]->content == NULL){ xmlXPathFreeContext(xpathCtx); return -1; } } /* Set localtimezone */ current_time = time(NULL); gmtime_r(¤t_time, &time_tm1); localtime_r(¤t_time, &time_tm2); localtimezone = (mktime(&time_tm2) - mktime(&time_tm1))/3600; /* fprintf(stderr,"Local Time Zone %i\n", localtimezone); */ if (xpathObj && xpathObj->nodesetval->nodeTab[0]->content){ tm = localtime(¤t_time); snprintf(buffer, sizeof(buffer)-1,"%i %s", tm->tm_year + 1900, xpathObj->nodesetval->nodeTab[0]->content); /* fprintf(stderr, "Time %s", buffer); */ setlocale(LC_TIME, "POSIX"); strptime((const char*)buffer, "%Y%n%d/%m %H", &tmp_tm); hour = tmp_tm.tm_hour; day = tmp_tm.tm_mday; month = tmp_tm.tm_mon; if (xpathObj) xmlXPathFreeObject(xpathObj); xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div[@class='hourlyfc']/*[@class='symbcol']/preceding-sibling::div[@class='timecol']/p/text()", xpathCtx); if (xpathObj && xpathObj->nodesetval->nodeTab[0]->content){ int hour = atoi((const char*)xpathObj->nodesetval->nodeTab[0]->content) -1; tmp_tm.tm_hour = hour; } time_tm1.tm_min = 0; tmp_tm.tm_min = 0; remotetimezone = (mktime(&tmp_tm) - mktime(&time_tm1))/3600; if (abs(remotetimezone) < 13) fprintf(file_out," <timezone>%i</timezone>\n", remotetimezone); /* fprintf(stderr,"Remote timezone %i\n", remotetimezone); */ strptime((const char*)buffer, "%Y%n%d/%m %H:%M", &tmp_tm); /* fprintf (stderr, "Time %s\n", buffer); */ setlocale(LC_TIME, ""); /* set begin of day in localtime */ //tmp_tm.tm_year = tm->tm_year; t_start = mktime(&tmp_tm) - 3600*remotetimezone + 3600*localtimezone; fprintf(file_out," <period start=\"%li\"", (t_start + 1 - 2*3600)); /* set end of current time in localtime */ t_end = t_start + 3600*4 - 1; fprintf(file_out," end=\"%li\" current=\"true\" >\n", t_end); } if (xpathObj) xmlXPathFreeObject(xpathObj); xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div[@id='cc']/div[@class='right']/span/text()", xpathCtx); /* added temperature */ if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) && xpathObj->nodesetval->nodeTab[0] && xpathObj->nodesetval->nodeTab[0]->content){ /* fprintf (stderr, "temperature %s\n", xpathObj->nodesetval->nodeTab[0]->content); */ snprintf(buffer, sizeof(buffer)-1,"%s", xpathObj->nodesetval->nodeTab[0]->content); memset(temp_buffer, 0, sizeof(temp_buffer)); for (j = 0 ; (j<(strlen(buffer)) && j < buff_size); j++ ){ if (buffer[j] == '&') break; if ((uint)buffer[j] == 226 || buffer[j] == '-' || (buffer[j]>='0' && buffer[j]<='9')){ if ((uint)buffer[j] == 226) sprintf(temp_buffer,"%s-",temp_buffer); else sprintf(temp_buffer,"%s%c",temp_buffer, buffer[j]); } } /* fprintf(stderr, " <temperature>%s</temperature>\n", temp_buffer); */ fprintf(file_out," <temperature>%s</temperature>\n", temp_buffer); } if (xpathObj) xmlXPathFreeObject(xpathObj); xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div[@id='cc']/div[@class='right wind']/div/@class", xpathCtx); /* fprintf(stderr,"sss %s", xpathObj->nodesetval->nodeTab[0]->children->content); */ /* added wind direction */ if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) && xpathObj->nodesetval->nodeTab[0] && xpathObj->nodesetval->nodeTab[0]->children->content){ temp_char = strstr((char*)xpathObj->nodesetval->nodeTab[0]->children->content, " "); temp_char++; temp_char++; switch (atoi(temp_char)){ case 0: snprintf(buff, sizeof(buff)-1,"N"); break; case 45: snprintf(buff, sizeof(buff)-1,"NE"); break; case 90: snprintf(buff, sizeof(buff)-1,"E"); break; case 135: snprintf(buff, sizeof(buff)-1,"SE"); break; case 180: snprintf(buff, sizeof(buff)-1,"S"); break; case 225: snprintf(buff, sizeof(buff)-1,"SW"); break; case 270: snprintf(buff, sizeof(buff)-1,"W"); break; case 315: snprintf(buff, sizeof(buff)-1,"NW"); break; case 360: snprintf(buff, sizeof(buff)-1,"N"); break; default: snprintf(buff, sizeof(buff)-1,""); break; } /* fprintf(stderr, "Wind direction .%s. \n", xpathObj->nodesetval->nodeTab[0]->children->content); */ fprintf(file_out," <wind_direction>%s</wind_direction>\n", buff); } if (xpathObj) xmlXPathFreeObject(xpathObj); xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div[@id='cc']/div[@class='right wind']/strong/text()", xpathCtx); /* added wind speed */ if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) && xpathObj->nodesetval->nodeTab[0] && xpathObj->nodesetval->nodeTab[0]->content){ /* fprintf(stderr, "Wind direction %s \n", xpathObj4->nodesetval->nodeTab[i]->children->content); */ fprintf(file_out," <wind_speed>%i</wind_speed>\n", atoi((const char*)xpathObj->nodesetval->nodeTab[0]->content)); } if (xpathObj) xmlXPathFreeObject(xpathObj); xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div[@id='cc']/div[@class='left']/img/@src", xpathCtx); /* added icon */ if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) && xpathObj->nodesetval->nodeTab[0] && xpathObj->nodesetval->nodeTab[0]->children->content){ temp_char = strrchr((char*)xpathObj->nodesetval->nodeTab[0]->children->content, '/'); temp_char ++; /* fprintf (stderr, "icon %s %s \n", xpathObj6->nodesetval->nodeTab[i]->children->content, choose_hour_weather_icon(hash_for_icons, temp_char)); */ // fprintf(file_out," <icon>%s</icon>\n", // choose_hour_weather_icon(temp_char).toStdString().c_str()); if ((char*)xmlHashLookup(hash_for_icons, (const xmlChar*)temp_char)){ fprintf(file_out," <icon>%s</icon>\n", (char*)xmlHashLookup(hash_for_icons, (const xmlChar*)temp_char)); }else fprintf(file_out," <icon>49</icon>\n"); } if (xpathObj) xmlXPathFreeObject(xpathObj); index = 1; // snprintf(buffer, sizeof(buffer)-1,"/html/body/div[@id='cc']/div[@class='cctext']/p/strong/text()", index); snprintf(buffer, sizeof(buffer)-1,"/html/body/div[@id='cc']/div[@class='cctext']/p[not(contains(text(), ':'))]/strong/text()"); xpathObj = xmlXPathEvalExpression((const xmlChar*)buffer, xpathCtx); /* added text */ if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) && xpathObj->nodesetval->nodeTab[0] && xpathObj->nodesetval->nodeTab[0]->content){ /* fprintf (stderr, "description %s\n", xpathObj7->nodesetval->nodeTab[i]->content); */ fprintf(file_out," <description>%s</description>\n", xpathObj->nodesetval->nodeTab[0]->content); } if (xpathObj) xmlXPathFreeObject(xpathObj); snprintf(buffer, sizeof(buffer)-1,"/html/body/div[@id='cc']/div[@class='cctext']/p/strong[1]/text()"); xpathObj = xmlXPathEvalExpression((const xmlChar*)buffer, xpathCtx); if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) && xpathObj->nodesetval->nodeTab[1] && xpathObj->nodesetval->nodeTab[1]->content){ fprintf(file_out," <flike>%i</flike>\n", atoi((char*)xpathObj->nodesetval->nodeTab[1]->content)); } if (xpathObj) xmlXPathFreeObject(xpathObj); snprintf(buffer, sizeof(buffer)-1,"/html/body/div[@id='cc']/div[@class='cctext']/p/strong/text()"); xpathObj = xmlXPathEvalExpression((const xmlChar*)buffer, xpathCtx); if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) && xpathObj->nodesetval->nodeTab[2] && xpathObj->nodesetval->nodeTab[2]->content){ fprintf(file_out," <humidity>%i</humidity>\n", atoi((char*)xpathObj->nodesetval->nodeTab[2]->content)); } if (xpathObj) xmlXPathFreeObject(xpathObj); snprintf(buffer, sizeof(buffer)-1,"/html/body/div[@id='cc']/div[@class='cctext']/p/strong/text()"); xpathObj = xmlXPathEvalExpression((const xmlChar*)buffer, xpathCtx); if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) && xpathObj->nodesetval->nodeTab[3] && xpathObj->nodesetval->nodeTab[3]->content){ fprintf(file_out," <pressure>%i</pressure>\n", atoi((char*)xpathObj->nodesetval->nodeTab[3]->content)); } if (xpathObj) xmlXPathFreeObject(xpathObj); snprintf(buffer, sizeof(buffer)-1,"/html/body/div[@id='cc']/div[@class='cctext']/p/strong/text()"); xpathObj = xmlXPathEvalExpression((const xmlChar*)buffer, xpathCtx); if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) && xpathObj->nodesetval->nodeTab[4] && xpathObj->nodesetval->nodeTab[4]->content){ fprintf(file_out," <visible>%i</visible>\n", atoi((char*)xpathObj->nodesetval->nodeTab[4]->content)*1000); } if (xpathObj) xmlXPathFreeObject(xpathObj); fprintf(file_out," </period>\n"); //return 1; /* To DO sunrise and sunset */ xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div[@class='hourlyfc']/*[@class='symbcol']/preceding-sibling::div[@class='timecol']/p/strong/text()", xpathCtx); snprintf(buff_day, sizeof(buff_day)-1,"%s", (const char*)xpathObj->nodesetval->nodeTab[0]->content); /* fprintf(stderr, "buff_day %s\n", buff_day); */ if (xpathObj) xmlXPathFreeObject(xpathObj); snprintf(buffer, sizeof(buffer)-1,"(/html/body/div[@class='hourlyfc']/*[@class='symbcol']/preceding-sibling::div[@class='timecol']/p/strong/text() | /html/body/div[@class='hourlyfc']/*[@class='symbcol']/preceding-sibling::div[@class='timecol']/p/text() | /html/body/div[@class='hourlyfc']/*[@class='symbcol']/img/@title | /html/body/div[@class='hourlyfc']/*[@class='symbcol']/img/@src | /html/body/div[@class='hourlyfc']/*[@class='symbcol']/following-sibling::div/span/text() | /html/body/div[@class='hourlyfc']/*[@class='symbcol']/following-sibling::div/div/@class | /html/body/div[@class='hourlyfc']/*[@class='symbcol']/following-sibling::div/strong/text() ) "); xpathObj2 = xmlXPathEvalExpression((const xmlChar*)buffer, xpathCtx); k = 0; nodes = xpathObj2->nodesetval; flag = true; for (j = 0; j <(nodes->nodeNr); ++j){ if (k >6) flag = false; if (flag){ switch (k){ case 1: { current_time = time(NULL); tm = localtime(¤t_time); snprintf(buffer, sizeof(buffer)-1,"%i %i %s", month + 1, day, xpathObj2->nodesetval->nodeTab[j]->content); /* fprintf(stderr," Buffer %s\n", buffer); */ setlocale(LC_TIME, "POSIX"); strptime((const char*)buffer, "%m %d %H:%M", &tmp_tm); setlocale(LC_TIME, ""); /* set begin of day in localtime */ tmp_tm.tm_year = tm->tm_year; t_start = mktime(&tmp_tm) - 3600*remotetimezone + 3600*localtimezone; if (strcmp((const char*)xpathObj2->nodesetval->nodeTab[j-1]->content, buff_day)){ t_start = t_start + 24*3600; } fprintf(file_out," <period start=\"%li\"", (t_start)); /* set end of current time in localtime */ t_end = t_start + 3600*3 - 1; fprintf(file_out," end=\"%li\" hour=\"true\">\n", t_end); break; } case 2: { if (xpathObj2->nodesetval->nodeTab[j] && xpathObj2->nodesetval->nodeTab[j]->children && xpathObj2->nodesetval->nodeTab[j]->children->content && strlen((char*)xpathObj2->nodesetval->nodeTab[j]->children->content)>0){ temp_char = strrchr((char*)xpathObj2->nodesetval->nodeTab[j]->children->content, '/'); temp_char ++; // fprintf(file_out," <icon>%s</icon>\n", // choose_hour_weather_icon(temp_char).toStdString().c_str()); if ((char*)xmlHashLookup(hash_for_icons, (const xmlChar*)temp_char)){ fprintf(file_out," <icon>%s</icon>\n", (char*)xmlHashLookup(hash_for_icons, (const xmlChar*)temp_char)); }else fprintf(file_out," <icon>49</icon>\n"); } } break; case 3: { if (strlen((char*)xpathObj2->nodesetval->nodeTab[j]->children->content)>0){ fprintf(file_out," <description>%s</description>\n", xpathObj2->nodesetval->nodeTab[j]->children->content); } } break; case 4: { /* added temperature */ if (xpathObj2->nodesetval->nodeTab[j]->content){ snprintf(buffer, sizeof(buffer)-1,"%s", xpathObj2->nodesetval->nodeTab[j]->content); memset(temp_buffer, 0, sizeof(temp_buffer)); for (l = 0 ; (l<(strlen(buffer)) && l < buff_size); l++ ){ if (buffer[l] == '&') break; if ((uint)buffer[l] == 226 || buffer[l] == '-' || (buffer[l]>='0' && buffer[l]<='9')){ if ((uint)buffer[l] == 226) sprintf(temp_buffer,"%s-",temp_buffer); else sprintf(temp_buffer,"%s%c",temp_buffer, buffer[l]); } } fprintf(file_out," <temperature>%s</temperature>\n", temp_buffer); } break; } case 5: { if (strlen((char*)xpathObj2->nodesetval->nodeTab[j]->children->content)>0){ temp_char = strstr((char*)xpathObj2->nodesetval->nodeTab[j]->children->content, " "); temp_char++; temp_char++; switch (atoi(temp_char)){ case 0: snprintf(buff, sizeof(buff)-1,"N"); break; case 45: snprintf(buff, sizeof(buff)-1,"NE"); break; case 90: snprintf(buff, sizeof(buff)-1,"E"); break; case 135: snprintf(buff, sizeof(buff)-1,"SE"); break; case 180: snprintf(buff, sizeof(buff)-1,"S"); break; case 225: snprintf(buff, sizeof(buff)-1,"SW"); break; case 270: snprintf(buff, sizeof(buff)-1,"W"); break; case 315: snprintf(buff, sizeof(buff)-1,"NW"); break; case 360: snprintf(buff, sizeof(buff)-1,"N"); break; default: snprintf(buff, sizeof(buff)-1,""); break; } fprintf(file_out," <wind_direction>%s</wind_direction>\n", buff); } break; } case 6: { if (strlen((char*)xpathObj2->nodesetval->nodeTab[j]->content)>0){ fprintf(file_out," <wind_speed>%s</wind_speed>\n", xpathObj2->nodesetval->nodeTab[j]->content); } k = -1; fprintf(file_out," </period>\n"); } break; } k++; } } fclose(file_out); if (xpathObj2) xmlXPathFreeObject(xpathObj2); xmlHashFree(hash_for_icons, NULL); xmlXPathFreeContext(xpathCtx); return 1; }
GPtrArray * dict_get_translate(const gchar * keyword) { gchar * key_url = NULL; htmlDocPtr html_doc = NULL; xmlDocPtr doc = NULL; xmlNodePtr clone = NULL; xmlXPathContextPtr ctx = NULL; xmlXPathObjectPtr obj = NULL; xmlNodeSetPtr nodeset = NULL; gchar * tmp_str = NULL; GPtrArray * result = NULL; gint i; key_url = g_strjoin(NULL, KEY_URL_PREFIX, keyword, KEY_URL_SUFIX, NULL); g_debug("KEY-URL: %s", key_url); html_doc = htmlReadFile(key_url, NULL, HTML_PARSE_NOWARNING | HTML_PARSE_NOERROR); if (!html_doc) { g_message("KEY-URL %s get failed", key_url); goto out; } ctx = xmlXPathNewContext((xmlDocPtr)html_doc); if (!ctx) { g_message("XPath context creat failed"); goto out; } obj = xmlXPathEvalExpression(KEY_WORD_XPATH, ctx); if (!obj) { g_message("XPath eval key word xpath failed"); goto out; } if (xmlXPathNodeSetIsEmpty(obj->nodesetval)) { g_message("XPath search keyword failed"); goto out; } nodeset = obj->nodesetval; g_debug("Key word node set have %d object", nodeset->nodeNr); clone = xmlCopyNode(nodeset->nodeTab[0], 1); if (!clone) goto out; doc = xmlNewDoc("1.0"); if (!doc) goto out; xmlDocSetRootElement(doc, clone); xmlXPathFreeContext(ctx); ctx = NULL; xmlXPathFreeObject(obj); obj = NULL; ctx = xmlXPathNewContext(doc); if (!ctx) goto out; obj = xmlXPathEvalExpression("//span[@class='keyword']", ctx); if (!obj) goto out; nodeset = obj->nodesetval; tmp_str = xmlNodeGetContent(nodeset->nodeTab[0]->xmlChildrenNode); g_debug("The word to search %s", tmp_str); xmlFree(tmp_str); tmp_str = NULL; xmlXPathFreeObject(obj); obj = NULL; obj = xmlXPathEvalExpression("//ul/li", ctx); if (!obj) goto out; if (xmlXPathNodeSetIsEmpty(obj->nodesetval)) { g_message("Result value is empty"); goto out; } nodeset = obj->nodesetval; result = g_ptr_array_sized_new(nodeset->nodeNr + 1); for (i = 0; i < nodeset->nodeNr; ++i) { tmp_str = xmlNodeGetContent(nodeset->nodeTab[i]->xmlChildrenNode); g_ptr_array_add(result, tmp_str); } g_ptr_array_add(result, NULL); out: if (doc) xmlFreeDoc(doc); if (key_url) g_free(key_url); if (html_doc) xmlFreeDoc((xmlDocPtr)html_doc); if (ctx) xmlXPathFreeContext(ctx); if (obj) xmlXPathFreeObject(obj); return result; }
int with_words_at_xpath(void (*function)(char *[], size_t, xmlNodePtr), xmlDocPtr document, const char * xpath, FILE *logfile, long parameters, long dnm_parameters) { //create xpath context xmlXPathContextPtr xpath_context = xmlXPathNewContext(document); if (xpath_context == NULL) { fprintf(logfile, "Unable to create xpath context\n"); xmlFreeDoc(document); return 0; } //evaluate xpath xmlXPathObjectPtr xpath_result = xmlXPathEvalExpression((xmlChar *)xpath,xpath_context); if ((xpath_result == NULL) || (xpath_result->nodesetval == NULL) || (xpath_result->nodesetval->nodeNr == 0)) { //didn't find anything // Clean up if (xpath_result != NULL) { if (xpath_result->nodesetval != NULL) { free(xpath_result->nodesetval->nodeTab); free(xpath_result->nodesetval); } xmlFree(xpath_result); } //function call should be done anyway function(NULL, 0, NULL); return 0; //return false, stating that no nodes where found } //prepare array for words size_t word_array_allocated = 2048; char ** word_array = (char **) malloc(word_array_allocated * sizeof(char *)); size_t word_array_index = 0; //iterate over results xmlNodeSetPtr nodeset = xpath_result->nodesetval; int index; for (index=0; index < nodeset->nodeNr; index++) { xmlNodePtr current_node = nodeset->nodeTab[index]; // Obtain NLP-friendly plain-text of the paragraph: dnmPtr current_dnm = create_DNM(current_node, dnm_parameters); if (current_dnm == NULL) { fprintf(stderr, "Couldn't create DNM for xpath %s at position %d in document (fatal)\n", xpath, index); exit(1); } //tokenize sentences char* plaintext = current_dnm->plaintext; if (strlen(plaintext) < 2) { //not a real sentence, additionally it would cause errors in tokenizer free_DNM(current_dnm); continue; } dnmRanges sentences = tokenize_sentences(plaintext); // tokenize words int sentence_index = 0; for (sentence_index = 0; sentence_index < sentences.length; sentence_index++) { // Obtaining only the content words here dnmRanges words = tokenize_words(plaintext, sentences.range[sentence_index], 0); int word_index; for(word_index=0; word_index<words.length; word_index++) { char* word_string = plain_range_to_string(plaintext, words.range[word_index]); if (parameters & WORDS_NORMALIZE_WORDS) { normalize_word(&word_string); } if (parameters & WORDS_STEM_WORDS) { char* word_stem; full_morpha_stem(word_string, &word_stem); free(word_string); word_string = word_stem; } POSSIBLE_RESIZE(word_array, word_array_index, &word_array_allocated, word_array_allocated*2, char *); word_array[word_array_index++] = word_string; } free(words.range); if (parameters&WORDS_MARK_END_OF_SENTENCE) { POSSIBLE_RESIZE(word_array, word_array_index, &word_array_allocated, word_array_allocated*2, char *); word_array[word_array_index++] = strdup("endofsentence"); } } free(sentences.range); free_DNM(current_dnm); function(word_array, word_array_index, current_node); //free memory size_t i = 0; while (i < word_array_index) free(word_array[i++]); word_array_index = 0; }
int libvirt_open() { conn = virConnectOpenReadOnly("xen:///"); if(conn == NULL) { printf("libvirt connection open error on uri \n"); return 0; } int n; n = virConnectNumOfDomains(conn); if(n < 0) { printf("Error reading number of domains \n"); return -1; } int i; int *domids; domids = malloc(sizeof(int) * n); if(domids == 0) { printf("libvirt domain ids malloc failed"); return -1; } n = virConnectListDomains(conn, domids, n); if(n < 0) { printf("Error reading list of domains \n"); free(domids); return -1; } free_block_devices(); free_interface_devices(); free_domains(); for (i = 0; i < n ; ++i) { virDomainPtr dom = NULL; const char *name; char *xml = NULL; xmlDocPtr xml_doc = NULL; xmlXPathContextPtr xpath_ctx = NULL; xmlXPathObjectPtr xpath_obj = NULL; int j; //printf("Publishing Domain Id : %d \n ", domids[i]); dom = virDomainLookupByID(conn, domids[i]); if(dom == NULL) { printf("Domain no longer active or moved away .. \n"); } name = virDomainGetName(dom); //printf("Publishing Domain Name : %s \n ", name); if(name == NULL) { printf("Domain name not valid .. \n"); goto cont; } if(add_domain(dom) < 0) { printf("libvirt plugin malloc failed .. \n"); goto cont; } xml = virDomainGetXMLDesc(dom, 0); if(!xml) { printf("Virt domain xml description error ..\n"); goto cont; } //printf("Publishing XML : \n %s \n ", xml); xml_doc = xmlReadDoc((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET); if(xml_doc == NULL) { printf("XML read doc error ..\n"); goto cont; } xpath_ctx = xmlXPathNewContext(xml_doc); xpath_obj = xmlXPathEval((xmlChar *) "/domain/devices/disk/target[@dev]", xpath_ctx); if(xpath_obj == NULL || xpath_obj->type != XPATH_NODESET || xpath_obj->nodesetval == NULL) { goto cont; } for(j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) { xmlNodePtr node; char *path = NULL; node = xpath_obj->nodesetval->nodeTab[j]; if(!node) continue; path = (char *) xmlGetProp (node, (xmlChar *) "dev"); if(!path) continue; add_block_device(dom, path); } xmlXPathFreeObject(xpath_obj); xpath_obj = xmlXPathEval((xmlChar *) "/domain/devices/interface/target[@dev]", xpath_ctx); if(xpath_obj == NULL || xpath_obj->type != XPATH_NODESET || xpath_obj->nodesetval == NULL) { goto cont; } for(j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) { xmlNodePtr node; char *path = NULL; node = xpath_obj->nodesetval->nodeTab[j]; if(!node) continue; path = (char *) xmlGetProp(node, (xmlChar *) "dev"); if(!path) continue; add_interface_device(dom, path); } cont: if(xpath_obj) xmlXPathFreeObject(xpath_obj); if(xpath_ctx) xmlXPathFreeContext(xpath_ctx); if(xml_doc) xmlFreeDoc(xml_doc); if(xml) free(xml); } free(domids); return 0; }
static void save_obconf_settings (void) { const char *session_name; char *user_config_file, *c, *font, *fname; int count; const gchar *size = NULL, *bold = NULL, *italic = NULL; // construct the file path session_name = g_getenv ("DESKTOP_SESSION"); if (!session_name) session_name = DEFAULT_SES; fname = g_strconcat (g_ascii_strdown (session_name, -1), "-rc.xml", NULL); user_config_file = g_build_filename (g_get_user_config_dir (), "openbox/", fname, NULL); g_free (fname); // set the font description variables for XML from the font name font = g_strdup (desktop_font); while ((c = strrchr (font, ' '))) { if (!bold && !italic && !size && atoi (c + 1)) size = c + 1; else if (!bold && !italic && !g_ascii_strcasecmp (c + 1, "italic")) italic = c + 1; else if (!bold && !g_ascii_strcasecmp (c + 1, "bold")) bold = c + 1; else break; *c = '\0'; } if (!bold) bold = "Normal"; if (!italic) italic = "Normal"; // read in data from XML file xmlInitParser (); LIBXML_TEST_VERSION xmlDocPtr xDoc = xmlParseFile (user_config_file); if (xDoc == NULL) { g_free (font); g_free (user_config_file); return; } xmlXPathContextPtr xpathCtx = xmlXPathNewContext (xDoc); xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression ((xmlChar *) "/*[local-name()='openbox_config']/*[local-name()='theme']/*[local-name()='font']", xpathCtx); // update relevant nodes with new values for (count = 0; count < xpathObj->nodesetval->nodeNr; count++) { xmlNode *node = xpathObj->nodesetval->nodeTab[count]; xmlAttr *attr = node->properties; xmlNode *cur_node = NULL; for (cur_node = node->children; cur_node; cur_node = cur_node->next) { if (cur_node->type == XML_ELEMENT_NODE) { if (!strcmp (cur_node->name, "name")) xmlNodeSetContent (cur_node, font); if (!strcmp (cur_node->name, "size")) xmlNodeSetContent (cur_node, size); if (!strcmp (cur_node->name, "weight")) xmlNodeSetContent (cur_node, bold); if (!strcmp (cur_node->name, "slant")) xmlNodeSetContent (cur_node, italic); } } } // cleanup XML xmlXPathFreeObject (xpathObj); xmlXPathFreeContext (xpathCtx); xmlSaveFile (user_config_file, xDoc); xmlFreeDoc (xDoc); xmlCleanupParser (); g_free (font); g_free (user_config_file); }
static void check_themes (void) { const char *session_name, *ret; char *user_config_file, *cptr, *nptr, *fname; GKeyFile *kf; GError *err; int count; orig_lxsession_theme = ""; orig_openbox_theme = ""; // construct the file path for lxsession settings session_name = g_getenv ("DESKTOP_SESSION"); if (!session_name) session_name = DEFAULT_SES; user_config_file = g_build_filename (g_get_user_config_dir (), "lxsession/", session_name, "/desktop.conf", NULL); // read in data from file to a key file structure kf = g_key_file_new (); if (g_key_file_load_from_file (kf, user_config_file, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL)) { // get data from the key file err = NULL; ret = g_key_file_get_string (kf, "GTK", "sNet/ThemeName", &err); if (err == NULL) orig_lxsession_theme = ret; } // construct the file path for openbox settings fname = g_strconcat (g_ascii_strdown (session_name, -1), "-rc.xml", NULL); user_config_file = g_build_filename (g_get_user_config_dir (), "openbox/", fname, NULL); g_free (fname); // read in data from XML file xmlInitParser (); LIBXML_TEST_VERSION xmlDocPtr xDoc = xmlParseFile (user_config_file); if (xDoc) { xmlXPathContextPtr xpathCtx = xmlXPathNewContext (xDoc); xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression ((xmlChar *) "/*[local-name()='openbox_config']/*[local-name()='theme']", xpathCtx); // find relevant node and read value for (count = 0; count < xpathObj->nodesetval->nodeNr; count++) { xmlNode *node = xpathObj->nodesetval->nodeTab[count]; xmlAttr *attr = node->properties; xmlNode *cur_node = NULL; for (cur_node = node->children; cur_node; cur_node = cur_node->next) { if (cur_node->type == XML_ELEMENT_NODE) { if (!strcmp (cur_node->name, "name")) orig_openbox_theme = xmlNodeGetContent (cur_node); } } } // cleanup XML xmlXPathFreeObject (xpathObj); xmlXPathFreeContext (xpathCtx); xmlSaveFile (user_config_file, xDoc); xmlFreeDoc (xDoc); xmlCleanupParser (); } g_free (user_config_file); // set the new themes if needed if (strcmp ("PiX", orig_lxsession_theme)) { set_lxsession_theme ("PiX"); if (needs_refresh) system (RELOAD_LXSESSION); } if (strcmp ("PiX", orig_openbox_theme)) { set_openbox_theme ("PiX"); system (RELOAD_OPENBOX); } }