From 40ac244e49aff6ef8e9ded6d1a8c9ba8e0850ddc Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Mon, 15 Sep 2008 17:11:30 -0400 Subject: [PATCH] Update -getUploadSafeName: to not fail with certain characters * Source/AppController.m: (getUploadSafeName:): Use a simple for() loop rather than strange str manipulation for stripping chars --- PrintDrop.xcodeproj/project.pbxproj | 2 ++ Source/AppController.m | 36 ++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/PrintDrop.xcodeproj/project.pbxproj b/PrintDrop.xcodeproj/project.pbxproj index 400552e..47c6ed1 100644 --- a/PrintDrop.xcodeproj/project.pbxproj +++ b/PrintDrop.xcodeproj/project.pbxproj @@ -555,6 +555,7 @@ C01FCF4F08A954540054247B /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { + GCC_C_LANGUAGE_STANDARD = gnu99; GCC_WARN_ABOUT_RETURN_TYPE = YES; GCC_WARN_UNUSED_VARIABLE = YES; PREBINDING = NO; @@ -569,6 +570,7 @@ ppc, i386, ); + GCC_C_LANGUAGE_STANDARD = gnu99; GCC_WARN_ABOUT_RETURN_TYPE = YES; GCC_WARN_UNUSED_VARIABLE = YES; PREBINDING = NO; diff --git a/Source/AppController.m b/Source/AppController.m index 7cfb733..f878d00 100644 --- a/Source/AppController.m +++ b/Source/AppController.m @@ -21,6 +21,7 @@ #include #include #include +#include @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 -- 2.22.5