Beispiel #1
0
int print_xml_time_element(xmlTextWriterPtr writer, const char *name,
                            struct timeval *time) {
    int rc;

    rc = xmlTextWriterStartElement(writer, BAD_CAST name);
    if(rc < 0) {
        printf("print_xml_time_element: Error opening time element\n");
        return rc;
    }

    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "seconds", "%ld",
                                         (long)time->tv_sec);
    if(rc < 0) {
        printf("print_xml_time_element: Error writing seconds element\n");
        return rc;
    }

    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "useconds", "%ld",
                                         (long)time->tv_usec);
    if(rc < 0) {
        printf("print_xml_time_element: Error writing useconds\n");
        return rc;
    }

    rc = xmlTextWriterEndElement(writer);
    if(rc < 0) {
        printf("print_xml_time_element: Error closing time element\n");
        return rc;
    }

    return rc;

}
static void
default_fill_writer (ProponoConnection *conn, xmlTextWriter *writer)
{
  if (conn->priv->protocol)
    xmlTextWriterWriteElement (writer, "protocol", conn->priv->protocol);
  xmlTextWriterWriteElement (writer, "name", conn->priv->name);
  xmlTextWriterWriteElement (writer, "host", conn->priv->host);
  xmlTextWriterWriteElement (writer, "username", conn->priv->username ? conn->priv->username : "");
  xmlTextWriterWriteFormatElement (writer, "port", "%d", conn->priv->port);
  xmlTextWriterWriteFormatElement (writer, "fullscreen", "%d", conn->priv->fullscreen);
}
Beispiel #3
0
// SAX save settings
short int settingsXmlSave(const char *filename) {

    int rc;
    xmlTextWriterPtr writer;

    // Create a new XmlWriter with no compression
    writer = xmlNewTextWriterFilename(filename, 0);
    if (writer == NULL) return 1;

    // Start document with xml default encoding ISO 8859-1
    rc = xmlTextWriterStartDocument(writer, NULL, XML_ENCODING, NULL);
    if (rc < 0) return 2;

    // Start root node "settings"
    rc = xmlTextWriterStartElement(writer, BAD_CAST "settings");
    if (rc < 0) return 2;

	// Create child node "xap"
    rc = xmlTextWriterStartElement(writer, BAD_CAST "xap");
    if (rc < 0) return 2;
    
	// Write <xap> elements
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "iface", "%s", hubConfig->interfacename);
	if (rc < 0) return 4;
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "instance", "%s", hubConfig->xap_addr.instance);
	if (rc < 0) return 4;
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "hbeatf", "%d", hubConfig->xap_hbeat);
    if (rc < 0) return 4;
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "uid", "%s", hubConfig->xap_uid);
    if (rc < 0) return 4;
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "port", "%d", hubConfig->xap_port);
    if (rc < 0) return 4;        
    
    // Close node "xap"
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) return 5;
    
    // Close the rest nodes pending to close
    rc = xmlTextWriterEndDocument(writer);
	if (rc < 0) return 5;

	// Free the writer
    xmlFreeTextWriter(writer);
    
    // Cleanup function for the XML library.
    xmlCleanupParser();
    
    // this is to debug memory for regression tests
    xmlMemoryDump();
    
    return 0;
}
Beispiel #4
0
static void
default_fill_writer (VinagreConnection *conn, xmlTextWriter *writer)
{
  if (conn->priv->protocol)
    xmlTextWriterWriteElement (writer, BAD_CAST "protocol", BAD_CAST conn->priv->protocol);
  xmlTextWriterWriteElement (writer, BAD_CAST "name", BAD_CAST conn->priv->name);
  xmlTextWriterWriteElement (writer, BAD_CAST "host", BAD_CAST conn->priv->host);
  xmlTextWriterWriteElement (writer, BAD_CAST "username", BAD_CAST (conn->priv->username ? conn->priv->username : ""));
  xmlTextWriterWriteFormatElement (writer, BAD_CAST "port", "%d", conn->priv->port);
  xmlTextWriterWriteFormatElement (writer, BAD_CAST "fullscreen", "%d", conn->priv->fullscreen);
  xmlTextWriterWriteFormatElement (writer, BAD_CAST "width", "%d", conn->priv->width);
  xmlTextWriterWriteFormatElement (writer, BAD_CAST "height", "%d", conn->priv->height);
}
Beispiel #5
0
int save_gpx(const char *fname,GList *save_list)
{
  time_t t=time(NULL);
  struct tm tm;
  GList *l;
  xmlTextWriterPtr writer;
  writer=xmlNewTextWriterFilename(fname,0);
  if (!writer)
    return 0;
  xmlTextWriterStartDocument(writer,NULL,"UTF-8",NULL);
  xmlTextWriterStartElement(writer,(xmlChar *)"gpx");
  xmlTextWriterWriteAttribute(writer,(xmlChar *)"version",(xmlChar *)"1.0");
  xmlTextWriterWriteAttribute(writer,(xmlChar *)"creator",(xmlChar *)PACKAGE " " VERSION);
  xmlTextWriterWriteAttribute(writer,(xmlChar *)"xmlns:xsi",(xmlChar *)"http://www.w3.org/2001/XMLSchema-instance");
  xmlTextWriterWriteAttribute(writer,(xmlChar *)"xmlns",
			      (xmlChar *)"http://www.topografix.com/GPX/1/0");
  xmlTextWriterWriteAttribute(writer,(xmlChar *)"xsi:schemaLocation",(xmlChar *)"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd");
  tm=*gmtime(&t);
  xmlTextWriterWriteFormatElement(writer,(xmlChar *)"time","%04d-%02d-%02dT%02d:%02d:%02d",
			    tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,
			    tm.tm_hour,tm.tm_min,tm.tm_sec);
  xmlTextWriterStartElement(writer,(xmlChar *)"trk");
  xmlTextWriterStartElement(writer,(xmlChar *)"trkseg");
  for(l=g_list_first(save_list);l;l=g_list_next(l)) {
    struct t_punkt32 *p=(struct t_punkt32 *)l->data;
    xmlTextWriterStartElement(writer,(xmlChar *)"trkpt");
    xmlTextWriterWriteFormatAttribute(writer,(xmlChar *)"lat","%f",
				      p->latt);
    xmlTextWriterWriteFormatAttribute(writer,(xmlChar *)"lon","%f",
				      p->longg);
    if (p->time) {
      t=p->time;
      tm=*gmtime(&t);
      xmlTextWriterWriteFormatElement(writer,(xmlChar*)"time",
				"%04d-%02d-%02dT%02d:%02d:%02d",
				tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,
				tm.tm_hour,tm.tm_min,tm.tm_sec);
    }
    if (p->speed>0)
      xmlTextWriterWriteFormatElement(writer,(xmlChar *)"speed","%f",p->speed*1.852/3.6);
    xmlTextWriterEndElement(writer); /* /trkpt */
    
  }
  xmlTextWriterEndElement(writer); /*trkseg */
  xmlTextWriterEndElement(writer); /* trk */
  xmlTextWriterEndElement(writer); /* gpx */
  xmlTextWriterEndDocument(writer);
  xmlFreeTextWriter(writer);
  return 1;
}
Beispiel #6
0
char *gen_lote_xml(LOTE *lote, EVP_PKEY *key, X509 *cert){
	int rc;
	xmlTextWriterPtr writer;
	xmlDocPtr doc;
	xmlBufferPtr buf = xmlBufferCreate();

	writer = xmlNewTextWriterDoc(&doc, 0);
	if (writer == NULL)
		return NULL;
	xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL);
	rc = xmlTextWriterStartElement(writer, BAD_CAST "enviNFe");
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xmlns",
			BAD_CAST "http://www.portalfiscal.inf.br/nfe");
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "versao",
			BAD_CAST NFE_VERSAO);
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "idLote",
			"%d", lote->id);
	if (rc < 0)
		return NULL;
	int indSinc = lote->qtd == 1? 1 : 0;
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "indSinc",
			"%d", indSinc);
	if (rc < 0)
		return NULL;
	int i;
	LOTE_ITEM *it = lote->nfes;
	for (i = 0; i < lote->qtd; i++){
		char *xml;
		xml = generate_xml(it->nfe, key, cert);
		printf("%s\n", xml);
		rc = xmlTextWriterWriteRaw(writer, BAD_CAST xml);
		if (rc < 0)
			return NULL;
		it = it->next;
	}
	rc = xmlTextWriterEndElement(writer);
	if (rc < 0)
		return NULL;
	xmlTextWriterEndDocument(writer);
	xmlNodeDump(buf, NULL, xmlDocGetRootElement(doc), 0, 0);
	return (char*)buf->content;
}
Beispiel #7
0
int write_volumeindex(opendcp_t *opendcp) {
    xmlIndentTreeOutput = 1;
    xmlDocPtr        doc;
    xmlTextWriterPtr xml;
    int              rc;

    dcp_log(LOG_INFO,"Writing VOLINDEX file %.256s",opendcp->volindex.filename);

    /* create XML document */
    xml = xmlNewTextWriterDoc(&doc,0);

    /* volumeindex XML Start */
    rc = xmlTextWriterStartDocument(xml, NULL, XML_ENCODING, NULL);
    if (rc < 0) {
        dcp_log(LOG_ERROR,"xmlTextWriterStartDocument failed");
        return DCP_FATAL;
    }

    xmlTextWriterStartElement(xml, BAD_CAST "VolumeIndex");
    xmlTextWriterWriteAttribute(xml, BAD_CAST "xmlns", BAD_CAST NS_AM[opendcp->ns]);
    xmlTextWriterWriteFormatElement(xml, BAD_CAST "Index","%d",1);
    xmlTextWriterEndElement(xml); 

    rc = xmlTextWriterEndDocument(xml);
    if (rc < 0) {
        dcp_log(LOG_ERROR,"xmlTextWriterEndDocument failed %s",opendcp->volindex.filename);
        return DCP_FATAL;
    }

    xmlFreeTextWriter(xml);
    xmlSaveFormatFile(opendcp->volindex.filename, doc, 1);
    xmlFreeDoc(doc);

    return DCP_SUCCESS;
}
Beispiel #8
0
char* fileinfo_get(char* path) {
    FILE* file;
    xmlBufferPtr buf;
    xmlTextWriterPtr writer;
    char* ret;
    int wordCount;

    file  = fopen(path, "r");
    wordCount = countWords(file);
    fclose(file);

    buf = xmlBufferCreate();
    writer = xmlNewTextWriterMemory(buf, 0);

    xmlTextWriterStartDocument(writer, NULL, XML_E, NULL);


    xmlTextWriterStartElement(writer, BAD_CAST "fileInfo");
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "wordCount", "%d", wordCount);
    xmlTextWriterEndElement(writer);

    xmlTextWriterEndDocument(writer);

    ret = malloc(buf->size);
    strcpy(ret,(char*)buf->content);
    xmlBufferFree(buf);

    return ret;
}
Beispiel #9
0
int generate_report(char dst_dir[DIR_LENGTH_MAX], char filename[FILENAME_LENGTH_MAX]) {
	// for the xml writer, refer to http://xmlsoft.org/html/libxml-xmlwriter.html 
	
	char dst_file[FILENAME_LENGTH_MAX + DIR_LENGTH_MAX] = "";
	strcat(dst_file, dst_dir);
	strcat(dst_file, filename);
	
	xmlTextWriterPtr writer;

	writer = xmlNewTextWriterFilename(dst_file, 0);
	
	// set the output format of the XML file 
	xmlTextWriterSetIndent(writer, 1);
	xmlTextWriterSetIndentString(writer, "	");

	xmlTextWriterStartDocument(writer, NULL, NULL, NULL);

    /* Write an element named "X_ORDER_ID" as child of HEADER. */
   xmlTextWriterWriteFormatElement(writer, "COMMENT           ", "	in this output file, %d means NOT_PROCESSED; %d means NO_FILE; %d means ERROR; %d means OK	", MODULE_NOT_PROCESSED, NO_FILE, MODULE_ERROR, MODULE_OK);
	xmlTextWriterWriteFormatElement(writer, "OCG_GET_OPT       ", "	%d	", get_opt_OK);
	xmlTextWriterWriteFormatElement(writer, "OCG_DETECT_FILE   ", "	%d	", detect_file_OK);
	xmlTextWriterWriteFormatElement(writer, "OCG_PARSE_FILENAME", "	%d	", parse_filename_OK);
	xmlTextWriterWriteFormatElement(writer, "OCG_CREATE_DIR    ", "	%d	", create_dir_OK);
	xmlTextWriterWriteFormatElement(writer, "OCG_PARSE_XML     ", "	%d	", parse_XML_OK);
	xmlTextWriterWriteFormatElement(writer, "OCG_SAVE_XML      ", "	%d	", save_XML_OK);
//	xmlTextWriterWriteFormatElement(writer, "OCG_CALL_EMU      ", "	%d	", call_emu_OK);

	xmlTextWriterEndDocument(writer);

	xmlFreeTextWriter(writer);

	LOG_I(OCG, "A report of OCG is generated in directory \"%s\"\n\n", dst_dir);
	return MODULE_OK;
}
Beispiel #10
0
char *gen_cons_status(int ambiente, int cuf){
	int rc;
	xmlTextWriterPtr writer;
	xmlDocPtr doc;
	xmlBufferPtr buf = xmlBufferCreate();
	
	writer = xmlNewTextWriterDoc(&doc, 0);
	if (writer == NULL) {
		printf("Error NewDoc");
		return NULL;
	}
	xmlTextWriterStartDocument(writer, NULL, "utf-8", NULL);
	rc = xmlTextWriterStartElement(writer, BAD_CAST "consStatServ");
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xmlns",
			BAD_CAST "http://www.portalfiscal.inf.br/nfe");
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "versao",
			BAD_CAST NFE_VERSAO);
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "tpAmb",
			"%d", ambiente);
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "cUF",
			"%d", cuf);
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "xServ",
			"%s", "STATUS");
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterEndElement(writer);
	if (rc < 0)
		return NULL;
	xmlTextWriterEndDocument(writer);
	xmlNodeDump(buf, NULL, xmlDocGetRootElement(doc), 0, 0);
	return (char*) buf->content;
}
Beispiel #11
0
char *gen_cons_nfe(LOTE *lote, int ambiente){
	int rc;
	xmlTextWriterPtr writer;
	xmlDocPtr doc;
	xmlBufferPtr buf = xmlBufferCreate();
	
	writer = xmlNewTextWriterDoc(&doc, 0);
	if (writer == NULL)
		return NULL;
	xmlTextWriterStartDocument(writer, NULL, "utf-8", NULL);
	rc = xmlTextWriterStartElement(writer, BAD_CAST "consReciNFe");
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xmlns",
			BAD_CAST "http://www.portalfiscal.inf.br/nfe");
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "versao",
			BAD_CAST NFE_VERSAO);
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "tpAmb",
			"%d", ambiente);
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "nRec",
			"%s", lote->recibo);
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterEndElement(writer);
	if (rc < 0)
		return NULL;
	xmlTextWriterEndDocument(writer);
	xmlNodeDump(buf, NULL, xmlDocGetRootElement(doc), 0, 0);
	return (char*)buf->content;
}
Beispiel #12
0
void write_config()
{
	const char *uri=config_path;
	int rc;
	char str_ip[IP6_STR_SIZE];
	xmlTextWriterPtr writer;
	router_list_t *tmp = routers;
#ifdef _COUNTERMEASURES_
	char config_kill_illegitimate_router[CM_GUARD_REPRESENTATION_SIZE];
	char config_kill_wrong_prefix[CM_GUARD_REPRESENTATION_SIZE];
	char config_propagate_router_params[CM_GUARD_REPRESENTATION_SIZE];
	char config_indicate_ndpmon_presence[CM_GUARD_REPRESENTATION_SIZE];
#endif

	printf("Writing config...\n");
	print_routers(routers);

	/* Create a new XmlWriter for uri, with no compression. */
	writer = xmlNewTextWriterFilename(uri, 0);
	if (writer == NULL)
	{
		printf("testXmlwriterFilename: Error creating the xml writer\n");
		return;
	}

	xmlTextWriterSetIndent(writer, 1);

	/* Start the document with the xml default for the version,
	 * encoding ISO 8859-1 and the default for the standalone
	 * declaration. */
	rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterStartDocument\n");
		return;
	}

	xmlTextWriterStartDTD	(writer, (xmlChar*)"config_ndpmon", NULL, (xmlChar*)dtd_config_path);
	xmlTextWriterEndDTD (writer);

	/* Give the stylesheet for display in the web interface */
	xmlTextWriterWriteRaw(writer, (xmlChar*)"<?xml-stylesheet type=\"text/xsl\" href=\"config.xsl\" ?>\n");

	/* Start an element named "config_ndpmon". Since this is the first
	 * element, this will be the root element of the document. */
	rc = xmlTextWriterStartElement(writer, BAD_CAST "config_ndpmon");
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
		return;
	}

	/* Attribute ignor_autoconf */
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ignor_autoconf", "%d", ignor_autoconf);
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
		return;
	}
	
	/* Attribute syslog_facility */
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "syslog_facility", "%s", syslog_facility);
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
		return;
	}

	/* Attribute admin_mail */
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "admin_mail", "%s", admin_mail);
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
		return;
	}


	/* Elements of actions_low_pri */
	rc =  xmlTextWriterStartElement(writer, BAD_CAST "actions_low_pri");
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
		return;
	}

	/* Attribute sendmail */
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "sendmail", "%d", action_low_pri.sendmail);
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
		return;
	}

	/* Attribute syslog */
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "syslog", "%d", action_low_pri.syslog);
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
		return;
	}

	
	/* Attribute exec_pipe_program */
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "exec_pipe_program", "%s", action_low_pri.exec_pipe_program);
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
		return;
	}


	/* Close  actions_low_pri  */
	rc = xmlTextWriterEndElement(writer);
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
		return;
	}


	/* Elements of actions_high_pri */
	rc =  xmlTextWriterStartElement(writer, BAD_CAST "actions_high_pri");
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
		return;
	}

	/* Attribute sendmail */
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "sendmail", "%d", action_high_pri.sendmail);
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
		return;
	}

	/* Attribute syslog */
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "syslog", "%d", action_high_pri.syslog);
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
		return;
	}

	
	/* Attribute exec_pipe_program */
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "exec_pipe_program", "%s", action_high_pri.exec_pipe_program);
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
		return;
	}


	/* Close  actions_high_pri  */
	rc = xmlTextWriterEndElement(writer);
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
		return;
	}


	/* Attribute use_reverse_hostlookups */
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "use_reverse_hostlookups", "%d", use_reverse_hostlookups);
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
		return;
	}

	

	/* Start an element named routers containing the routers' definition */
	rc = xmlTextWriterStartElement(writer, BAD_CAST "routers");
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
		return;
	}


	/*for each router a new neighbor with its attributes is created in the file */
	while(tmp != NULL)
	{
		address_t *tmp_address = tmp->addresses;
		prefix_t *tmp_prefix   = tmp->prefixes;
		ipv6_ntoa(str_ip, tmp->lla);

		/* Start an element named "router" as child of routers. */
		rc = xmlTextWriterStartElement(writer, BAD_CAST "router");
		if (rc < 0) goto start_element_error;
		/* Element mac */
		rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "mac", "%s", ether_ntoa(&(tmp->mac)));
		if (rc < 0) goto format_element_error;
		/* Element lla */
		rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "lla", "%s", str_ip);
		if (rc < 0) goto format_element_error;
		/* Elements for Router Advertisement Parameters: */
		rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "param_curhoplimit", "%u", tmp->param_curhoplimit);
		if (rc < 0) goto format_element_error;
		rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "param_flags_reserved", "%u", tmp->param_flags_reserved);
		if (rc < 0) goto format_element_error;
		rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "param_router_lifetime", "%u", tmp->param_router_lifetime);
		if (rc < 0) goto format_element_error;
		rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "param_reachable_timer", "%u", tmp->param_reachable_timer);
		if (rc < 0) goto format_element_error;
		rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "param_retrans_timer", "%u", tmp->param_retrans_timer);
		if (rc < 0) goto format_element_error;
		rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "param_mtu", "%u", tmp->param_mtu);
		if (rc < 0) goto format_element_error;
		rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "params_volatile", "%i", tmp->params_volatile);
		if (rc < 0) goto format_element_error;

		/* Start an element named prefixes */
		rc = xmlTextWriterStartElement(writer, BAD_CAST "prefixes");
		if (rc < 0) goto start_element_error;
		while(tmp_prefix != NULL)
		{
			ipv6_ntoa(str_ip, tmp_prefix->prefix);
			rc = xmlTextWriterStartElement(writer, BAD_CAST "prefix");
			if (rc < 0) goto start_element_error;
			/* Elements for prefix address and mask:*/
			rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "address", "%s", str_ip);
			if (rc < 0) goto format_element_error;		
			rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "mask", "%i", tmp_prefix->mask);
			if (rc < 0) goto format_element_error;		
                        /* Elements for prefix parameters:*/
			rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "param_flags_reserved", "%u", tmp_prefix->param_flags_reserved);
			if (rc < 0) goto format_element_error;		
			rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "param_valid_time", "%u", tmp_prefix->param_valid_time);
			if (rc < 0) goto format_element_error;		
			rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "param_preferred_time", "%u", tmp_prefix->param_preferred_time);
			if (rc < 0) goto format_element_error;
			rc = xmlTextWriterEndElement(writer);
			if (rc < 0) goto end_element_error;
                        /* Fetch next prefix:*/
			tmp_prefix = tmp_prefix->next;
		}   

		rc = xmlTextWriterEndElement(writer);
		if (rc < 0) goto end_element_error;

		/* Addresses */
		rc = xmlTextWriterStartElement(writer, BAD_CAST "addresses");
		if (rc < 0) goto start_element_error;

		while(tmp_address != NULL)
		{
			ipv6_ntoa(str_ip, tmp_address->address);
			/*Element for address.*/
			rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "address", "%s", str_ip);
			if (rc < 0) goto format_element_error;
			tmp_address = tmp_address->next;
		}    

		/* close addresses */
		rc = xmlTextWriterEndElement(writer);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
			return;
		}

		/* close router */
		rc = xmlTextWriterEndElement(writer);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
			return;
		}

		tmp = tmp->next;
	}

	/* Close routers  */
	rc = xmlTextWriterEndElement(writer);
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
		return;
	}

