@@ -113,11 +113,11 @@ OFString *date_str, *me, *msg; va_list args; date = [OFDate date]; date_str = [date localDateStringWithFormat: @"%Y-%m-%d %H:%M:%S"]; - me = [OFFile lastComponentOfPath: [OFApplication programName]]; + me = [[OFApplication programName] lastPathComponent]; va_start(args, fmt); msg = [[[OFString alloc] initWithFormat: fmt arguments: args] autorelease]; va_end(args); @@ -132,13 +132,10 @@ @end @implementation OFFile + (void)load { - if (self != [OFFile class]) - return; - of_stdin = [[OFFileSingleton alloc] initWithFileDescriptor: 0]; of_stdout = [[OFFileSingleton alloc] initWithFileDescriptor: 1]; of_stderr = [[OFFileSingleton alloc] initWithFileDescriptor: 2]; } @@ -160,137 +157,10 @@ + 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]; - - /* - * Class swizzle the array to be immutable. We declared the return type - * to be OFArray*, so it can't be modified anyway. But not swizzling it - * would create a real copy each time -[copy] is called. - */ - ret->isa = [OFArray class]; - return ret; -} - -+ (OFString*)lastComponentOfPath: (OFString*)path -{ - const char *path_c = [path cString]; - size_t path_len = [path cStringLength]; - ssize_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--; - - for (i = path_len - 1; i >= 0; i--) { -#ifndef _WIN32 - if (path_c[i] == OF_PATH_DELIM) { -#else - if (path_c[i] == '/' || path_c[i] == '\\') { -#endif - i++; - break; - } - } - - /* - * Only one component, but the trailing delimiter might have been - * removed, so return a new string anyway. - */ - 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) @@ -518,11 +388,11 @@ OFFile *src; OFFile *dest; char buf[4096]; if ([self directoryExistsAtPath: to]) { - OFString *filename = [self lastComponentOfPath: from]; + OFString *filename = [from lastPathComponent]; to = [OFString stringWithPath: to, filename, nil]; } override = [self fileExistsAtPath: to]; @@ -560,11 +430,11 @@ + (void)renameFileAtPath: (OFString*)from toPath: (OFString*)to { if ([self directoryExistsAtPath: to]) { - OFString *filename = [self lastComponentOfPath: from]; + OFString *filename = [from lastPathComponent]; to = [OFString stringWithPath: to, filename, nil]; } #ifndef _WIN32 if (rename([from cString], [to cString])) @@ -597,11 +467,11 @@ #ifndef _WIN32 + (void)linkFileAtPath: (OFString*)src toPath: (OFString*)dest { if ([self directoryExistsAtPath: dest]) { - OFString *filename = [self lastComponentOfPath: src]; + OFString *filename = [src lastPathComponent]; dest = [OFString stringWithPath: dest, filename, nil]; } if (link([src cString], [dest cString]) != 0) @throw [OFLinkFailedException newWithClass: self @@ -613,11 +483,11 @@ #if !defined(_WIN32) && !defined(_PSP) + (void)symlinkFileAtPath: (OFString*)src toPath: (OFString*)dest { if ([self directoryExistsAtPath: dest]) { - OFString *filename = [self lastComponentOfPath: src]; + OFString *filename = [src lastPathComponent]; dest = [OFString stringWithPath: dest, filename, nil]; } if (symlink([src cString], [dest cString]) != 0) @throw [OFSymlinkFailedException newWithClass: self