10.6 compatibility: Fix some new compiler warnings, and work around apparent regressions in NSTask and -stringByStandardizingPath.
5 // Created by Jens Alfke on 4/28/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
12 @implementation NSURL (MYUtilities)
14 + (NSString*) my_stringByTrimmingURLString: (NSString*)string
16 NSMutableString *trimmed = [[string mutableCopy] autorelease];
18 // Remove all whitespace and newlines:
20 r = [trimmed rangeOfCharacterFromSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
23 [trimmed replaceCharactersInRange: r withString: @""];
26 // Delete surrounding "<...>":
27 r = NSMakeRange(0,trimmed.length);
28 if( [trimmed hasPrefix: @"<"] ) {
32 if( [trimmed hasSuffix: @">"] )
34 return [trimmed substringWithRange: r];
37 + (NSURL*) my_URLWithLenientString: (NSString*)string
38 defaultScheme: (NSString*)defaultScheme
39 allowedSchemes: (NSArray*)allowedSchemes
42 string = [self my_stringByTrimmingURLString: string];
43 if( string.length==0 )
45 NSURL *url = [NSURL URLWithString: string];
48 // Apply default scheme (if any):
49 NSString *scheme = url.scheme.lowercaseString;
53 string = $sprintf(@"%@://%@", defaultScheme,string);
54 url = [NSURL URLWithString: string];
55 scheme = [url scheme];
59 // Check that scheme is allowed:
60 if( allowedSchemes && ![allowedSchemes containsObject: scheme] )
70 @implementation NSHTTPURLResponse (MYUtilities)
73 - (NSError*) HTTPError
75 // HTTP status >= 300 is considered an error:
76 int status = self.statusCode;
78 NSString *reason = [NSHTTPURLResponse localizedStringForStatusCode: status];
79 NSDictionary *info = $dict({NSLocalizedFailureReasonErrorKey,reason});
80 return [NSError errorWithDomain: MyHTTPErrorDomain code: status userInfo: info];
86 NSString* const MyHTTPErrorDomain = @"HTTP";
93 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
95 Redistribution and use in source and binary forms, with or without modification, are permitted
96 provided that the following conditions are met:
98 * Redistributions of source code must retain the above copyright notice, this list of conditions
99 and the following disclaimer.
100 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
101 and the following disclaimer in the documentation and/or other materials provided with the
104 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
105 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
106 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
107 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
108 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
109 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
110 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
111 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.