#ifdef _COUNTERMEASURES_
	cm_guard_all_to_representation(
		config_kill_illegitimate_router,
		config_kill_wrong_prefix,
		config_propagate_router_params,
		config_indicate_ndpmon_presence
	);

	/* Start an element named countermeasures containing the counter measures configuration. */
	rc = xmlTextWriterStartElement(writer, BAD_CAST "countermeasures");
	if (rc < 0) goto start_element_error;

	/* Write guard configurations. */
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "kill_illegitimate_router", "%s", config_kill_illegitimate_router);
	if (rc < 0) goto format_element_error;
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "kill_wrong_prefix", "%s", config_kill_wrong_prefix);
	if (rc < 0) goto format_element_error;
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "propagate_router_params", "%s", config_propagate_router_params);
	if (rc < 0) goto format_element_error;
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "indicate_ndpmon_presence", "%s", config_indicate_ndpmon_presence);
	if (rc < 0) goto format_element_error;

	/* Close countermeasures  */
	rc = xmlTextWriterEndElement(writer);
	if (rc < 0) goto end_element_error;
#endif

	/* Close config_ndpmon */
	rc = xmlTextWriterEndElement(writer);
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
		return;
	}

	xmlFreeTextWriter(writer);
	return;

	format_element_error:
	printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
	xmlFreeTextWriter(writer);
	return;
        start_element_error:
	printf("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
	xmlFreeTextWriter(writer);
	return;
        end_element_error:
	printf("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
	xmlFreeTextWriter(writer);
	return;
}
Beispiel #13
0
void write_cache()
{
	const char *uri=cache_path;
	int rc;
	char str_ip[IP6_STR_SIZE];
	xmlTextWriterPtr writer;
	neighbor_list_t *tmp = neighbors;
	FILE *dat = NULL;

	printf("Writing cache...\n");

	/* Create a new XmlWriter for uri, with no compression. */
	writer = xmlNewTextWriterFilename(uri, 0);
	if (writer == NULL)
	{
		printf("testXmlwriterFilename: Error creating the xml writer\n");
		return;
	}

	xmlTextWriterSetIndent(writer, 1);

	/* Start the document with the xml default for the version,
	 * encoding ISO 8859-1 and the default for the standalone
	 * declaration. */
	rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterStartDocument\n");
		return;
	}

	xmlTextWriterStartDTD	(writer, (xmlChar*)"neighbor_list", NULL, (xmlChar*)dtd_path);
	xmlTextWriterEndDTD (writer);	

	/* Give the stylesheet for display in the web interface */
	xmlTextWriterWriteRaw(writer, (xmlChar*)"<?xml-stylesheet type=\"text/xsl\" href=\"neighbor.xsl\" ?>\n");

	/* Start an element named "neighbor_list". Since this is the first
	 * element, this will be the root element of the document. */
	rc = xmlTextWriterStartElement(writer, BAD_CAST "neighbor_list");
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
		return;
	}

	/*for each neighbor in the cache a new neighbor element with its 
	 *attributes is created in the file */
	while(tmp != NULL)
	{
		address_t *atmp = tmp->addresses;
		ethernet_t *etmp = tmp->old_mac;
		/* to format the time */
		time_t timep;
		char time_str[27];
		char vlan_str[10];

		/* Start an element named "neighbor" as child of neighbor_list. */
		rc = xmlTextWriterStartElement(writer, BAD_CAST "neighbor");
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
			return;
		}
		/* Attribute vlan_id */

		snprintf(vlan_str,9,"%d",(int) tmp->vlan_id);
		rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "vlan_id", "%s", vlan_str);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
			return;
		}

		/* Attribute mac */
		rc = xmlTextWriterStartElement(writer, BAD_CAST "mac");
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
			return;
		}
#ifdef _MACRESOLUTION_
		rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "vendor", "%s", tmp->vendor);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteAttribute %d\n",rc);
		}
#endif
		rc = xmlTextWriterWriteRaw(writer, BAD_CAST ether_ntoa(&(tmp->mac)));
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
			return;
		}

		rc = xmlTextWriterEndElement(writer);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
			return;
		}
		/*
		rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "mac", "%s", ether_ntoa(&(tmp->mac)));
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
			return;
		}
		*/

		/* Attribute lla */
		ipv6_ntoa(str_ip, tmp->lla);
		rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "lla", "%s", str_ip);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
			return;
		}

		/* element time */
		rc = xmlTextWriterStartElement(writer, BAD_CAST "time");
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
			return;
		}

#if 0
		rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "time", "%d",tmp->timer);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
			return;
		}
#endif

		/* convert to str representation in order to display it in the web interface */
		timep = tmp->timer;
		strcpy(time_str, ctime(&timep));
		rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "timestr", "%s", time_str);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteAttribute %d\n",rc);
		}

		/* the content */
		snprintf(time_str,27,"%d",(int) tmp->timer);
		rc = xmlTextWriterWriteRaw(writer, BAD_CAST time_str);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
			return;
		}

		rc = xmlTextWriterEndElement(writer);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
			return;
		}

		/* Addresses */
		/* rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "adresses", NULL, NULL); */
		rc = xmlTextWriterStartElement(writer, BAD_CAST "addresses");
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
			return;
		}

		while(atmp != NULL)
		{
			/* to store a char * version of the int + \O */
			ipv6_ntoa(str_ip, atmp->address);

			/* the address element */
			rc = xmlTextWriterStartElement(writer, BAD_CAST "address");
			if (rc < 0)
			{
				printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
				return;
			}

			/* lastseen timer */
			rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "lastseen", "%d", (int)atmp->lastseen);
			if (rc < 0)
			{
				printf("testXmlwriterFilename: Error at xmlTextWriterWriteAttribute %d\n",rc);
			}

			/* convert to str representation in order to display it in the web interface */
			timep = atmp->lastseen;
			strcpy(time_str, ctime(&timep));
			rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "lastseenstr", "%s", time_str);
			if (rc < 0)
			{
				printf("testXmlwriterFilename: Error at xmlTextWriterWriteAttribute %d\n",rc);
			}

			/* firstseen timer */
			rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "firstseen", "%d", (int)atmp->firstseen);
			if (rc < 0)
			{
				printf("testXmlwriterFilename: Error at xmlTextWriterWriteAttribute %d\n",rc);
			}

			/* convert to str representation in order to display it in the web interface */
			timep = atmp->firstseen;
			strcpy(time_str, ctime(&timep));
			rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "firstseenstr", "%s", time_str);
			if (rc < 0)
			{
				printf("testXmlwriterFilename: Error at xmlTextWriterWriteAttribute %d\n",rc);
			}

			/* the content */
			rc = xmlTextWriterWriteRaw(writer, BAD_CAST str_ip);
			if (rc < 0)
			{
				printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
				return;
			}
	
			rc = xmlTextWriterEndElement(writer);
			if (rc < 0)
			{
				printf("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
				return;
			}

			atmp = atmp->next;
		}    

		rc = xmlTextWriterEndElement(writer);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
			return;
		}


		/* Old Mac */
		rc = xmlTextWriterStartElement(writer, BAD_CAST "old_mac");
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
			return;
		}

		while(etmp != NULL)
		{
			rc = xmlTextWriterStartElement(writer, BAD_CAST "mac");
			if (rc < 0)
			{
				printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
				return;
			}
			if(!MEMCMP(&(etmp->mac),&(tmp->previous_mac), sizeof(struct ether_addr)))
			{
				rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "last", "%s", "true");
				if (rc < 0)
				{
					printf("testXmlwriterFilename: Error at xmlTextWriterWriteAttribute %d\n",rc);
				}
			}
