From a312d7cec7d84b63af9ce7ab42b83d350b883686 Mon Sep 17 00:00:00 2001 From: CaS Date: Sat, 24 Aug 2002 06:00:44 +0000 Subject: [PATCH] iHacks for MacOS-X compatibility git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@14322 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 13 ++++++++++--- Source/NSURL.m | 36 ++++++++++++++++++++++++------------ Testing/basic.m | 30 ++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index f3558f88a..0b0bec94d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,7 +1,14 @@ -2002-08-23 Richard Frith-Macdonald +2002-08-24 Richard Frith-Macdonald - * Source/NSURL.m: Hack for compatibility with MacOS-X in returning - path of a file URL ... the RFC says we shouldn't. + * Source/NSURL.m: Hacks for compatibility with MacOS-X in returning + path of a file URL ... the RFC says we shouldn't, but MacOS-X does. + Also when creating relative file URLs the MacOS-X code doesn't + strip the last patch component of the base URL as it should ... + we emulate that too. + Final note ... the MacOS-X code inserts 'localhost' as a host name + for a file URL which it shouldnt (a file URL has no host part) but + we don't emulate that at present (perhaps never as I can't see it + being very useful). 2002-08-22 Richard Frith-Macdonald diff --git a/Source/NSURL.m b/Source/NSURL.m index 59943be69..4c7792363 100644 --- a/Source/NSURL.m +++ b/Source/NSURL.m @@ -72,6 +72,7 @@ typedef struct { BOOL pathIsAbsolute; BOOL hasNoPath; BOOL isGeneric; + BOOL isFile; } parsedURL; #define myData ((parsedURL*)(self->_data)) @@ -210,7 +211,26 @@ static char *buildURL(parsedURL *base, parsedURL *rel, BOOL standardize) else { char *start = base->path; - char *end = strrchr(start, '/'); + char *end; + + /* + * Evil hack for MacOS-X compatibility. + * RFCs state that '/foo/bar' combined with 'xxx' + * gives '/foo/xxx', but MacOS-X treats file URLs + * differently and returns '/foo/bar/xxx' + */ + if (rel->isFile == YES) + { + end = &start[strlen(start)]; + if (end > start && end[-1] == '/') + { + end--; + } + } + else + { + end = strrchr(start, '/'); + } if (end != 0) { @@ -662,6 +682,7 @@ static void unescape(const char *from, char * to) usesFragments = NO; usesParameters = NO; usesQueries = NO; + buf->isFile = YES; } else if (strcmp(buf->scheme, "mailto") == 0) { @@ -1059,11 +1080,7 @@ static void unescape(const char *from, char * to) */ - (BOOL) isFileURL { - if (myData->scheme != 0 && strcmp(myData->scheme, "file") == 0) - { - return YES; - } - return NO; + return myData->isFile; } /** @@ -1186,7 +1203,7 @@ static void unescape(const char *from, char * to) /* * If this scheme is from a URL without generic format, there is no path. */ - if (myData->isGeneric == YES) + if (myData->isGeneric == YES || myData->isFile == YES) { unsigned int len = (_baseURL ? strlen(baseData->path) : 0) + strlen(myData->path) + 3; @@ -1231,11 +1248,6 @@ static void unescape(const char *from, char * to) unescape(buf, buf); path = [NSString stringWithUTF8String: buf]; } - else if (myData->scheme != 0 && myData->path != 0 - && strcmp(myData->scheme, "file") == 0) - { - path = [NSString stringWithUTF8String: myData->path]; - } return path; } diff --git a/Testing/basic.m b/Testing/basic.m index f9aa69f9f..d9f444ff3 100644 --- a/Testing/basic.m +++ b/Testing/basic.m @@ -3,12 +3,42 @@ #if 1 + +static void test1(void) +{ + NSURL *baseURL = [NSURL fileURLWithPath:@"/usr/local/bin"]; + NSURL *url = [NSURL URLWithString:@"filename" relativeToURL:baseURL]; + NSString *result = [url absoluteString]; + NSString *expected = @"file:/usr/local/bin/filename"; + + if ([result isEqualToString:expected]) + NSLog(@"test 1 ok"); + else + NSLog(@"-[NSURL absoluteString] returned \"%@\", expected \"%@\"", result, expected); +} + +static void test2(void) +{ + NSURL *url = [NSURL fileURLWithPath:@"/tmp/foo"]; + NSString *result = [url path]; + NSString *expected = @"/tmp/foo"; + + if ([result isEqualToString:expected]) + NSLog(@"Test 2 ok"); + else + NSLog(@"-[NSURL path] returned \"%@\", expected \"%@\"", result, expected); +} + int main () { id pool = [NSAutoreleasePool new]; id o = [NSObject new]; NSArray *a = [NSArray arrayWithObjects: @"a", @"b", nil]; + + test1(); + test2(); + printf ("Hello from object at 0x%x\n", (unsigned)[o self]); NSLog(@"Value for foo is %@", [a valueForKey: @"foo"]);