Ejemplo n.º 1
0
/* }}} */
ContactList *cl_new(Account *ac, const char *ticket)/*{{{*/
{
	CL *cl;
	cl = (CL*)xmalloc(sizeof(CL));
	memset(cl, 0, sizeof(CL));
	cl->account = ac;
	cl->ticket = (char*)xmalloc(strlen(ticket)+1);
	strcpy(cl->ticket, ticket);
	cl->table = xmlHashCreate(30);
	cl->emailtable = xmlHashCreate(250);

	return cl;
}/*}}}*/
void _cl_free_table(void *payload, xmlChar *name)/*{{{*/
{
}/*}}}*/
void _cl_free_emailtable(void *payload, xmlChar *name)/*{{{*/
{
}/*}}}*/
void cl_destroy(CL *cl)/*{{{*/
{
	char filename[64];
	Contact *tmp;
	char *dn = NULL;
	MD5((unsigned char*)cl->account->username, strlen(cl->account->username), (unsigned char*)filename);
	sprintf(filename, "%x%x.xml", (unsigned char)filename[0], (unsigned char)filename[1]);
	cl_save(cl, filename);
	xmlHashFree(cl->table, _cl_free_table);
	xmlHashFree(cl->emailtable, _cl_free_emailtable);
	while(cl->list)
	{
		if(cl->list->domain != dn)
		{
			dn = cl->list->domain;
			xfree(cl->list->domain);
		}
		tmp = cl->list->g_next;
		contact_destroy(cl->list);
		cl->list = tmp;
	}
	xfree(cl->lastchange);
	xfree(cl->ticket);
	xfree(cl);
}/*}}}*/
Ejemplo n.º 2
0
void close_session(struct handler_args* hargs, struct session* this_session){

    if (this_session == NULL) return;

    this_session->is_logged_in = false;
    if (this_session->logged_out_time == 0){
        this_session->logged_out_time = time(NULL);
    }    

    fprintf(hargs->log, "%f %d %s:%d Closing sesssion for %s\n",
        gettime(), hargs->request_id, __func__, __LINE__,
        this_session->user);

    if (this_session->pgtype_datum != NULL){
        xmlHashFree(this_session->pgtype_datum, (xmlHashDeallocator)xmlFree);
        this_session->pgtype_datum = NULL;
    }
    
    if (this_session->form_tags != NULL){
        xmlHashFree(this_session->form_tags, (xmlHashDeallocator)xmlFree);
        this_session->form_tags = NULL;
    }

    if (this_session->form_sets != NULL){
        clear_form_sets(this_session);
    }    

    if (this_session->conn != NULL){
        // close_all_tables  clears ->opentables
        close_all_tables(hargs, this_session);

        PQfinish(this_session->conn);
        
        this_session->conn = NULL;
    }else{
        if (this_session->opentables != NULL){
            // PG session ended, but local data needs cleanup
            fprintf(hargs->log, "%f %d %s:%d %s %s\n",
                gettime(), hargs->request_id, __func__, __LINE__,
                "clearing opentables on bad connection for",
                this_session->user);
            fflush(hargs->log); 

            xmlHashFree(this_session->opentables, 
                (xmlHashDeallocator) free_it_scanner);

            this_session->opentables = NULL;
        }
    }
    return;
}
Ejemplo n.º 3
0
static void
dlterm(void)

{
    void * p;

    if (dlflags & MULTIBUF) {
        p = pthread_getspecific(dlkey);

        if (p)
            dlthreadterm(p);
    }

    if (dlflags & THREADS)
        pthread_mutex_lock(&dlmutex);

    if (dldir) {
        xmlHashFree(dldir, (xmlHashDeallocator) NULL);
        dldir = NULL;
    }

    if (dlflags & MULTIBUF)
        pthread_key_delete(dlkey);

    dlflags |= ~(INITED | MULTIBUF);
    pthread_mutex_unlock(&dlmutex);
    pthread_mutex_destroy(&dlmutex);
}
Ejemplo n.º 4
0
/**
 * xsltFreeAttributeSetsHashes:
 * @style: an XSLT stylesheet
 *
 * Free up the memory used by attribute sets
 */
