-
Notifications
You must be signed in to change notification settings - Fork 2
/
StreamLineReader.m
180 lines (143 loc) · 3.72 KB
/
StreamLineReader.m
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
/*
* Dictionary Reader - A Dict client for GNUstep
* Copyright (C) 2006 Guenther Noack
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See COPYING.
*/
#import "StreamLineReader.h"
#import "GNUstep.h"
@implementation StreamLineReader
- (id) initWithInputStream: (NSInputStream *) anInputStream
{
return [self initWithInputStream: anInputStream andDelimiter: @"\r\n"];
}
- (id) initWithInputStream: (NSInputStream *) anInputStream
andDelimiter: (NSString *) aDelimiter
{
self = [self init];
ASSIGN(inputStream, anInputStream);
NSData* delimData;
delimData = [aDelimiter dataUsingEncoding: NSUTF8StringEncoding];
delimSize = [delimData length];
delim = malloc(delimSize);
if (delim != NULL)
memcpy(delim, [delimData bytes], delimSize);
strBufSize = 0x1000;
strBufPos = 0;
strBuf = malloc(strBufSize);
if (strBuf == NULL || delim == NULL)
{
[self dealloc];
return nil;
}
return self;
}
- (void) dealloc
{
NSLog(@"%@ dealloc start", self);
free(delim);
free(strBuf);
// DictConnection takes care of closing and releasing inputStream but we
// retained it in our initializer
DESTROY(inputStream);
NSLog(@"%@ dealloc end", self);
[super dealloc];
}
- (NSString *) readLineAndRetry
{
NSString* result = nil;
while (result == nil)
{
result = [self readLine];
}
return result;
}
- (NSString *) readLine
{
BOOL canProceed = YES;
while([self canExtractNextLine] == NO && canProceed)
{
//NSLog(@"getting more, bufSize=%d bufPos=%d", strBufSize, strBufPos);
canProceed = [self getMoreCharacters];
}
if ([self canExtractNextLine] == YES)
return [self extractNextLine];
else
return nil;
}
- (BOOL) getMoreCharacters
{
//NSLog(@"getMoreChars opened (stream status = %d)",
// [inputStream streamStatus]);
// cancel if nothing in queue
if ([inputStream hasBytesAvailable] == NO)
{
NSLog(@"stream says nothing available (GNUstep impl b0rken?)");
//return NO;
}
if ([inputStream streamStatus] == NSStreamStatusClosed)
{
NSLog(@"Stream is closed (by server?)");
return NO;
}
// make buffer bigger if needed
if (strBufPos > (strBufSize>>1))
{
strBufSize = strBufSize<<1;
strBuf = realloc(strBuf, strBufSize);
}
NSAssert(strBufPos < strBufSize, @"strBufPos >= strBufSize");
//NSLog(@"getMoreChars: now reading from %@", inputStream);
// read bytes
int numBytesRead = [inputStream read: (strBuf+strBufPos)
maxLength: (strBufSize-strBufPos)];
//NSLog(@"getMoreChars closed with %d bytes read", numBytesRead);
if (numBytesRead > 0)
{
strBufPos += numBytesRead;
return YES;
}
else
{
return NO;
}
}
- (NSString *) extractNextLine
{
NSString* result;
int resultLength;
resultLength = [self delimPosInBuffer];
//NSLog(@"buf@0x%08x, resLen=%d, delSize=%d, strBufPos=%d",
// strBuf, resultLength, delimSize, strBufPos);
NSAssert(resultLength < strBufPos, @"resultLength >= strBufPos");
NSAssert(resultLength >= 0, @"resultLength < 0");
if (resultLength < 0)
return nil;
result = [[NSString alloc] initWithBytes: strBuf
length: resultLength
encoding: NSUTF8StringEncoding];
AUTORELEASE(result);
memmove(strBuf, strBuf+resultLength+delimSize,
strBufPos-(resultLength+delimSize));
strBufPos -= (resultLength+delimSize);
return result;
}
- (int) delimPosInBuffer
{
if (delimSize > strBufPos)
return -1;
int pos = 0;
while (pos < strBufPos - delimSize + 1)
{
if (memcmp(strBuf+pos, delim, delimSize) == 0)
return pos;
pos++;
}
return -1;
}
- (BOOL) canExtractNextLine
{
return ([self delimPosInBuffer] == -1) ? NO : YES;
}
@end