ObjFW  Check-in [1e52247065]

Overview
Comment:Add +[componentsOfPath:] and +[directoryNameOfPath:] to OFFile.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 1e52247065bdd02813d2aa19d9a4e70c860a5416edcab9082c159801f7ff698a
User & Date: js on 2010-12-23 18:16:51
Other Links: manifest | tags
Context
2010-12-25
13:14
Add OFComparing protocol. check-in: 6b45991a6a user: js tags: trunk
2010-12-23
18:16
Add +[componentsOfPath:] and +[directoryNameOfPath:] to OFFile. check-in: 1e52247065 user: js tags: trunk
2010-12-20
21:41
Add +[changeToDirectory:] to OFFile. check-in: 4cf3648097 user: js tags: trunk
Changes

Modified src/OFFile.h from [905b14b0b8] to [9662e52e67].

37
38
39
40
41
42
43






44
45
46
47
48
49






50
51
52
53
54
55
56
/**
 * \param fd A file descriptor, returned from for example open().
 *	     It is not closed when the OFFile object is deallocated!
 * \return A new autoreleased OFFile
 */
+ fileWithFileDescriptor: (int)fd;







/**
 * \param path The path for which the last component should be returned
 * \return The last component of the path
 */
+ (OFString*)lastComponentOfPath: (OFString*)path;







/**
 * \param path The path to check
 * \return A boolean whether there is a file at the specified path
 */
+ (BOOL)fileExistsAtPath: (OFString*)path;

/**







>
>
>
>
>
>






>
>
>
>
>
>







37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
 * \param fd A file descriptor, returned from for example open().
 *	     It is not closed when the OFFile object is deallocated!
 * \return A new autoreleased OFFile
 */
+ fileWithFileDescriptor: (int)fd;

/**
 * \param path The path for which the components should be returned
 * \return The components of the path
 */
+ (OFArray*)componentsOfPath: (OFString*)path;

/**
 * \param path The path for which the last component should be returned
 * \return The last component of the path
 */
+ (OFString*)lastComponentOfPath: (OFString*)path;

/**
 * \param path The path for which the directory name should be returned
 * \return The directory name of the path
 */
+ (OFString*)directoryNameOfPath: (OFString*)path;

/**
 * \param path The path to check
 * \return A boolean whether there is a file at the specified path
 */
+ (BOOL)fileExistsAtPath: (OFString*)path;

/**

Modified src/OFFile.m from [213cb19bf6] to [527617a46d].

124
125
126
127
128
129
130











































131
132
133
134
135
136
137
				      mode: mode] autorelease];
}

+ fileWithFileDescriptor: (int)fd_
{
	return [[[self alloc] initWithFileDescriptor: fd_] autorelease];
}












































+ (OFString*)lastComponentOfPath: (OFString*)path
{
	const char *path_c = [path cString];
	size_t path_len = [path cStringLength];
	ssize_t i;








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
				      mode: mode] autorelease];
}

+ fileWithFileDescriptor: (int)fd_
{
	return [[[self alloc] initWithFileDescriptor: fd_] autorelease];
}

+ (OFArray*)componentsOfPath: (OFString*)path
{
	OFMutableArray *ret;
	OFAutoreleasePool *pool;
	const char *path_c = [path cString];
	size_t path_len = [path cStringLength];
	size_t i, last = 0;

	ret = [OFMutableArray array];

	if (path_len == 0)
		return ret;

	pool = [[OFAutoreleasePool alloc] init];

#ifndef _WIN32
	if (path_c[path_len - 1] == OF_PATH_DELIM)
#else
	if (path_c[path_len - 1] == '/' || path_c[path_len - 1] == '\\')
#endif
		path_len--;

	for (i = 0; i < path_len; i++) {
#ifndef _WIN32
		if (path_c[i] == OF_PATH_DELIM) {
#else
		if (path_c[i] == '/' || path_c[i] == '\\') {
#endif
			[ret addObject:
			    [OFString stringWithCString: path_c + last
						 length: i - last]];
			last = i + 1;
		}
	}

	[ret addObject: [OFString stringWithCString: path_c + last
					     length: i - last]];

	[pool release];

	return ret;
}

+ (OFString*)lastComponentOfPath: (OFString*)path
{
	const char *path_c = [path cString];
	size_t path_len = [path cStringLength];
	ssize_t i;

162
163
164
165
166
167
168








































169
170
171
172
173
174
175
	 */
	if (i < 0)
		i = 0;

	return [OFString stringWithCString: path_c + i
				    length: path_len - i];
}









































+ (BOOL)fileExistsAtPath: (OFString*)path
{
	struct stat s;

	if (stat([path cString], &s) == -1)
		return NO;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
	 */
	if (i < 0)
		i = 0;

	return [OFString stringWithCString: path_c + i
				    length: path_len - i];
}

+ (OFString*)directoryNameOfPath: (OFString*)path
{
	const char *path_c = [path cString];
	size_t path_len = [path cStringLength];
	size_t i;

	if (path_len == 0)
		return @"";

#ifndef _WIN32
	if (path_c[path_len - 1] == OF_PATH_DELIM)
#else
	if (path_c[path_len - 1] == '/' || path_c[path_len - 1] == '\\')
#endif
		path_len--;

	if (path_len == 0)
		return [OFString stringWithCString: path_c
					    length: 1];

	for (i = path_len - 1; i >= 1; i--)
#ifndef _WIN32
		if (path_c[i] == OF_PATH_DELIM)
#else
		if (path_c[i] == '/' || path_c[i] == '\\')
#endif
			return [OFString stringWithCString: path_c
						    length: i];

#ifndef _WIN32
	if (path_c[0] == OF_PATH_DELIM)
#else
	if (path_c[i] == '/' || path_c[i] == '\\')
#endif
		return [OFString stringWithCString: path_c
					    length: 1];

	return @".";
}

