Update -getUploadSafeName: to not fail with certain characters
authorRobert Sesek <rsesek@bluestatic.org>
Mon, 15 Sep 2008 21:11:30 +0000 (17:11 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Mon, 15 Sep 2008 21:11:30 +0000 (17:11 -0400)
* Source/AppController.m:
(getUploadSafeName:): Use a simple for() loop rather than strange str manipulation for stripping chars

PrintDrop.xcodeproj/project.pbxproj
Source/AppController.m

index 400552eca9432c44ce81b06a615a24f52c2951b6..47c6ed190d63a43bb59bffc9184143e8dc1cebcd 100644 (file)
                C01FCF4F08A954540054247B /* Debug */ = {
                        isa = XCBuildConfiguration;
                        buildSettings = {
+                               GCC_C_LANGUAGE_STANDARD = gnu99;
                                GCC_WARN_ABOUT_RETURN_TYPE = YES;
                                GCC_WARN_UNUSED_VARIABLE = YES;
                                PREBINDING = NO;
                                        ppc,
                                        i386,
                                );
+                               GCC_C_LANGUAGE_STANDARD = gnu99;
                                GCC_WARN_ABOUT_RETURN_TYPE = YES;
                                GCC_WARN_UNUSED_VARIABLE = YES;
                                PREBINDING = NO;
index 7cfb7333f5b6a0c8c6bfc57a9097d1ce0cb401bc..f878d00af589d38663e460e1071bc5382f1104d0 100644 (file)
@@ -21,6 +21,7 @@
 #include <netdb.h>
 #include <stdlib.h>
 #include <WebKit/WebKit.h>
+#include <unistd.h>
 
 @interface AppController (Private)
 - (void)setStatus:(NSString *)msg isError:(BOOL)error;
@@ -289,16 +290,39 @@ shutdown:
  */
 - (NSString *)getUploadSafeName:(NSString *)name
 {
-       NSMutableString *n = [NSMutableString stringWithString:name];
-       [n replaceOccurrencesOfString:@" " withString:@"" options:NSBackwardsSearch range:NSMakeRange(0, [name length])];
-       name = (NSString *)n;
+       NSMutableString *safeName = [NSMutableString string];
+       
        name = [name lastPathComponent];
        name = [name stringByDeletingPathExtension];
-       if ([name length] > 20)
-               name = [name substringToIndex:20];
+       
+       for (int i = 0, j = 0; i < [name length] && j < 20; i++)
+       {
+               char c = [name characterAtIndex:i];
+               if ((c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && 122) || c == 95)
+               {
+                       [safeName appendFormat:@"%c", c];
+                       j++;
+               }
+               else if (c == 32)
+               {
+                       if (j > 0 && [safeName characterAtIndex:j - 1] != '-')
+                       {
+                               [safeName appendString:@"_"];
+                               j++;
+                       }
+               }
+               else if (c >= 38 && c <= 46)
+               {
+                       if (j > 0 && [safeName characterAtIndex:j - 1] != '_')
+                       {
+                               [safeName appendString:@"-"];
+                               j++;
+                       }
+               }
+       }
        
        srandomdev();
-       return [NSString stringWithFormat:@"~/%@.%d.pdf", name, random()];
+       return [NSString stringWithFormat:@"~/%@.%d.pdf", safeName, random()];
 }
 
 @end