]> err.no Git - sope/blobdiff - sope-core/NGExtensions/FdExt.subproj/NSString+URLEscaping.m
fixed a bug in isNotEmpty
[sope] / sope-core / NGExtensions / FdExt.subproj / NSString+URLEscaping.m
index f6d30d738a712827d437ef9c4ac9d652f93dd207..acd88786e2588000900950afb0ceb5422926902a 100644 (file)
@@ -1,28 +1,33 @@
 /*
-  Copyright (C) 2000-2003 SKYRIX Software AG
+  Copyright (C) 2000-2005 SKYRIX Software AG
 
-  This file is part of OGo
+  This file is part of SOPE.
 
-  OGo is free software; you can redistribute it and/or modify it under
+  SOPE is free software; you can redistribute it and/or modify it under
   the terms of the GNU Lesser General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version.
 
-  OGo is distributed in the hope that it will be useful, but WITHOUT ANY
+  SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
   License for more details.
 
   You should have received a copy of the GNU Lesser General Public
-  License along with OGo; see the file COPYING.  If not, write to the
+  License along with SOPE; see the file COPYING.  If not, write to the
   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
   02111-1307, USA.
 */
-// $Id$
 
 #include "NSString+misc.h"
 #include "common.h"
 
+/*
+  TODO: support new Panther API?:
+- (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)e
+- (NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)e
+*/
+
 @implementation NSString(URLEscaping)
 
 static int useUTF8Encoding = -1;
@@ -49,8 +54,11 @@ static inline BOOL isUrlDigit(unsigned char _c) {
 }
 static inline BOOL isUrlSafeChar(unsigned char _c) {
   switch (_c) {
-    case '$': case '-': case '_': case '@':
-    case '.': case '&': case '+':
+    case '$': case '-': case '_': case '.':
+#if 0 /* see OGo bug #1260, required for forms */
+    case '+':
+#endif
+    case '@': // TODO: not a safe char?!
       return YES;
 
     default:
@@ -102,7 +110,7 @@ static inline BOOL isUrlAlphaNum(unsigned char _c) {
 }
 
 static inline BOOL isToBeEscaped(unsigned char _c) {
-  return (isUrlAlphaNum(_c) || (_c == '_')) ? NO : YES;
+  return (isUrlAlphaNum(_c) || (_c == '_') || isUrlSafeChar(_c)) ? NO : YES;
 }
 
 static void
@@ -123,7 +131,7 @@ NGEscapeUrlBuffer(const unsigned char *_source, unsigned char *_dest,
     } 
     else { // any other char is escaped ..
       *_dest = '%'; _dest++;
-      sprintf(_dest, "%02X", (unsigned)*src);
+      sprintf((char *)_dest, "%02X", (unsigned)*src);
       _dest += 2;
     }
   }
@@ -228,7 +236,7 @@ NGUnescapeUrlBuffer(const unsigned char *_source, unsigned char *_dest)
 
 - (NSString *)stringByUnescapingURL {
   /* 
-     input is a URL string - per definition ASCII(?!), like "hello%98%88.txt"
+     Input is a URL string - per definition ASCII(?!), like "hello%98%88.txt"
      output is a unicode string (never longer than the input)
      
      Note that the input itself is in some encoding! That is, the input is
@@ -250,12 +258,12 @@ NGUnescapeUrlBuffer(const unsigned char *_source, unsigned char *_dest)
   cstr[len] = '\0';
   
   buffer = malloc(len + 4);
-  NGUnescapeUrlBuffer(cstr, buffer);
+  NGUnescapeUrlBuffer((unsigned char *)cstr, (unsigned char *)buffer);
   
   if (doUseUTF8Encoding()) {
     /* OK, the input is considered UTF-8 encoded in a string */
     s = [[NSString alloc] initWithUTF8String:buffer];
-    if (buffer) free(buffer);
+    if (buffer != NULL) free(buffer); buffer = NULL;
   }
   else {
     s = [[NSString alloc]
@@ -263,7 +271,7 @@ NGUnescapeUrlBuffer(const unsigned char *_source, unsigned char *_dest)
          length:strlen(buffer)
          freeWhenDone:YES];
   }
-  if (cstr) free(cstr);
+  if (cstr != NULL) free(cstr); cstr = NULL;
   return [s autorelease];
 }
 
@@ -290,18 +298,18 @@ NGUnescapeUrlBuffer(const unsigned char *_source, unsigned char *_dest)
       return @"";
     
     buffer = malloc(len * 3 + 2);
-    NGEscapeUrlBuffer([data bytes], buffer, len);
+    NGEscapeUrlBuffer([data bytes], (unsigned char *)buffer, len);
   }
   else {
     unsigned char *cstr;
     
     len  = [self cStringLength];
     cstr = malloc(len + 4);
-    [self getCString:cstr]; // Unicode!
+    [self getCString:(char *)cstr]; // Unicode!
     cstr[len] = '\0';
     
     buffer = malloc(len * 3 + 2);
-    NGEscapeUrlBuffer(cstr, buffer, len);
+    NGEscapeUrlBuffer(cstr, (unsigned char *)buffer, len);
     if (cstr) free(cstr);
   
   }