forked from johnjohndoe/LineReader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectoryReader.m
More file actions
67 lines (53 loc) · 1.55 KB
/
DirectoryReader.m
File metadata and controls
67 lines (53 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
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
//
// DirectoryReader.m
// LineReader
//
// Created by Tobias Preuss on 05.10.10.
// Copyright 2010 Tobias Preuss. All rights reserved.
//
#import "DirectoryReader.h"
/**
A directory reader.
*/
@implementation DirectoryReader
/**
Initializes a directory reader.
@param path A directory path.
@returns An initialized DirectoryReader object or nil if the object could not be created.
*/
- (id)initWithPath:(NSString*)path {
self = [super init];
if (self != nil) {
if (!path || [path length] <= 0) {
return nil;
}
// Remove trailing slash if appended.
NSMutableString* mutablePath = [NSMutableString stringWithString:path];
[mutablePath replaceOccurrencesOfString:@"/" withString:@"" options:NSBackwardsSearch range:NSMakeRange([path length] - 1, 1)];
m_path = mutablePath;
}
return self;
}
/**
Reads the content of a directory.
@param files Container for the listing.
@returns YES if the directory was read otherwise NO.
*/
- (BOOL)readDirectory:(NSArray**)files {
BOOL success = false;
NSArray* fileNames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:m_path error:nil];
if (!fileNames || [fileNames count] <= 0) {
return success;
}
NSMutableArray* fullNames = [NSMutableArray arrayWithCapacity:[fileNames count]];
for (NSString* fileName in fileNames) {
NSString* fullPath = [m_path stringByAppendingString:[NSString stringWithFormat:@"/%@", fileName]];
[fullNames addObject:fullPath];
}
if (fullNames && [fullNames count] > 0 && files) {
*files = fullNames;
success = YES;
}
return success;
}
@end