]> sjero.net Git - linphone/blob - tools/lpc2xml.c
tools: comment unused function in lpc2xml
[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 #include <libxml/xmlversion.h>
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 void lpc2xml_genericxml_error(void *ctx, const char *fmt, ...) {
76         lpc2xml_context *xmlCtx = (lpc2xml_context *)ctx;
77         int sl = strlen(xmlCtx->errorBuffer);
78         va_list args;   
79         va_start(args, fmt);    
80         vsnprintf(xmlCtx->errorBuffer + sl, LPC2XML_BZ-sl, fmt, args);
81         va_end(args);
82 }
83
84 /*
85 static void lpc2xml_genericxml_warning(void *ctx, const char *fmt, ...) {
86         lpc2xml_context *xmlCtx = (lpc2xml_context *)ctx;
87         int sl = strlen(xmlCtx->warningBuffer);
88         va_list args;   
89         va_start(args, fmt);    
90         vsnprintf(xmlCtx->warningBuffer + sl, LPC2XML_BZ-sl, fmt, args);
91         va_end(args);
92 }
93 */
94
95 static int processEntry(const char *section, const char *entry, xmlNode *node, lpc2xml_context *ctx) {
96         const char *content = lp_config_get_string(ctx->lpc, section, entry, NULL);
97         if(content == NULL) {
98                 lpc2xml_log(ctx->ctx, LPC2XML_ERROR, "Issue when reading the lpc");
99                 return -1;
100         }
101         xmlNodeSetContent(node, (const xmlChar *) content);
102         return 0;
103 }
104
105 struct __processSectionCtx {
106         int ret;
107         const char *section;
108         xmlNode *node;
109         lpc2xml_context *ctx;
110 };
111
112 static void processSection_cb(const char *entry, struct __processSectionCtx *ctx) {
113         if(ctx->ret == 0) {
114                 xmlNode *node = xmlNewChild(ctx->node, NULL, (const xmlChar *)"entry", NULL);
115                 if(node == NULL) {
116                         lpc2xml_log(ctx->ctx, LPC2XML_ERROR, "Can't create \"entry\" element");
117                         ctx->ret = -1;
118                         return;
119                 }
120                 xmlAttr *name_attr = xmlSetProp(node, (const xmlChar *)"name", (const xmlChar *)entry);
121                 if(name_attr == NULL) {
122                         lpc2xml_log(ctx->ctx, LPC2XML_ERROR, "Can't create name attribute for \"entry\" element");
123                         ctx->ret = -1;
124                         return;
125                 }
126                 ctx->ret = processEntry(ctx->section, entry, node, ctx->ctx);
127         }
128 }
129
130 static int processSection(const char *section, xmlNode *node, lpc2xml_context *ctx) {
131         struct __processSectionCtx pc_ctx = {0, section, node, ctx};
132         lp_config_for_each_entry(ctx->lpc, section, (void (*)(const char *, void *))processSection_cb, (void*)&pc_ctx);
133         return pc_ctx.ret;
134 }
135
136
137
138 struct __processConfigCtx {
139         int ret;
140         xmlNode *node;
141         lpc2xml_context *ctx;
142 };
143
144 static void processConfig_cb(const char *section, struct __processConfigCtx *ctx) {
145         if(ctx->ret == 0) {
146                 xmlNode *node = xmlNewChild(ctx->node, NULL, (const xmlChar *)"section", NULL);
147                 if(node == NULL) {
148                         lpc2xml_log(ctx->ctx, LPC2XML_ERROR, "Can't create \"section\" element");
149                         ctx->ret = -1;
150                         return;
151                 }
152                 xmlAttr *name_attr = xmlSetProp(node, (const xmlChar *)"name", (const xmlChar *)section);
153                 if(name_attr == NULL) {
154                         lpc2xml_log(ctx->ctx, LPC2XML_ERROR, "Can't create name attribute for \"section\" element");
155                         ctx->ret = -1;
156                         return;
157                 }
158                 ctx->ret = processSection(section, node, ctx->ctx);
159         }
160 }
161
162 static int processConfig(xmlNode *node, lpc2xml_context *ctx) {
163         struct __processConfigCtx pc_ctx = {0, node, ctx};
164         lp_config_for_each_section(ctx->lpc, (void (*)(const char *, void *))processConfig_cb, (void*)&pc_ctx);
165         return pc_ctx.ret;
166 }
167
168 static int processDoc(xmlDoc *doc, lpc2xml_context *ctx) {
169         int ret = 0;
170         xmlNode *root_node = xmlNewNode(NULL, (const xmlChar *)"config");
171         if(root_node == NULL) {
172                 lpc2xml_log(ctx, LPC2XML_ERROR, "Can't create \"config\" element");
173                 return -1;
174         }
175         xmlNs *lpc_ns = xmlNewNs(root_node, (const xmlChar *)"http://www.linphone.org/xsds/lpconfig.xsd", NULL);
176         if(lpc_ns == NULL) {
177                 lpc2xml_log(ctx, LPC2XML_WARNING, "Can't create lpc namespace");
178         } else {
179                 xmlSetNs(root_node, lpc_ns);
180         }
181         xmlNs *xsi_ns = xmlNewNs(root_node, (const xmlChar *)"http://www.w3.org/2001/XMLSchema-instance", (const xmlChar *)"xsi");
182         if(lpc_ns == NULL) {
183                 lpc2xml_log(ctx, LPC2XML_WARNING, "Can't create xsi namespace");
184         }
185         xmlAttr *schemaLocation = xmlNewNsProp(root_node, xsi_ns, (const xmlChar *)"schemaLocation", (const xmlChar *)"http://www.linphone.org/xsds/lpconfig.xsd lpconfig.xsd");
186         if(schemaLocation == NULL) {
187                 lpc2xml_log(ctx, LPC2XML_WARNING, "Can't create schemaLocation");
188         }
189         ret = processConfig(root_node, ctx);
190         xmlDocSetRootElement(doc, root_node);
191         return ret;
192 }
193
194 static int internal_convert_lpc2xml(lpc2xml_context *ctx) {
195         int ret = 0;
196         lpc2xml_log(ctx, LPC2XML_DEBUG, "Generation started");
197         if(ctx->doc != NULL) {
198                 xmlFreeDoc(ctx->doc);
199                 ctx->doc = NULL;
200         }
201         xmlDoc *doc = xmlNewDoc((const xmlChar *)"1.0");
202         ret  = processDoc(doc, ctx);
203         if(ret == 0) {
204                 ctx->doc = doc;
205         } else {
206                 xmlFreeDoc(doc);
207         }
208         lpc2xml_log(ctx, LPC2XML_DEBUG, "Generation ended ret:%d", ret);
209         return ret;
210 }
211
212 int lpc2xml_set_lpc(lpc2xml_context* context, const LpConfig *lpc) {
213         context->lpc = lpc;
214         return 0;
215 }
216
217 int lpc2xml_convert_file(lpc2xml_context* context, const char *filename) {
218         int ret = -1;
219         lpc2xml_context_clear_logs(context);
220         xmlSetGenericErrorFunc(context, lpc2xml_genericxml_error);
221         xmlSaveCtxtPtr save_ctx = xmlSaveToFilename(filename, "UTF-8", XML_SAVE_FORMAT);
222         if(save_ctx != NULL) {
223                 ret = internal_convert_lpc2xml(context);
224                 if(ret == 0) {
225                         ret = xmlSaveDoc(save_ctx, context->doc);
226                         if(ret != 0) {
227                                 lpc2xml_log(context, LPC2XML_ERROR, "Can't save document");
228                                 lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
229                         }
230                 }
231                 xmlSaveClose(save_ctx);
232         } else {
233                 lpc2xml_log(context, LPC2XML_ERROR, "Can't open file:%s", filename);
234                 lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
235         }
236         return ret;
237 }
238
239 int lpc2xml_convert_fd(lpc2xml_context* context, int fd) {
240         int ret = -1;
241         lpc2xml_context_clear_logs(context);
242         xmlSetGenericErrorFunc(context, lpc2xml_genericxml_error);
243         xmlSaveCtxtPtr save_ctx = xmlSaveToFd(fd, "UTF-8", XML_SAVE_FORMAT);
244         if(save_ctx != NULL) {
245                 ret = internal_convert_lpc2xml(context);
246                 if(ret == 0) {
247                         ret = xmlSaveDoc(save_ctx, context->doc);
248                         if(ret != 0) {
249                                 lpc2xml_log(context, LPC2XML_ERROR, "Can't save document");
250                                 lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
251                         }
252                 }
253                 xmlSaveClose(save_ctx);
254         } else {
255                 lpc2xml_log(context, LPC2XML_ERROR, "Can't open fd:%d", fd);
256                 lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
257         }
258         return ret;
259 }
260
261 int lpc2xml_convert_string(lpc2xml_context* context, char **content) {
262         int ret = -1;
263         xmlBufferPtr buffer = xmlBufferCreate();
264         lpc2xml_context_clear_logs(context);
265         xmlSetGenericErrorFunc(context, lpc2xml_genericxml_error);
266         xmlSaveCtxtPtr save_ctx = xmlSaveToBuffer(buffer, "UTF-8", XML_SAVE_FORMAT);
267         if(save_ctx != NULL) {
268                 ret = internal_convert_lpc2xml(context);
269                 if(ret == 0) {
270                         ret = xmlSaveDoc(save_ctx, context->doc);
271                         if(ret != 0) {
272                                 lpc2xml_log(context, LPC2XML_ERROR, "Can't save document");
273                                 lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
274                         }
275                 }
276                 xmlSaveClose(save_ctx);
277         } else {
278                 lpc2xml_log(context, LPC2XML_ERROR, "Can't initialize internal buffer");
279                 lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
280         }
281         if(ret == 0) {
282 #if LIBXML_VERSION >= 20800
283                 *content = (char *)xmlBufferDetach(buffer);
284 #else
285                 *content = strdup((const char *)xmlBufferContent(buffer));
286 #endif
287         }
288         xmlBufferFree(buffer);
289         return ret;
290 }