Index: src/OFFile.h ================================================================== --- src/OFFile.h +++ src/OFFile.h @@ -93,10 +93,19 @@ * * \param path The path of the directory */ + (void)createDirectoryAtPath: (OFString*)path; +/** + * \brief Creates a directory at the specified path. + * + * \param path The path of the directory + * \param createParents Whether to create the parents of the directory + */ ++ (void)createDirectoryAtPath: (OFString*)path + createParents: (BOOL)createParents; + /** * \brief Returns an array with the files in the specified directory. * * \param path The path of the directory * \return An array of OFStrings with the files at the specified path Index: src/OFFile.m ================================================================== --- src/OFFile.m +++ src/OFFile.m @@ -246,10 +246,47 @@ #endif @throw [OFCreateDirectoryFailedException exceptionWithClass: self path: path]; } + ++ (void)createDirectoryAtPath: (OFString*)path + createParents: (BOOL)createParents +{ + OFAutoreleasePool *pool, *pool2; + OFArray *pathComponents; + OFString *currentPath = nil, *component; + OFEnumerator *enumerator; + + if (!createParents) { + [OFFile createDirectoryAtPath: path]; + return; + } + + pool = [[OFAutoreleasePool alloc] init]; + + pathComponents = [path pathComponents]; + enumerator = [pathComponents objectEnumerator]; + pool2 = [[OFAutoreleasePool alloc] init]; + while ((component = [enumerator nextObject]) != nil) { + if (currentPath != nil) + currentPath = [OFString + stringWithPath: currentPath, component, nil]; + else + currentPath = component; + + if (![currentPath isEqual: @""] && + ![OFFile directoryExistsAtPath: currentPath]) + [OFFile createDirectoryAtPath: currentPath]; + + [currentPath retain]; + [pool2 releaseObjects]; + [currentPath autorelease]; + } + + [pool release]; +} + (OFArray*)filesInDirectoryAtPath: (OFString*)path { OFAutoreleasePool *pool; OFMutableArray *files = [OFMutableArray array];