2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
6 OGo 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 OGo 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 OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "NGStringTextStream.h"
25 #include "NGStreamExceptions.h"
27 @implementation NGStringTextStream
29 + (id)textStreamWithString:(NSString *)_string {
30 return [[[self alloc] initWithString:_string] autorelease];
32 - (id)initWithString:(NSString *)_string {
33 if ((self = [super init])) {
34 self->string = [_string retain];
36 self->isMutable = [_string isKindOfClass:[NSMutableString class]];
42 [self->string release];
48 - (NSString *)string {
56 [self->string release]; self->string = nil;
62 - (unichar)readCharacter {
64 // NGStreamNotOpenException when the stream is not open
65 unsigned currentLength = [string length];
69 [NGStreamNotOpenException raiseWithReason:
70 @"tried to read from a string text stream which was closed"];
73 if (currentLength == index)
74 [[[NGEndOfStreamException alloc] init] raise];
76 result = [string characterAtIndex:index];
81 // NGExtendedTextInputStream
83 - (NSString *)readLineAsString {
85 // NGStreamNotOpenException when the stream is not open
87 unsigned currentLength = [string length];
91 [NGStreamNotOpenException raiseWithReason:
92 @"tried to read from a string text stream which was closed"];
95 if (currentLength == index)
96 //[[[NGEndOfStreamException alloc] init] raise]
99 range.location = index;
100 range.length = (currentLength - index);
102 range = [string rangeOfString:@"\n" options:NSLiteralSearch range:range];
103 if (range.length == 0) { // did not found newline
104 NSString *result = [string substringFromIndex:index];
105 index = currentLength;
109 NSString *result = nil;
111 range.length = (range.location - index);
112 range.location = index;
114 result = [string substringWithRange:range];
116 index += range.length + 1;
122 // NGTextOutputStream
124 - (BOOL)writeCharacter:(unichar)_character {
126 // NGReadOnlyStreamException when the stream is not writeable
127 // NGStreamNotOpenException when the stream is not open
130 [NGStreamNotOpenException raiseWithReason:
131 @"tried to write to a string text stream which was closed"];
135 [[[NGReadOnlyStreamException alloc] init] raise];
139 [(NSMutableString *)string appendString:
140 [NSString stringWithCharacters:&_character length:1]];
144 - (BOOL)writeString:(NSString *)_string {
146 // NGReadOnlyStreamException when the stream is not writeable
147 // NGStreamNotOpenException when the stream is not open
150 [NGStreamNotOpenException raiseWithReason:
151 @"tried to write to a string text stream which was closed"];
155 [[[NGReadOnlyStreamException alloc] init] raise];
159 [(NSMutableString *)string appendString:_string];
167 @end /* NGStringTextStream */