void closeFile() { mXPath.clear(); // This section writes to file std::ofstream fileStream; if(!mStream) { fileStream.open(mXMLFilePath.c_str()); mStream = &fileStream; } xmlBufferPtr buffer = xmlBufferCreate(); xmlOutputBuffer * outputBuffer = xmlOutputBufferCreateBuffer(buffer, NULL); xmlSaveFormatFileTo(outputBuffer, mXMLDocument, "utf-8", 1); *mStream << buffer->content; xmlBufferFree(buffer); if(fileStream.is_open()) { fileStream.close(); mStream = NULL; } xmlFreeDoc(mXMLDocument); xmlCleanupParser(); if(mHeavyDataWriter->getMode() == XdmfHeavyDataWriter::Default) { mHeavyDataWriter->closeFile(); } };
void XMLNode::GetDoc(std::string& out_xml_str, bool user_friendly) const { out_xml_str.resize(0); if (!node_) return; xmlDocPtr doc = node_->doc; if (doc == NULL) return; xmlOutputBufferPtr buf = xmlOutputBufferCreateIO(&write_to_string,&close_string,&out_xml_str,NULL); if(buf == NULL) return; /* xmlChar *buf = NULL; int bufsize = 0; if (user_friendly) xmlDocDumpFormatMemory(doc, &buf, &bufsize, 1); else xmlDocDumpMemory(doc, &buf, &bufsize); if (buf) { out_xml_str = (char*)buf; xmlFree(buf); } */ // Note xmlSaveFormatFileTo/xmlSaveFileTo call xmlOutputBufferClose if (user_friendly) xmlSaveFormatFileTo(buf, doc, (const char*)(doc->encoding), 1); else xmlSaveFileTo(buf, doc, (const char*)(doc->encoding)); }
char * noit_xmlSaveToBuffer(xmlDocPtr doc) { xmlOutputBufferPtr out; xmlCharEncodingHandlerPtr enc; struct noit_xml_buffer_ptr buf = { NULL,0,0,0 }; enc = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8); out = xmlOutputBufferCreateIO(noit_xml_save_writer, noit_xml_save_closer, &buf, enc); assert(out); xmlSaveFormatFileTo(out, doc, "utf8", 1); return buf.buff; }
/** * Save configuration on disk. * Only valid operation for root element. */ void XmlStore::save(const char *filename) { WriteFile* file = FileSystem::openWrite(filename); XMLPhysfsWriter xmlio(file); if (m_doc == 0) { throw Exception("xml store: child cannot save document"); } if (xmlSaveFormatFileTo(xmlio.getXMLBuffer(), m_doc, 0, 1) == -1) { throw Exception("xml store: cannot save xml to file '%s'", filename); } LOGGER.debug("saved xml config '%s'", filename); }
void XMLStream::toStream( xmlDocPtr doc, std::ostream &os ) { void *IOContexte = &os; const char *encoding="UTF-8"; int formatWithSpace=1; xmlCharEncodingHandlerPtr handler = xmlFindCharEncodingHandler(encoding); //xmlCharEncodingHandlerPtr handler = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8); doesnt work :/ SLM_ASSERT("handler not instanced", handler); //xmlThrDefIndentTreeOutput(4); xmlOutputBufferPtr outBuff = xmlOutputBufferCreateIO( streamIOWrite , streamIOClose, IOContexte, handler); xmlSaveFormatFileTo( outBuff, doc, encoding, formatWithSpace ); }
void COXml::flush () { if (_Document) { // Generate indentation xmlKeepBlanksDefault (0); // Create a output context xmlOutputBufferPtr outputBuffer = xmlOutputBufferCreateIO ( xmlOutputWriteCallbackForNeL, xmlOutputCloseCallbackForNeL, this, NULL ); // Save the file int res = xmlSaveFormatFileTo (outputBuffer, _Document, NULL, 1); // No error should be returned because, exception should be raised by the internal stream nlassert (res!=-1); // Free the document xmlFreeDoc (_Document); _Document = NULL; } }
int oscap_xml_save_filename(const char *filename, xmlDocPtr doc) { xmlOutputBufferPtr buff; int xmlCode; if (strcmp(filename, "-") == 0) { xmlCode = xmlSaveFormatFileEnc(filename, doc, "UTF-8", 1); } else { int fd = open(filename, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); if (fd < 0) { oscap_seterr(OSCAP_EFAMILY_GLIBC, "%s '%s'", strerror(errno), filename); xmlFreeDoc(doc); return -1; } buff = xmlOutputBufferCreateFd(fd, NULL); if (buff == NULL) { close(fd); oscap_setxmlerr(xmlGetLastError()); oscap_dlprintf(DBG_W, "xmlOutputBufferCreateFile() failed.\n"); xmlFreeDoc(doc); return -1; } xmlCode = xmlSaveFormatFileTo(buff, doc, "UTF-8", 1); close(fd); } if (xmlCode <= 0) { oscap_setxmlerr(xmlGetLastError()); oscap_dlprintf(DBG_W, "No bytes exported: xmlCode: %d.\n", xmlCode); } return (xmlCode >= 1) ? 1 : -1; }
static bool hidrd_xml_snk_flush(hidrd_snk *snk) { bool result = false; hidrd_xml_snk_inst *xml_snk = (hidrd_xml_snk_inst *)snk; bool valid; xmlBufferPtr xml_buf = NULL; xmlOutputBufferPtr xml_out_buf = NULL; void *new_buf; size_t new_size; XML_ERR_FUNC_BACKUP_DECL; free(xml_snk->err); xml_snk->err = strdup(""); XML_ERR_FUNC_SET(&xml_snk->err); /* Break any unfinished groups */ if (!xml_snk_group_break_branch(snk)) goto cleanup; /* Validate the document, if the schema is specified */ if (*xml_snk->schema != '\0' && (!xml_validate(&valid, xml_snk->doc, xml_snk->schema) || !valid)) goto cleanup; /* Create an XML buffer */ xml_buf = xmlBufferCreate(); if (xml_buf == NULL) goto cleanup; /* Create an XML output buffer from the generic buffer */ xml_out_buf = xmlOutputBufferCreateBuffer(xml_buf, NULL); if (xml_out_buf == NULL) goto cleanup; /* Format XML from the document */ if (xmlSaveFormatFileTo(xml_out_buf, xml_snk->doc, NULL, xml_snk->format) < 0) goto cleanup; /* xml_out_buf is closed by xmlSaveFormatFileTo */ xml_out_buf = NULL; /* Retrieve resulting size */ new_size = xmlBufferLength(xml_buf); /* If we have a location for the buffer pointer */ if (snk->pbuf != NULL) { /* Retention and update the buffer */ new_buf = realloc(*snk->pbuf, new_size); if (new_size > 0 && new_buf == NULL) XML_ERR_CLNP("failed to retention the output buffer"); memcpy(new_buf, xmlBufferContent(xml_buf), new_size); /* Update the buffer pointer */ *snk->pbuf = new_buf; } /* Output size */ if (snk->psize != NULL) *snk->psize = new_size; result = true; cleanup: if (xml_out_buf != NULL) xmlOutputBufferClose(xml_out_buf); if (xml_buf != NULL) xmlBufferFree(xml_buf); XML_ERR_FUNC_RESTORE; return result; }
int cpdSendCpPositionResponseToModem(pCPD_CONTEXT pCpd) { int result = 0; xmlDoc *pDoc; xmlBuffer *pXmlBuffer; xmlOutputBuffer *pOutBuffer; int sendMultipleResponses = CPD_NOK; CPD_LOG(CPD_LOG_ID_TXT , "\n %u: %s(%u)\n", getMsecTime(), __FUNCTION__, pCpd->request.dbgStats.posRequestId); LOGD("%u: %s(%u)", getMsecTime(), __FUNCTION__, pCpd->request.dbgStats.posRequestId); CPD_LOG(CPD_LOG_ID_TXT , "\n Location : %f, %f, %d", pCpd->response.location.location_parameters.shape_data.point_alt_uncertellipse.coordinate.longitude, pCpd->response.location.location_parameters.shape_data.point_alt_uncertellipse.coordinate.latitude.degrees, pCpd->response.location.location_parameters.shape_data.point_alt_uncertellipse.altitude.height ); LOGD("Rx Location : %f, %f, %d", pCpd->response.location.location_parameters.shape_data.point_alt_uncertellipse.coordinate.longitude, pCpd->response.location.location_parameters.shape_data.point_alt_uncertellipse.coordinate.latitude.degrees, pCpd->response.location.location_parameters.shape_data.point_alt_uncertellipse.altitude.height ); CPD_LOG(CPD_LOG_ID_TXT | CPD_LOG_ID_CONSOLE, "\n TTFF : %u, %u, %u, %u, %u", (pCpd->response.dbgStats.posReceivedFromGps1 - pCpd->response.dbgStats.posRequestedFromGps), pCpd->response.dbgStats.posRequestedByNetwork, pCpd->response.dbgStats.posRequestedFromGps, pCpd->response.dbgStats.posReceivedFromGps1, pCpd->response.dbgStats.posReceivedFromGps ); LOGD("TTFF : %u, %u, %u, %u, %u", (pCpd->response.dbgStats.posReceivedFromGps1 - pCpd->response.dbgStats.posRequestedFromGps), pCpd->response.dbgStats.posRequestedByNetwork, pCpd->response.dbgStats.posRequestedFromGps, pCpd->response.dbgStats.posReceivedFromGps1, pCpd->response.dbgStats.posReceivedFromGps ); /* is this contignous-reporting mode? */ if (pCpd->request.posMeas.flag == POS_MEAS_RRC) { if (pCpd->request.posMeas.posMeas_u.rrc_meas.rep_crit.period_rep_crit.rep_amount == 0){ sendMultipleResponses = CPD_OK; } } pDoc = xmlNewDoc((xmlChar *) "1.0"); pXmlBuffer = xmlBufferCreate(); pOutBuffer = xmlOutputBufferCreateBuffer(pXmlBuffer, NULL); /* Create the XML document from data */ if (pCpd->response.flag == RESPONSE_FLAG_POS_MEAS) { result = cpdXmlFormatLocation(pCpd, pDoc); } else if (pCpd->response.flag == RESPONSE_FLAG_GPS_MEAS) { result = cpdXmlFormatMeasurements(pCpd, pDoc); } if (result == CPD_OK) { /* Convert XML Document into pXmlBuffer */ xmlSaveFormatFileTo(pOutBuffer, pDoc, "utf-8", 1); CPD_LOG(CPD_LOG_ID_TXT, "\r\npOutBuffer["); CPD_LOG_DATA(CPD_LOG_ID_TXT | CPD_LOG_ID_XML_TX, (char *)pXmlBuffer->content ,strlen((char *)pXmlBuffer->content)); CPD_LOG(CPD_LOG_ID_XML_TX, "\r\n"); CPD_LOG(CPD_LOG_ID_TXT, "]\r\n"); /* Send response to the modem */ CPD_LOG(CPD_LOG_ID_TXT , "\n %u: %u, dT=%u, alreadySent=%d, sendMultipleResponses = %d\n", getMsecTime(), pCpd->modemInfo.sendingCPOSat, getMsecDt(pCpd->modemInfo.sendingCPOSat), pCpd->modemInfo.sentCPOSok, sendMultipleResponses); if ((pCpd->modemInfo.sentCPOSok != CPD_OK) || (sendMultipleResponses == CPD_OK)) { CPD_LOG(CPD_LOG_ID_TXT , "\n %u: %u!= 0\n", getMsecTime(), pCpd->modemInfo.sendingCPOSat); if (getMsecDt(pCpd->modemInfo.sendingCPOSat) >= 1000UL) { pCpd->modemInfo.sendingCPOSat = getMsecTime(); pCpd->modemInfo.haveResponse = 0; pCpd->modemInfo.responseValue = CPD_ERROR; result = cpdSendCposResponse(pCpd, (char *)pXmlBuffer->content); if (result == CPD_OK) { usleep(50 * MODEM_POOL_INTERVAL); /* wait for modem response, which comes in in another thread */ if ((pCpd->modemInfo.haveResponse != 0) && (pCpd->modemInfo.responseValue == AT_RESPONSE_OK)) { pCpd->request.status.nResponsesSent++; pCpd->modemInfo.sentCPOSok = CPD_OK; pCpd->systemMonitor.processingRequest = CPD_NOK; pCpd->request.status.responseSentToModemAt = getMsecTime(); if (cpdIsNumberOfResponsesSufficientForRequest(pCpd) == CPD_OK) { cpdSendAbortToGps(pCpd); } } else { result = CPD_NOK; } } CPD_LOG(CPD_LOG_ID_TXT , "\n %u: cpdSendCposResponse() = %d, Modem: %d, %d, CPOSsent=%d", getMsecTime(), result, pCpd->modemInfo.haveResponse, pCpd->modemInfo.responseValue, pCpd->modemInfo.sentCPOSok); } else { CPD_LOG(CPD_LOG_ID_TXT , "\n %u:Not sending response to modem, dT=%u\n", getMsecTime(), getMsecDt(pCpd->modemInfo.sendingCPOSat)); } } } xmlBufferFree(pXmlBuffer); /* Release allocated resoucrs */ xmlFreeDoc(pDoc); //xmlOutputBufferClose(pOutBuffer); CPD_LOG(CPD_LOG_ID_TXT, "\r\n %u: END %s() = %d", getMsecTime(), __FUNCTION__, result); LOGD("%u: END %s() = %d", getMsecTime(), __FUNCTION__, result); return result; }