#ifdef _MACRESOLUTION_
			rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "vendor", "%s", etmp->vendor);
			if (rc < 0)
			{
				printf("testXmlwriterFilename: Error at xmlTextWriterWriteAttribute %d\n",rc);
			}
#endif
			rc = xmlTextWriterWriteRaw(writer, BAD_CAST ether_ntoa(&(etmp->mac)));
			if (rc < 0)
			{
				printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
				return;
			}

			rc = xmlTextWriterEndElement(writer);
			if (rc < 0)
			{
				printf("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
				return;
			}

			etmp = etmp->next;
		}    

		rc = xmlTextWriterEndElement(writer);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
			return;
		}

		/* Close neighbor */
		rc = xmlTextWriterEndElement(writer);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
			return;
		}
		tmp = tmp->next;
	}

	/* Close neighbor_list */
	rc = xmlTextWriterEndElement(writer);
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
		return;
	}

	xmlFreeTextWriter(writer);

	/* Write in discovery_history.dat the number of neighbors in the cache for statistics */
	if( (dat = fopen(discovery_history_path,"a")) != NULL)
	{
		fprintf(dat,"%d %d\n", (int)time(NULL), nb_neighbor(neighbors) );
		fclose(dat);
	}

}
int
rsstool_write_xml (st_rsstool_t *rt)
{
#define XMLPRINTF(s) xmlTextWriterWriteString(writer,BAD_CAST s)
  st_rss_t rss;
  int i = 0;
  xmlTextWriterPtr writer;
  xmlBufferPtr buffer;
  st_hash_t *dl_url_h = NULL;
  st_hash_t *url_h = NULL;
  st_hash_t *title_h = NULL;
  int items = rsstool_get_item_count (rt);
#define ENCODE(s) s
//#define ENCODE(s) base64_enc(s,0)
//#define ENCODE(s) str_escape_xml(s)

  memset (&rss, 0, sizeof (st_rss_t));

  if (!(buffer = xmlBufferCreate ()))
    return -1;

  if (!(writer = xmlNewTextWriterMemory (buffer, 0)))
    return -1;

  xmlTextWriterStartDocument (writer, NULL, "UTF-8", NULL);

  xmlTextWriterWriteComment (writer, BAD_CAST " RSStool - read, parse, merge and write RSS and Atom feeds\n"                            
    "http://rsstool.berlios.de ");

  XMLPRINTF("\n");

  xmlTextWriterWriteComment (writer, BAD_CAST "\n"
         "format:\n"
         "item[]\n"
         "  dl_url           \n"
         "  dl_url_md5\n"
         "  dl_url_crc32\n"
         "  dl_date\n"
         "  user             author\n"
         "  site\n"
         "  url              \n"
         "  url_md5\n"
         "  url_crc32\n"
         "  date             default: current time\n"
         "  title            used by searches for related items\n"
         "  title_md5\n"
         "  title_crc32\n"
         "  desc             description\n"
         "  media_keywords   default: keywords from title and description\n"
         "  media_duration   event length\n"
         "  media_image      thumbnail\n"
         "  event_start      default: date\n"
         "  event_end        default: event_start + media_duration\n"
);

  XMLPRINTF("\n");

  xmlTextWriterStartElement (writer, BAD_CAST "rsstool");  // <rsstool>
  xmlTextWriterWriteAttribute (writer, BAD_CAST "version", BAD_CAST RSSTOOL_VERSION_S);

  for (i = 0; i < items && i < RSSMAXITEM; i++)
//  for (i = 0; i < items; i++)
    {
      dl_url_h = hash_open (HASH_MD5|HASH_CRC32);
      url_h = hash_open (HASH_MD5|HASH_CRC32);
      title_h = hash_open (HASH_MD5|HASH_CRC32);

      dl_url_h = hash_update (dl_url_h, (const unsigned char *) rt->item[i]->feed_url, strlen (rt->item[i]->feed_url));
      url_h = hash_update (url_h, (const unsigned char *) rt->item[i]->url, strlen (rt->item[i]->url));
      title_h = hash_update (title_h, (const unsigned char *) rt->item[i]->title, strlen (rt->item[i]->title));

      XMLPRINTF("\n  ");

      xmlTextWriterStartElement (writer, BAD_CAST "item"); // <item>

      XMLPRINTF("\n    ");

//      xmlTextWriterWriteElement (writer, BAD_CAST "dl_url", BAD_CAST rt->item[i]->feed_url);
      xmlTextWriterWriteFormatElement (writer, BAD_CAST "dl_url", "%s", rt->item[i]->feed_url);

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "dl_url_md5", "%s", hash_get_s (dl_url_h, HASH_MD5));

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "dl_url_crc32", "%u", hash_get_crc32 (dl_url_h));

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "dl_date", "%ld", rsstool.start_time);

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "user", "%s", ENCODE (rt->item[i]->user));

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "site", "%s", ENCODE (rt->item[i]->site));

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "url", "%s", rt->item[i]->url);

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "url_md5", "%s", hash_get_s (url_h, HASH_MD5));

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "url_crc32", "%u", hash_get_crc32 (url_h));

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "date", "%ld", rt->item[i]->date);

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "title", "%s", ENCODE (rt->item[i]->title));


      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "title_md5", "%s", hash_get_s (title_h, HASH_MD5));

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "title_crc32", "%u", hash_get_crc32 (title_h));

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "desc", "%s", ENCODE (rt->item[i]->desc));

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "media_keywords", "%s", ENCODE (rt->item[i]->media_keywords));

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "media_duration", "%ld", rt->item[i]->media_duration);

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "media_image", "%s", rt->item[i]->media_image);

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "event_start", "%ld", rt->item[i]->event_start);

      XMLPRINTF("\n    ");

      xmlTextWriterWriteFormatElement (writer, BAD_CAST "event_end", "%ld", rt->item[i]->event_end);

      XMLPRINTF("\n  ");

      xmlTextWriterEndElement (writer); // </item>

      hash_close (dl_url_h);
      hash_close (url_h);
      hash_close (title_h);
    }

  XMLPRINTF("\n");

  xmlTextWriterEndDocument (writer);  // </rsstool>

  xmlFreeTextWriter (writer);

  fputs ((const char *) buffer->content, rt->output_file);

  xmlBufferFree (buffer);

  if (items >= RSSMAXITEM)
    {
      char buf[MAXBUFSIZE];

      sprintf (buf, "can write only RSS feeds with up to %d items (was %d items)\n",
        RSSMAXITEM, items);
      rsstool_log (rt, buf);
    }

  return 0;
}
Beispiel #15
0
char *generate_evento_xml(EVENTO *e, EVP_PKEY *key, X509 *cert) {
	int rc;
	xmlTextWriterPtr writer;
	xmlDocPtr doc;
	xmlBufferPtr buf = xmlBufferCreate();

	NFE *nfe = e->nfe;

	writer = xmlNewTextWriterDoc(&doc, 0);
	if (writer == NULL)
		return NULL;
	xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL);
	rc = xmlTextWriterStartElement(writer, BAD_CAST "evento");
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xmlns",
			BAD_CAST "http://www.portalfiscal.inf.br/nfe");
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "versao",
			BAD_CAST "1.00");
	if (rc < 0)
		return NULL;

	rc = xmlTextWriterStartElement(writer, BAD_CAST "infEvento");
	if (rc < 0)
		return NULL;

	char id[70];
	sprintf(id, "ID%d%s%02d", e->type, nfe->idnfe->chave, e->seq);

	rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "Id",
			BAD_CAST id);
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "cOrgao",
			"%d", nfe->idnfe->municipio->uf->cUF);
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "tpAmb",
			"%d", nfe->idnfe->tipo_ambiente);
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "CNPJ",
			"%s", nfe->emitente->cnpj);
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "chNFe",
			"%s", nfe->idnfe->chave);
	if (rc < 0)
		return NULL;
	time_t now;
	char buffer[26];
	struct tm *tm_info;
	time(&now);
	tm_info = localtime(&(now));
	strftime(buffer, 26, "%Y-%m-%dT%H:%M:%S-03:00", tm_info);

	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "dhEvento",
			"%s", buffer);
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "tpEvento",
			"%d", e->type);
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "nSeqEvento",
			"%d", e->seq);
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "verEvento",
			"%s", "1.00");
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterStartElement(writer, BAD_CAST "detEvento");
	if (rc < 0)
		return NULL;

	rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "versao",
			BAD_CAST "1.00");
	if (rc < 0)
		return NULL;

	switch(e->type){
		case CANCELAMENTO_TYPE: {
			EVENTO_CANCELAMENTO *ec;
			ec = (EVENTO_CANCELAMENTO *) e;
			rc = xmlTextWriterWriteFormatElement(writer, 
				BAD_CAST "descEvento",
				"%s", "Cancelamento");
			if (rc < 0)
				return NULL;
			rc = xmlTextWriterWriteFormatElement(writer, 
				BAD_CAST "nProt",
				"%s", nfe->protocolo->numero);
			if (rc < 0)
				return NULL;
			rc = xmlTextWriterWriteFormatElement(writer, 
				BAD_CAST "xJust",
				"%s", ec->justificativa);
			if (rc < 0)
				return NULL;
			break;	
		}
	}

	
	rc = xmlTextWriterEndElement(writer);
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterEndElement(writer);
	if (rc < 0)
		return NULL;
	rc = xmlTextWriterEndElement(writer);
	if (rc < 0)
		return NULL;
	xmlTextWriterEndDocument(writer);
	char *URI = malloc(sizeof(char) * 70);
	strcpy(URI, "#");
	strcat(URI, id);
	sign_xml(doc, key, cert, URI);
	xmlNodeDump(buf, NULL, xmlDocGetRootElement(doc), 0, 0);
	return (char*)buf->content;
}
// fetch contents of inventory folder
void fetch_system_folders(simgroup_ctx *sgrp, user_ctx *user,
			  void *user_priv) {
  uuid_t u; char tmp[40]; char uri[256];
  GRID_PRIV_DEF_SGRP(sgrp);
  USER_PRIV_DEF(user_priv);
  xmlTextWriterPtr writer;
  xmlBufferPtr buf;
  SoupMessage *msg;
  inv_items_req *req;
  
  assert(grid->inventory_server != NULL);

  buf = xmlBufferCreate();
  if(buf == NULL) goto fail;
  writer = xmlNewTextWriterMemory(buf, 0);
  if(writer == NULL) goto free_fail_1;
  
  if(xmlTextWriterStartDocument(writer,NULL,"UTF-8",NULL) < 0) 
    goto free_fail;
  if(xmlTextWriterStartElement(writer, BAD_CAST "RestSessionObjectOfGuid") < 0) 
    goto free_fail;
  user_get_session_id(user, u);
  uuid_unparse(u, tmp);
  if(xmlTextWriterWriteFormatElement(writer,BAD_CAST "SessionID",
				       "%s",tmp) < 0) goto free_fail;


  user_get_uuid(user, u);
  uuid_unparse(u, tmp);
  if(xmlTextWriterWriteFormatElement(writer,BAD_CAST "AvatarID",
				       "%s",tmp) < 0) goto free_fail;

  if(xmlTextWriterWriteFormatElement(writer,BAD_CAST "Body",
				       "%s",tmp) < 0) goto free_fail;
  if(xmlTextWriterEndElement(writer) < 0) 
    goto free_fail;

  if(xmlTextWriterEndDocument(writer) < 0) {
    printf("DEBUG: couldn't end XML document\n"); goto fail;
  }

  // FIXME - don't use fixed-length buffer, and handle missing trailing /
  snprintf(uri, 256, "%sSystemFolders/", grid->inventory_server);
  printf("DEBUG: sending inventory request to %s\n", uri);
  msg = soup_message_new ("POST", uri);
  // FIXME - avoid unnecessary strlen
  soup_message_set_request (msg, "application/xml",
			    SOUP_MEMORY_COPY, (char*)buf->content, 
			    strlen ((char*)buf->content));
  req = new inv_items_req();
  req->user_glue = user_glue; 
  user_grid_glue_ref(user_glue);
  caj_queue_soup_message(sgrp, SOUP_MESSAGE(msg),
			 got_system_folders_resp, req);
    
  xmlFreeTextWriter(writer);  
  xmlBufferFree(buf);
  return;

 free_fail:
  xmlFreeTextWriter(writer);  
 free_fail_1:
  xmlBufferFree(buf);
 fail:
  printf("DEBUG: ran into issues sending inventory request\n");
  // FIXME - handle this
}
Beispiel #17
0
void
write_preset_to_xml(Preset *preset, gchar *filename)
{
    int rc;
    xmlTextWriterPtr writer;
    GList *iter_params = preset->params;
    guint last_id = 0;
    guint last_position = 0;

    printf("Creating a new xml doc\n");
    /* Create a new XmlWriter for uri, with no compression. */
    writer = xmlNewTextWriterFilename(filename, 0);
    if (writer == NULL) {
        printf("testXmlwriterFilename: Error creating the xml writer\n");
        return;
    }

    /* 
     * Start the document with the xml default for the version,
     * encoding and the default for the standalone declaration.
     */
    rc = xmlTextWriterStartDocument(writer, NULL, GDIGI_ENCODING, NULL);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterStartDocument\n");
        return;
    }

    rc = xmlTextWriterSetIndent(writer, 1);
    rc = xmlTextWriterSetIndentString(writer, BAD_CAST "  ");
    /* Write the tag identifying type of prefix, schema version and ns. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST get_preset_filename(product_id));

    rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "SchemaVersion",
                                     BAD_CAST "1.2");

   
    rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xmlns",
                                     BAD_CAST "http://www.digitech.com/xml/preset");

    /* Write the Name tag. */
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "Name", BAD_CAST preset->name);

    rc = xmlTextWriterStartElement(writer, BAD_CAST "Params");

    while (iter_params) {
        XmlSettings *xml;
        SettingParam *param = (SettingParam *) iter_params->data;
 
        if (param->id == last_id && param->position == last_position) {
            g_warning("Skipping duplicate parameter id %d position %d",
                       last_id, last_position);
            iter_params  = iter_params->next;
            continue;
        }

        rc = xmlTextWriterStartElement(writer, BAD_CAST "Param");

        rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ID",
                                             "%d", param->id);
        rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "Position",
                                             "%d", param->position);
        rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "Value",
                                             "%d", param->value);

        last_id = param->id;
        last_position = param->position;
        xml  = get_xml_settings(param->id, param->position);
        if (!xml) {
            printf("Failed to get xml settings for id %d position %d\n",
                    param->id, param->position);
        } else {
            ValueType type;
            gchar *suffix = "";
            gdouble step = 1.0;
            gint offset = 0;
            gboolean        decimal = FALSE;
            EffectValues *values = NULL;

            rc = xmlTextWriterWriteElement(writer, BAD_CAST "Name",
                                                   BAD_CAST xml->label);
            values = xml->values;
            type = values->type;
            while ((type & VALUE_TYPE_EXTRA) && value_is_extra(values, param->value)) {
                values = values->extra;
                type = values->type;
            }
            type &= ~VALUE_TYPE_EXTRA;

            if (type & VALUE_TYPE_OFFSET) {
                offset = values->offset;
                type &= ~VALUE_TYPE_OFFSET;
            }

            if (type & VALUE_TYPE_STEP) {
                step = values->step;
                type &= ~VALUE_TYPE_STEP;
            }

            if (type & VALUE_TYPE_SUFFIX) {
                suffix = values->suffix;
                type &= ~VALUE_TYPE_SUFFIX;
            }

            if (type & VALUE_TYPE_DECIMAL) {
                decimal = TRUE;
                type &= ~VALUE_TYPE_DECIMAL;
            }

            switch (type) {
            case VALUE_TYPE_LABEL:
            {
                char *textp = map_xml_value(xml, param->value);
                if (!textp) {
                    g_warning("Unable to map %s value %d for id %d position %d",
                              xml->label, param->value, param->id,
                              param->position);
                    textp = "";
                }
                rc = xmlTextWriterWriteElement(writer, BAD_CAST "Text",
                                                       BAD_CAST textp);
                break;
            }

            case VALUE_TYPE_PLAIN:
            {
                if (decimal) {
                    double value = (param->value + offset) * step;
                    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "Text",
                            "%0.2f%s", value, suffix);
                } else {    
                    gint value = (param->value + offset) * step;
                    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "Text",
                            "%d%s", value, suffix);
                }
                break;

            }
            case VALUE_TYPE_NONE:
                rc = xmlTextWriterStartElement(writer, BAD_CAST "Text");
                rc = xmlTextWriterEndElement(writer);
                break;

            default:
                g_warning("Unhandled value type %d", type);
                break;
            }
        }
        
        rc = xmlTextWriterEndElement(writer);

        iter_params  = iter_params->next;
    }

    rc = xmlTextWriterEndDocument(writer);
    if (rc < 0) {
        printf("testXmlwriterFilename: Error at xmlTextWriterEndDocument\n");
        return;
    }

    xmlFreeTextWriter(writer);
}
Beispiel #18
0
int write_pkl(opendcp_t *opendcp, pkl_t *pkl) {
    int a,r,c,rc;
    struct stat st;
    xmlIndentTreeOutput = 1;
    xmlDocPtr        doc;
    xmlTextWriterPtr xml;

    /* create XML document */
    xml = xmlNewTextWriterDoc(&doc,0);

    /* pkl start */
    rc = xmlTextWriterStartDocument(xml, NULL, XML_ENCODING, NULL);
    if (rc < 0) {
        dcp_log(LOG_ERROR,"xmlTextWriterStartDocument failed");
        return DCP_FATAL;
    }

    xmlTextWriterStartElement(xml, BAD_CAST "PackingList");
    xmlTextWriterWriteAttribute(xml, BAD_CAST "xmlns", BAD_CAST NS_PKL[opendcp->ns]);
    if (opendcp->xml_signature.sign) {
        xmlTextWriterWriteAttribute(xml, BAD_CAST "xmlns:dsig", BAD_CAST DS_DSIG);
    }

    /* cpl attributes */
    xmlTextWriterWriteFormatElement(xml, BAD_CAST "Id","%s%s","urn:uuid:",pkl->uuid);
    xmlTextWriterWriteFormatElement(xml, BAD_CAST "AnnotationText","%s",pkl->annotation);
    xmlTextWriterWriteFormatElement(xml, BAD_CAST "IssueDate","%s",opendcp->xml.timestamp);
    xmlTextWriterWriteFormatElement(xml, BAD_CAST "Issuer","%s",opendcp->xml.issuer);
    xmlTextWriterWriteFormatElement(xml, BAD_CAST "Creator","%s",opendcp->xml.creator);

    dcp_log(LOG_INFO,"CPLS: %d",pkl->cpl_count);

    /* asset(s) Start */
    xmlTextWriterStartElement(xml, BAD_CAST "AssetList");
    for (c=0;c<pkl->cpl_count;c++) {
        cpl_t cpl = pkl->cpl[c];
        dcp_log(LOG_INFO,"REELS: %d",cpl.reel_count);
        for (r=0;r<cpl.reel_count;r++) {
            reel_t reel = cpl.reel[r];

            for (a=0;a<reel.asset_count;a++) {
                asset_t asset = reel.asset[a];
                xmlTextWriterStartElement(xml, BAD_CAST "Asset");
                xmlTextWriterWriteFormatElement(xml, BAD_CAST "Id","%s%s","urn:uuid:",asset.uuid);
                xmlTextWriterWriteFormatElement(xml, BAD_CAST "AnnotationText","%s",asset.annotation);
                xmlTextWriterWriteFormatElement(xml, BAD_CAST "Hash","%s",asset.digest);
                xmlTextWriterWriteFormatElement(xml, BAD_CAST "Size","%s",asset.size);
                if (opendcp->ns == XML_NS_SMPTE) {
                    xmlTextWriterWriteFormatElement(xml, BAD_CAST "Type","%s","application/mxf");
                } else {
                    if (asset.essence_class == ACT_PICTURE) {
                        xmlTextWriterWriteFormatElement(xml, BAD_CAST "Type","%s","application/x-smpte-mxf;asdcpKind=Picture");
                    }
                    if (asset.essence_class == ACT_SOUND) {
                        xmlTextWriterWriteFormatElement(xml, BAD_CAST "Type","%s","application/x-smpte-mxf;asdcpKind=Sound");
                    }
                    if (asset.essence_class == ACT_TIMED_TEXT) {
                        xmlTextWriterWriteFormatElement(xml, BAD_CAST "Type","%s","application/x-smpte-mxf;asdcpKind=Subtitle");
                    }
                }
                xmlTextWriterEndElement(xml);      /* end asset */
            }
        }

        /* cpl */
        xmlTextWriterStartElement(xml, BAD_CAST "Asset");
        xmlTextWriterWriteFormatElement(xml, BAD_CAST "Id","%s%s","urn:uuid:",cpl.uuid);
        xmlTextWriterWriteFormatElement(xml, BAD_CAST "Hash","%s",cpl.digest);
        xmlTextWriterWriteFormatElement(xml, BAD_CAST "Size","%s",cpl.size);
        if (opendcp->ns == XML_NS_SMPTE) {
            xmlTextWriterWriteFormatElement(xml, BAD_CAST "Type","%s","text/xml");
        } else {
            xmlTextWriterWriteFormatElement(xml, BAD_CAST "Type","%s","text/xml;asdcpKind=CPL");
        }
        xmlTextWriterEndElement(xml);      /* end cpl asset */
    }
    xmlTextWriterEndElement(xml);      /* end assetlist */

#ifdef XMLSEC
    if (opendcp->xml_signature.sign) {
        write_dsig_template(opendcp, xml);
    }
#endif

    xmlTextWriterEndElement(xml);      /* end packinglist */

    rc = xmlTextWriterEndDocument(xml);
    if (rc < 0) {
        dcp_log(LOG_ERROR,"xmlTextWriterEndDocument failed %s",pkl->filename);
        return DCP_FATAL;
    }

    xmlFreeTextWriter(xml);
    xmlSaveFormatFile(pkl->filename, doc, 1);
    xmlFreeDoc(doc);

#ifdef XMLSEC
    /* sign the XML file */
    if (opendcp->xml_signature.sign) {
        xml_sign(opendcp, pkl->filename);
    }
#endif

    /* store PKL file size */
    stat(pkl->filename, &st);
    sprintf(pkl->size,"%"PRIu64,st.st_size);

    return DCP_SUCCESS;
}
void fetch_inventory_item(simgroup_ctx *sgrp, user_ctx *user,
			    void *user_priv, const uuid_t item_id,
			    void(*cb)(struct inventory_item* item, 
				      void* priv),
			    void *cb_priv) {
  uuid_t u, user_id; char tmp[40]; char uri[256];
  GRID_PRIV_DEF_SGRP(sgrp);
  USER_PRIV_DEF(user_priv);
  xmlTextWriterPtr writer;
  xmlBufferPtr buf;
  SoupMessage *msg;
  inv_item_req *req;
  struct os_inv_item invitem; // don't ask. Please.
  
  assert(grid->inventory_server != NULL);

  buf = xmlBufferCreate();
  if(buf == NULL) goto fail;
  writer = xmlNewTextWriterMemory(buf, 0);
  if(writer == NULL) goto free_fail_1;
  
  if(xmlTextWriterStartDocument(writer,NULL,"UTF-8",NULL) < 0) 
    goto free_fail;
  if(xmlTextWriterStartElement(writer, BAD_CAST "RestSessionObjectOfInventoryItemBase") < 0) 
    goto free_fail;
  user_get_session_id(user, u);
  uuid_unparse(u, tmp);
  if(xmlTextWriterWriteFormatElement(writer,BAD_CAST "SessionID",
				       "%s",tmp) < 0) goto free_fail;


  user_get_uuid(user, user_id);
  uuid_unparse(user_id, tmp);
  if(xmlTextWriterWriteFormatElement(writer,BAD_CAST "AvatarID",
				       "%s",tmp) < 0) goto free_fail;

  // okay, this is just painful... we have to serialise an entire complex
  // object in this cruddy .Net XML serialisation format... and the only bit
  // they actually use or need is a single UUID. Bletch  *vomit*.
  if(xmlTextWriterStartElement(writer,BAD_CAST "Body") < 0) 
    goto free_fail;
  memset(&invitem, 0, sizeof(invitem));
  invitem.name = invitem.description = const_cast<char*>("");
  invitem.creator_id = const_cast<char*>("");
  uuid_copy(invitem.owner_id, user_id);
  uuid_copy(invitem.item_id, item_id);

  osglue_serialise_xml(writer, deserialise_inv_item, &invitem);

  if(xmlTextWriterEndElement(writer) < 0) 
    goto free_fail;

  // now we should be done serialising the XML crud.

  if(xmlTextWriterEndElement(writer) < 0) 
    goto free_fail;

  if(xmlTextWriterEndDocument(writer) < 0) {
    printf("DEBUG: couldn't end XML document\n"); goto fail;
  }

  // FIXME - don't use fixed-length buffer, and handle missing trailing /
  snprintf(uri, 256, "%sQueryItem/", grid->inventory_server);
  printf("DEBUG: sending inventory request to %s\n", uri);
  msg = soup_message_new ("POST", uri);
  // FIXME - avoid unnecessary strlen
  soup_message_set_request (msg, "text/xml",
			    SOUP_MEMORY_COPY, (char*)buf->content, 
			    strlen ((char*)buf->content));
  req = new inv_item_req();
  req->user_glue = user_glue; req->cb = cb;
  req->cb_priv = cb_priv;
  uuid_copy(req->item_id, item_id);
  user_grid_glue_ref(user_glue);
  caj_queue_soup_message(sgrp, SOUP_MESSAGE(msg),
			 got_inventory_item_resp, req);
    
  xmlFreeTextWriter(writer);  
  xmlBufferFree(buf);
  return;

 free_fail:
  xmlFreeTextWriter(writer);  
 free_fail_1:
  xmlBufferFree(buf);
 fail:
  printf("DEBUG: ran into issues sending inventory QueryItem request\n");
  cb(NULL, cb_priv);
  // FIXME - handle this
  
}
Beispiel #20
0
xmlChar *get_uri(fsp_link *link, fs_rid rid)
{
  if (cache[rid & CACHE_MASK].rid == rid) {
    return (xmlChar *) cache[rid & CACHE_MASK].lex;
  }

  fs_rid_vector onerid = { .length = 1, .size = 1, .data = &rid };
  fs_resource resource;
  fsp_resolve(link, FS_RID_SEGMENT(rid, segments), &onerid, &resource);

  return (xmlChar *) resource.lex;
}

