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
22 #include "NGTextStream.h"
25 @implementation NGTextStream
32 [self->lastException release];
36 /* NGTextInputStream */
38 - (NSException *)lastException {
41 - (void)setLastException:(NSException *)_exception {
42 ASSIGN(self->lastException, _exception);
44 - (void)resetLastException {
45 [self->lastException release];
46 self->lastException = nil;
49 - (unichar)readCharacter {
50 [self subclassResponsibility:_cmd];
58 /* NGExtendedTextInputStream */
60 - (NSString *)readLineAsString {
64 *(&str) = (id)[NSMutableString string];
67 while ((c = [self readCharacter]) != '\n')
68 [str appendString:[NSString stringWithCharacters:&c length:1]];
71 if ([localException isKindOfClass:[NGEndOfStreamException class]]) {
72 if ([str length] == 0) str = nil;
75 [localException raise];
82 - (unsigned)readCharacters:(unichar *)_chars count:(unsigned)_count {
84 Read up to _count characters, but one at the minimum.
86 volatile unsigned pos;
89 for (pos = 0; pos < _count; pos++)
90 _chars[pos] = [self readCharacter];
93 if ([localException isKindOfClass:[NGEndOfStreamException class]]) {
95 [localException raise];
98 [localException raise];
102 NSAssert1(pos > 0, @"invalid character count to be returned: %i", pos);
106 - (BOOL)safeReadCharacters:(unichar *)_chars count:(unsigned)_count {
107 volatile unsigned pos;
109 for (pos = 0; pos < _count; pos++)
110 _chars[pos] = [self readCharacter];
115 /* NGTextOutputStream */
117 - (BOOL)writeCharacter:(unichar)_character {
118 [self subclassResponsibility:_cmd];
122 - (BOOL)writeString:(NSString *)_string {
123 unsigned length = [_string length], cnt = 0;
124 unichar buffer[length];
125 void (*writeChar)(id, SEL, unichar);
127 writeChar = (void (*)(id,SEL,unichar))
128 [self methodForSelector:@selector(writeCharacter:)];
130 [_string getCharacters:buffer];
131 for (cnt = 0; cnt < length; cnt++)
132 writeChar(self, @selector(writeCharacter:), buffer[cnt]);
141 /* NGExtendedTextOutputStream */
143 - (BOOL)writeFormat:(NSString *)_format arguments:(va_list)_ap {
146 tmp = [[[NSString alloc] initWithFormat:_format arguments:_ap] autorelease];
147 [self writeString:tmp];
150 - (BOOL)writeFormat:(NSString *)_format, ... {
154 va_start(ap, _format);
157 res = [self writeFormat:_format arguments:ap];
161 [localException raise];
169 - (BOOL)writeNewline {
170 if (![self writeString:@"\n"]) return NO;
174 - (unsigned)writeCharacters:(const unichar *)_chars count:(unsigned)_count {
176 Write up to _count characters, but one at the minimum.
178 volatile unsigned pos;
181 for (pos = 0; pos < _count; pos++)
182 [self writeCharacter:_chars[pos]];
185 if ([localException isKindOfClass:[NGEndOfStreamException class]]) {
187 [localException raise];
190 [localException raise];
194 NSAssert1(pos > 0, @"invalid character count to be returned: %i", pos);
198 - (BOOL)safeWriteCharacters:(const unichar *)_chars count:(unsigned)_count {
201 for (pos = 0; pos < _count; pos++) {
202 if (![self writeCharacter:_chars[pos]])
209 @end /* NGTextStream */