Index: src/OFApplication.m ================================================================== --- src/OFApplication.m +++ src/OFApplication.m @@ -24,10 +24,11 @@ #import "OFApplication.h" #import "OFString.h" #import "OFArray.h" #import "OFDictionary.h" +#import "OFSystemInfo.h" #import "OFRunLoop.h" #import "OFRunLoop+Private.h" #import "OFThread.h" #import "OFThread+Private.h" @@ -232,11 +233,11 @@ if (env != NULL) { for (; *env != NULL; env++) { OFString *key, *value; char *sep; const of_string_encoding_t encoding = - [OFString nativeOSEncoding]; + [OFSystemInfo native8BitEncoding]; pool = objc_autoreleasePoolPush(); if ((sep = strchr(*env, '=')) == NULL) { fprintf(stderr, "Warning: Invalid " @@ -334,11 +335,11 @@ of_string_encoding_t encoding; _argc = argc; _argv = argv; - encoding = [OFString nativeOSEncoding]; + encoding = [OFSystemInfo native8BitEncoding]; # ifndef OF_NINTENDO_DS if (*argc > 0) { # else if (__system_argv->argvMagic == ARGV_MAGIC && Index: src/OFFile.m ================================================================== --- src/OFFile.m +++ src/OFFile.m @@ -124,26 +124,26 @@ of_stat(OFString *path, of_stat_t *buffer) { #ifdef _WIN32 return _wstat([path UTF16String], buffer); #else - return stat([path cStringWithEncoding: [OFString nativeOSEncoding]], - buffer); + return stat([path cStringWithEncoding: + [OFSystemInfo native8BitEncoding]], buffer); #endif } int of_lstat(OFString *path, of_stat_t *buffer) { #if defined(_WIN32) return _wstat([path UTF16String], buffer); #elif defined(HAVE_LSTAT) - return lstat([path cStringWithEncoding: [OFString nativeOSEncoding]], - buffer); + return lstat([path cStringWithEncoding: + [OFSystemInfo native8BitEncoding]], buffer); #else - return stat([path cStringWithEncoding: [OFString nativeOSEncoding]], - buffer); + return stat([path cStringWithEncoding: + [OFSystemInfo native8BitEncoding]], buffer); #endif } static int parseMode(const char *mode) @@ -235,12 +235,13 @@ wchar_t *buffer = _wgetcwd(NULL, 0); #endif @try { #ifndef _WIN32 - ret = [OFString stringWithCString: buffer - encoding: [OFString nativeOSEncoding]]; + ret = [OFString + stringWithCString: buffer + encoding: [OFSystemInfo native8BitEncoding]]; #else ret = [OFString stringWithUTF16String: buffer]; #endif } @finally { free(buffer); @@ -303,14 +304,14 @@ { if (path == nil) @throw [OFInvalidArgumentException exception]; #ifndef _WIN32 - if (mkdir([path cStringWithEncoding: [OFString nativeOSEncoding]], - DIR_MODE)) + if (mkdir([path cStringWithEncoding: [OFSystemInfo native8BitEncoding]], + DIR_MODE) != 0) #else - if (_wmkdir([path UTF16String])) + if (_wmkdir([path UTF16String]) != 0) #endif @throw [OFCreateDirectoryFailedException exceptionWithPath: path]; } @@ -370,11 +371,11 @@ files = [OFMutableArray array]; #ifndef _WIN32 DIR *dir; - encoding = [OFString nativeOSEncoding]; + encoding = [OFSystemInfo native8BitEncoding]; if ((dir = opendir([path cStringWithEncoding: encoding])) == NULL) @throw [OFOpenFileFailedException exceptionWithPath: path mode: @"r"]; @@ -465,13 +466,14 @@ { if (path == nil) @throw [OFInvalidArgumentException exception]; #ifndef _WIN32 - if (chdir([path cStringWithEncoding: [OFString nativeOSEncoding]])) + if (chdir([path cStringWithEncoding: + [OFSystemInfo native8BitEncoding]]) != 0) #else - if (_wchdir([path UTF16String])) + if (_wchdir([path UTF16String]) != 0) #endif @throw [OFChangeCurrentDirectoryPathFailedException exceptionWithPath: path]; } @@ -513,14 +515,14 @@ { if (path == nil) @throw [OFInvalidArgumentException exception]; # ifndef _WIN32 - if (chmod([path cStringWithEncoding: [OFString nativeOSEncoding]], - permissions)) + if (chmod([path cStringWithEncoding: [OFSystemInfo native8BitEncoding]], + permissions) != 0) # else - if (_wchmod([path UTF16String], permissions)) + if (_wchmod([path UTF16String], permissions) != 0) # endif @throw [OFChangePermissionsFailedException exceptionWithPath: path permissions: permissions]; } @@ -536,11 +538,11 @@ of_string_encoding_t encoding; if (path == nil || (owner == nil && group == nil)) @throw [OFInvalidArgumentException exception]; - encoding = [OFString nativeOSEncoding]; + encoding = [OFSystemInfo native8BitEncoding]; # ifdef OF_HAVE_THREADS if (!of_mutex_lock(&mutex)) @throw [OFLockFailedException exception]; @@ -576,11 +578,11 @@ if (!of_mutex_unlock(&mutex)) @throw [OFUnlockFailedException exception]; } # endif - if (chown([path cStringWithEncoding: encoding], uid, gid)) + if (chown([path cStringWithEncoding: encoding], uid, gid) != 0) @throw [OFChangeOwnerFailedException exceptionWithPath: path owner: owner group: group]; } #endif @@ -601,11 +603,11 @@ @throw [OFCopyItemFailedException exceptionWithSourcePath: source destinationPath: destination]; } - if (of_lstat(source, &s)) + if (of_lstat(source, &s) != 0) @throw [OFCopyItemFailedException exceptionWithSourcePath: source destinationPath: destination]; if (S_ISDIR(s.st_mode)) { @@ -722,16 +724,16 @@ exceptionWithSourcePath: source destinationPath: destination]; } #ifndef _WIN32 - encoding = [OFString nativeOSEncoding]; + encoding = [OFSystemInfo native8BitEncoding]; if (rename([source cStringWithEncoding: encoding], - [destination cStringWithEncoding: encoding])) { + [destination cStringWithEncoding: encoding]) != 0) { #else - if (_wrename([source UTF16String], [destination UTF16String])) { + if (_wrename([source UTF16String], [destination UTF16String]) != 0) { #endif if (errno != EXDEV) @throw [OFMoveItemFailedException exceptionWithSourcePath: source destinationPath: destination]; @@ -766,11 +768,11 @@ if (path == nil) @throw [OFInvalidArgumentException exception]; pool = objc_autoreleasePoolPush(); - if (of_lstat(path, &s)) + if (of_lstat(path, &s) != 0) @throw [OFRemoveItemFailedException exceptionWithPath: path]; if (S_ISDIR(s.st_mode)) { OFArray *contents; OFEnumerator *enumerator; @@ -793,13 +795,14 @@ objc_autoreleasePoolPop(pool2); } } #ifndef _WIN32 - if (remove([path cStringWithEncoding: [OFString nativeOSEncoding]])) + if (remove([path cStringWithEncoding: + [OFSystemInfo native8BitEncoding]]) != 0) #else - if (_wremove([path UTF16String])) + if (_wremove([path UTF16String]) != 0) #endif @throw [OFRemoveItemFailedException exceptionWithPath: path]; objc_autoreleasePoolPop(pool); } @@ -813,11 +816,11 @@ if (source == nil || destination == nil) @throw [OFInvalidArgumentException exception]; pool = objc_autoreleasePoolPush(); - encoding = [OFString nativeOSEncoding]; + encoding = [OFSystemInfo native8BitEncoding]; if (link([source cStringWithEncoding: encoding], [destination cStringWithEncoding: encoding]) != 0) @throw [OFLinkFailedException exceptionWithSourcePath: source @@ -836,11 +839,11 @@ if (source == nil || destination == nil) @throw [OFInvalidArgumentException exception]; pool = objc_autoreleasePoolPush(); - encoding = [OFString nativeOSEncoding]; + encoding = [OFSystemInfo native8BitEncoding]; if (symlink([source cStringWithEncoding: encoding], [destination cStringWithEncoding: encoding]) != 0) @throw [OFCreateSymbolicLinkFailedException exceptionWithSourcePath: source @@ -856,11 +859,11 @@ of_string_encoding_t encoding; if (path == nil) @throw [OFInvalidArgumentException exception]; - encoding = [OFString nativeOSEncoding]; + encoding = [OFSystemInfo native8BitEncoding]; length = readlink([path cStringWithEncoding: encoding], destination, PATH_MAX); if (length < 0) @throw [OFOpenFileFailedException exceptionWithPath: path @@ -887,12 +890,12 @@ if ((flags = parseMode([mode UTF8String])) == -1) @throw [OFInvalidArgumentException exception]; #ifndef _WIN32 - if ((_fd = open([path cStringWithEncoding: - [OFString nativeOSEncoding]], flags, DEFAULT_MODE)) == -1) + if ((_fd = open([path cStringWithEncoding: [OFSystemInfo + native8BitEncoding]], flags, DEFAULT_MODE)) == -1) #else if ((_fd = _wopen([path UTF16String], flags, DEFAULT_MODE)) == -1) #endif @throw [OFOpenFileFailedException Index: src/OFObject.m ================================================================== --- src/OFObject.m +++ src/OFObject.m @@ -24,10 +24,11 @@ #include #import "OFObject.h" #import "OFArray.h" +#import "OFSystemInfo.h" #import "OFTimer.h" #import "OFRunLoop.h" #import "OFThread.h" #import "OFAllocFailedException.h" @@ -97,21 +98,22 @@ static void uncaughtExceptionHandler(id exception) { OFString *description = [exception description]; OFArray *backtrace = nil; + of_string_encoding_t encoding = [OFSystemInfo native8BitEncoding]; fprintf(stderr, "\nRuntime error: Unhandled exception:\n%s\n", - [description cStringWithEncoding: [OFString nativeOSEncoding]]); + [description cStringWithEncoding: encoding]); if ([exception respondsToSelector: @selector(backtrace)]) backtrace = [exception backtrace]; if (backtrace != nil) { OFString *s = [backtrace componentsJoinedByString: @"\n "]; fprintf(stderr, "\nBacktrace:\n %s\n\n", - [s cStringWithEncoding: [OFString nativeOSEncoding]]); + [s cStringWithEncoding: encoding]); } abort(); } #endif @@ -200,11 +202,11 @@ const char* _NSPrintForDebugger(id object) { return [[object description] - cStringWithEncoding: [OFString nativeOSEncoding]]; + cStringWithEncoding: [OFSystemInfo native8BitEncoding]]; } /* References for static linking */ void _references_to_categories_of_OFObject(void) { Index: src/OFPlugin.m ================================================================== --- src/OFPlugin.m +++ src/OFPlugin.m @@ -23,10 +23,11 @@ # include #endif #import "OFPlugin.h" #import "OFString.h" +#import "OFSystemInfo.h" #import "OFInitializationFailedException.h" #ifdef _WIN32 # define dlsym(handle, symbol) GetProcAddress(handle, symbol) @@ -45,11 +46,11 @@ path = [path stringByAppendingString: @PLUGIN_SUFFIX]; #ifndef _WIN32 if ((handle = dlopen([path cStringWithEncoding: - [OFString nativeOSEncoding]], RTLD_LAZY)) == NULL) + [OFSystemInfo native8BitEncoding]], RTLD_LAZY)) == NULL) #else if ((handle = LoadLibraryW([path UTF16String])) == NULL) #endif @throw [OFInitializationFailedException exceptionWithClass: self]; Index: src/OFProcess.m ================================================================== --- src/OFProcess.m +++ src/OFProcess.m @@ -36,10 +36,11 @@ #import "OFProcess.h" #import "OFString.h" #import "OFArray.h" #import "OFDictionary.h" #import "OFDataArray.h" +#import "OFSystemInfo.h" #import "OFInitializationFailedException.h" #import "OFOutOfRangeException.h" #import "OFReadFailedException.h" #import "OFWriteFailedException.h" @@ -146,11 +147,11 @@ of_string_encoding_t encoding; argv = [self allocMemoryWithSize: sizeof(char*) count: count + 2]; - encoding = [OFString nativeOSEncoding]; + encoding = [OFSystemInfo native8BitEncoding]; argv[0] = (char*)[programName cStringWithEncoding: encoding]; for (i = 0; i < count; i++) @@ -311,11 +312,11 @@ of_string_encoding_t encoding; if (environment == nil) return NULL; - encoding = [OFString nativeOSEncoding]; + encoding = [OFSystemInfo native8BitEncoding]; count = [environment count]; envp = [self allocMemoryWithSize: sizeof(char*) count: count + 1]; Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -324,20 +324,10 @@ * @param components An array of components for the path * @return A new autoreleased OFString */ + (OFString*)pathWithComponents: (OFArray*)components; -/*! - * @brief Returns the string encoding native to the operating system. - * - * This is useful to encode strings correctly for passing them to operating - * system calls. - * - * @return The string encoding native to the operating system - */ -+ (of_string_encoding_t)nativeOSEncoding; - /*! * @brief Initializes an already allocated OFString from a UTF-8 encoded C * string. * * @param UTF8String A UTF-8 encoded C string to initialize the OFString with Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -26,20 +26,21 @@ #import "OFString.h" #import "OFString_UTF8.h" #import "OFString_UTF8+Private.h" #import "OFArray.h" #import "OFDictionary.h" +#import "OFDataArray.h" +#import "OFSystemInfo.h" #ifdef OF_HAVE_FILES # import "OFFile.h" #endif #import "OFURL.h" #ifdef OF_HAVE_SOCKETS # import "OFHTTPClient.h" # import "OFHTTPRequest.h" # import "OFHTTPResponse.h" #endif -#import "OFDataArray.h" #import "OFXMLElement.h" #ifdef OF_HAVE_SOCKETS # import "OFHTTPRequestFailedException.h" #endif @@ -665,16 +666,10 @@ objc_autoreleasePoolPop(pool); return ret; } -+ (of_string_encoding_t)nativeOSEncoding -{ - /* FIXME */ - return OF_STRING_ENCODING_UTF_8; -} - - init { if (object_getClass(self) == [OFString class]) { @try { [self doesNotRecognizeSelector: _cmd]; @@ -832,12 +827,12 @@ OFFile *file; /* Make sure the file system is initialized */ [OFFile class]; - if (stat([path cStringWithEncoding: [OFString - nativeOSEncoding]], &st) == -1) + if (stat([path cStringWithEncoding: + [OFSystemInfo native8BitEncoding]], &st) == -1) @throw [OFOpenFileFailedException exceptionWithPath: path mode: @"rb"]; if (st.st_size > SIZE_MAX) @@ -1158,12 +1153,11 @@ encoding: encoding lossy: lossy]; break; default: - @throw [OFNotImplementedException exceptionWithSelector: _cmd - object: self]; + @throw [OFInvalidEncodingException exception]; } return cString; } @@ -1216,12 +1210,11 @@ case OF_STRING_ENCODING_ISO_8859_15: case OF_STRING_ENCODING_WINDOWS_1252: case OF_STRING_ENCODING_CODEPAGE_437: return [self length]; default: - @throw [OFNotImplementedException exceptionWithSelector: _cmd - object: self]; + @throw [OFInvalidEncodingException exception]; } } - (size_t)UTF8StringLength { Index: src/OFSystemInfo.h ================================================================== --- src/OFSystemInfo.h +++ src/OFSystemInfo.h @@ -13,12 +13,11 @@ * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #import "OFObject.h" - -@class OFString; +#import "OFString.h" /*! * @class OFSystemInfo OFSystemInfo.h ObjFW/OFSystemInfo.h * * @brief A class for querying information about the system. @@ -38,10 +37,20 @@ * * @return The number of CPUs installed in the system */ + (size_t)numberOfCPUs; +/*! + * @brief Returns the native 8-bit string encoding of the operating system. + * + * This is useful to encode strings correctly for passing them to operating + * system calls. + * + * @return The native 8-bit string encoding of the operating system + */ ++ (of_string_encoding_t)native8BitEncoding; + /*! * @brief Returns the path where user data for the application can be stored. * * On Unix systems, this adheres to the XDG Base Directory specification.@n * On Mac OS X and iOS, it uses the `NSApplicationSupportDirectory` directory.@n Index: src/OFSystemInfo.m ================================================================== --- src/OFSystemInfo.m +++ src/OFSystemInfo.m @@ -89,10 +89,16 @@ + (size_t)numberOfCPUs { return numberOfCPUs; } + ++ (of_string_encoding_t)native8BitEncoding +{ + /* FIXME */ + return OF_STRING_ENCODING_UTF_8; +} + (OFString*)userDataPath { #if defined(__APPLE__) void *pool = objc_autoreleasePoolPush();