]> sjero.net Git - linphone/blob - tools/lpc2xml.c
Remove useless prefix(use default)
[linphone] / tools / lpc2xml.c
1 /*
2 linphone
3 Copyright (C) 2012 Belledonne Communications SARL
4 Yann DIORCET (yann.diorcet@linphone.org)
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 */
20
21 #include "lpc2xml.h"
22 #include <string.h>
23 #include <libxml/xmlsave.h>
24
25
26 #define LPC2XML_BZ 2048
27
28 struct _lpc2xml_context {
29         const LpConfig *lpc;
30         lpc2xml_function cbf;
31         void *ctx;
32         
33         xmlDoc *doc;
34         char errorBuffer[LPC2XML_BZ];
35         char warningBuffer[LPC2XML_BZ];
36 };
37
38
39 lpc2xml_context* lpc2xml_context_new(lpc2xml_function cbf, void *ctx) {
40         lpc2xml_context *xmlCtx = (lpc2xml_context*)malloc(sizeof(lpc2xml_context));
41         if(xmlCtx != NULL) {
42                 xmlCtx->lpc = NULL;
43                 xmlCtx->cbf = cbf;
44                 xmlCtx->ctx = ctx;
45                 
46                 xmlCtx->doc = NULL;
47                 xmlCtx->errorBuffer[0]='\0';
48                 xmlCtx->warningBuffer[0]='\0';
49         }
50         return xmlCtx;
51 }
52
53 void lpc2xml_context_destroy(lpc2xml_context *ctx) {
54         if(ctx->doc != NULL) {
55                 xmlFreeDoc(ctx->doc);
56                 ctx->doc = NULL;
57         }
58         free(ctx);
59 }
60 /*
61 static void lpc2xml_context_clear_logs(lpc2xml_context *ctx) {
62         ctx->errorBuffer[0]='\0';
63         ctx->warningBuffer[0]='\0';
64 }*/
65
66 static void lpc2xml_log(lpc2xml_context *xmlCtx, int level, const char *fmt, ...) {
67         va_list args;   
68         va_start(args, fmt);    
69         if(xmlCtx->cbf != NULL) {
70                 xmlCtx->cbf((xmlCtx)->ctx, level, fmt, args);
71         }
72         va_end(args);
73 }
74
75 static int processEntry(const char *section, const char *entry, xmlNode *node, lpc2xml_context *ctx) {
76         const char *content = lp_config_get_string(ctx->lpc, section, entry, NULL);
77         if(content == NULL) {
78                 lpc2xml_log(ctx->ctx, LPC2XML_ERROR, "Issue when reading the lpc");
79                 return -1;
80         }
81         xmlNodeSetContent(node, (const xmlChar *) content);
82         return 0;
83 }
84
85 struct __processSectionCtx {
86         int ret;
87         const char *section;
88         xmlNode *node;
89         lpc2xml_context *ctx;
90 };
91
92 static void processSection_cb(const char *entry, struct __processSectionCtx *ctx) {
93         if(ctx->ret == 0) {
94                 xmlNode *node = xmlNewChild(ctx->node, NULL, (const xmlChar *)"entry", NULL);
95                 if(node == NULL) {
96                         lpc2xml_log(ctx->ctx, LPC2XML_ERROR, "Can't create \"entry\" element");
97                         ctx->ret = -1;
98                         return;
99                 }
100                 xmlAttr *name_attr = xmlSetProp(node, (const xmlChar *)"name", (const xmlChar *)entry);
101                 if(name_attr == NULL) {
102                         lpc2xml_log(ctx->ctx, LPC2XML_ERROR, "Can't create name attribute for \"entry\" element");
103                         ctx->ret = -1;
104                         return;
105                 }
106                 ctx->ret = processEntry(ctx->section, entry, node, ctx->ctx);
107         }
108 }
109
110 static int processSection(const char *section, xmlNode *node, lpc2xml_context *ctx) {
111         struct __processSectionCtx pc_ctx = {0, section, node, ctx};
112         lp_config_for_each_entry(ctx->lpc, section, (void (*)(const char *, void *))processSection_cb, (void*)&pc_ctx);
113         return pc_ctx.ret;
114 }
115
116
117
118 struct __processConfigCtx {
119         int ret;
120         xmlNode *node;
121         lpc2xml_context *ctx;
122 };
123
124 static void processConfig_cb(const char *section, struct __processConfigCtx *ctx) {
125         if(ctx->ret == 0) {
126                 xmlNode *node = xmlNewChild(ctx->node, NULL, (const xmlChar *)"section", NULL);
127                 if(node == NULL) {
128                         lpc2xml_log(ctx->ctx, LPC2XML_ERROR, "Can't create \"section\" element");
129                         ctx->ret = -1;
130                         return;
131                 }
132                 xmlAttr *name_attr = xmlSetProp(node, (const xmlChar *)"name", (const xmlChar *)section);
133                 if(name_attr == NULL) {
134                         lpc2xml_log(ctx->ctx, LPC2XML_ERROR, "Can't create name attribute for \"section\" element");
135                         ctx->ret = -1;
136                         return;
137                 }
138                 ctx->ret = processSection(section, node, ctx->ctx);
139         }
140 }
141
142 static int processConfig(xmlNode *node, lpc2xml_context *ctx) {
143         struct __processConfigCtx pc_ctx = {0, node, ctx};
144         lp_config_for_each_section(ctx->lpc, (void (*)(const char *, void *))processConfig_cb, (void*)&pc_ctx);
145         return pc_ctx.ret;
146 }
147
148 static int processDoc(xmlDoc *doc, lpc2xml_context *ctx) {
149         int ret = 0;
150         xmlNode *root_node = xmlNewNode(NULL, (const xmlChar *)"config");
151         if(root_node == NULL) {
152                 lpc2xml_log(ctx, LPC2XML_ERROR, "Can't create \"config\" element");
153                 return -1;
154         }
155         xmlNs *lpc_ns = xmlNewNs(root_node, (const xmlChar *)"http://www.linphone.org/xsds/lpconfig.xsd", NULL);
156         if(lpc_ns == NULL) {
157                 lpc2xml_log(ctx, LPC2XML_WARNING, "Can't create lpc namespace");
158         } else {
159                 xmlSetNs(root_node, lpc_ns);
160         }
161         xmlNs *xsi_ns = xmlNewNs(root_node, (const xmlChar *)"http://www.w3.org/2001/XMLSchema-instance", (const xmlChar *)"xsi");
162         if(lpc_ns == NULL) {
163                 lpc2xml_log(ctx, LPC2XML_WARNING, "Can't create xsi namespace");
164         }
165         xmlAttr *schemaLocation = xmlNewNsProp(root_node, xsi_ns, (const xmlChar *)"schemaLocation", (const xmlChar *)"http://www.linphone.org/xsds/lpconfig.xsd lpconfig.xsd");
166         if(schemaLocation == NULL) {
167                 lpc2xml_log(ctx, LPC2XML_WARNING, "Can't create schemaLocation");
168         }
169         ret = processConfig(root_node, ctx);
170         xmlDocSetRootElement(doc, root_node);
171         return ret;
172 }
173
174 static int internal_convert_lpc2xml(lpc2xml_context *ctx) {
175         int ret = 0;
176         lpc2xml_log(ctx, LPC2XML_DEBUG, "Generation started");
177         if(ctx->doc != NULL) {
178                 xmlFreeDoc(ctx->doc);
179                 ctx->doc = NULL;
180         }
181         xmlDoc *doc = xmlNewDoc((const xmlChar *)"1.0");
182         ret  = processDoc(doc, ctx);
183         if(ret == 0) {
184                 ctx->doc = doc;
185         } else {
186                 xmlFreeDoc(doc);
187         }
188         lpc2xml_log(ctx, LPC2XML_DEBUG, "Generation ended ret:%d", ret);
189         return ret;
190 }
191
192 int lpc2xml_set_lpc(lpc2xml_context* context, const LpConfig *lpc) {
193         context->lpc = lpc;
194         return 0;
195 }
196
197 int lpc2xml_convert_file(lpc2xml_context* context, const char *filename) {
198         int ret = 0;
199         xmlSaveCtxtPtr save_ctx = xmlSaveToFilename(filename, "UTF-8", XML_SAVE_FORMAT);
200         ret = internal_convert_lpc2xml(context);
201         if(ret == 0) {
202                 ret = xmlSaveDoc(save_ctx, context->doc);
203         }
204         xmlSaveClose(save_ctx);
205         return ret;
206 }
207
208 int lpc2xml_convert_fd(lpc2xml_context* context, int fd) {
209         int ret = 0;
210         xmlSaveCtxtPtr save_ctx = xmlSaveToFd(fd, "UTF-8", XML_SAVE_FORMAT);
211         ret = internal_convert_lpc2xml(context);
212         if(ret == 0) {
213                 ret = xmlSaveDoc(save_ctx, context->doc);
214         }
215         xmlSaveClose(save_ctx);
216         return ret;
217 }
218
219 int lpc2xml_convert_string(lpc2xml_context* context, unsigned char **content) {
220         int ret = 0;
221         xmlBufferPtr buffer = xmlBufferCreate();
222         xmlSaveCtxtPtr save_ctx = xmlSaveToBuffer(buffer, "UTF-8", XML_SAVE_FORMAT);
223         internal_convert_lpc2xml(context);
224         if(ret == 0) {
225                 ret = xmlSaveDoc(save_ctx, context->doc);
226         }
227         xmlSaveClose(save_ctx);
228         if(ret == 0) {
229                 *content = xmlBufferDetach(buffer);
230         }
231         xmlBufferFree(buffer);
232         return ret;
233 }