xmlChar *get_attr(fsp_link *link, fs_rid rid)
{
  if (attr_cache[rid & CACHE_MASK].rid == rid) {
    return (xmlChar *) attr_cache[rid & CACHE_MASK].lex;
  }

  fs_rid_vector onerid = { .length = 1, .size = 1, .data = &rid };
  fs_resource resource;

  fsp_resolve(link, FS_RID_SEGMENT(rid, segments), &onerid, &resource);
  memcpy(&attr_cache[rid & ATTR_CACHE_MASK], &resource, sizeof(fs_resource));

  return (xmlChar *) resource.lex;
}

xmlChar *get_literal(fsp_link *link, fs_rid rid, fs_rid *attr)
{
  if (cache[rid & CACHE_MASK].rid == rid) {
    *attr = cache[rid & CACHE_MASK].attr;
    return (xmlChar *) cache[rid & CACHE_MASK].lex;
  }

  fs_rid_vector onerid = { .length = 1, .size = 1, .data = &rid };
  fs_resource resource;

  fsp_resolve(link, FS_RID_SEGMENT(rid, segments), &onerid, &resource);
  *attr = resource.attr;

  return (xmlChar *) resource.lex;
}

void resolve_triples(fsp_link *link, fs_rid_vector **rids)
{
  int quads = rids[0]->length;
  fs_rid_vector *todo[segments];
  fs_segment segment;

  for (segment = 0; segment < segments; ++segment) {
    todo[segment] = fs_rid_vector_new(0);
  }
  for (int c = 0; c < 3; ++c) {
    for (int k = 0; k < quads; ++k) {
      const fs_rid rid = rids[c]->data[k];
      if (FS_IS_BNODE(rid) || cache[rid & CACHE_MASK].rid == rid) continue;
      fs_rid_vector_append(todo[FS_RID_SEGMENT(rid, segments)], rid);
      cache[rid & CACHE_MASK].rid = rid; /* well, it will be soon */
    }
  } 

  int length[segments];
  fs_resource *resources[segments];
  for (segment = 0; segment < segments; ++segment) {
    length[segment] = todo[segment]->length;
    resources[segment] = calloc(length[segment], sizeof(fs_resource));
  }

  fsp_resolve_all(link, todo, resources);

  for (segment = 0; segment < segments; ++segment) {
    fs_resource *res = resources[segment];
    for (int k = 0; k < length[segment]; ++k) {
      free(cache[res[k].rid & CACHE_MASK].lex);
      memcpy(&cache[res[k].rid & CACHE_MASK], &res[k], sizeof(fs_resource));
    }

    fs_rid_vector_free(todo[segment]);
    free(resources[segment]);
  }
}

