Add ezxml (#4362), thanks Raphael
[openwrt/svn-archive/archive.git] / libs / ezxml / patches / 300-ezxml_int.patch
1 diff -pruN ezxml-0.8.6.orig/ezxml.c ezxml-0.8.6/ezxml.c
2 --- ezxml-0.8.6.orig/ezxml.c 2008-12-16 17:02:17.262312778 +0100
3 +++ ezxml-0.8.6/ezxml.c 2008-12-16 17:03:24.231073153 +0100
4 @@ -25,6 +25,7 @@
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <stdarg.h>
8 +#include <errno.h>
9 #include <string.h>
10 #include <ctype.h>
11 #include <unistd.h>
12 @@ -72,6 +73,19 @@ ezxml_t ezxml_idx(ezxml_t xml, int idx)
13 return xml;
14 }
15
16 +int ezxml_int(ezxml_t xml, int default_value)
17 +{
18 + int ret;
19 +
20 + if(!xml)
21 + return default_value;
22 +
23 + errno = 0;
24 + ret = strtol(xml->txt, NULL, 10);
25 +
26 + return (errno == 0 ? ret : default_value);
27 +}
28 +
29 // returns the value of the requested tag attribute or NULL if not found
30 const char *ezxml_attr(ezxml_t xml, const char *attr)
31 {
32 @@ -89,6 +103,23 @@ const char *ezxml_attr(ezxml_t xml, cons
33 return (root->attr[i][j]) ? root->attr[i][j + 1] : NULL; // found default
34 }
35
36 +int ezxml_attr_int(ezxml_t xml, const char *attr, int default_value)
37 +{
38 + int ret;
39 + const char *val = NULL;
40 +
41 + if(!xml)
42 + return default_value;
43 +
44 + if((val = ezxml_attr(xml, attr)) == NULL)
45 + return default_value;
46 +
47 + errno = 0;
48 + ret = strtol(val, NULL, 10);
49 +
50 + return (errno == 0 ? ret : default_value);
51 +}
52 +
53 // same as ezxml_get but takes an already initialized va_list
54 ezxml_t ezxml_vget(ezxml_t xml, va_list ap)
55 {
56 @@ -926,6 +957,16 @@ ezxml_t ezxml_set_txt(ezxml_t xml, const
57 return xml;
58 }
59
60 +ezxml_t ezxml_set_int(ezxml_t xml, int data)
61 +{
62 + char *buf = NULL;
63 + if (! xml) return NULL;
64 +
65 + asprintf(&buf, "%d", data);
66 +
67 + return ezxml_set_flag(ezxml_set_txt(xml, buf), EZXML_TXTM);
68 +}
69 +
70 // Sets the given tag attribute or adds a new attribute if not found. A value
71 // of NULL will remove the specified attribute. Returns the tag given.
72 ezxml_t ezxml_set_attr(ezxml_t xml, const char *name, const char *value)
73 @@ -968,6 +1009,15 @@ ezxml_t ezxml_set_attr(ezxml_t xml, cons
74 return xml;
75 }
76
77 +ezxml_t ezxml_set_attr_int(ezxml_t xml, const char *name, int data)
78 +{
79 + char *buf = NULL;
80 + if (! xml) return NULL;
81 +
82 + asprintf(&buf, "%d", data);
83 + return ezxml_set_attr(ezxml_set_flag(xml, EZXML_DUP), name, buf);
84 +}
85 +
86 // sets a flag for the given tag and returns the tag
87 ezxml_t ezxml_set_flag(ezxml_t xml, short flag)
88 {
89 diff -pruN ezxml-0.8.6.orig/ezxml.h ezxml-0.8.6/ezxml.h
90 --- ezxml-0.8.6.orig/ezxml.h 2008-12-16 17:02:17.262312778 +0100
91 +++ ezxml-0.8.6/ezxml.h 2008-12-16 17:03:44.189450448 +0100
92 @@ -95,9 +95,13 @@ ezxml_t ezxml_idx(ezxml_t xml, int idx);
93 // returns the given tag's character content or empty string if none
94 #define ezxml_txt(xml) ((xml) ? xml->txt : "")
95
96 +int ezxml_int(ezxml_t xml, int default_value);
97 +
98 // returns the value of the requested tag attribute, or NULL if not found
99 const char *ezxml_attr(ezxml_t xml, const char *attr);
100
101 +int ezxml_attr_int(ezxml_t xml, const char *attr, int default_value);
102 +
103 // Traverses the ezxml sturcture to retrieve a specific subtag. Takes a
104 // variable length list of tag names and indexes. The argument list must be
105 // terminated by either an index of -1 or an empty string tag name. Example:
106 @@ -137,6 +141,9 @@ ezxml_t ezxml_add_child(ezxml_t xml, con
107 // sets the character content for the given tag and returns the tag
108 ezxml_t ezxml_set_txt(ezxml_t xml, const char *txt);
109
110 +// set int value
111 +ezxml_t ezxml_set_int(ezxml_t xml, int data);
112 +
113 // wrapper for ezxml_set_txt() that strdup()s txt
114 #define ezxml_set_txt_d(xml, txt) \
115 ezxml_set_flag(ezxml_set_txt(xml, strdup(txt)), EZXML_TXTM)
116 @@ -144,6 +151,9 @@ ezxml_t ezxml_set_txt(ezxml_t xml, const
117 // Sets the given tag attribute or adds a new attribute if not found. A value
118 // of NULL will remove the specified attribute. Returns the tag given.
119 ezxml_t ezxml_set_attr(ezxml_t xml, const char *name, const char *value);
120 +
121 +// set int value for an attr
122 +ezxml_t ezxml_set_attr_int(ezxml_t xml, const char *name, int data);
123
124 // Wrapper for ezxml_set_attr() that strdup()s name/value. Value cannot be NULL
125 #define ezxml_set_attr_d(xml, name, value) \