2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "NGStringTextStream.h"
24 #include "NGStreamExceptions.h"
26 @implementation NGStringTextStream
28 + (id)textStreamWithString:(NSString *)_string {
29 return [[[self alloc] initWithString:_string] autorelease];
31 - (id)initWithString:(NSString *)_string {
32 if ((self = [super init])) {
33 self->string = [_string retain];
35 self->isMutable = [_string isKindOfClass:[NSMutableString class]];
41 [self->string release];
47 - (NSString *)string {
55 [self->string release]; self->string = nil;
61 - (unichar)readCharacter {
63 // NGStreamNotOpenException when the stream is not open
64 unsigned currentLength = [string length];
68 [NGStreamNotOpenException raiseWithReason:
69 @"tried to read from a string text stream which was closed"];
72 if (currentLength == index)
73 [[[NGEndOfStreamException alloc] init] raise];
75 result = [string characterAtIndex:index];
80 // NGExtendedTextInputStream
82 - (NSString *)readLineAsString {
84 // NGStreamNotOpenException when the stream is not open
86 unsigned currentLength = [string length];
90 [NGStreamNotOpenException raiseWithReason:
91 @"tried to read from a string text stream which was closed"];
94 if (currentLength == index)
95 //[[[NGEndOfStreamException alloc] init] raise]
98 range.location = index;
99 range.length = (currentLength - index);
101 range = [string rangeOfString:@"\n" options:NSLiteralSearch range:range];
102 if (range.length == 0) { // did not found newline
103 NSString *result = [string substringFromIndex:index];
104 index = currentLength;
108 NSString *result = nil;
110 range.length = (range.location - index);
111 range.location = index;
113 result = [string substringWithRange:range];
115 index += range.length + 1;
121 // NGTextOutputStream
123 - (BOOL)writeCharacter:(unichar)_character {
125 // NGReadOnlyStreamException when the stream is not writeable
126 // NGStreamNotOpenException when the stream is not open
129 [NGStreamNotOpenException raiseWithReason:
130 @"tried to write to a string text stream which was closed"];
134 [[[NGReadOnlyStreamException alloc] init] raise];
138 [(NSMutableString *)string appendString:
139 [NSString stringWithCharacters:&_character length:1]];
143 - (BOOL)writeString:(NSString *)_string {
145 // NGReadOnlyStreamException when the stream is not writeable
146 // NGStreamNotOpenException when the stream is not open
149 [NGStreamNotOpenException raiseWithReason:
150 @"tried to write to a string text stream which was closed"];
154 [[[NGReadOnlyStreamException alloc] init] raise];
158 [(NSMutableString *)string appendString:_string];
166 @end /* NGStringTextStream */