]> err.no Git - sope/blob - sope-core/NGStreams/NGStringTextStream.m
git-svn-id: http://svn.opengroupware.org/SOPE/trunk@181 e4a50df8-12e2-0310-a44c-efbce...
[sope] / sope-core / NGStreams / NGStringTextStream.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21 // $Id$
22
23 #include "common.h"
24 #include "NGStringTextStream.h"
25 #include "NGStreamExceptions.h"
26
27 @implementation NGStringTextStream
28
29 + (id)textStreamWithString:(NSString *)_string {
30   return [[[self alloc] initWithString:_string] autorelease];
31 }
32 - (id)initWithString:(NSString *)_string {
33   if ((self = [super init])) {
34     self->string    = [_string retain];
35     self->index     = 0;
36     self->isMutable = [_string isKindOfClass:[NSMutableString class]];
37   }
38   return self;
39 }
40
41 - (void)dealloc {
42   [self->string release];
43   [super dealloc];
44 }
45
46 /* accessors */
47
48 - (NSString *)string {
49   return string;
50 }
51
52 /* operations */
53
54 - (BOOL)close {
55   // releases string
56   [self->string release]; self->string = nil;
57   return YES;
58 }
59
60 // NGTextInputStream
61
62 - (unichar)readCharacter {
63   // throws
64   //   NGStreamNotOpenException  when the stream is not open
65   unsigned currentLength = [string length];
66   unichar  result;
67
68   if (string == nil) {
69     [NGStreamNotOpenException raiseWithReason:
70        @"tried to read from a string text stream which was closed"];
71   }
72   
73   if (currentLength == index)
74     [[[NGEndOfStreamException alloc] init] raise];
75
76   result = [string characterAtIndex:index];
77   index++;
78   return result;
79 }
80
81 // NGExtendedTextInputStream
82
83 - (NSString *)readLineAsString {
84   // throws
85   //   NGStreamNotOpenException  when the stream is not open
86   
87   unsigned currentLength = [string length];
88   NSRange  range;
89   
90   if (string == nil) {
91     [NGStreamNotOpenException raiseWithReason:
92             @"tried to read from a string text stream which was closed"];
93   }
94   
95   if (currentLength == index)
96     //[[[NGEndOfStreamException alloc] init] raise]
97     return nil;
98   
99   range.location = index;
100   range.length   = (currentLength - index);
101
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;
106     return result;
107   }
108   else {
109     NSString *result = nil;
110
111     range.length   = (range.location - index);
112     range.location = index;
113
114     result = [string substringWithRange:range];
115
116     index += range.length + 1;
117
118     return result;
119   }
120 }
121
122 // NGTextOutputStream
123
124 - (BOOL)writeCharacter:(unichar)_character {
125   // throws
126   //   NGReadOnlyStreamException when the stream is not writeable
127   //   NGStreamNotOpenException  when the stream is not open
128   
129   if (string == nil) {
130     [NGStreamNotOpenException raiseWithReason:
131             @"tried to write to a string text stream which was closed"];
132     return NO;
133   }
134   if (!isMutable) {
135     [[[NGReadOnlyStreamException alloc] init] raise];
136     return NO;
137   }
138   
139   [(NSMutableString *)string appendString:
140     [NSString stringWithCharacters:&_character length:1]];
141   return YES;
142 }
143
144 - (BOOL)writeString:(NSString *)_string {
145   // throws
146   //   NGReadOnlyStreamException when the stream is not writeable
147   //   NGStreamNotOpenException  when the stream is not open
148   
149   if (string == nil) {
150     [NGStreamNotOpenException raiseWithReason:
151             @"tried to write to a string text stream which was closed"];
152     return NO;
153   }
154   if (!isMutable) {
155     [[[NGReadOnlyStreamException alloc] init] raise];
156     return NO;
157   }
158   
159   [(NSMutableString *)string appendString:_string];
160   return YES;
161 }
162
163 - (BOOL)flush {
164   return YES;
165 }
166
167 @end /* NGStringTextStream */