Permalink
Newer
Older
100644 76 lines (63 sloc) 1.81 KB
uli
Jul 4, 2009
1
//
2
// NSStream+ReadWriteLine.m
3
// RSSToPOP
4
//
5
// Created by Uli Kusterer on 11.11.05.
6
// Copyright 2005 Uli Kusterer.
7
//
8
// This software is provided 'as-is', without any express or implied
9
// warranty. In no event will the authors be held liable for any damages
10
// arising from the use of this software.
11
//
12
// Permission is granted to anyone to use this software for any purpose,
13
// including commercial applications, and to alter it and redistribute it
14
// freely, subject to the following restrictions:
15
//
16
// 1. The origin of this software must not be misrepresented; you must not
17
// claim that you wrote the original software. If you use this software
18
// in a product, an acknowledgment in the product documentation would be
19
// appreciated but is not required.
20
//
21
// 2. Altered source versions must be plainly marked as such, and must not be
22
// misrepresented as being the original software.
23
//
24
// 3. This notice may not be removed or altered from any source
25
// distribution.
uli
Jul 4, 2009
26
//
27
28
#import "NSStream+ReadWriteLine.h"
29
30
31
@implementation NSInputStream (UKReadWriteLine)
32
33
-(NSString*) readOneLine
34
{
35
uint8_t theChar = 0;
36
NSMutableString* str = [NSMutableString string];
37
BOOL hadCR = NO;
38
BOOL hadData = NO;
39
40
while( [self read: &theChar maxLength: 1] == 1 )
41
{
42
hadData = YES;
43
if( theChar == '\r' )
44
{
45
hadCR = YES;
46
continue;
47
}
48
else if( theChar == '\n' && hadCR )
49
break;
50
hadCR = NO;
51
52
[str appendFormat: @"%c", theChar];
53
54
NSLog( @"%@", str );
55
}
56
57
if( !hadData )
58
return nil;
59
else
60
return str;
61
}
62
63
@end
64
65
@implementation NSOutputStream (UKReadWriteLine)
66
67
-(void) writeOneLine: (NSString*)lineStr
68
{
69
NSData* dt = [lineStr dataUsingEncoding: NSASCIIStringEncoding];
70
[self write: [dt bytes] maxLength: [dt length]];
71
unsigned char* crlf = "\r\n";
72
[self write: crlf maxLength: 2];
73
}
74
75
@end