Skip to content
This repository
Newer
Older
100644 117 lines (88 sloc) 2.246 kb
05eedb5e »
2010-01-25 initial add
1 // Copyright 2010 Todd Ditchendorf
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #import <ParseKit/PKReader.h>
16
17 @implementation PKReader
18
19 - (id)init {
20 return [self initWithString:nil];
21 }
22
23
24 - (id)initWithString:(NSString *)s {
3875fb65 »
2013-03-06 merging from SVN
25 self = [super init];
26 if (self) {
05eedb5e »
2010-01-25 initial add
27 self.string = s;
28 }
29 return self;
30 }
31
32
d0034af3 »
2013-04-29 Merge from SVN. All new Static features included
33 - (id)initWithStream:(NSStream *)s {
34 self = [super init];
35 if (self) {
36 self.stream = s;
37 }
38 return self;
39 }
40
41
05eedb5e »
2010-01-25 initial add
42 - (void)dealloc {
43 self.string = nil;
d0034af3 »
2013-04-29 Merge from SVN. All new Static features included
44 self.stream = nil;
05eedb5e »
2010-01-25 initial add
45 [super dealloc];
46 }
47
48
49 - (NSString *)string {
50 return [[string retain] autorelease];
51 }
52
53
d0034af3 »
2013-04-29 Merge from SVN. All new Static features included
54 - (NSStream *)stream {
55 return [[stream retain] autorelease];
56 }
57
58
05eedb5e »
2010-01-25 initial add
59 - (void)setString:(NSString *)s {
d0034af3 »
2013-04-29 Merge from SVN. All new Static features included
60 NSAssert(!stream, @"");
61
05eedb5e »
2010-01-25 initial add
62 if (string != s) {
63 [string autorelease];
64 string = [s copy];
65 length = [string length];
66 }
67 // reset cursor
68 offset = 0;
69 }
70
71
d0034af3 »
2013-04-29 Merge from SVN. All new Static features included
72 - (void)setStream:(NSInputStream *)s {
73 NSAssert(!string, @"");
74
75 if (stream != s) {
76 [stream autorelease];
77 stream = [s retain];
78 length = NSNotFound;
79 }
80 // reset cursor
81 offset = 0;
82 }
83
84
05eedb5e »
2010-01-25 initial add
85 - (PKUniChar)read {
d0034af3 »
2013-04-29 Merge from SVN. All new Static features included
86 PKUniChar result = PKEOF;
87
88 if (string) {
89 if (length && offset < length) {
90 result = [string characterAtIndex:offset++];
91 }
92 } else {
93 NSUInteger maxLen = 1; // 2 for wide char?
94 uint8_t c;
95 if ([stream read:&c maxLength:maxLen]) {
96 result = (PKUniChar)c;
97 }
05eedb5e »
2010-01-25 initial add
98 }
d0034af3 »
2013-04-29 Merge from SVN. All new Static features included
99
100 return result;
05eedb5e »
2010-01-25 initial add
101 }
102
103
104 - (void)unread {
105 offset = (0 == offset) ? 0 : offset - 1;
106 }
107
108
109 - (void)unread:(NSUInteger)count {
3875fb65 »
2013-03-06 merging from SVN
110 for (NSUInteger i = 0; i < count; i++) {
05eedb5e »
2010-01-25 initial add
111 [self unread];
112 }
113 }
114
115 @synthesize offset;
116 @end
Something went wrong with that request. Please try again.