ObjFW  Check-in [7fc221e592]

Overview
Comment:A few convenience methods for OFXMLFactory.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 7fc221e592fece9e374fa43cf96908d796fef61cd1d54f38598ff9cc028a97e7
User & Date: js on 2009-04-25 13:09:36
Other Links: manifest | tags
Context
2009-04-25
14:28
Separate C and ObjC checks. check-in: a632386b7f user: js tags: trunk
13:09
A few convenience methods for OFXMLFactory. check-in: 7fc221e592 user: js tags: trunk
12:52
Better exception handling in OFXMLFactory. check-in: e2a94b8f9d user: js tags: trunk
Changes

Modified src/OFXMLFactory.h from [6869b6a153] to [1d8b135f8c].

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
 * \return The escaped C string.
 *	   You need to free it manually!
 */
+ (char*)escapeCString: (const char*)s;

/**
 * Creates an XML stanza.





























 *
 * \param name The name of the tag as a C string
 * \param close A boolean whether the tag should be closed
 * \param data Data that should be inside the tag as a C string.
 *	  It will NOT be escaped, so you can also include other stanzas.
 * \param ... Field / value pairs for the tag in the form "field", "value" as
 *	  C strings.
 *	  Last element must be NULL.
 *	  Example: "field1", "value1", "field2", "value2", NULL
 * \return The created XML stanza as a C string.
 *	   You need to free it manually!
 */
+ (char*)createStanza: (const char*)name
	 withCloseTag: (BOOL)close
	      andData: (const char*)data, ...;




















/**
 * Concats an array of C strings into one C string and frees the array of C
 * strings.
 *
 * \param strs An array of C strings
 * \return The concatenated C strings.
 *	   You need to free it manually!
 */
+ (char*)concatAndFreeCStrings: (char**)strs;
@end







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
















>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>










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
 * \return The escaped C string.
 *	   You need to free it manually!
 */
+ (char*)escapeCString: (const char*)s;

/**
 * Creates an XML stanza.
 *
 * \param name The name of the tag as a C string
 * \param ... Field / value pairs for the tag in the form "field", "value" as
 *	  C strings.
 *	  Last element must be NULL.
 *	  Example: "field1", "value1", "field2", "value2", NULL
 * \return The created XML stanza as a C string.
 *	   You need to free it manually!
 */
+ (char*)createStanza: (const char*)name, ...;

/**
 * Creates an XML stanza.
 *
 * \param name The name of the tag as a C string
 * \param data Data that should be inside the tag as a C string.
 *	  It will NOT be escaped, so you can also include other stanzas.
 * \param ... Field / value pairs for the tag in the form "field", "value" as
 *	  C strings.
 *	  Last element must be NULL.
 *	  Example: "field1", "value1", "field2", "value2", NULL
 * \return The created XML stanza as a C string.
 *	   You need to free it manually!
 */
+ (char*)createStanza: (const char*)name
	     withData: (const char*)data, ...;

/**
 * Creates an XML stanza.
 *
 * \param name The name of the tag as a C string
 * \param close A boolean whether the tag should be closed
 * \param data Data that should be inside the tag as a C string.
 *	  It will NOT be escaped, so you can also include other stanzas.
 * \param ... Field / value pairs for the tag in the form "field", "value" as
 *	  C strings.
 *	  Last element must be NULL.
 *	  Example: "field1", "value1", "field2", "value2", NULL
 * \return The created XML stanza as a C string.
 *	   You need to free it manually!
 */
+ (char*)createStanza: (const char*)name
	 withCloseTag: (BOOL)close
	      andData: (const char*)data, ...;

/**
 * Creates an XML stanza.
 *
 * \param name The name of the tag as a C string
 * \param close A boolean whether the tag should be closed
 * \param attrs Field / value pairs for the tag in the form "field", "value" as
 *	  C strings inside a va_list.
 *	  Last element must be NULL.
 *	  Example: "field1", "value1", "field2", "value2", NULL in a va_list
 * \param data Data that should be inside the tag as a C string.
 *	  It will NOT be escaped, so you can also include other stanzas.
 * \return The created XML stanza as a C string.
 *	   You need to free it manually!
 */
+ (char*)createStanza: (const char*)name
	 withCloseTag: (BOOL)close
	andAttributes: (va_list)attrs
	      andData: (const char*)data;

/**
 * Concats an array of C strings into one C string and frees the array of C
 * strings.
 *
 * \param strs An array of C strings
 * \return The concatenated C strings.
 *	   You need to free it manually!
 */