void
xsltFreeAttributeSetsHashes(xsltStylesheetPtr style) {
    if (style->attributeSets != NULL)
	xmlHashFree((xmlHashTablePtr) style->attributeSets,
		    (xmlHashDeallocator) xsltFreeAttrSet);
    style->attributeSets = NULL;
}
Ejemplo n.º 5
0
/**
 * xsltFreeKeyTable:
 * @keyt:  an XSLT key table
 *
 * Free up the memory allocated by @keyt
 */
static void
xsltFreeKeyTable(xsltKeyTablePtr keyt) {
    if (keyt == NULL)
	return;
    if (keyt->name != NULL)
	xmlFree(keyt->name);
    if (keyt->nameURI != NULL)
	xmlFree(keyt->nameURI);
    if (keyt->keys != NULL)
	xmlHashFree(keyt->keys,
		    (xmlHashDeallocator) xmlXPathFreeNodeSet);
    memset(keyt, -1, sizeof(xsltKeyTable));
    xmlFree(keyt);
}
Ejemplo n.º 6
0
Archivo: schema.c Proyecto: r6144/wine
static ULONG WINAPI schema_cache_Release(IXMLDOMSchemaCollection2* iface)
{
    schema_cache* This = impl_from_IXMLDOMSchemaCollection2(iface);
    LONG ref = InterlockedDecrement(&This->ref);
    TRACE("%p new ref %d\n", This, ref);

    if (ref == 0)
    {
        xmlHashFree(This->cache, cache_free);
        heap_free(This);
    }

    return ref;
}
Ejemplo n.º 7
0
/**
 * lineNoItemFree:
 * @item: valid hashtable of break points
 * 
 * Free @item and all its contents
 */
void
lineNoItemFree(void *item)
{
    xmlHashTablePtr hash = (xmlHashTablePtr) item;

    if (item) {
#if 0
#ifdef WITH_XSLDBG_DEBUG_BREAKPOINTS
        xsltGenericError(xsltGenericErrorContext,
                         "Freeing breakpoint line hash"
                         " with %d elements \n", xmlHashSize(item));
#endif
#endif
        xmlHashFree(hash, breakPointItemFree);
    }
}
Ejemplo n.º 8
0
static ULONG WINAPI schema_cache_Release(IXMLDOMSchemaCollection2* iface)
{
    schema_cache* This = impl_from_IXMLDOMSchemaCollection2(iface);
    LONG ref = InterlockedDecrement(&This->ref);
    TRACE("(%p)->(%d)\n", This, ref);

    if (ref == 0)
    {
        int i;

        for (i = 0; i < This->count; i++)
            heap_free(This->uris[i]);
        heap_free(This->uris);
        xmlHashFree(This->cache, cache_free);
        heap_free(This);
    }

    return ref;
}
Ejemplo n.º 9
0
/**
 * xsltResolveStylesheetAttributeSet:
 * @style:  the XSLT stylesheet
 *
 * resolve the references between attribute sets.
 */
