#include "lpc2xml.h"
#include <string.h>
#include <libxml/xmlsave.h>
-
+#include <libxml/xmlversion.h>
#define LPC2XML_BZ 2048
}
free(ctx);
}
-/*
+
static void lpc2xml_context_clear_logs(lpc2xml_context *ctx) {
ctx->errorBuffer[0]='\0';
ctx->warningBuffer[0]='\0';
-}*/
+}
static void lpc2xml_log(lpc2xml_context *xmlCtx, int level, const char *fmt, ...) {
va_list args;
}
va_end(args);
}
-/*
+
static void lpc2xml_genericxml_error(void *ctx, const char *fmt, ...) {
lpc2xml_context *xmlCtx = (lpc2xml_context *)ctx;
int sl = strlen(xmlCtx->errorBuffer);
va_end(args);
}
+/*
static void lpc2xml_genericxml_warning(void *ctx, const char *fmt, ...) {
lpc2xml_context *xmlCtx = (lpc2xml_context *)ctx;
int sl = strlen(xmlCtx->warningBuffer);
va_start(args, fmt);
vsnprintf(xmlCtx->warningBuffer + sl, LPC2XML_BZ-sl, fmt, args);
va_end(args);
-}*/
-
-#if 0
-static void dumpNodes(int level, xmlNode * a_node, lpc2xml_context *ctx) {
- xmlNode *cur_node = NULL;
-
- for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
- if (cur_node->type == XML_ELEMENT_NODE) {
- lpc2xml_log(ctx, LPC2XML_DEBUG, "node level: %d type: Element, name: %s", level, cur_node->name);
- } else {
- lpc2xml_log(ctx, LPC2XML_DEBUG, "node level: %d type: %d, name: %s", level, cur_node->type, cur_node->name);
- }
-
- dumpNodes(level + 1, cur_node->children, ctx);
- }
-}
-#endif
-
-#if 0
-static void dumpNode(xmlNode *node, lpc2xml_context *ctx) {
- lpc2xml_log(ctx, LPC2XML_DEBUG, "node type: %d, name: %s", node->type, node->name);
}
-
-static void dumpAttr(xmlNode *node, lpc2xml_context *ctx) {
- lpc2xml_log(ctx, LPC2XML_DEBUG, "attr name: %s value:%s", node->name, node->children->content);
-}
-
-static void dumpContent(xmlNode *node, lpc2xml_context *ctx) {
- lpc2xml_log(ctx, LPC2XML_DEBUG, "content: %s", node->children->content);
-}
-
-
-
-#endif
+*/
static int processEntry(const char *section, const char *entry, xmlNode *node, lpc2xml_context *ctx) {
const char *content = lp_config_get_string(ctx->lpc, section, entry, NULL);
lpc2xml_log(ctx->ctx, LPC2XML_ERROR, "Issue when reading the lpc");
return -1;
}
+
+ lpc2xml_log(ctx, LPC2XML_MESSAGE, "Set %s|%s = %s", section, entry, content);
xmlNodeSetContent(node, (const xmlChar *) content);
return 0;
}
ctx->ret = -1;
return;
}
+
ctx->ret = processEntry(ctx->section, entry, node, ctx->ctx);
}
}
lpc2xml_log(ctx, LPC2XML_ERROR, "Can't create \"config\" element");
return -1;
}
- xmlNs *lpc_ns = xmlNewNs(root_node, (const xmlChar *)"http://www.linphone.org/xsds/lpconfig.xsd", (const xmlChar *)"lpc");
+ xmlNs *lpc_ns = xmlNewNs(root_node, (const xmlChar *)"http://www.linphone.org/xsds/lpconfig.xsd", NULL);
if(lpc_ns == NULL) {
lpc2xml_log(ctx, LPC2XML_WARNING, "Can't create lpc namespace");
} else {
if(lpc_ns == NULL) {
lpc2xml_log(ctx, LPC2XML_WARNING, "Can't create xsi namespace");
}
- xmlAttr *schemaLocation = xmlNewNsProp(root_node, xsi_ns, (const xmlChar *)"schemaLocation", (const xmlChar *)"http://www.linphone.org/xsds/lpconfig.xsd lpconfig.xsd ");
+ xmlAttr *schemaLocation = xmlNewNsProp(root_node, xsi_ns, (const xmlChar *)"schemaLocation", (const xmlChar *)"http://www.linphone.org/xsds/lpconfig.xsd lpconfig.xsd");
if(schemaLocation == NULL) {
lpc2xml_log(ctx, LPC2XML_WARNING, "Can't create schemaLocation");
}
}
int lpc2xml_convert_file(lpc2xml_context* context, const char *filename) {
- int ret = 0;
+ int ret = -1;
+ lpc2xml_context_clear_logs(context);
+ xmlSetGenericErrorFunc(context, lpc2xml_genericxml_error);
xmlSaveCtxtPtr save_ctx = xmlSaveToFilename(filename, "UTF-8", XML_SAVE_FORMAT);
- ret = internal_convert_lpc2xml(context);
- if(ret == 0) {
- ret = xmlSaveDoc(save_ctx, context->doc);
+ if(save_ctx != NULL) {
+ ret = internal_convert_lpc2xml(context);
+ if(ret == 0) {
+ ret = xmlSaveDoc(save_ctx, context->doc);
+ if(ret != 0) {
+ lpc2xml_log(context, LPC2XML_ERROR, "Can't save document");
+ lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
+ }
+ }
+ xmlSaveClose(save_ctx);
+ } else {
+ lpc2xml_log(context, LPC2XML_ERROR, "Can't open file:%s", filename);
+ lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
}
- xmlSaveClose(save_ctx);
return ret;
}
int lpc2xml_convert_fd(lpc2xml_context* context, int fd) {
- int ret = 0;
+ int ret = -1;
+ lpc2xml_context_clear_logs(context);
+ xmlSetGenericErrorFunc(context, lpc2xml_genericxml_error);
xmlSaveCtxtPtr save_ctx = xmlSaveToFd(fd, "UTF-8", XML_SAVE_FORMAT);
- ret = internal_convert_lpc2xml(context);
- if(ret == 0) {
- ret = xmlSaveDoc(save_ctx, context->doc);
+ if(save_ctx != NULL) {
+ ret = internal_convert_lpc2xml(context);
+ if(ret == 0) {
+ ret = xmlSaveDoc(save_ctx, context->doc);
+ if(ret != 0) {
+ lpc2xml_log(context, LPC2XML_ERROR, "Can't save document");
+ lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
+ }
+ }
+ xmlSaveClose(save_ctx);
+ } else {
+ lpc2xml_log(context, LPC2XML_ERROR, "Can't open fd:%d", fd);
+ lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
}
- xmlSaveClose(save_ctx);
return ret;
}
-int lpc2xml_convert_string(lpc2xml_context* context, unsigned char **content) {
- int ret = 0;
+int lpc2xml_convert_string(lpc2xml_context* context, char **content) {
+ int ret = -1;
xmlBufferPtr buffer = xmlBufferCreate();
+ lpc2xml_context_clear_logs(context);
+ xmlSetGenericErrorFunc(context, lpc2xml_genericxml_error);
xmlSaveCtxtPtr save_ctx = xmlSaveToBuffer(buffer, "UTF-8", XML_SAVE_FORMAT);
- internal_convert_lpc2xml(context);
- if(ret == 0) {
- ret = xmlSaveDoc(save_ctx, context->doc);
+ if(save_ctx != NULL) {
+ ret = internal_convert_lpc2xml(context);
+ if(ret == 0) {
+ ret = xmlSaveDoc(save_ctx, context->doc);
+ if(ret != 0) {
+ lpc2xml_log(context, LPC2XML_ERROR, "Can't save document");
+ lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
+ }
+ }
+ xmlSaveClose(save_ctx);
+ } else {
+ lpc2xml_log(context, LPC2XML_ERROR, "Can't initialize internal buffer");
+ lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
}
- xmlSaveClose(save_ctx);
if(ret == 0) {
- *content = xmlBufferDetach(buffer);
+#if LIBXML_VERSION >= 20800
+ *content = (char *)xmlBufferDetach(buffer);
+#else
+ *content = strdup((const char *)xmlBufferContent(buffer));
+#endif
}
xmlBufferFree(buffer);
return ret;