Index: src/OFFile.h ================================================================== --- src/OFFile.h +++ src/OFFile.h @@ -12,10 +12,11 @@ #include #import "OFSeekableStream.h" @class OFString; +@class OFArray; /** * \brief A class which provides functions to read, write and manipulate files. */ @interface OFFile: OFSeekableStream @@ -39,14 +40,27 @@ * \return A new autoreleased OFFile */ + fileWithFileDescriptor: (int)fd; /** + * \param path The path to check * \return A boolean whether there is a file at the specified path */ + (BOOL)fileExistsAtPath: (OFString*)path; +/** + * \param path The path to check + * \return A boolean whether there is a directory at the specified path + */ ++ (BOOL)directoryExistsAtPath: (OFString*)path; + +/** + * \param path The path of the directory + * \return An array of OFStrings with the files at the specified path + */ ++ (OFArray*)filesInDirectoryAtPath: (OFString*)path; + /** * Changes the mode of a file. * * Only changes read-only flag on Windows. * Index: src/OFFile.m ================================================================== --- src/OFFile.m +++ src/OFFile.m @@ -16,13 +16,16 @@ #include #include #include #include +#include #import "OFFile.h" #import "OFString.h" +#import "OFArray.h" +#import "OFAutoreleasePool.h" #import "OFExceptions.h" #ifdef _WIN32 # import #endif @@ -112,10 +115,61 @@ if (S_ISREG(s.st_mode)) return YES; return NO; } + ++ (BOOL)directoryExistsAtPath: (OFString*)path +{ + struct stat s; + + if (stat([path cString], &s) == -1) + return NO; + + if (S_ISDIR(s.st_mode)) + return YES; + + return NO; +} + ++ (OFArray*)filesInDirectoryAtPath: (OFString*)path +{ + OFAutoreleasePool *pool; + OFMutableArray *files; + DIR *dir; + struct dirent *dirent; + + files = [OFMutableArray array]; + + if ((dir = opendir([path cString])) == NULL) + @throw [OFOpenFileFailedException newWithClass: self + path: path + mode: @"r"]; + + @try { + pool = [[OFAutoreleasePool alloc] init]; + + while ((dirent = readdir(dir)) != NULL) { + OFString *file; + + if (!strcmp(dirent->d_name, ".") || + !strcmp(dirent->d_name, "..")) + continue; + + file = [OFString stringWithCString: dirent->d_name]; + [files addObject: file]; + + [pool releaseObjects]; + } + + [pool release]; + } @finally { + closedir(dir); + } + + return files; +} + (void)changeModeOfFile: (OFString*)path toMode: (mode_t)mode { #ifndef _WIN32