+ (char*)concatAndFreeCStrings: (char**)strs;
@end

Modified src/OFXMLFactory.m from [49b07c4eb9] to [47b9222801].

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
		free(ret);
		@throw e;
	}

	ret[i] = 0;
	return ret;
}
















+ (char*)createStanza: (const char*)name
















	 withCloseTag: (BOOL)close
	      andData: (const char*)data, ...
{


















	char *arg, *val, *xml, *esc_val = NULL;
	size_t i, len;
	va_list args;

	/* Start of tag */
	len = strlen(name);
	if (SIZE_MAX - len < 3)
		@throw [OFOutOfRangeException newWithClass: self];
	len += 3;

	if ((xml = malloc(len)) == NULL)
		@throw [OFNoMemException newWithClass: self
					      andSize: len];

	i = 0;
	xml[i++] = '<';
	memcpy(xml + i, name, strlen(name));
	i += strlen(name);

	/*
	 * Arguments
	 *
	 * va_start / va_end need to be INSIDE the @try block due to a bug in
	 * gcc 4.0.1. (Only in Apple gcc?)
	 */
	@try {
		va_start(args, data);

		while ((arg = va_arg(args, char*)) != NULL &&
		    (val = va_arg(args, char*)) != NULL) {
			esc_val = NULL;	/* Needed for our @catch */
			esc_val = [self escapeCString: val];

			resize(&xml, &len, 1 + strlen(arg) + 2 +
			    strlen(esc_val) + 1, self);

			xml[i++] = ' ';
			memcpy(xml + i, arg, strlen(arg));
			i += strlen(arg);
			xml[i++] = '=';
			xml[i++] = '\'';
			memcpy(xml + i, esc_val, strlen(esc_val));
			i += strlen(esc_val);
			xml[i++] = '\'';

			free(esc_val);
		}

		va_end(args);

		/* End of tag */
		if (close) {
			if (data == NULL) {
				resize(&xml, &len, 2 - 1, self);

				xml[i++] = '/';
				xml[i++] = '>';








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


<
















<
<
<
<
<
<

<
<
|
|


















<
<







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
		free(ret);
		@throw e;
	}

	ret[i] = 0;
	return ret;
}

+ (char*)createStanza: (const char*)name, ...
{
	char *ret;
	va_list attrs;

	va_start(attrs, name);
	ret = [self createStanza: name
		    withCloseTag: YES
		   andAttributes: attrs
			 andData: NULL];
	va_end(attrs);

	return ret;
}

+ (char*)createStanza: (const char*)name
	     withData: (const char*)data, ...
{
	char *ret;
	va_list attrs;

	va_start(attrs, data);
	ret = [self createStanza: name
		    withCloseTag: YES
		   andAttributes: attrs
			 andData: data];
	va_end(attrs);

	return ret;
}

+ (char*)createStanza: (const char*)name
	 withCloseTag: (BOOL)close
	      andData: (const char*)data, ...
{
	char *ret;
	va_list attrs;

	va_start(attrs, data);
	ret = [self createStanza: name
		    withCloseTag: close
		   andAttributes: attrs
			 andData: data];
	va_end(attrs);

	return ret;
}

+ (char*)createStanza: (const char*)name
	 withCloseTag: (BOOL)close
	andAttributes: (va_list)attrs
	      andData: (const char*)data
{
	char *arg, *val, *xml, *esc_val = NULL;
	size_t i, len;


	/* Start of tag */
	len = strlen(name);
	if (SIZE_MAX - len < 3)
		@throw [OFOutOfRangeException newWithClass: self];
	len += 3;

	if ((xml = malloc(len)) == NULL)
		@throw [OFNoMemException newWithClass: self
					      andSize: len];

	i = 0;
	xml[i++] = '<';
	memcpy(xml + i, name, strlen(name));
	i += strlen(name);







	@try {


		while ((arg = va_arg(attrs, char*)) != NULL &&
		    (val = va_arg(attrs, char*)) != NULL) {
			esc_val = NULL;	/* Needed for our @catch */
			esc_val = [self escapeCString: val];

			resize(&xml, &len, 1 + strlen(arg) + 2 +
			    strlen(esc_val) + 1, self);

			xml[i++] = ' ';
			memcpy(xml + i, arg, strlen(arg));
			i += strlen(arg);
			xml[i++] = '=';
			xml[i++] = '\'';
			memcpy(xml + i, esc_val, strlen(esc_val));
			i += strlen(esc_val);
			xml[i++] = '\'';

			free(esc_val);
		}



		/* End of tag */
		if (close) {
			if (data == NULL) {
				resize(&xml, &len, 2 - 1, self);

				xml[i++] = '/';
				xml[i++] = '>';

Modified tests/OFXMLFactory/OFXMLFactory.m from [fed8fd03ae] to [ef76f5e27e].

36
37
38
39
40
41
42

43
44
45
46
47
48
49
		printf("\r\033[1;%dmTests successful: %2" ZD "/%d\033[0m",
		    (i == NUM_TESTS ? 32 : 33), i, NUM_TESTS);
		fflush(stdout);
	} else {
		printf("\r\033[K\033[1;31mTest %" ZD "/%d failed!\033[0m\n",
		    i, NUM_TESTS);
		printf("%s is NOT expected result!\n", result);

		exit(1);
	}

	free(result);
}

inline void







>







36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
		printf("\r\033[1;%dmTests successful: %2" ZD "/%d\033[0m",
		    (i == NUM_TESTS ? 32 : 33), i, NUM_TESTS);
		fflush(stdout);
	} else {
		printf("\r\033[K\033[1;31mTest %" ZD "/%d failed!\033[0m\n",
		    i, NUM_TESTS);
		printf("%s is NOT expected result!\n", result);
		printf("Should have been: %s\n", result);
		exit(1);
	}

	free(result);
}

inline void
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
	check_result([OFXMLFactory createStanza: "foo"
				   withCloseTag: NO
					andData: NULL,
						 "bar", "baz",
						 "blub", "asd",
						 NULL],
	    "<foo bar='baz' blub='asd'>");
	check_result([OFXMLFactory createStanza: "foo"
				   withCloseTag: YES
					andData: NULL,
						 NULL],
	    "<foo/>");
	check_result([OFXMLFactory createStanza: "foo"
				   withCloseTag: YES
					andData: "bar",
						 NULL],
	    "<foo>bar</foo>");
	check_result([OFXMLFactory createStanza: "foo"
				   withCloseTag: YES
					andData: NULL,
						 "bar", "b&az",
						 NULL],
	    "<foo bar='b&amp;az'/>");
	check_result([OFXMLFactory createStanza: "foo"
				   withCloseTag: YES
					andData: "bar",
						 "bar", "b'az",
						 NULL],
	    "<foo bar='b&apos;az'>bar</foo>");
	check_result([OFXMLFactory createStanza: "foo"
				   withCloseTag: YES
					andData: NULL,
						 "bar", "b&az",
						 "x", "asd\"",
						 NULL],
	    "<foo bar='b&amp;az' x='asd&quot;'/>");
	check_result([OFXMLFactory createStanza: "foo"
				   withCloseTag: YES
					andData: "bar",
						 "bar", "b'az",
						 "x", "y",
						 "a", "b",
						 NULL],
	    "<foo bar='b&apos;az' x='y' a='b'>bar</foo>");
}








|
<
<
<
<

<
|


|
<
<




<
|



|
<
<





<
|







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
	check_result([OFXMLFactory createStanza: "foo"
				   withCloseTag: NO
					andData: NULL,
						 "bar", "baz",
						 "blub", "asd",
						 NULL],
	    "<foo bar='baz' blub='asd'>");
	check_result([OFXMLFactory createStanza: "foo", NULL], "<foo/>");




	check_result([OFXMLFactory createStanza: "foo"

				       withData: "bar",
						 NULL],
	    "<foo>bar</foo>");
	check_result([OFXMLFactory createStanza: "foo",


						 "bar", "b&az",
						 NULL],
	    "<foo bar='b&amp;az'/>");
	check_result([OFXMLFactory createStanza: "foo"

				       withData: "bar",
						 "bar", "b'az",
						 NULL],
	    "<foo bar='b&apos;az'>bar</foo>");
	check_result([OFXMLFactory createStanza: "foo",


						 "bar", "b&az",
						 "x", "asd\"",
						 NULL],
	    "<foo bar='b&amp;az' x='asd&quot;'/>");
	check_result([OFXMLFactory createStanza: "foo"

				       withData: "bar",
						 "bar", "b'az",
						 "x", "y",
						 "a", "b",
						 NULL],
	    "<foo bar='b&apos;az' x='y' a='b'>bar</foo>");
}