File: myx_xml_util_functions.c

package info (click to toggle)
mysql-gui-tools 5.0r12-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 105,540 kB
  • ctags: 50,897
  • sloc: sql: 348,439; pascal: 285,780; cpp: 94,578; ansic: 90,768; objc: 33,761; sh: 25,629; xml: 10,924; yacc: 10,755; java: 9,986; php: 2,806; python: 2,068; makefile: 1,945; perl: 3
file content (376 lines) | stat: -rw-r--r-- 12,123 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* Copyright (C) 2005 MySQL AB

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */


/*
*
* (XML Load/Save) Helper Functions
*
*
*/

#include <sys/types.h>
#include <sys/stat.h>
#ifdef __GNUC__
  #include <unistd.h>
#endif

#include "myx_xml_util_functions.h"
#include "myx_shared_util_functions.h"
#include <string.h>
#include <stdlib.h>
#include <glib.h>

///////////////////////////////////////////////////////////////////////////////
/** @brief print the number to g_malloc-ed string
    @param number number to print
    @return g_malloc-ed string with number
*//////////////////////////////////////////////////////////////////////////////
char* int_to_str(int number)
{
  char buf[12];
  size_t len= sprintf(buf, "%d", number)+1;
  char *result= (char*) g_malloc((gulong)len);
  if (result)
    memcpy(result,buf,len);
  return result;
}

///////////////////////////////////////////////////////////////////////////////
/** @brief print the myx_bool value to g_malloc-ed string
    @param b value to print
    @return g_malloc-ed string ("0" or "1")
*//////////////////////////////////////////////////////////////////////////////
char* bool_to_str(myx_bool b)
{
  char *result= (char*) g_malloc(2);
  result[0]= b ? '1' : '0';
  result[1]= '\0';
  return result;
}

///////////////////////////////////////////////////////////////////////////////
/** @brief atoi string and xmlFree it
    @param string string for atoi
    @return parsed number, 0 if the string was empty
*//////////////////////////////////////////////////////////////////////////////
int atoi_and_free(xmlChar *string)
{
  int a= 0;
  if (string != NULL)
  {
    a= atoi((char*)string);
    xmlFree(string);
  }
  return a;
}

///////////////////////////////////////////////////////////////////////////////
/** @brief atof string and xmlFree it
    @param string string for atof
    @return parsed number, 0.0 if the string was empty
*//////////////////////////////////////////////////////////////////////////////
double atof_and_free(xmlChar *string)
{
  double a= 0.0;
  if (string != NULL) 
  {
    a= atof((char*)string);
    xmlFree(string);
  }
  return a;
}

///////////////////////////////////////////////////////////////////////////////
/** @brief skip all symbols in string greater than 127
    @param latin_str string to process
    @return result string alloced by g_malloc

    we just skip all characters above 127 and return safe ascii-chars only
*//////////////////////////////////////////////////////////////////////////////
xmlChar* get_failsafe_utf8(const unsigned char* latin_str)
{
  int i,j;
  xmlChar *result;
  long len;

  len= (long)strlen((char*)latin_str);
  result= (xmlChar*) g_malloc(len + 1);

  j= 0;
  for (i=0; i < len; i++)
  {
    if (latin_str[i] <= 127)
      result[j++]= latin_str[i];
  }
  result[j]= '\0';

  if (j < i)
    result= (xmlChar*) realloc(result,j+1);

  return result;
}

///////////////////////////////////////////////////////////////////////////////
/** @brief get file size as 64 bit value
    @param filename file name
    @return result lon long file name, zero if an error occured
*//////////////////////////////////////////////////////////////////////////////
long long file_size(const char *filename)
{
#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
  LARGE_INTEGER fsize;
  HANDLE f= CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
  ZeroMemory(&fsize, sizeof(fsize));
  GetFileSizeEx(f, &fsize);
  CloseHandle(f);
  return fsize.QuadPart;
#else
  struct stat buf;
  if(stat(filename, &buf))
  {
    return 0;
  }
  return buf.st_size;
#endif
}

///////////////////////////////////////////////////////////////////////////////
/** @brief checks if filename really exists
    @param filename path to file
    @return 0 if the file doesn't exist else 1
*//////////////////////////////////////////////////////////////////////////////
int file_exists(const char *filename)
{
#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
  return _access(filename, 0) != -1 ? 1 : 0;
#else
  return access(filename, R_OK) == 0 ? 1 : 0;
#endif
}

///////////////////////////////////////////////////////////////////////////////
/** @brief create new xml node in xml-dom-document with int content
    @param parent parent for node
    @param ns node sequence?? \b vva_todo
    @param name name of new node
    @param content_int value of new node
    @return created node
*//////////////////////////////////////////////////////////////////////////////
xmlNodePtr NewTextChild_int_content(xmlNodePtr parent, xmlNsPtr ns,
                                    const xmlChar *name, int content_int)
{
  xmlNodePtr node;
  char content[12];
  sprintf(content,"%d",content_int);
  node= xmlNewTextChild(parent, ns, name, (xmlChar*)content);
  return node;
}

///////////////////////////////////////////////////////////////////////////////
/** @brief create new xml node in xml-dom-document with double content
    @param parent parent for node
    @param ns node sequence?? \b vva_todo
    @param name name of new node
    @param content_double value of new node
    @return created node
*//////////////////////////////////////////////////////////////////////////////
xmlNodePtr NewTextChild_double_content(xmlNodePtr parent, xmlNsPtr ns,
                                       const xmlChar *name,
                                       double content_double)
{
  xmlNodePtr node;
  char* content =  g_strdup_printf("%f", content_double);
  node = xmlNewTextChild(parent, ns, name, (xmlChar*)content);
  g_free(content);
  return node;
}