void dump_model(fsp_link *link, fs_rid model, xmlTextWriterPtr xml)
{
  fs_rid_vector none = { .length = 0, .size = 0, .data = 0 };
  fs_rid_vector one = { .length = 1, .size = 1, .data = &model };

  fs_rid_vector **results;

  double then; /* for time keeping */

  then = fs_time();
  fsp_bind_first_all(link, BIND_SPO, &one, &none, &none, &none, &results, QUAD_LIMIT);
  time_bind_first += (fs_time() - then);

  while (results != NULL) {

    long length = results[0]->length;

    if (length == 0) break;

    then = fs_time();
    resolve_triples(link, results);
    time_resolving += (fs_time() - then);

    then = fs_time();
    for (int k = 0; k < length; ++k) {
      xmlTextWriterStartElement(xml, (xmlChar *) "triple");

      for (int r = 0; r < 3; ++r) {
        fs_rid rid = results[r]->data[k];
        if (FS_IS_BNODE(rid)) {
          unsigned long long node = FS_BNODE_NUM(rid);
          xmlTextWriterWriteFormatElement(xml, (xmlChar *) "id", "%llu", node);
        } else if (FS_IS_URI(rid)) {
          xmlChar *uri = get_uri(link, rid);
          xmlTextWriterWriteElement(xml, (xmlChar *) "uri", uri);
        } else if (FS_IS_LITERAL(rid)) {
          fs_rid attr;
          xmlChar *lex = get_literal(link, rid, &attr);
          if (attr == fs_c.empty) {
            xmlTextWriterWriteElement(xml, (xmlChar *) "plainLiteral", lex);
          } else if (FS_IS_URI(attr)) {
            xmlChar *type = get_uri(link, attr);
            xmlTextWriterStartElement(xml, (xmlChar *) "typedLiteral");
            xmlTextWriterWriteString(xml, (xmlChar *) lex);
            xmlTextWriterWriteAttribute(xml, (xmlChar *) "datatype", type);
            xmlTextWriterEndElement(xml);
          } else if (FS_IS_LITERAL(attr)) {
            xmlChar *lang = get_attr(link, attr);
            xmlTextWriterStartElement(xml, (xmlChar *) "plainLiteral");
            xmlTextWriterWriteAttribute(xml, (xmlChar *) "xml:lang", lang);
            xmlTextWriterWriteString(xml, (xmlChar *) lex);
            xmlTextWriterEndElement(xml);
          }
        }
      }
      xmlTextWriterEndElement(xml);
      xmlTextWriterWriteString(xml, (xmlChar *) "\n");

    }
    time_write_out += (fs_time() - then);

    fs_rid_vector_free(results[0]);
    fs_rid_vector_free(results[1]);
    fs_rid_vector_free(results[2]);
    free(results);

    then = fs_time();
    fsp_bind_next_all(link, BIND_SPO, &results, QUAD_LIMIT);
    time_bind_next += (fs_time() - then);
  }

  fsp_bind_done_all(link);
}

void dump_trix(fsp_link *link, xmlTextWriterPtr xml)
{
  fs_rid_vector **models;
  fs_rid_vector none = { .length = 0, .size = 0, .data = 0 };

  fsp_bind_all(link, FS_BIND_DISTINCT | FS_BIND_MODEL | FS_BIND_BY_SUBJECT, &none, &none, &none, &none, &models);

  fs_rid_vector_sort(models[0]);
  fs_rid_vector_uniq(models[0], 1);

  long length = models[0]->length;

  for (int k = 0; k < length; ++k) {
    fs_rid model = models[0]->data[k];
    xmlChar *model_uri = get_uri(link, model);
    xmlTextWriterStartElement(xml, (xmlChar *) "graph");
    if (FS_IS_URI(model)) {
      xmlTextWriterWriteElement(xml, (xmlChar *) "uri", model_uri);
    } else {
      fs_error(LOG_WARNING, "model %lld is not a URI", model);
    }

    dump_model(link, model, xml);
    xmlTextWriterEndElement(xml);
    xmlTextWriterWriteString(xml, (xmlChar *) "\n");
printf("%5d/%ld: %4.5f %4.5f %4.5f %4.5f\n", k + 1, length, time_resolving, time_bind_first, time_bind_next, time_write_out);
  }
}

void dump_file(fsp_link *link, char *filename)
{
  xmlTextWriterPtr xml  = xmlNewTextWriterFilename(filename, TRUE);

  if (!xml) {
    fs_error(LOG_ERR, "Couldn't write output file, giving up");
    exit(4);
  }

  xmlTextWriterStartDocument(xml, NULL, NULL, NULL);
  xmlTextWriterStartElement(xml, (xmlChar *) "TriX");
  dump_trix(link, xml);
  xmlTextWriterEndDocument(xml); /* also closes TriX */
  xmlFreeTextWriter(xml);
}

int main(int argc, char *argv[])
{
  char *password = fsp_argv_password(&argc, argv);

  if (argc != 3) {
    fprintf(stderr, "%s revision %s\n", argv[0], FS_FRONTEND_VER);
    fprintf(stderr, "Usage: %s <kbname> <uri>\n", argv[0]);
    exit(1);
  }

  fsp_link *link = fsp_open_link(argv[1], password, FS_OPEN_HINT_RO);

  if (!link) {
    fs_error (LOG_ERR, "couldn't connect to “%s”", argv[1]);
    exit(2);
  }

  fs_hash_init(fsp_hash_type(link));
  segments = fsp_link_segments(link);
  dump_file(link, argv[2]);

  fsp_close_link(link);
}
Beispiel #21
0
void
rs_metadata_cache_save(RSMetadata *metadata, const gchar *filename)
{
	if (!filename)
	  return;

	gchar *cache_filename;
	gchar *thumb_filename;
	xmlTextWriterPtr writer;
	static GMutex lock;

	g_return_if_fail(RS_IS_METADATA(metadata));

	g_mutex_lock(&lock);

	cache_filename = rs_metadata_dotdir_helper(filename, DOTDIR_METACACHE);

	writer = xmlNewTextWriterFilename(cache_filename, 0);
	if (writer)
	{
		xmlTextWriterSetIndent(writer, 1);

		xmlTextWriterStartDocument(writer, NULL, "ISO-8859-1", NULL);
		xmlTextWriterStartElement(writer, BAD_CAST "rawstudio-metadata");
		xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "version", "%d", METACACHEVERSION);
		if (metadata->make != MAKE_UNKNOWN)
			xmlTextWriterWriteFormatElement(writer, BAD_CAST "make", "%d", metadata->make);
		if (metadata->make_ascii)
			xmlTextWriterWriteFormatElement(writer, BAD_CAST "make_ascii","%s", metadata->make_ascii);
		if (metadata->model_ascii)
			xmlTextWriterWriteFormatElement(writer, BAD_CAST "model_ascii", "%s", metadata->model_ascii);
		if (metadata->time_ascii)
			xmlTextWriterWriteFormatElement(writer, BAD_CAST "time_ascii", "%s", metadata->time_ascii);
		if (metadata->timestamp > -1)
			xmlTextWriterWriteFormatElement(writer, BAD_CAST "timestamp", "%d", metadata->timestamp);
		/* Can we make orientation conditional? */
		xmlTextWriterWriteFormatElement(writer, BAD_CAST "orientation", "%u", metadata->orientation);
		if (metadata->aperture > -1.0)
			xmlTextWriterWriteFormatElement(writer, BAD_CAST "aperture", "%f", metadata->aperture);
		if (metadata->exposurebias != -999.0)
			xmlTextWriterWriteFormatElement(writer, BAD_CAST "exposurebias", "%f", metadata->exposurebias);
		if (metadata->iso > 0)
			xmlTextWriterWriteFormatElement(writer, BAD_CAST "iso", "%u", metadata->iso);
		if (metadata->shutterspeed > -1.0)
			xmlTextWriterWriteFormatElement(writer, BAD_CAST "shutterspeed", "%f", metadata->shutterspeed);
		if (metadata->cam_mul[0] > 0.0)
			xmlTextWriterWriteFormatElement(writer, BAD_CAST "cam_mul", "%f %f %f %f", metadata->cam_mul[0], metadata->cam_mul[1], metadata->cam_mul[2], metadata->cam_mul[3]);
		if (metadata->contrast > -1.0)
			xmlTextWriterWriteFormatElement(writer, BAD_CAST "contrast", "%f", metadata->contrast);
		if (metadata->saturation > -1.0)
			xmlTextWriterWriteFormatElement(writer, BAD_CAST "saturation", "%f", metadata->saturation);
		if (metadata->color_tone > -1.0)
			xmlTextWriterWriteFormatElement(writer, BAD_CAST "color_tone", "%f", metadata->color_tone);
		if (metadata->focallength > 0)
			xmlTextWriterWriteFormatElement(writer, BAD_CAST "focallength", "%d", metadata->focallength);
		if (metadata->lens_id > -1.0)
			xmlTextWriterWriteFormatElement(writer, BAD_CAST "lens_id", "%d", metadata->lens_id);
		if (metadata->lens_min_focal > -1)
			xmlTextWriterWriteFormatElement(writer, BAD_CAST "lens_min_focal", "%f", metadata->lens_min_focal);
		if (metadata->lens_max_focal > -1.0)
			xmlTextWriterWriteFormatElement(writer, BAD_CAST "lens_max_focal", "%f", metadata->lens_max_focal);
		if (metadata->lens_min_aperture > -1.0)
			xmlTextWriterWriteFormatElement(writer, BAD_CAST "lens_min_aperture", "%f", metadata->lens_min_aperture);
		if (metadata->lens_max_aperture > -1.0)
			xmlTextWriterWriteFormatElement(writer, BAD_CAST "lens_max_aperture", "%f", metadata->lens_max_aperture);
		if (metadata->fixed_lens_identifier)
			xmlTextWriterWriteFormatElement(writer, BAD_CAST "fixed_lens_identifier", "%s", metadata->fixed_lens_identifier);
		xmlTextWriterEndDocument(writer);
		xmlFreeTextWriter(writer);
	}
	g_free(cache_filename);
	g_mutex_unlock(&lock);

	if (metadata->thumbnail)
	{
		thumb_filename = rs_metadata_dotdir_helper(filename, DOTDIR_THUMB);
		gdk_pixbuf_save(metadata->thumbnail, thumb_filename, "jpeg", NULL, "quality", "90", NULL);
		g_free(thumb_filename);
	}
}
Beispiel #22
0
/**
 * testXmlwriterTree:
 * @file: the output file
 *
 * test the xmlWriter interface when writing to a subtree
 */
