/*
- Copyright (C) 2000-2007 SKYRIX Software AG
- Copyright (C) 2007 Helge Hess
+ Copyright (C) 2000-2008 SKYRIX Software AG
+ Copyright (C) 2007-2008 Helge Hess
This file is part of SOPE.
[self release];
return nil;
}
- if ((self = [super init])) {
+ if ((self = [super init]) != nil) {
self->source = [_stream retain];
+
+ /* On MacOS 10.5 this is per default 30 aka MacOS Roman */
+ self->encoding = [NSString defaultCStringEncoding];
#ifdef __APPLE__
//# warning no selector caching on MacOSX ...
autorelease];
}
-// NGTextOutputStream
+
+/* NGTextOutputStream */
- (BOOL)writeCharacter:(unichar)_character {
unsigned char c;
// character is not in range of maximum system encoding
[NSException raise:@"NGCTextStreamEncodingException"
format:
- @"called writeCharacter: with character code (0x%X) exceeding"
- @" the maximum system character code (0x%X)",
+ @"called writeCharacter: with character code (0x%X)"
+ @" exceeding the maximum system character code (0x%X)",
_character, ((sizeof(unsigned char) * 256) - 1)];
}
c = _character;
- if (writeBytes) {
- res = writeBytes(self->source, @selector(writeBytes:count:),
- &c, sizeof(unsigned char));
+ if (self->writeBytes != NULL) {
+ res = self->writeBytes(self->source, @selector(writeBytes:count:),
+ &c, sizeof(unsigned char));
}
else
res = [self->source writeBytes:&c count:sizeof(unsigned char)];
unsigned toGo;
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1040
- NSStringEncoding enc = [NSString defaultCStringEncoding];
-
- // TBD: better use -maximumLengthOf... and then search for \0
- if ((toGo = [_string lengthOfBytesUsingEncoding:enc]) == 0)
+ if ((toGo = [_string maximumLengthOfBytesUsingEncoding:self->encoding]) == 0)
return YES;
-
- buf = str = calloc(toGo + 1, sizeof(unsigned char));
- [_string getCString:(char *)str maxLength:toGo encoding:enc];
+
+ buf = str = calloc(toGo + 2, sizeof(unsigned char));
+ // Note: maxLength INCLUDES the 0-terminator. And -getCString: does
+ // 0-terminate the buffer
+ if (![_string getCString:(char *)str maxLength:(toGo + 1)
+ encoding:self->encoding]) {
+ NSLog(@"ERROR(%s): failed to extract cString in defaultCStringEncoding(%i)"
+ @" from NSString: '%@'\n", __PRETTY_FUNCTION__,
+ self->encoding, _string);
+ return NO;
+ }
+
+ // we need to update the *real* (not the max) length
+ toGo = strlen((char *)str);
#else
if ((toGo = [_string cStringLength]) == 0)
return YES;
- buf = str = calloc(toGo + 1, sizeof(unsigned char));
+ buf = str = calloc(toGo + 2, sizeof(unsigned char));
[_string getCString:(char *)str];
-#endif
str[toGo] = '\0';
+#endif
NS_DURING {
while (toGo > 0) {