///////////////////////////////////////////////////////////////////////////////
/** @brief returns the number of children of the specified type 
            the parent-node has
    @param parent node to calculate children of
    @param element_type_name name of childrens to calculate
    @return the calculated number of children
*//////////////////////////////////////////////////////////////////////////////
int get_child_count(const xmlNodePtr parent, const xmlChar *element_type_name)
{
  xmlNodePtr cur;
  int node_count = 0;

  for (cur= parent->children; cur != NULL; cur= cur->next)
  {
    if ( !xmlStrcmp(cur->name, element_type_name) &&
        cur->type == XML_ELEMENT_NODE)
      node_count++;
  }

  return node_count;
}

///////////////////////////////////////////////////////////////////////////////
/** @brief Wrapper around xmlParseFile that expects a filename
                                in UTF-8 encoding
    @param filename name of file to parse
    @return NULL if execution of g_filename_from_utf8 wasn't success else
            parsed xml document
*//////////////////////////////////////////////////////////////////////////////
xmlDocPtr myx_xmlParseFile(const char *filename)
{
  char * local_filename;
  xmlDocPtr doc;

  if ((local_filename= g_filename_from_utf8(filename,-1,NULL,NULL,NULL)) == NULL)
    return NULL;

  doc= xmlParseFile(local_filename);
  g_free(local_filename);

  return doc;
}

///////////////////////////////////////////////////////////////////////////////
/** @brief Wrapper around xmlSaveFormatFile that expects a filename 
             in UTF-8 encoding
    @param filename name of file to save to
    @param doc xml document to save
    @return -1 if execution of g_filename_from_utf8 wasn't success else
                result of xmlSaveFormatFile (number of bytes written, or -1)
*//////////////////////////////////////////////////////////////////////////////
int myx_xmlSaveFile(const char *filename, xmlDocPtr doc)
{
  char * local_filename;
  int result;
  FILE* file;

  if ((local_filename= g_filename_from_utf8(filename,-1,NULL,NULL,NULL)) == NULL)
    return -1;

  // Check if the file already exists and if so store under a temporary name first.
  file = fopen(local_filename, "r");
  if (file != NULL)
  {
    char *tempName= g_strdup_printf("%s.tmp", local_filename);

    fclose(file);
    result = xmlSaveFormatFile(tempName, doc, 1);
    if (result > 0)
    {
      // If saving the content was successful then delete the old file and use the new one.
      remove(local_filename);
      rename(tempName, local_filename);
    };
    g_free(tempName);
  }
  else
    result= xmlSaveFormatFile(local_filename,doc,1);
  g_free(local_filename);

  return result;
}



///////////////////////////////////////////////////////////////////////////////
/** @brief if there's node's name asked, get its value
    @param doc xml-document
    @param cur xml-node
    @param name asked name
    @param field value to return

    if  field was not null then xmlFree it
*//////////////////////////////////////////////////////////////////////////////
void try_to_get_string_field(xmlDocPtr doc, xmlNodePtr cur,
                             const char * name, char ** field)
{
  if ( !xmlStrcmp(cur->name, (xmlChar*)name) )
  {
    if (*field)
      xmlFree(*field);
    *field= (char*)xmlNodeListGetString(doc, cur->children, 1);
  }
}

///////////////////////////////////////////////////////////////////////////////
/** @brief if there's node's name asked, get its value
    @param doc xml-document
    @param cur xml-node
    @param name asked name
    @param field value to return

    calls atoi_and_free()
*//////////////////////////////////////////////////////////////////////////////
void try_to_get_int_field(xmlDocPtr doc, xmlNodePtr cur,
                          const char * name, int * field)
{
  if ( !xmlStrcmp(cur->name, (xmlChar*)name) )
    *field= atoi_and_free(xmlNodeListGetString(doc, cur->children, 1));
}

///////////////////////////////////////////////////////////////////////////////
/** @brief if there's node's name asked, get its value
    @param doc xml-document
    @param cur xml-node
    @param name asked name
    @param field value to return

    calls atoi_and_free()
*//////////////////////////////////////////////////////////////////////////////
void try_to_get_char_field(xmlDocPtr doc, xmlNodePtr cur,
                           const char * name, char * field)
{
  if ( !xmlStrcmp(cur->name, (xmlChar*)name) )
    *field= (char)atoi_and_free(xmlNodeListGetString(doc, cur->children, 1));
}

///////////////////////////////////////////////////////////////////////////////
/** @brief if there's node's name asked, get its value
    @param doc xml-document
    @param cur xml-node
    @param name asked name
    @param field value to return

    calls atof_and_free()
*//////////////////////////////////////////////////////////////////////////////
void try_to_get_float_field(xmlDocPtr doc, xmlNodePtr cur,
                           const char * name, double * field)
{
  if ( !xmlStrcmp(cur->name, (xmlChar*)name) )
    *field= (char)atof_and_free(xmlNodeListGetString(doc, cur->children, 1));
}

///////////////////////////////////////////////////////////////////////////////
/** @brief try to get a child with given name
    @param doc xml-document
    @param cur xml-node
    @param name asked name
    @return child xmlNodePtr or NULL

*//////////////////////////////////////////////////////////////////////////////
xmlNodePtr try_to_get_child(xmlDocPtr doc, 
                            xmlNodePtr cur,
                            const char * name)
{
  xmlNodePtr child;
  for (child= cur->children; child; child= child->next)
  {
    if (!xmlStrcmp(child->name, (xmlChar*)name) )
    {
      return child;
    }
  }
  return NULL;
}