]> err.no Git - sope/blobdiff - sope-core/NGStreams/NGCTextStream.m
replaced usage of getCString on MacOS >= 10.4
[sope] / sope-core / NGStreams / NGCTextStream.m
index a061c5a6f981869b7ff667a668bebf260d55f20f..3e40a3df85c4df3c225268f6cf328a2fe6e95fe2 100644 (file)
@@ -1,5 +1,6 @@
 /*
-  Copyright (C) 2000-2005 SKYRIX Software AG
+  Copyright (C) 2000-2007 SKYRIX Software AG
+  Copyright (C) 2007      Helge Hess
 
   This file is part of SOPE.
 
@@ -290,12 +291,24 @@ static void _flushAtExit(void) {
 - (BOOL)writeString:(NSString *)_string {
   unsigned char *str, *buf;
   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)
+    return YES;
+
+  buf = str = calloc(toGo + 1, sizeof(unsigned char));
+  [_string getCString:(char *)str maxLength:toGo encoding:enc];
+#else
   if ((toGo = [_string cStringLength]) == 0)
     return YES;
-  
+
   buf = str = calloc(toGo + 1, sizeof(unsigned char));
-  [_string getCString:(char *)str]; str[toGo] = '\0';
+  [_string getCString:(char *)str];
+#endif
+  str[toGo] = '\0';
   
   NS_DURING {
     while (toGo > 0) {
@@ -313,7 +326,7 @@ static void _flushAtExit(void) {
     }
   }
   NS_HANDLER {
-    if (buf) { free(buf); buf = NULL; };
+    if (buf != NULL) { free(buf); buf = NULL; };
     [localException raise];
   }
   NS_ENDHANDLER;