summaryrefslogtreecommitdiffstats
path: root/libconfig/.svn/text-base/Parser.m.svn-base
blob: f95cb3d6981ebd2ba170cc3b543e72a067915813 (plain)
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
#include "Parser.h"
#include "ArrayData.h"
#include "DictData.h"
#include "NumData.h"
#include "StringData.h"
#include "String.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

@implementation Parser
-(int)
getLineLenFrom: (const char *)startAddr to: (const char *)endAddr
{
	int len;
	char quote = '\0';
	int frenchBraces = 0, squareBraces = 0;

	for (len = 0; startAddr+len < endAddr; len++) {
		if (!quote && !frenchBraces && !squareBraces &&
		    *(startAddr+len) == ';')
			break;

		switch (*(startAddr+len)) {
		case '{':
			frenchBraces++;
			break;
		case '}':
			if (!frenchBraces)
				fprintf(stderr,
					"WARNING: found `}' without `{'.\n");
			else
				frenchBraces--;
			break;
		case '[':
			squareBraces++;
			break;
		case ']':
			if (!squareBraces)
				fprintf(stderr,
					"WARNING: found ']' without '['.\n");
			else
				squareBraces--;
			break;
		case '"':
			if (!quote)
				quote = *(startAddr+len);
			else
				quote = '\0';
			break;
		}
	}

	return len;
}

-init
{
	return self;
}

-free
{
	return [super free];
}

-(DataObject *)
getDataFrom: (String *)dataStr
{
	const char *dataBuff;
	id dataElem;

	dataBuff = [dataStr getStr];

	/* Figure out what type of node to allocate. */
	switch (dataBuff[0]) {
	case '{':
		dataElem = [[DictData new] init];
		break;
	case '[':
		dataElem = [[ArrayData new] init];
		break;
	case '"':
		dataElem = [[StringData new] init];
		break;
	default:
		dataElem = [[NumData new] init];
		break;
	}

	return [dataElem setFromBuffer: dataBuff withLength: [dataStr getLen]];
}
@end