void
xsltResolveStylesheetAttributeSet(xsltStylesheetPtr style) {
    xsltStylesheetPtr cur;

#ifdef WITH_XSLT_DEBUG_ATTRIBUTES
    xsltGenericDebug(xsltGenericDebugContext,
                     "Resolving attribute sets references\n");
#endif
    /*
     * First aggregate all the attribute sets definitions from the imports
     */
    cur = xsltNextImport(style);
    while (cur != NULL) {
        if (cur->attributeSets != NULL) {
            if (style->attributeSets == NULL) {
#ifdef WITH_XSLT_DEBUG_ATTRIBUTES
                xsltGenericDebug(xsltGenericDebugContext,
                                 "creating attribute set table\n");
#endif
                style->attributeSets = xmlHashCreate(10);
            }
            xmlHashScanFull(cur->attributeSets,
                            (xmlHashScannerFull) xsltMergeSASCallback, style);
            /*
             * the attribute lists have either been migrated to style
             * or freed directly in xsltMergeSASCallback()
             */
            xmlHashFree(cur->attributeSets, NULL);
            cur->attributeSets = NULL;
        }
        cur = xsltNextImport(cur);
    }

    /*
     * Then resolve all the references and computes the resulting sets
     */
    if (style->attributeSets != NULL) {
        xmlHashScanFull(style->attributeSets,
                        (xmlHashScannerFull) xsltResolveSASCallback, style);
    }
}
Ejemplo n.º 10
0
/**
 * xsltResolveStylesheetAttributeSet:
 * @style:  the XSLT stylesheet
 *
 * resolve the references between attribute sets.
 */