void
testXmlwriterTree(const char *file)
{
    int rc;
    xmlTextWriterPtr writer;
    xmlDocPtr doc;
    xmlNodePtr node;
    xmlChar *tmp;

    /* Create a new XML DOM tree, to which the XML document will be
     * written */
    doc = xmlNewDoc(BAD_CAST XML_DEFAULT_VERSION);
    if (doc == NULL) {
        printf
            ("testXmlwriterTree: Error creating the xml document tree\n");
        return;
    }

    /* Create a new XML node, to which the XML document will be
     * appended */
    node = xmlNewDocNode(doc, NULL, BAD_CAST "EXAMPLE", NULL);
    if (node == NULL) {
        printf("testXmlwriterTree: Error creating the xml node\n");
        return;
    }

    /* Make ELEMENT the root node of the tree */
    xmlDocSetRootElement(doc, node);

    /* Create a new XmlWriter for DOM tree, with no compression. */
    writer = xmlNewTextWriterTree(doc, node, 0);
    if (writer == NULL) {
        printf("testXmlwriterTree: Error creating the xml writer\n");
        return;
    }

    /* Start the document with the xml default for the version,
     * encoding ISO 8859-1 and the default for the standalone
     * declaration. */
    rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterStartDocument\n");
        return;
    }

    /* Write a comment as child of EXAMPLE.
     * Please observe, that the input to the xmlTextWriter functions
     * HAS to be in UTF-8, even if the output XML is encoded
     * in iso-8859-1 */
    tmp = ConvertInput("This is a comment with special chars: <äöü>",
                       MY_ENCODING);
    rc = xmlTextWriterWriteComment(writer, tmp);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterWriteComment\n");
        return;
    }
    if (tmp != NULL) xmlFree(tmp);

    /* Start an element named "ORDER" as child of EXAMPLE. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "ORDER");
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Add an attribute with name "version" and value "1.0" to ORDER. */
    rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "version",
                                     BAD_CAST "1.0");
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteAttribute\n");
        return;
    }

    /* Add an attribute with name "xml:lang" and value "de" to ORDER. */
    rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
                                     BAD_CAST "de");
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteAttribute\n");
        return;
    }

    /* Write a comment as child of ORDER */
    tmp = ConvertInput("<äöü>", MY_ENCODING);
    rc = xmlTextWriterWriteFormatComment(writer,
			 "This is another comment with special chars: %s",
					  tmp);
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteFormatComment\n");
        return;
    }
    if (tmp != NULL) xmlFree(tmp);

    /* Start an element named "HEADER" as child of ORDER. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "HEADER");
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Write an element named "X_ORDER_ID" as child of HEADER. */
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "X_ORDER_ID",
                                         "%010d", 53535L);
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteFormatElement\n");
        return;
    }

    /* Write an element named "CUSTOMER_ID" as child of HEADER. */
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "CUSTOMER_ID",
                                         "%d", 1010);
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteFormatElement\n");
        return;
    }

    /* Write an element named "NAME_1" as child of HEADER. */
    tmp = ConvertInput("Müller", MY_ENCODING);
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_1", tmp);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterWriteElement\n");
        return;
    }
    if (tmp != NULL) xmlFree(tmp);

    /* Write an element named "NAME_2" as child of HEADER. */
    tmp = ConvertInput("Jörg", MY_ENCODING);
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_2", tmp);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriter
Beispiel #23
0
int write_cpl(opendcp_t *opendcp, cpl_t *cpl) {
    int a,r, rc;
    struct stat st;
    xmlIndentTreeOutput = 1;
    xmlDocPtr        doc; 
    xmlTextWriterPtr xml;

    /* create XML document */
    xml = xmlNewTextWriterDoc(&doc,0);

    /* cpl start */
    rc = xmlTextWriterStartDocument(xml, NULL, XML_ENCODING, NULL);
    if (rc < 0) {
        dcp_log(LOG_ERROR,"xmlTextWriterStartDocument failed");
        return DCP_FATAL;
    }

    xmlTextWriterStartElement(xml, BAD_CAST "CompositionPlaylist");
    xmlTextWriterWriteAttribute(xml, BAD_CAST "xmlns", BAD_CAST NS_CPL[opendcp->ns]);
    if (opendcp->xml_signature.sign) {
        xmlTextWriterWriteAttribute(xml, BAD_CAST "xmlns:dsig", BAD_CAST DS_DSIG);
    }

    /* cpl attributes */
    xmlTextWriterWriteFormatElement(xml, BAD_CAST "Id","%s%s","urn:uuid:",cpl->uuid);
    xmlTextWriterWriteFormatElement(xml, BAD_CAST "AnnotationText","%s",cpl->annotation);
    xmlTextWriterWriteFormatElement(xml, BAD_CAST "IssueDate","%s",cpl->timestamp);
    xmlTextWriterWriteFormatElement(xml, BAD_CAST "Creator","%s",cpl->creator);
    xmlTextWriterWriteFormatElement(xml, BAD_CAST "ContentTitleText","%s",cpl->title);
    xmlTextWriterWriteFormatElement(xml, BAD_CAST "ContentKind","%s",cpl->kind);

    /* content version */
    if (opendcp->ns == XML_NS_SMPTE) {
        xmlTextWriterStartElement(xml, BAD_CAST "ContentVersion"); 
        xmlTextWriterWriteFormatElement(xml, BAD_CAST "Id","%s%s_%s","urn:uri:",cpl->uuid,cpl->timestamp);
        xmlTextWriterWriteFormatElement(xml, BAD_CAST "LabelText","%s_%s",cpl->uuid,cpl->timestamp);
        xmlTextWriterEndElement(xml);
    }

    /* rating */
    xmlTextWriterStartElement(xml, BAD_CAST "RatingList");
    if (strcmp(cpl->rating,"")) {
        xmlTextWriterWriteFormatElement(xml, BAD_CAST "Agency","%s",RATING_AGENCY[1]);
        xmlTextWriterWriteFormatElement(xml, BAD_CAST "Label","%s",cpl->rating);
    }
    xmlTextWriterEndElement(xml);

    /* reel(s) Start */
    xmlTextWriterStartElement(xml, BAD_CAST "ReelList");
    for (r=0;r<cpl->reel_count;r++) {
        reel_t reel = cpl->reel[r];
        xmlTextWriterStartElement(xml, BAD_CAST "Reel");
        xmlTextWriterWriteFormatElement(xml, BAD_CAST "Id","%s%s","urn:uuid:",reel.uuid);
        xmlTextWriterStartElement(xml, BAD_CAST "AssetList");

        /* Asset(s) Start */
        for (a=0;a<cpl->reel[r].asset_count;a++) {
            asset_t asset = cpl->reel[r].asset[a];
            if (asset.essence_class == ACT_PICTURE) {
                if (asset.stereoscopic) {
                    xmlTextWriterStartElement(xml, BAD_CAST "msp-cpl:MainStereoscopicPicture");
                    xmlTextWriterWriteAttribute(xml, BAD_CAST "xmlns:msp-cpl", BAD_CAST NS_CPL_3D[opendcp->ns]);
                } else {
                    xmlTextWriterStartElement(xml, BAD_CAST "MainPicture");
                }
            }
            if (asset.essence_class == ACT_SOUND) {
                xmlTextWriterStartElement(xml, BAD_CAST "MainSound");
            }
            if (asset.essence_class == ACT_TIMED_TEXT) {
                xmlTextWriterStartElement(xml, BAD_CAST "MainSubtitle");
            }

            xmlTextWriterWriteFormatElement(xml, BAD_CAST "Id","%s%s","urn:uuid:",asset.uuid);
            xmlTextWriterWriteFormatElement(xml, BAD_CAST "AnnotationText","%s",asset.annotation);
            xmlTextWriterWriteFormatElement(xml, BAD_CAST "EditRate","%s",asset.edit_rate);
            xmlTextWriterWriteFormatElement(xml, BAD_CAST "IntrinsicDuration","%d",asset.intrinsic_duration);
            xmlTextWriterWriteFormatElement(xml, BAD_CAST "EntryPoint","%d",asset.entry_point);
            xmlTextWriterWriteFormatElement(xml, BAD_CAST "Duration","%d",asset.duration);

            if (asset.essence_class == ACT_PICTURE) {
                xmlTextWriterWriteFormatElement(xml, BAD_CAST "FrameRate","%s",asset.frame_rate);
                if (opendcp->ns == XML_NS_SMPTE) {
                    xmlTextWriterWriteFormatElement(xml, BAD_CAST "ScreenAspectRatio","%s",asset.aspect_ratio);
                } else {
                    xmlTextWriterWriteFormatElement(xml, BAD_CAST "ScreenAspectRatio","%s",get_aspect_ratio(asset.aspect_ratio));
                }
            }

            if ( opendcp->xml.digest_flag ) {
                xmlTextWriterWriteFormatElement(xml, BAD_CAST "Hash","%s",asset.digest);
            }
            
            xmlTextWriterEndElement(xml); /* end asset */
        }
     
        xmlTextWriterEndElement(xml);     /* end assetlist */
        xmlTextWriterEndElement(xml);     /* end reel */
    }
    xmlTextWriterEndElement(xml);         /* end reel list */

#ifdef XMLSEC
    if (opendcp->xml_signature.sign) {
        write_dsig_template(opendcp, xml);
    }
#endif

    xmlTextWriterEndElement(xml);         /* end compositionplaylist */

    rc = xmlTextWriterEndDocument(xml);
    if (rc < 0) {
        dcp_log(LOG_ERROR,"xmlTextWriterEndDocument failed %s",cpl->filename);
        return DCP_FATAL;
    }

    xmlFreeTextWriter(xml);
    xmlSaveFormatFile(cpl->filename, doc, 1);
    xmlFreeDoc(doc);

#ifdef XMLSEC
    /* sign the XML file */
    if (opendcp->xml_signature.sign) {
        xml_sign(opendcp, cpl->filename);
    }
#endif

    /* store CPL file size */
    dcp_log(LOG_INFO,"Writing CPL file info");
    stat(cpl->filename, &st);
    sprintf(cpl->size,"%"PRIu64,st.st_size);
    calculate_digest(cpl->filename,cpl->digest);
    
    return DCP_SUCCESS;
}
Beispiel #24
0
int write_assetmap(opendcp_t *opendcp) {
    xmlIndentTreeOutput = 1;
    xmlDocPtr        doc;
    xmlTextWriterPtr xml;
    int              a,c,r,rc;
    char             uuid_s[40];
    cpl_t            cpl;
    reel_t           reel;

    /* generate assetmap UUID */
    uuid_random(uuid_s);

    dcp_log(LOG_INFO,"Writing ASSETMAP file %.256s",opendcp->assetmap.filename);

    /* create XML document */
    xml = xmlNewTextWriterDoc(&doc,0);

    /* assetmap XML Start */
    rc = xmlTextWriterStartDocument(xml, NULL, XML_ENCODING, NULL);
    if (rc < 0) {
        dcp_log(LOG_ERROR,"xmlTextWriterStartDocument failed");
        return DCP_FATAL;
    }

    xmlTextWriterStartElement(xml, BAD_CAST "AssetMap");
    xmlTextWriterWriteAttribute(xml, BAD_CAST "xmlns", BAD_CAST NS_AM[opendcp->ns]);

    /* assetmap attributes */
    xmlTextWriterWriteFormatElement(xml, BAD_CAST "Id","%s%s","urn:uuid:",uuid_s);
    xmlTextWriterWriteFormatElement(xml, BAD_CAST "Creator","%s",opendcp->xml.creator);
    xmlTextWriterWriteFormatElement(xml, BAD_CAST "VolumeCount","%d",1);
    xmlTextWriterWriteFormatElement(xml, BAD_CAST "IssueDate","%s",opendcp->xml.timestamp);
    xmlTextWriterWriteFormatElement(xml, BAD_CAST "Issuer","%s",opendcp->xml.issuer);

    xmlTextWriterStartElement(xml, BAD_CAST "AssetList");

    dcp_log(LOG_INFO,"Writing ASSETMAP PKL");

    /* PKL */
    xmlTextWriterStartElement(xml, BAD_CAST "Asset");
    xmlTextWriterWriteFormatElement(xml, BAD_CAST "Id","%s%s","urn:uuid:",opendcp->pkl[0].uuid);
    xmlTextWriterWriteFormatElement(xml, BAD_CAST "PackingList","%s","true");
    xmlTextWriterStartElement(xml, BAD_CAST "ChunkList");
    xmlTextWriterStartElement(xml, BAD_CAST "Chunk");
    xmlTextWriterWriteFormatElement(xml, BAD_CAST "Path","%s",basename(opendcp->pkl[0].filename));
    xmlTextWriterWriteFormatElement(xml, BAD_CAST "VolumeIndex","%d",1);
    xmlTextWriterWriteFormatElement(xml, BAD_CAST "Offset","%d",0);
    xmlTextWriterWriteFormatElement(xml, BAD_CAST "Length","%s",opendcp->pkl[0].size);
    xmlTextWriterEndElement(xml); /* end chunk */
    xmlTextWriterEndElement(xml); /* end chunklist */
    xmlTextWriterEndElement(xml); /* end pkl asset */
  
    dcp_log(LOG_INFO,"Writing ASSETMAP CPLs");

    /* CPL */
    for (c=0;c<opendcp->pkl[0].cpl_count;c++) {
        cpl = opendcp->pkl[0].cpl[c];
        xmlTextWriterStartElement(xml, BAD_CAST "Asset");
        xmlTextWriterWriteFormatElement(xml, BAD_CAST "Id","%s%s","urn:uuid:",cpl.uuid);
        xmlTextWriterStartElement(xml, BAD_CAST "ChunkList");
        xmlTextWriterStartElement(xml, BAD_CAST "Chunk");
        xmlTextWriterWriteFormatElement(xml, BAD_CAST "Path","%s",basename(cpl.filename));
        xmlTextWriterWriteFormatElement(xml, BAD_CAST "VolumeIndex","%d",1);
        xmlTextWriterWriteFormatElement(xml, BAD_CAST "Offset","%d",0);
        xmlTextWriterWriteFormatElement(xml, BAD_CAST "Length","%s",cpl.size);
        xmlTextWriterEndElement(xml); /* end chunk */
        xmlTextWriterEndElement(xml); /* end chunklist */
        xmlTextWriterEndElement(xml); /* end cpl asset */

        /* assets(s) start */
        for (r=0;r<cpl.reel_count;r++) {
            reel = cpl.reel[r];
            for (a=0;a<reel.asset_count;a++) {
                asset_t asset = reel.asset[a];
                xmlTextWriterStartElement(xml, BAD_CAST "Asset");
                xmlTextWriterWriteFormatElement(xml, BAD_CAST "Id","%s%s","urn:uuid:",asset.uuid);
                xmlTextWriterStartElement(xml, BAD_CAST "ChunkList");
                xmlTextWriterStartElement(xml, BAD_CAST "Chunk");
                xmlTextWriterWriteFormatElement(xml, BAD_CAST "Path","%s",basename(asset.filename));
                xmlTextWriterWriteFormatElement(xml, BAD_CAST "VolumeIndex","%d",1);
                xmlTextWriterWriteFormatElement(xml, BAD_CAST "Offset","%d",0);
                xmlTextWriterWriteFormatElement(xml, BAD_CAST "Length","%s",asset.size);
                xmlTextWriterEndElement(xml); /* end chunk */
                xmlTextWriterEndElement(xml); /* end chunklist */
                xmlTextWriterEndElement(xml); /* end cpl asset */
            }
        }
    }

    xmlTextWriterEndElement(xml); /* end assetlist */
    xmlTextWriterEndElement(xml); /* end assetmap */

    rc = xmlTextWriterEndDocument(xml);
    if (rc < 0) {
        dcp_log(LOG_ERROR,"xmlTextWriterEndDocument failed %s",opendcp->assetmap.filename);
        return DCP_FATAL;
    }

    xmlFreeTextWriter(xml);
    xmlSaveFormatFile(opendcp->assetmap.filename, doc, 1);
    xmlFreeDoc(doc);

    return DCP_SUCCESS;
}
/**
 * testXmlwriterFilename:
 * @uri: the output URI
 *
 * test the xmlWriter interface when writing to a new file
 */
