-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCDataScanner.m
More file actions
249 lines (207 loc) · 4.53 KB
/
Copy pathCDataScanner.m
File metadata and controls
249 lines (207 loc) · 4.53 KB
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
//
// CDataScanner.m
// TouchJSON
//
// Created by Jonathan Wight on 04/16/08.
// Copyright 2008 Toxic Software. All rights reserved.
//
#import "CDataScanner.h"
#import "CDataScanner_Extensions.h"
@interface CDataScanner ()
@property (readwrite, nonatomic, retain) NSCharacterSet *doubleCharacters;
@end
#pragma mark -
inline static unichar CharacterAtPointer(void *start, void *end)
{
#pragma unused(end)
const u_int8_t theByte = *(u_int8_t *)start;
if (theByte & 0x80)
{
// TODO -- UNICODE!!!! (well in theory nothing todo here)
}
const unichar theCharacter = theByte;
return(theCharacter);
}
@implementation CDataScanner
@dynamic data;
@dynamic scanLocation;
@dynamic isAtEnd;
@synthesize doubleCharacters;
+ (id)scannerWithData:(NSData *)inData
{
CDataScanner *theScanner = [[[self alloc] init] autorelease];
theScanner.data = inData;
return(theScanner);
}
- (id)init
{
if ((self = [super init]) != nil)
{
self.doubleCharacters = [NSCharacterSet characterSetWithCharactersInString:@"0123456789eE-."];
}
return(self);
}
- (void)dealloc
{
self.data = NULL;
self.doubleCharacters = NULL;
//
[super dealloc];
}
- (NSUInteger)scanLocation
{
return(current - start);
}
- (NSData *)data
{
return(data);
}
- (void)setData:(NSData *)inData
{
if (data != inData)
{
if (data)
{
[data release];
data = NULL;
}
if (inData)
{
data = [inData retain];
//
start = (u_int8_t *)data.bytes;
end = start + data.length;
current = start;
length = data.length;
}
}
}
- (void)setScanLocation:(NSUInteger)inScanLocation
{
current = start + inScanLocation;
}
- (BOOL)isAtEnd
{
return(self.scanLocation >= length);
}
- (unichar)currentCharacter
{
return(CharacterAtPointer(current, end));
}
#pragma mark -
- (unichar)scanCharacter
{
const unichar theCharacter = CharacterAtPointer(current++, end);
return(theCharacter);
}
- (BOOL)scanCharacter:(unichar)inCharacter
{
unichar theCharacter = CharacterAtPointer(current, end);
if (theCharacter == inCharacter)
{
++current;
return(YES);
}
else
return(NO);
}
- (BOOL)scanUTF8String:(const char *)inString intoString:(NSString **)outValue;
{
const size_t theLength = strlen(inString);
if ((size_t)(end - current) < theLength)
return(NO);
if (strncmp((char *)current, inString, theLength) == 0)
{
current += theLength;
if (outValue)
*outValue = [NSString stringWithUTF8String:inString];
return(YES);
}
return(NO);
}
- (BOOL)scanString:(NSString *)inString intoString:(NSString **)outValue
{
if ((size_t)(end - current) < inString.length)
return(NO);
if (strncmp((char *)current, [inString UTF8String], inString.length) == 0)
{
current += inString.length;
if (outValue)
*outValue = inString;
return(YES);
}
return(NO);
}
- (BOOL)scanCharactersFromSet:(NSCharacterSet *)inSet intoString:(NSString **)outValue
{
u_int8_t *P;
for (P = current; P < end && [inSet characterIsMember:*P] == YES; ++P)
;
if (P == current)
{
return(NO);
}
if (outValue)
{
*outValue = [[[NSString alloc] initWithBytes:current length:P - current encoding:NSUTF8StringEncoding] autorelease];
}
current = P;
return(YES);
}
- (BOOL)scanUpToString:(NSString *)inString intoString:(NSString **)outValue
{
const char *theToken = [inString UTF8String];
const char *theResult = strnstr((char *)current, theToken, end - current);
if (theResult == NULL)
{
return(NO);
}
if (outValue)
{
*outValue = [[[NSString alloc] initWithBytes:current length:theResult - (char *)current encoding:NSUTF8StringEncoding] autorelease];
}
current = (u_int8_t *)theResult;
return(YES);
}
- (BOOL)scanUpToCharactersFromSet:(NSCharacterSet *)inSet intoString:(NSString **)outValue
{
u_int8_t *P;
for (P = current; P < end && [inSet characterIsMember:*P] == NO; ++P)
;
if (P == current)
{
return(NO);
}
if (outValue)
{
*outValue = [[[NSString alloc] initWithBytes:current length:P - current encoding:NSUTF8StringEncoding] autorelease];
}
current = P;
return(YES);
}
- (BOOL)scanNumber:(NSNumber **)outValue
{
// Replace all of this with a strtod call
NSString *theString = NULL;
if ([self scanCharactersFromSet:doubleCharacters intoString:&theString])
{
if (outValue)
*outValue = [NSNumber numberWithDouble:[theString doubleValue]]; // TODO dont use doubleValue
return(YES);
}
return(NO);
}
- (void)skipWhitespace
{
u_int8_t *P;
for (P = current; P < end && (isspace(*P)); ++P)
;
current = P;
}
- (NSString *)remainingString
{
NSData *theRemainingData = [NSData dataWithBytes:current length:end - current];
NSString *theString = [[[NSString alloc] initWithData:theRemainingData encoding:NSUTF8StringEncoding] autorelease];
return(theString);
}
@end