void
xsltResolveStylesheetAttributeSet(xsltStylesheetPtr style) {
    xsltStylesheetPtr cur;
    xsltAttrSetContext asctx;

#ifdef WITH_XSLT_DEBUG_ATTRIBUTES
    xsltGenericDebug(xsltGenericDebugContext,
	    "Resolving attribute sets references\n");
#endif
    asctx.topStyle = style;
    cur = style;
    while (cur != NULL) {
	if (cur->attributeSets != NULL) {
	    if (style->attributeSets == NULL) {
#ifdef WITH_XSLT_DEBUG_ATTRIBUTES
		xsltGenericDebug(xsltGenericDebugContext,
		    "creating attribute set table\n");
#endif
		style->attributeSets = xmlHashCreate(10);
	    }
            asctx.style = cur;
	    xmlHashScanFull(cur->attributeSets,
		(xmlHashScannerFull) xsltResolveSASCallback, &asctx);

            if (cur != style) {
                /*
                 * the attribute lists have either been migrated to style
                 * or freed directly in xsltResolveSASCallback()
                 */
                xmlHashFree(cur->attributeSets, NULL);
                cur->attributeSets = NULL;
            }
	}
	cur = xsltNextImport(cur);
    }
}
Ejemplo n.º 11
0
int nominatim_import(const char *conninfo, const char *partionTagsFilename, const char *filename)
{
    xmlTextReaderPtr	reader;
    int 				ret = 0;
    PGresult * 			res;
    FILE *				partionTagsFile;
    char * 				partionQueryName;
    char 				partionQuerySQL[1024];

    conn = PQconnectdb(conninfo);
    if (PQstatus(conn) != CONNECTION_OK)
    {
        fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(conn));
        exit(EXIT_FAILURE);
    }

    partionTableTagsHash = xmlHashCreate(200);
    partionTableTagsHashDelete = xmlHashCreate(200);

    partionTagsFile = fopen(partionTagsFilename, "rt");
    if (!partionTagsFile)
    {
        fprintf(stderr, "Unable to read partition tags file: %s\n", partionTagsFilename);
        exit(EXIT_FAILURE);
    }

    char buffer[1024], osmkey[256], osmvalue[256];
    int fields;
    while (fgets(buffer, sizeof(buffer), partionTagsFile) != NULL)
    {
        fields = sscanf( buffer, "%23s %63s", osmkey, osmvalue );

        if ( fields <= 0 ) continue;

        if ( fields != 2  )
        {
            fprintf( stderr, "Error partition file\n");
            exit_nicely();
        }
        partionQueryName = malloc(strlen("partition_insert_")+strlen(osmkey)+strlen(osmvalue)+2);
        strcpy(partionQueryName, "partition_insert_");
        strcat(partionQueryName, osmkey);
        strcat(partionQueryName, "_");
        strcat(partionQueryName, osmvalue);

        strcpy(partionQuerySQL, "insert into place_classtype_");
        strcat(partionQuerySQL, osmkey);
        strcat(partionQuerySQL, "_");
        strcat(partionQuerySQL, osmvalue);
        strcat(partionQuerySQL, " (place_id, centroid) values ($1, ST_Centroid(st_setsrid($2, 4326)))");

        res = PQprepare(conn, partionQueryName, partionQuerySQL, 2, NULL);
        if (PQresultStatus(res) != PGRES_COMMAND_OK)
        {
            fprintf(stderr, "Failed to prepare %s: %s\n", partionQueryName, PQerrorMessage(conn));
            exit(EXIT_FAILURE);
        }

        xmlHashAddEntry2(partionTableTagsHash, BAD_CAST osmkey, BAD_CAST osmvalue, BAD_CAST partionQueryName);

        partionQueryName = malloc(strlen("partition_delete_")+strlen(osmkey)+strlen(osmvalue)+2);
        strcpy(partionQueryName, "partition_delete_");
        strcat(partionQueryName, osmkey);
        strcat(partionQueryName, "_");
        strcat(partionQueryName, osmvalue);

        strcpy(partionQuerySQL, "delete from place_classtype_");
        strcat(partionQuerySQL, osmkey);
        strcat(partionQuerySQL, "_");
        strcat(partionQuerySQL, osmvalue);
        strcat(partionQuerySQL, " where place_id = $1::integer");

        res = PQprepare(conn, partionQueryName, partionQuerySQL, 1, NULL);
        if (PQresultStatus(res) != PGRES_COMMAND_OK)
        {
            fprintf(stderr, "Failed to prepare %s: %s\n", partionQueryName, PQerrorMessage(conn));
            exit(EXIT_FAILURE);
        }

        xmlHashAddEntry2(partionTableTagsHashDelete, BAD_CAST osmkey, BAD_CAST osmvalue, BAD_CAST partionQueryName);
    }

    res = PQprepare(conn, "get_new_place_id",
                    "select nextval('seq_place')",
                    0, NULL);
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "Failed to prepare get_new_place_id: %s\n", PQerrorMessage(conn));
        exit(EXIT_FAILURE);
    }

    res = PQprepare(conn, "get_place_id",
                    "select place_id from placex where osm_type = $1 and osm_id = $2 and class = $3 and type = $4",
                    4, NULL);
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "Failed to prepare get_place_id: %s\n", PQerrorMessage(conn));
        exit(EXIT_FAILURE);
    }

    res = PQprepare(conn, "placex_insert",
                    "insert into placex (place_id,osm_type,osm_id,class,type,name,country_code,extratags,parent_place_id,admin_level,housenumber,rank_address,rank_search,geometry) "
                    "values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, st_setsrid($14, 4326))",
                    12, NULL);
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "Failed to prepare placex_insert: %s\n", PQerrorMessage(conn));
        exit(EXIT_FAILURE);
    }

    res = PQprepare(conn, "search_name_insert",
                    "insert into search_name (place_id, search_rank, address_rank, country_code, name_vector, nameaddress_vector, centroid) "
                    "select place_id, rank_search, rank_address, country_code, make_keywords(name), "
                    "(select uniq(sort(array_agg(parent_search_name.name_vector))) from search_name as parent_search_name where place_id in "
                     "(select distinct address_place_id from place_addressline where place_addressline.place_id = $1 limit 1000)"
                    "), st_centroid(geometry) from placex "
                    "where place_id = $1",
                    1, NULL);

    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "Failed to prepare search_name_insert: %s\n", PQerrorMessage(conn));
        exit(EXIT_FAILURE);
    }

    res = PQprepare(conn, "search_name_from_parent_insert",
                    "insert into search_name (place_id, search_rank, address_rank, country_code, name_vector, nameaddress_vector, centroid) "
                    "select place_id, rank_search, rank_address, country_code, make_keywords(name), "
                    "(select uniq(sort(name_vector+nameaddress_vector)) from search_name as parent_search_name "
                    "where parent_search_name.place_id = $2 ), st_centroid(geometry) from placex "
                    "where place_id = $1",
                    2, NULL);
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "Failed to prepare search_name_insert: %s\n", PQerrorMessage(conn));
        exit(EXIT_FAILURE);
    }

    res = PQprepare(conn, "place_addressline_insert",
                    "insert into place_addressline (place_id, address_place_id, fromarea, isaddress, distance, cached_rank_address) "
                    "select $1, place_id, false, $7, $2, rank_address from placex where osm_type = $3 and osm_id = $4 and class = $5 and type = $6",
                    7, NULL);
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "Failed to prepare place_addressline_insert: %s\n", PQerrorMessage(conn));
        exit(EXIT_FAILURE);
    }

    res = PQprepare(conn, "placex_delete",
                    "delete from placex where place_id = $1",
                    1, NULL);
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "Failed to prepare placex_delete: %s\n", PQerrorMessage(conn));
        exit(EXIT_FAILURE);
    }

    res = PQprepare(conn, "search_name_delete",
                    "delete from search_name where place_id = $1",
                    1, NULL);
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "Failed to prepare search_name_delete: %s\n", PQerrorMessage(conn));
        exit(EXIT_FAILURE);
    }

    res = PQprepare(conn, "place_addressline_delete",
                    "delete from place_addressline where place_id = $1",
                    1, NULL);
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "Failed to prepare place_addressline_delete: %s\n", PQerrorMessage(conn));
        exit(EXIT_FAILURE);
    }

    featureCount = 0;

    reader = inputUTF8(filename);

    if (reader == NULL)
    {
        fprintf(stderr, "Unable to open %s\n", filename);
        return 1;
    }

    ret = xmlTextReaderRead(reader);
    while (ret == 1)
    {
        processNode(reader);
        ret = xmlTextReaderRead(reader);
    }
    if (ret != 0)
    {
        fprintf(stderr, "%s : failed to parse\n", filename);
        return ret;
    }

    xmlFreeTextReader(reader);
    xmlHashFree(partionTableTagsHash, NULL);
    xmlHashFree(partionTableTagsHashDelete, NULL);

    return 0;
}
Ejemplo n.º 12
0
int
test (const char* base) {
    xmlDocPtr docPtr = NULL;
    char filename[100];
    xmlSchemaPtr wxschemas = NULL;

    Handle2Path = xmlHashCreate(0);

    /* Read the schema. */
    {
        /* There is no visitibility into parserCtxt. */
	xmlSchemaParserCtxtPtr parserCtxt;

        /* parserCtxt->ctxtType is xmlSchemaTypePtr */

        snprintf(filename, 100, "%s.xsd", base);
        printf("\n\n\n----------------------------------------------------------------\n\n\n");
        printf("\n----> Reading schema %s...\n", filename);

	parserCtxt = xmlSchemaNewParserCtxt(filename);
	xmlSchemaSetParserErrors(parserCtxt,
		(xmlSchemaValidityErrorFunc) fprintf,
		(xmlSchemaValidityWarningFunc) fprintf,
		stdout);
	xmlSchemaSetParserAnnotation(parserCtxt, schema_annotation_callback, NULL);
	wxschemas = xmlSchemaParse(parserCtxt);
	if (wxschemas == NULL)
        {
            printf("***** schema parsing failed!\n");
	}
	xmlSchemaFreeParserCtxt(parserCtxt);
        printf("\n<---- Schema read!\n\n");
    }

    /* Read the XML. */
    {
        snprintf(filename, 100, "%s.xml", base);
        if ((docPtr = xmlReadFile(filename, NULL, 0)) == NULL)
        {
            printf("failed to parse \"%s\".\n", filename);
            return -1;
        }
    }

    if (!SKIP) {
        /* There is no visibility into schemaCtxt. */
	xmlSchemaValidCtxtPtr schemaCtxt;
	int ret;

        printf("\n----------------------------------------------------------------\n");
        printf("\n----> Validating document %s...\n", filename);

        /* This sets up the schemaCtxt, including a pointer to wxschemas. */
	schemaCtxt = xmlSchemaNewValidCtxt(wxschemas);
	xmlSchemaSetValidErrors(schemaCtxt,
		(xmlSchemaValidityErrorFunc) fprintf,
		(xmlSchemaValidityWarningFunc) fprintf,
		stdout);
	ret = xmlSchemaValidateDoc(schemaCtxt, docPtr);	/* read me! */
	if (ret == 0)
        {
	    /* printf("%s validates\n", filename); */
	}
        else if (ret > 0)
        {
	    printf("%s fails to validate\n", filename);
	}
        else
        {
	    printf("%s validation generated an internal error\n",
		   filename);
	}
	xmlSchemaFreeValidCtxt(schemaCtxt);
        printf("\n<---- Document validated!\n");

    }

    /* Generate a doc and validate it. */
    {
	xmlDocPtr newDoc = xmlNewDoc(BAD_CAST "1.0");
	{
	    xmlSchemaValidCtxtPtr schemaCtxt;
	    int ret;

	    schemaCtxt = xmlSchemaNewValidCtxt(wxschemas);
	    xmlSchemaSetValidErrors(schemaCtxt,
				    (xmlSchemaValidityErrorFunc) fprintf,
				    (xmlSchemaValidityWarningFunc) fprintf,
				    stdout);
	    xmlSchemaSetGeneratorCallback(schemaCtxt, BAD_CAST "people", NULL, &generation_callback, newDoc);
	    ret = xmlSchemaValidateDoc(schemaCtxt, newDoc);
	    if (ret == 0) {
/* 		xmlDocSetRootElement(newDoc, vctxt->node); */
		dump_doc(newDoc, NULL);
	    } else if (ret > 0)
		printf("%s fails to validate\n", filename);
	    else
		printf("%s validation generated an internal error\n",
		       filename);
	    xmlSchemaFreeValidCtxt(schemaCtxt);
	    printf("\n<---- Schema read!\n\n");
	}

	{
	    xmlSchemaValidCtxtPtr schemaCtxt;
	    int ret;

	    schemaCtxt = xmlSchemaNewValidCtxt(wxschemas);
	    xmlSchemaSetValidErrors(schemaCtxt,
				    (xmlSchemaValidityErrorFunc) fprintf,
				    (xmlSchemaValidityWarningFunc) fprintf,
				    stdout);
	    ret = xmlSchemaValidateDoc(schemaCtxt, newDoc);
	    if (ret == 0)
		;
	    else if (ret > 0)
		printf("%s fails to validate\n", filename);
	    else
		printf("%s validation generated an internal error\n",
		       filename);
	    xmlSchemaFreeValidCtxt(schemaCtxt);
	    printf("\n<---- Schema read!\n\n");
	}

	xmlFreeDoc(newDoc);
    }

#if 0
    /* why can't I just start with doc->children? */
    tree_trunk = xmlDocGetRootElement(docPtr);
#endif

#if 0
    tree_trunk = docPtr->children;

    printf("\n\n\n----------------------------------------------------------------\n\n\n");
    printf("\nWalking doc tree...\n");
    walk_doc_tree(tree_trunk, 0);
    printf("\n");
#endif
    printf("\n\n\n----------------------------------------------------------------\n\n\n");
    printf("\nWalking schema tree...\n");
    walk_schema_tree(wxschemas);
    printf("\n");

    /*****************************************************************/
    /*****************************************************************/
    /*****************************************************************/
    /*****************************************************************/
    /* This will tell me, for example, how to decode sequences. */
    printf("\n\n\n----------------------------------------------------------------\n\n\n");
    xmlSchemaDump(stdout, wxschemas);
    /*****************************************************************/
    /*****************************************************************/
    /*****************************************************************/
    /*****************************************************************/

    xmlFreeDoc(docPtr);

    xmlCleanupParser();

    xmlHashFree(Handle2Path, NULL);

    return 0;
}
Ejemplo n.º 13
0
/**
 * xmlFreeEntitiesTable:
 * @table:  An entity table
 *
 * Deallocate the memory used by an entities hash table.
 */