+ (BOOL)fileExistsAtPath: (OFString*)path
{
	struct stat s;

	if (stat([path cString], &s) == -1)
		return NO;

Modified tests/OFFileTests.m from [4fdd569a6c] to [1dc4ca2de6].

10
11
12
13
14
15
16

17
18
19
20
21
22
23
24
25
26

27

































28
29
30
31
32
33
34
35








36
37
38
 */

#include "config.h"

#import "OFFile.h"
#import "OFAutoreleasePool.h"
#import "OFString.h"

#import "OFExceptions.h"

#import "TestsAppDelegate.h"

static OFString *module = @"OFFile";

@implementation TestsAppDelegate (OFFileTests)
- (void)fileTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];



































	TEST(@"+[lastComponentOfPath]",
	    [[OFFile lastComponentOfPath: @"/tmp"] isEqual: @"tmp"] &&
	    [[OFFile lastComponentOfPath: @"/tmp/"] isEqual: @"tmp"] &&
	    [[OFFile lastComponentOfPath: @"/"] isEqual: @""] &&
	    [[OFFile lastComponentOfPath: @"foo"] isEqual: @"foo"] &&
	    [[OFFile lastComponentOfPath: @"foo/bar"] isEqual: @"bar"] &&
	    [[OFFile lastComponentOfPath: @"foo/bar/baz/"] isEqual: @"baz"])









	[pool drain];
}
@end







>










>

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







>
>
>
>
>
>
>
>



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
 */

#include "config.h"

#import "OFFile.h"
#import "OFAutoreleasePool.h"
#import "OFString.h"
#import "OFArray.h"
#import "OFExceptions.h"

#import "TestsAppDelegate.h"

static OFString *module = @"OFFile";

@implementation TestsAppDelegate (OFFileTests)
- (void)fileTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFArray *tmp;

	TEST(@"+[componentsOfPath:]",
	    /* /tmp */
	    (tmp = [OFFile componentsOfPath: @"/tmp"]) &&
	    [tmp count] == 2 &&
	    [[tmp objectAtIndex: 0] isEqual: @""] &&
	    [[tmp objectAtIndex: 1] isEqual: @"tmp"] &&
	    /* /tmp/ */
	    (tmp = [OFFile componentsOfPath: @"/tmp/"]) &&
	    [tmp count] == 2 &&
	    [[tmp objectAtIndex: 0] isEqual: @""] &&
	    [[tmp objectAtIndex: 1] isEqual: @"tmp"] &&
	    /* / */
	    (tmp = [OFFile componentsOfPath: @"/"]) &&
	    [tmp count] == 1 &&
	    [[tmp objectAtIndex: 0] isEqual: @""] &&
	    /* foo/bar */
	    (tmp = [OFFile componentsOfPath: @"foo/bar"]) &&
	    [tmp count] == 2 &&
	    [[tmp objectAtIndex: 0] isEqual: @"foo"] &&
	    [[tmp objectAtIndex: 1] isEqual: @"bar"] &&
	    /* foo/bar/baz/ */
	    (tmp = [OFFile componentsOfPath: @"foo/bar/baz"]) &&
	    [tmp count] == 3 &&
	    [[tmp objectAtIndex: 0] isEqual: @"foo"] &&
	    [[tmp objectAtIndex: 1] isEqual: @"bar"] &&
	    [[tmp objectAtIndex: 2] isEqual: @"baz"] &&
	    /* foo// */
	    (tmp = [OFFile componentsOfPath: @"foo//"]) &&
	    [tmp count] == 2 &&
	    [[tmp objectAtIndex: 0] isEqual: @"foo"] &&
	    [[tmp objectAtIndex: 1] isEqual: @""] &&
	    [[OFFile componentsOfPath: @""] count] == 0)

	TEST(@"+[lastComponentOfPath:]",
	    [[OFFile lastComponentOfPath: @"/tmp"] isEqual: @"tmp"] &&
	    [[OFFile lastComponentOfPath: @"/tmp/"] isEqual: @"tmp"] &&
	    [[OFFile lastComponentOfPath: @"/"] isEqual: @""] &&
	    [[OFFile lastComponentOfPath: @"foo"] isEqual: @"foo"] &&
	    [[OFFile lastComponentOfPath: @"foo/bar"] isEqual: @"bar"] &&
	    [[OFFile lastComponentOfPath: @"foo/bar/baz/"] isEqual: @"baz"])

	TEST(@"+[directoryNameOfPath:]",
	    [[OFFile directoryNameOfPath: @"/tmp"] isEqual: @"/"] &&
	    [[OFFile directoryNameOfPath: @"/tmp/"] isEqual: @"/"] &&
	    [[OFFile directoryNameOfPath: @"/tmp/foo/"] isEqual: @"/tmp"] &&
	    [[OFFile directoryNameOfPath: @"foo/bar"] isEqual: @"foo"] &&
	    [[OFFile directoryNameOfPath: @"/"] isEqual: @"/"] &&
	    [[OFFile directoryNameOfPath: @"foo"] isEqual: @"."])

	[pool drain];
}
@end