ObjFW  Check-in [9e556d7dca]

Overview
Comment:Improve file URL path handling on Windows/DOS
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 9e556d7dcad4010e2c4ec5745a5850610fdd37dfe370c363cfbe02efb9a3d10d
User & Date: js on 2019-04-07 22:36:13
Other Links: manifest | tags
Context
2019-04-07
23:05
Add -[OFMutableURL appendPathComponent:] check-in: 7bf788b164 user: js tags: trunk
22:36
Improve file URL path handling on Windows/DOS check-in: 9e556d7dca user: js tags: trunk
19:30
Improve -[OFURL pathComponents] check-in: b1d795b650 user: js tags: trunk
Changes

Modified src/OFURL.m from [eacf437ac9] to [a34547048c].

928
929
930
931
932
933
934

935














936
937
938
939
940
941
942
943
944
945





946
947
948
949
950

951
952
953
954
955
956
957
958
959
960
961
- (OFString *)URLEncodedPath
{
	return _URLEncodedPath;
}

- (OFArray *)pathComponents
{

	OFMutableArray *ret = [[[_URLEncodedPath














	    componentsSeparatedByString: @"/"] mutableCopy] autorelease];
	void *pool = objc_autoreleasePoolPush();
	size_t count = ret.count;

	if (count > 0 && [ret.firstObject length] == 0)
		[ret replaceObjectAtIndex: 0
			       withObject: @"/"];

	for (size_t i = 0; i < count; i++) {
		OFString *component = [ret objectAtIndex: i];





		[ret replaceObjectAtIndex: i
			       withObject: component.stringByURLDecoding];
	}

	[ret makeImmutable];


	objc_autoreleasePoolPop(pool);

	return ret;
}

- (OFString *)lastPathComponent
{
	void *pool = objc_autoreleasePoolPush();
	OFString *path = _URLEncodedPath;
	const char *UTF8String, *lastComponent;







>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|







>
>
>
>
>





>



|







928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
- (OFString *)URLEncodedPath
{
	return _URLEncodedPath;
}

- (OFArray *)pathComponents
{
	void *pool = objc_autoreleasePoolPush();
	OFMutableArray *ret;
	size_t count;

#if defined(OF_WINDOWS) || defined(OF_MSDOS)
	if ([_URLEncodedScheme isEqual: @"file"]) {
		OFString *path = [_URLEncodedPath substringWithRange:
		    of_range(1, _URLEncodedPath.length - 1)];
		path = [path stringByReplacingOccurrencesOfString: @"/"
						       withString: @"\\"];
		ret = [[path.pathComponents mutableCopy] autorelease];
		[ret insertObject: @"/"
			  atIndex: 0];
	} else
#endif
		ret = [[[_URLEncodedPath componentsSeparatedByString: @"/"]
		    mutableCopy] autorelease];

	count = ret.count;

	if (count > 0 && [ret.firstObject length] == 0)
		[ret replaceObjectAtIndex: 0
			       withObject: @"/"];

	for (size_t i = 0; i < count; i++) {
		OFString *component = [ret objectAtIndex: i];
#if defined(OF_WINDOWS) || defined(OF_MSDOS)
		component = [component
		    stringByReplacingOccurrencesOfString: @"\\"
					      withString: @"/"];
#endif
		[ret replaceObjectAtIndex: i
			       withObject: component.stringByURLDecoding];
	}

	[ret makeImmutable];
	[ret retain];

	objc_autoreleasePoolPop(pool);

	return [ret autorelease];
}

- (OFString *)lastPathComponent
{
	void *pool = objc_autoreleasePoolPush();
	OFString *path = _URLEncodedPath;
	const char *UTF8String, *lastComponent;
1086
1087
1088
1089
1090
1091
1092

1093




1094
1095
1096
1097
1098
1099
1100
		@throw [OFInvalidArgumentException exception];

	if (![_URLEncodedPath hasPrefix: @"/"])
		@throw [OFInvalidFormatException exception];

	path = self.path;


	if (path.length > 1 && [path hasSuffix: @"/"])




		path = [path substringWithRange: of_range(0, path.length - 1)];

	path = URLPathToPath(path);

#ifdef OF_WINDOWS
	if (_URLEncodedHost != nil) {
		if (path.length == 0)







>

>
>
>
>







1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
		@throw [OFInvalidArgumentException exception];

	if (![_URLEncodedPath hasPrefix: @"/"])
		@throw [OFInvalidFormatException exception];

	path = self.path;

#if !defined(OF_WINDOWS) && !defined(OF_MSDOS)
	if (path.length > 1 && [path hasSuffix: @"/"])
#else
	if (path.length > 1 && [path hasSuffix: @"/"] &&
	    ![path hasSuffix: @":/"])
#endif
		path = [path substringWithRange: of_range(0, path.length - 1)];

	path = URLPathToPath(path);

#ifdef OF_WINDOWS
	if (_URLEncodedHost != nil) {
		if (path.length == 0)

Modified tests/OFURLTests.m from [7afa9cebe7] to [ed392bfe35].

102
103
104
105
106
107
108
109
110







111
112
113
114
115
116
117

#ifdef OF_HAVE_FILES
	TEST(@"+[fileURLWithPath:]",
	    [[[OFURL fileURLWithPath: @"testfile.txt"] fileSystemRepresentation]
	    isEqual: [[OFFileManager defaultManager].currentDirectoryPath
	    stringByAppendingPathComponent: @"testfile.txt"]])

# ifdef OF_WINDOWS
	OFURL *tmp;







	TEST(@"+[fileURLWithPath:] with UNC",
	    (tmp = [OFURL fileURLWithPath: @"\\\\foo\\bar"]) &&
	    [tmp.host isEqual: @"foo"] && [tmp.path isEqual: @"/bar"] &&
	    [tmp.string isEqual: @"file://foo/bar"] &&
	    [tmp.fileSystemRepresentation isEqual: @"\\\\foo\\bar"] &&
	    (tmp = [OFURL fileURLWithPath: @"\\\\test"]) &&
	    [tmp.host isEqual: @"test"] && [tmp.path isEqual: @"/"] &&







|

>
>
>
>
>
>
>







102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

#ifdef OF_HAVE_FILES
	TEST(@"+[fileURLWithPath:]",
	    [[[OFURL fileURLWithPath: @"testfile.txt"] fileSystemRepresentation]
	    isEqual: [[OFFileManager defaultManager].currentDirectoryPath
	    stringByAppendingPathComponent: @"testfile.txt"]])

# if defined(OF_WINDOWS) || defined(OF_MSDOS)
	OFURL *tmp;
	TEST(@"+[fileURLWithPath:] for c:\\",
	    (tmp = [OFURL fileURLWithPath: @"c:\\"]) &&
	    [tmp.string isEqual: @"file:///c:/"] &&
	    [tmp.fileSystemRepresentation isEqual: @"c:\\"])
# endif

# ifdef OF_WINDOWS
	TEST(@"+[fileURLWithPath:] with UNC",
	    (tmp = [OFURL fileURLWithPath: @"\\\\foo\\bar"]) &&
	    [tmp.host isEqual: @"foo"] && [tmp.path isEqual: @"/bar"] &&
	    [tmp.string isEqual: @"file://foo/bar"] &&
	    [tmp.fileSystemRepresentation isEqual: @"\\\\foo\\bar"] &&
	    (tmp = [OFURL fileURLWithPath: @"\\\\test"]) &&
	    [tmp.host isEqual: @"test"] && [tmp.path isEqual: @"/"] &&