void
testXmlwriterFilename(const char *uri)
{
    int rc;
    xmlTextWriterPtr writer;
    xmlChar *tmp;

    /* Create a new XmlWriter for uri, with no compression. */
    writer = xmlNewTextWriterFilename(uri, 0);
    if (writer == NULL) {
        printf("testXmlwriterFilename: Error creating the xml writer\n");
        return;
    }

    /* Start the document with the xml default for the version,
     * encoding ISO 8859-1 and the default for the standalone
     * declaration. */
    rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterStartDocument\n");
        return;
    }

    /* Start an element named "EXAMPLE". Since thist is the first
     * element, this will be the root element of the document. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "EXAMPLE");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Write a comment as child of EXAMPLE.
     * Please observe, that the input to the xmlTextWriter functions
     * HAS to be in UTF-8, even if the output XML is encoded
     * in iso-8859-1 */
    tmp = ConvertInput("This is a comment with special chars: <äöü>",
                       MY_ENCODING);
    rc = xmlTextWriterWriteComment(writer, tmp);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteComment\n");
        return;
    }
    if (tmp != NULL) xmlFree(tmp);

    /* Start an element named "ORDER" as child of EXAMPLE. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "ORDER");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Add an attribute with name "version" and value "1.0" to ORDER. */
    rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "version",
                                     BAD_CAST "1.0");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteAttribute\n");
        return;
    }

    /* Add an attribute with name "xml:lang" and value "de" to ORDER. */
    rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
                                     BAD_CAST "de");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteAttribute\n");
        return;
    }

    /* Write a comment as child of ORDER */
    tmp = ConvertInput("<äöü>", MY_ENCODING);
    rc = xmlTextWriterWriteFormatComment(writer,
		     "This is another comment with special chars: %s",
		     tmp);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteFormatComment\n");
        return;
    }
    if (tmp != NULL) xmlFree(tmp);

    /* Start an element named "HEADER" as child of ORDER. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "HEADER");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Write an element named "X_ORDER_ID" as child of HEADER. */
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "X_ORDER_ID",
                                         "%010d", 53535);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
        return;
    }

    /* Write an element named "CUSTOMER_ID" as child of HEADER. */
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "CUSTOMER_ID",
                                         "%d", 1010);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
        return;
    }

    /* Write an element named "NAME_1" as child of HEADER. */
    tmp = ConvertInput("Müller", MY_ENCODING);
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_1", tmp);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteElement\n");
        return;
    }
    if (tmp != NULL) xmlFree(tmp);

    /* Write an element named "NAME_2" as child of HEADER. */
    tmp = ConvertInput("Jörg", MY_ENCODING);
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_2", tmp);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteElement\n");
        return;
    }
    if (tmp != NULL) xmlFree(tmp);

    /* Close the element named HEADER. */
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
        return;
    }

    /* Start an element named "ENTRIES" as child of ORDER. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRIES");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Start an element named "ENTRY" as child of ENTRIES. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Write an element named "ARTICLE" as child of ENTRY. */
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
                                   BAD_CAST "<Test>");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteElement\n");
        return;
    }

    /* Write an element named "ENTRY_NO" as child of ENTRY. */
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
                                         10);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
        return;
    }

    /* Close the element named ENTRY. */
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
        return;
    }

    /* Start an element named "ENTRY" as child of ENTRIES. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Write an element named "ARTICLE" as child of ENTRY. */
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
                                   BAD_CAST "<Test 2>");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteElement\n");
        return;
    }

    /* Write an element named "ENTRY_NO" as child of ENTRY. */
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
                                         20);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
        return;
    }

    /* Close the element named ENTRY. */
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
        return;
    }

    /* Close the element named ENTRIES. */
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
        return;
    }

    /* Start an element named "FOOTER" as child of ORDER. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "FOOTER");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Write an element named "TEXT" as child of FOOTER. */
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "TEXT",
                                   BAD_CAST "This is a text.");
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterWriteElement\n");
        return;
    }

    /* Close the element named FOOTER. */
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
        return;
    }

    /* Here we could close the elements ORDER and EXAMPLE using the
     * function xmlTextWriterEndElement, but since we do not want to
     * write any other elements, we simply call xmlTextWriterEndDocument,
     * which will do all the work. */
    rc = xmlTextWriterEndDocument(writer);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterEndDocument\n");
        return;
    }

    xmlFreeTextWriter(writer);
}
/**
 * testXmlwriterTree:
 * @file: the output file
 *
 * test the xmlWriter interface when writing to a subtree
 */
void
testXmlwriterTree(const char *file)
{
    int rc;
    xmlTextWriterPtr writer;
    xmlDocPtr doc;
    xmlNodePtr node;
    xmlChar *tmp;

    /* Create a new XML DOM tree, to which the XML document will be
     * written */
    doc = xmlNewDoc(BAD_CAST XML_DEFAULT_VERSION);
    if (doc == NULL) {
        printf
            ("testXmlwriterTree: Error creating the xml document tree\n");
        return;
    }

    /* Create a new XML node, to which the XML document will be
     * appended */
    node = xmlNewDocNode(doc, NULL, BAD_CAST "EXAMPLE", NULL);
    if (node == NULL) {
        printf("testXmlwriterTree: Error creating the xml node\n");
        return;
    }

    /* Make ELEMENT the root node of the tree */
    xmlDocSetRootElement(doc, node);

    /* Create a new XmlWriter for DOM tree, with no compression. */
    writer = xmlNewTextWriterTree(doc, node, 0);
    if (writer == NULL) {
        printf("testXmlwriterTree: Error creating the xml writer\n");
        return;
    }

    /* Start the document with the xml default for the version,
     * encoding ISO 8859-1 and the default for the standalone
     * declaration. */
    rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterStartDocument\n");
        return;
    }

    /* Write a comment as child of EXAMPLE.
     * Please observe, that the input to the xmlTextWriter functions
     * HAS to be in UTF-8, even if the output XML is encoded
     * in iso-8859-1 */
    tmp = ConvertInput("This is a comment with special chars: <äöü>",
                       MY_ENCODING);
    rc = xmlTextWriterWriteComment(writer, tmp);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterWriteComment\n");
        return;
    }
    if (tmp != NULL) xmlFree(tmp);

    /* Start an element named "ORDER" as child of EXAMPLE. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "ORDER");
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Add an attribute with name "version" and value "1.0" to ORDER. */
    rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "version",
                                     BAD_CAST "1.0");
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteAttribute\n");
        return;
    }

    /* Add an attribute with name "xml:lang" and value "de" to ORDER. */
    rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
                                     BAD_CAST "de");
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteAttribute\n");
        return;
    }

    /* Write a comment as child of ORDER */
    tmp = ConvertInput("<äöü>", MY_ENCODING);
    rc = xmlTextWriterWriteFormatComment(writer,
			 "This is another comment with special chars: %s",
					  tmp);
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteFormatComment\n");
        return;
    }
    if (tmp != NULL) xmlFree(tmp);

    /* Start an element named "HEADER" as child of ORDER. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "HEADER");
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Write an element named "X_ORDER_ID" as child of HEADER. */
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "X_ORDER_ID",
                                         "%010d", 53535);
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteFormatElement\n");
        return;
    }

    /* Write an element named "CUSTOMER_ID" as child of HEADER. */
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "CUSTOMER_ID",
                                         "%d", 1010);
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteFormatElement\n");
        return;
    }

    /* Write an element named "NAME_1" as child of HEADER. */
    tmp = ConvertInput("Müller", MY_ENCODING);
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_1", tmp);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterWriteElement\n");
        return;
    }
    if (tmp != NULL) xmlFree(tmp);

    /* Write an element named "NAME_2" as child of HEADER. */
    tmp = ConvertInput("Jörg", MY_ENCODING);
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_2", tmp);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterWriteElement\n");
        return;
    }
    if (tmp != NULL) xmlFree(tmp);

    /* Close the element named HEADER. */
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterEndElement\n");
        return;
    }

    /* Start an element named "ENTRIES" as child of ORDER. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRIES");
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Start an element named "ENTRY" as child of ENTRIES. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Write an element named "ARTICLE" as child of ENTRY. */
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
                                   BAD_CAST "<Test>");
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterWriteElement\n");
        return;
    }

    /* Write an element named "ENTRY_NO" as child of ENTRY. */
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
                                         10);
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteFormatElement\n");
        return;
    }

    /* Close the element named ENTRY. */
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterEndElement\n");
        return;
    }

    /* Start an element named "ENTRY" as child of ENTRIES. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Write an element named "ARTICLE" as child of ENTRY. */
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
                                   BAD_CAST "<Test 2>");
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterWriteElement\n");
        return;
    }

    /* Write an element named "ENTRY_NO" as child of ENTRY. */
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
                                         20);
    if (rc < 0) {
        printf
            ("testXmlwriterTree: Error at xmlTextWriterWriteFormatElement\n");
        return;
    }

    /* Close the element named ENTRY. */
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterEndElement\n");
        return;
    }

    /* Close the element named ENTRIES. */
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterEndElement\n");
        return;
    }

    /* Start an element named "FOOTER" as child of ORDER. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST "FOOTER");
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
        return;
    }

    /* Write an element named "TEXT" as child of FOOTER. */
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "TEXT",
                                   BAD_CAST "This is a text.");
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterWriteElement\n");
        return;
    }

    /* Close the element named FOOTER. */
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterEndElement\n");
        return;
    }

    /* Here we could close the elements ORDER and EXAMPLE using the
     * function xmlTextWriterEndElement, but since we do not want to
     * write any other elements, we simply call xmlTextWriterEndDocument,
     * which will do all the work. */
    rc = xmlTextWriterEndDocument(writer);
    if (rc < 0) {
        printf("testXmlwriterTree: Error at xmlTextWriterEndDocument\n");
        return;
    }

    xmlFreeTextWriter(writer);

    xmlSaveFileEnc(file, doc, MY_ENCODING);

    xmlFreeDoc(doc);
}
void add_inventory_item(simgroup_ctx *sgrp, user_ctx *user,
			void *user_priv, inventory_item *inv,
			void(*cb)(void* priv, int success, uuid_t item_id),
			void *cb_priv) {
  uuid_t u, user_id; char tmp[40]; char uri[256];
  GRID_PRIV_DEF_SGRP(sgrp);
  USER_PRIV_DEF(user_priv);
  xmlTextWriterPtr writer;
  xmlBufferPtr buf;
  SoupMessage *msg;
  add_inv_item_req *req;
  struct os_inv_item invitem;
  
  assert(grid->inventory_server != NULL);

  buf = xmlBufferCreate();
  if(buf == NULL) goto fail;
  writer = xmlNewTextWriterMemory(buf, 0);
  if(writer == NULL) goto free_fail_1;
  
  if(xmlTextWriterStartDocument(writer,NULL,"UTF-8",NULL) < 0) 
    goto free_fail;
  if(xmlTextWriterStartElement(writer, BAD_CAST "RestSessionObjectOfInventoryItemBase") < 0) 
    goto free_fail;
  user_get_session_id(user, u);
  uuid_unparse(u, tmp);
  if(xmlTextWriterWriteFormatElement(writer,BAD_CAST "SessionID",
				       "%s",tmp) < 0) goto free_fail;


  user_get_uuid(user, user_id);
  uuid_unparse(user_id, tmp);
  if(xmlTextWriterWriteFormatElement(writer,BAD_CAST "AvatarID",
				       "%s",tmp) < 0) goto free_fail;

  if(xmlTextWriterStartElement(writer,BAD_CAST "Body") < 0) 
    goto free_fail;

  inv_item_to_opensim(inv, invitem);
  osglue_serialise_xml(writer, deserialise_inv_item, &invitem);

  if(xmlTextWriterEndElement(writer) < 0) 
    goto free_fail;

  if(xmlTextWriterEndElement(writer) < 0) 
    goto free_fail;

  if(xmlTextWriterEndDocument(writer) < 0) {
    printf("DEBUG: couldn't end XML document\n"); goto fail;
  }

  // FIXME - don't use fixed-length buffer, and handle missing trailing /
  // (note: not AddNewItem, as that's not meant for sim use!)
  snprintf(uri, 256, "%sNewItem/", grid->inventory_server);
  printf("DEBUG: sending inventory add request to %s\n", uri);
  msg = soup_message_new ("POST", uri);
  // FIXME - avoid unnecessary strlen
  soup_message_set_request (msg, "text/xml",
			    SOUP_MEMORY_COPY, (char*)buf->content, 
			    strlen ((char*)buf->content));
  req = new add_inv_item_req();
  req->user_glue = user_glue; req->cb = cb;
  req->cb_priv = cb_priv;
  uuid_copy(req->item_id, inv->item_id);
  user_grid_glue_ref(user_glue);
  caj_queue_soup_message(sgrp, SOUP_MESSAGE(msg),
			 got_add_inv_item_resp, req);
    
  xmlFreeTextWriter(writer);  
  xmlBufferFree(buf);
  return;

 free_fail:
  xmlFreeTextWriter(writer);  
 free_fail_1:
  xmlBufferFree(buf);
 fail:
  printf("DEBUG: ran into issues sending inventory NewItem request\n");
  uuid_clear(u); cb(cb_priv, FALSE, u);
}
Beispiel #28
0
/* ========================================================================= */
static int writeDiscreteSwitchingStateContents(xmlTextWriterPtr writer,
                                               ghmm_xmlfile* f, int moNo,
                                               int sNo) {
#define CUR_PROC "writeDiscreteSwitchingStateContents"

  int bgId, cLabel, rc, order, tied;
  char * tmp=NULL;

  /* writing discrete distribution */
  if (0 > xmlTextWriterStartElement(writer, BAD_CAST "discrete")) {
    GHMM_LOG(LERROR, "Error at xmlTextWriterStartElement (discrete)");
    goto STOP;
  }

  if (0 > xmlTextWriterWriteAttribute(writer, BAD_CAST "id", BAD_CAST "0")) {
    GHMM_LOG(LERROR, "failed to write alphabet id");
    goto STOP;
  }

  if (f->model.ds[moNo]->s[sNo].fix)
    if (0 > xmlTextWriterWriteAttribute(writer, BAD_CAST "fixed", BAD_CAST "1")) {
      GHMM_LOG(LERROR, "failed to write fixed attriute");
      goto STOP;
    }

  if ((f->model.ds[moNo]->model_type & GHMM_kHigherOrderEmissions)
      && f->model.ds[moNo]->order[sNo]) {
    order = f->model.ds[moNo]->order[sNo];
    if (0 > xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "order", "%d", order)) {
      GHMM_LOG(LERROR, "failed to write order attribute for discrete distribution");
      goto STOP;
    }
  } else
    order = 0;

  tmp = doubleArrayToCSV(f->model.ds[moNo]->s[sNo].b, pow(f->model.ds[moNo]->M, order+1));
  if (tmp) {
    if (0 > xmlTextWriterWriteRaw(writer, BAD_CAST tmp)) {
      GHMM_LOG(LERROR, "Error at xmlTextWriterWriteRaw while writing"
               "discrete distribution CSV");
      m_free(tmp);
      goto STOP;
    }
    m_free(tmp);
  } else {
    GHMM_LOG(LERROR, "converting array to CSV failed for discrete distribution");
    goto STOP;
  }

  /* end discrete distribution */
  if (0 > xmlTextWriterEndElement(writer)) {
    GHMM_LOG(LERROR, "Error at xmlTextWriterEndElement (discrete)");
    goto STOP;
  }

  /* writing backgroung key */
  if (f->model.ds[moNo]->model_type & GHMM_kBackgroundDistributions) {
    bgId = f->model.ds[moNo]->background_id[sNo];
    if (bgId != GHMM_kNoBackgroundDistribution) {
      if (f->model.ds[moNo]->bp->name[bgId]) {
        rc = xmlTextWriterWriteElement(writer, BAD_CAST "backgroundKey",
                                       BAD_CAST f->model.ds[moNo]->bp->name[bgId]);
        if (rc<0) {
          GHMM_LOG(LERROR, "Error at xmlTextWriterWriteElement (backgroundKey)");
          goto STOP;
        }
      } else {
        GHMM_LOG(LERROR, "background name is NULL pointer, invalid model");
        goto STOP;
      }
    }
  }

  /* writing class label */
  if (f->model.ds[moNo]->model_type & GHMM_kLabeledStates) {
    cLabel = f->model.ds[moNo]->label[sNo];
    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "class", "%d", cLabel);
    if (rc<0) {
      GHMM_LOG(LERROR, "failed to write class label");
      goto STOP;
    }
  }

  /* duration (not implemented yet, maybe never */