void
xmlFreeEntitiesTable(xmlEntitiesTablePtr table) {
    xmlHashFree(table, (xmlHashDeallocator) xmlFreeEntity);
}
Ejemplo n.º 14
0
/**
 *  This is the main function for 'el' option
 */
int
elMain(int argc, char **argv)
{
    int errorno = 0;
    char* inp_file = "-";

    if (argc <= 1) elUsage(argc, argv, EXIT_BAD_ARGS);

    elInitOptions(&elOps);

    if (argc == 2)
        errorno = parse_xml_file("-");  
    else
    {
        if (!strcmp(argv[2], "--help") || !strcmp(argv[2], "-h") ||
            !strcmp(argv[2], "-?") || !strcmp(argv[2], "-Z"))
        {
            elUsage(argc, argv, EXIT_SUCCESS);
        }
        else if (!strcmp(argv[2], "-a"))
        {
            elOps.show_attr = 1;
            if (argc >= 4) inp_file = argv[3];
            errorno = parse_xml_file(inp_file);
        }
        else if (!strcmp(argv[2], "-v"))
        {
            elOps.show_attr_and_val = 1;
            if (argc >= 4) inp_file = argv[3];
            errorno = parse_xml_file(inp_file);
        }
        else if (!strcmp(argv[2], "-u"))
        {
            elOps.sort_uniq = 1;
            if (argc >= 4) inp_file = argv[3];
            uniq = xmlHashCreate(0);
            errorno = parse_xml_file(inp_file);
        }
        else if (!strncmp(argv[2], "-d", 2)) 
        { 
            elOps.check_depth = atoi(argv[2]+2); 
            /* printf("Checking depth (%d)\n", elOps.check_depth); */ 
            elOps.sort_uniq = 1; 
            if (argc >= 4) inp_file = argv[3];
            uniq = xmlHashCreate(0);
            errorno = parse_xml_file(inp_file); 
        }
        else if (argv[2][0] != '-')
        {
            errorno = parse_xml_file(argv[2]);
        }
        else
            elUsage(argc, argv, EXIT_BAD_ARGS);
    }

    if (uniq)
    {
        int i;
        ArrayDest lines;
        lines.array = xmlMalloc(sizeof(xmlChar*) * xmlHashSize(uniq));
        lines.offset = 0;
        xmlHashScan(uniq, hash_key_put, &lines);

        qsort(lines.array, lines.offset, sizeof(xmlChar*), compare_string_ptr);

        for (i = 0; i < lines.offset; i++)
        {
            printf("%s\n", lines.array[i]);
        }

        xmlFree(lines.array);
        xmlHashFree(uniq, NULL);
    }

    return errorno;
}
Ejemplo n.º 15
0
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(&current_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;
}
Ejemplo n.º 16
0
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(&current_time, &time_tm1);
    localtime_r(&current_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(&current_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(&current_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;
}
Ejemplo n.º 17
0
/**
 * xsltFreeNamespaceAliasHashes:
 * @style: an XSLT stylesheet
 *
 * Free up the memory used by namespaces aliases
 */
void
xsltFreeNamespaceAliasHashes(xsltStylesheetPtr style) {
    if (style->nsAliases != NULL)
	xmlHashFree((xmlHashTablePtr) style->nsAliases, NULL);
    style->nsAliases = NULL;
}