ObjFW  Check-in [08fcb79a9b]

Overview
Comment:Add -[parseString:] and -[parseFile:] to OFXMLParser.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 08fcb79a9b3af9b7db8f7a4bef653e2433dfa10667bf7b58d5cff82752122522
User & Date: js on 2010-11-17 22:35:21
Other Links: manifest | tags
Context
2010-11-20
22:49
New, better syntax for objfw-compile. check-in: 307c430b25 user: js tags: trunk
2010-11-17
22:35
Add -[parseString:] and -[parseFile:] to OFXMLParser. check-in: 08fcb79a9b user: js tags: trunk
22:31
Close file on exception in +[OFString stringWithContentsOfFile:]. check-in: cc34255955 user: js tags: trunk
Changes

Modified src/OFXMLParser.h from [13cc038ae9] to [257797093f].

262
263
264
265
266
267
268














269
270
271
272
 * Parses a buffer with the specified size.
 *
 * \param buf The buffer to parse
 * \param size The size of the buffer
 */
- (void)parseBuffer: (const char*)buf
	   withSize: (size_t)size;














@end

@interface OFObject (OFXMLParserDelegate) <OFXMLParserDelegate>
@end







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




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
 * Parses a buffer with the specified size.
 *
 * \param buf The buffer to parse
 * \param size The size of the buffer
 */
- (void)parseBuffer: (const char*)buf
	   withSize: (size_t)size;

/**
 * Parses the specified string.
 *
 * \param str The string to parse
 */
- (void)parseString: (OFString*)str;

/**
 * Parses the specified file.
 *
 * \param path The path to the file to parse
*/
- (void)parseFile: (OFString*)path;
@end

@interface OFObject (OFXMLParserDelegate) <OFXMLParserDelegate>
@end

Modified src/OFXMLParser.m from [e26bece34a] to [bcc7e9f4bd].

15
16
17
18
19
20
21

22
23
24
25
26
27
28
#include <unistd.h>

#import "OFXMLParser.h"
#import "OFString.h"
#import "OFArray.h"
#import "OFDictionary.h"
#import "OFXMLAttribute.h"

#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "macros.h"

typedef void (*state_function)(id, SEL, const char*, size_t*, size_t*);
static SEL selectors[OF_XMLPARSER_NUM_STATES];
static state_function lookup_table[OF_XMLPARSER_NUM_STATES];







>







15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <unistd.h>

#import "OFXMLParser.h"
#import "OFString.h"
#import "OFArray.h"
#import "OFDictionary.h"
#import "OFXMLAttribute.h"
#import "OFFile.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "macros.h"

typedef void (*state_function)(id, SEL, const char*, size_t*, size_t*);
static SEL selectors[OF_XMLPARSER_NUM_STATES];
static state_function lookup_table[OF_XMLPARSER_NUM_STATES];
265
266
267
268
269
270
271































272
273
274
275
276
277
278
		lookup_table[state](self, selectors[state], buf, &i, &last);

	/* In OF_XMLPARSER_IN_TAG, there can be only spaces */
	if (size - last > 0 && state != OF_XMLPARSER_IN_TAG)
		[cache appendCStringWithoutUTF8Checking: buf + last
						 length: size - last];
}
































/*
 * The following methods handle the different states of the parser. They are
 * lookup up in +[initialize] and put in a lookup table to speed things up.
 * One dispatch for every character would be way too slow!
 */








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







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
		lookup_table[state](self, selectors[state], buf, &i, &last);

	/* In OF_XMLPARSER_IN_TAG, there can be only spaces */
	if (size - last > 0 && state != OF_XMLPARSER_IN_TAG)
		[cache appendCStringWithoutUTF8Checking: buf + last
						 length: size - last];
}

- (void)parseString: (OFString*)str
{
	[self parseBuffer: [str cString]
		 withSize: [str cStringLength]];
}

- (void)parseFile: (OFString*)path
{
	OFFile *file = [[OFFile alloc] initWithPath: path
					       mode: @"rb"];

	@try {
		char *buf = [self allocMemoryWithSize: of_pagesize];

		@try {
			while (![file isAtEndOfStream]) {
				size_t size;

				size = [file readNBytes: of_pagesize
					     intoBuffer: buf];
				[self parseBuffer: buf
					 withSize: size];
			}
		} @finally {
			[self freeMemory: buf];
		}
	} @finally {
		[file release];
	}
}

/*
 * The following methods handle the different states of the parser. They are
 * lookup up in +[initialize] and put in a lookup table to speed things up.
 * One dispatch for every character would be way too slow!
 */