#if 0
  if (f->model.ds[moNo]->model_type & GHMM_kDurations) {
    if (f->model.ds[moNo]->duration[sNo] > 0) {
      rc = xmlTextWriterWriteElement(writer, BAD_CAST "duration",
                                     BAD_CAST f->model.ds[moNo]->duration[sNo]);
      if (rc<0) {
        GHMM_LOG(LERROR, "Error at xmlTextWriterWriteElement (duration)");
        goto STOP;
      }
    }
  }
#endif

  /* writing positions */
  if ((f->model.ds[moNo]->s[sNo].xPosition > 0)
      && (f->model.ds[moNo]->s[sNo].xPosition > 0)) {
    if (xmlTextWriterStartElement(writer, BAD_CAST "position") < 0) {
      GHMM_LOG(LERROR, "failed to start position element (position)"); goto STOP;}
    if (0 > xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "x", "%d",
                                          f->model.ds[moNo]->s[sNo].xPosition)) {
      GHMM_LOG(LERROR, "failed to write x position"); goto STOP;}
    if (0 > xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "y", "%d",
                                          f->model.ds[moNo]->s[sNo].yPosition)) {
      GHMM_LOG(LERROR, "failed to write y position"); goto STOP;}
    if (xmlTextWriterEndElement(writer) < 0) {
      GHMM_LOG(LERROR, "Error at xmlTextWriterEndElement (position)"); goto STOP;}
  }


  /* writing tied states */
  if (f->model.ds[moNo]->model_type & GHMM_kTiedEmissions) {
    tied = f->model.ds[moNo]->tied_to[sNo];
    if (tied != GHMM_kUntied) {
      rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "tiedTo", "%d", tied);
      if (rc<0) {
        GHMM_LOG(LERROR, "failed to write tiedTo element");
        goto STOP;
      }
    }
  }

  return 0;
STOP:
  return -1;
#undef CUR_PROC
}
Beispiel #29
0
void
dt_styles_save_to_file(const char *style_name,const char *filedir,gboolean overwrite)
{
  int rc = 0;
  char stylename[520];
  sqlite3_stmt *stmt;

  snprintf(stylename,512,"%s/%s.dtstyle",filedir,style_name);

  // check if file exists
  if( g_file_test(stylename, G_FILE_TEST_EXISTS) == TRUE )
  {
    if(overwrite)
    {
      if(unlink(stylename))
      {
        dt_control_log(_("failed to overwrite style file for %s"),style_name);
        return;
      }
    }
    else
    {
      dt_control_log(_("style file for %s exists"),style_name);
      return;
    }
  }

  if ( !dt_styles_exists (style_name) ) return;

  xmlTextWriterPtr writer = xmlNewTextWriterFilename(stylename, 0);
  if (writer == NULL)
  {
    fprintf(stderr,"[dt_styles_save_to_file] Error creating the xml writer\n, path: %s", stylename);
    return;
  }
  rc = xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL);
  if (rc < 0)
  {
    fprintf(stderr,"[dt_styles_save_to_file]: Error on encoding setting");
    return;
  }
  xmlTextWriterStartElement(writer, BAD_CAST "darktable_style");
  xmlTextWriterWriteAttribute(writer, BAD_CAST "version", BAD_CAST "1.0");

  xmlTextWriterStartElement(writer, BAD_CAST "info");
  xmlTextWriterWriteFormatElement(writer, BAD_CAST "name", "%s", style_name);
  xmlTextWriterWriteFormatElement(writer, BAD_CAST "description", "%s", dt_styles_get_description(style_name));
  xmlTextWriterEndElement(writer);

  xmlTextWriterStartElement(writer, BAD_CAST "style");
  DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), "select num,module,operation,op_params,enabled,blendop_params,blendop_version,multi_priority,multi_name from style_items where styleid =?1",-1, &stmt,NULL);
  DT_DEBUG_SQLITE3_BIND_INT(stmt,1,dt_styles_get_id_by_name(style_name));
  while (sqlite3_step (stmt) == SQLITE_ROW)
  {
    xmlTextWriterStartElement(writer, BAD_CAST "plugin");
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "num", "%d", sqlite3_column_int(stmt,0));
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "module", "%d", sqlite3_column_int(stmt,1));
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "operation", "%s", sqlite3_column_text(stmt,2));
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "op_params", "%s", dt_style_encode(stmt,3));
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "enabled", "%d", sqlite3_column_int(stmt,4));
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "blendop_params", "%s", dt_style_encode(stmt,5));
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "blendop_version", "%d", sqlite3_column_int(stmt,6));
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "multi_priority", "%d", sqlite3_column_int(stmt,7));
    xmlTextWriterWriteFormatElement(writer, BAD_CAST "multi_name", "%s", sqlite3_column_text(stmt,8));
    xmlTextWriterEndElement(writer);
  }
  sqlite3_finalize(stmt);
  xmlTextWriterEndDocument(writer);
  xmlFreeTextWriter(writer);
}
Beispiel #30
0
int ploop_store_diskdescriptor(const char *fname, struct ploop_disk_images_data *di)
{
	int i, rc = -1;
	xmlTextWriterPtr writer = NULL;
	xmlDocPtr doc = NULL;
	char tmp[PATH_MAX];
	char basedir[PATH_MAX];
	FILE *fp = NULL;

	ploop_log(0, "Storing %s", fname);

	if (convert_disk_descriptor(di))
		return -1;

	if (di->runtime->xml_fname == NULL)
		di->runtime->xml_fname = strdup(fname);

	get_basedir(fname, tmp, sizeof(tmp));
	if (tmp[0] == '\0')
		strcpy(tmp, "./");

	if (realpath(tmp, basedir) == NULL) {
		ploop_err(errno, "Can't resolve %s", tmp);
		return -1;
	}

	doc = xmlNewDoc(BAD_CAST XML_DEFAULT_VERSION);
	if (doc == NULL) {
		ploop_err(0, "Error creating xml document tree");
		return -1;
	}

	/* Create a new XmlWriter for DOM tree, with no compression. */
	writer = xmlNewTextWriterTree(doc, NULL, 0);
	if (writer == NULL) {
		ploop_err(0, "Error creating xml writer");
		goto err;
	}

	/* Start the document with the xml default for the version,
	 * encoding ISO 8859-1 and the default for the standalone
	 * declaration. */
	rc = xmlTextWriterStartDocument(writer, NULL, NULL, NULL);
	if (rc < 0) {
		ploop_err(0, "Error at xmlTextWriterStartDocument");
		goto err;
	}
	rc = xmlTextWriterStartElement(writer, BAD_CAST "Parallels_disk_image");
	if (rc < 0) {
		ploop_err(0, "Error at xmlTextWriterStartDocument");
		goto err;
	}
	/*********************************************
	 *	Disk_Parameters
	 ********************************************/
	rc = xmlTextWriterStartElement(writer, BAD_CAST "Disk_Parameters");
	if (rc < 0) {
		ploop_err(0, "Error at xmlTextWriter Disk_Parameters");
		goto err;
	}
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "Disk_size", "%llu", di->size);
	if (rc < 0) {
		ploop_err(0, "Error at xmlTextWriter Disk_size");
		goto err;
	}

	if (di->max_delta_size != 0) {
		rc = xmlTextWriterWriteFormatElement(writer,
				BAD_CAST "Max_delta_size", "%llu", di->max_delta_size);
		if (rc < 0) {
			ploop_err(0, "Error at xmlTextWriter Max_delta_size");
			goto err;
		}
	}

	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "Cylinders", "%u", di->cylinders);
	if (rc < 0) {
		ploop_err(0, "Error at xmlTextWriter Cylinders");
		goto err;
	}
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "Heads", "%u", di->heads);
	if (rc < 0) {
		ploop_err(0, "Error at xmlTextWriter Heads");
		goto err;
	}
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "Sectors", "%llu",
			di->size /(di->cylinders * di->heads));
	if (rc < 0) {
		ploop_err(0, "Error at xmlTextWriter Sectors");
		goto err;
	}
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "Padding", "%u", 0);
	if (rc < 0) {
		ploop_err(0, "Error at xmlTextWriter Padding");
		goto err;
	}

	/* Close   Disk_Parameters */
	rc = xmlTextWriterEndElement(writer);
	if (rc < 0) {
		ploop_err(0, "Error at xmlTextWriterEndElement");
		goto err;
	}
	/****************************************
	 * StorageData
	 ****************************************/
	rc = xmlTextWriterStartElement(writer, BAD_CAST "StorageData");
	if (rc < 0) {
		ploop_err(0, "Error at xmlTextWriter StorageData");
		goto err;
	}
	/* Start an element named "Storage" as child of StorageData. */
	rc = xmlTextWriterStartElement(writer, BAD_CAST "Storage");
	if (rc < 0) {
		ploop_err(0, "Error at xmlTextWriter Storage");
		goto err;
	}
	rc = xmlTextWriterWriteElement(writer, BAD_CAST "Start",  BAD_CAST "0");
	if (rc < 0) {
		ploop_err(0, "Error at xmlTextWriter Start");
		goto err;
	}
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "End", "%llu", di->size);
	if (rc < 0) {
		ploop_err(0, "Error at xmlTextWriter End");
		goto err;
	}
	rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "Blocksize", "%d",
			di->blocksize);
	if (rc < 0) {
		ploop_err(0, "Error at xmlTextWriter Blocksize");
		goto err;
	}
	if (di->mode == PLOOP_EXPANDED_PREALLOCATED_MODE) {
		rc = xmlTextWriterWriteElement(writer, BAD_CAST "Preallocated",
				BAD_CAST "1");
		if (rc < 0) {
			ploop_err(0, "Error at xmlTextWriter Preallocated");
			goto err;
		}
	}
	/****************************************
	 *	Images
	 ****************************************/
	for (i = 0; i < di->nimages; i++) {
		rc = xmlTextWriterStartElement(writer, BAD_CAST "Image");
		if (rc < 0) {
			ploop_err(0, "Error at xmlTextWriter Image");
			goto err;
		}

		rc = xmlTextWriterWriteElement(writer, BAD_CAST "GUID",
				BAD_CAST di->images[i]->guid);
		if (rc < 0) {
			ploop_err(0, "Error at xmlTextWriter GUID");
			goto err;
		}
		rc = xmlTextWriterWriteElement(writer, BAD_CAST "Type",
			BAD_CAST (di->mode == PLOOP_RAW_MODE ? "Plain" : "Compressed"));
		if (rc < 0) {
			ploop_err(0, "Error at xmlTextWriter Type");
			goto err;
		}

		normalize_image_name(basedir, di->images[i]->file, tmp, sizeof(tmp));
		rc = xmlTextWriterWriteElement(writer, BAD_CAST "File",
				BAD_CAST tmp);
		if (rc < 0) {
			ploop_err(0, "Error at xmlTextWriter File");
			goto err;
		}

		/*  Close  Image */
		rc = xmlTextWriterEndElement(writer);
		if (rc < 0) {
			ploop_err(0, "Error at xmlTextWriterEndElement");
			goto err;
		}
	}

	/* Close Storage */
	rc = xmlTextWriterEndElement(writer);
	if (rc < 0) {
		ploop_err(0, "Error at xmlTextWriterEndElement");
		goto err;
	}
	/* Close StorageData. */
	rc = xmlTextWriterEndElement(writer);
	if (rc < 0) {
		ploop_err(0, "Error at xmlTextWriterEndElement");
		goto err;
	}
	/****************************************
	 *	Snapshots
	 ****************************************/
	rc = xmlTextWriterStartElement(writer, BAD_CAST "Snapshots");
	if (rc < 0) {
		ploop_err(0, "Error at xmlTextWriter Snapshots");
		goto err;
	}

	if (di->top_guid != NULL) {
		rc = xmlTextWriterWriteElement(writer, BAD_CAST "TopGUID",
				BAD_CAST di->top_guid);
		if (rc < 0) {
			ploop_err(0, "Error at xmlTextWriter TopGUID");
			goto err;
		}
	}

	/****************************************
	 *      Shot
	 ****************************************/
	for (i = 0; i < di->nsnapshots; i++) {
		rc = xmlTextWriterStartElement(writer, BAD_CAST "Shot");
		if (rc < 0) {
			ploop_err(0, "Error at xmlTextWriter Shot");
			goto err;
		}

		rc = xmlTextWriterWriteElement(writer, BAD_CAST "GUID",
				BAD_CAST di->snapshots[i]->guid);
		if (rc < 0) {
			ploop_err(0, "Error at xmlTextWriterWrite GUID");
			goto err;
		}
		rc = xmlTextWriterWriteElement(writer, BAD_CAST "ParentGUID",
				BAD_CAST di->snapshots[i]->parent_guid);
		if (rc < 0) {
			ploop_err(0, "Error at xmlTextWriter ParentGUID");
			goto err;
		}

		if (di->snapshots[i]->temporary) {
			rc = xmlTextWriterWriteElement(writer, BAD_CAST "Temporary",  BAD_CAST "");
			if (rc < 0) {
				ploop_err(0, "Error at xmlTextWriter Temporary");
				goto err;
			}
		}

		/*  Close Shot */
		rc = xmlTextWriterEndElement(writer);
		if (rc < 0) {
			ploop_err(0, "Error at xmlTextWriterEndElement");
			goto err;
		}
	}
	/* Close Snapshots */
	rc = xmlTextWriterEndElement(writer);
	if (rc < 0) {
		ploop_err(0, "Error at xmlTextWriterEndElement");
		goto err;
	}

	/* Close Parallels_disk_image */
	rc = xmlTextWriterEndElement(writer);
	if (rc < 0) {
		ploop_err(0, "Error at xmlTextWriterEndElement");
		goto err;
	}
	xmlFreeTextWriter(writer);
	writer = NULL;

	snprintf(tmp, sizeof(tmp), "%s.tmp", fname);
	fp = fopen(tmp, "w+");
	if (fp == NULL) {
		ploop_err(errno, "Can't open %s", tmp);
		goto err;
	}

	rc = xmlDocFormatDump(fp, doc, 1);
	if (rc < 0) {
		ploop_err(0, "Error at xmlDocFormatDump %s", tmp);
		goto err;
	}

	rc = fsync(fileno(fp));
	if (rc) {
		ploop_err(errno, "Failed to sync %s", tmp);
		goto err;
	}
	fclose(fp);
	fp = NULL;

	rc = rename(tmp, fname);
	if (rc < 0) {
		ploop_err(errno, "Can't rename %s to %s", tmp, fname);
		goto err;
	}

	rc = 0;
err:
	if (fp)
		fclose(fp);

	if (writer)
		xmlFreeTextWriter(writer);
	if (doc)
		xmlFreeDoc(doc);
	if (rc)
		return SYSEXIT_DISKDESCR;

	return 0;
}