@@ -24,11 +24,11 @@ * write() suddenly returns the number of characters - instead of bytes - * written and read() just returns 0 as soon as a Unicode character is being * read. * * Therefore, instead of just using the UTF-8 codepage, this captures all reads - * and writes to of_std{in,out,err} on the low level, interprets the buffer as + * and writes to OFStd{In,Out,Err} on the low level, interprets the buffer as * UTF-8 and converts to / from UTF-16 to use ReadConsoleW() / WriteConsoleW(). * Doing so is safe, as the console only supports text anyway and thus it does * not matter if binary gets garbled by the conversion (e.g. because invalid * UTF-8 gets converted to U+FFFD). * @@ -35,12 +35,10 @@ * In order to not do this when redirecting input / output to a file (as the * file would then be read / written in the wrong encoding and break reading / * writing binary), it checks that the handle is indeed a console. */ -#define OF_STDIO_STREAM_WIN32_CONSOLE_M - #include "config.h" #include #include #include @@ -58,24 +56,24 @@ #import "OFReadFailedException.h" #import "OFWriteFailedException.h" #include -static of_string_encoding_t +static OFStringEncoding codepageToEncoding(UINT codepage) { switch (codepage) { case 437: - return OF_STRING_ENCODING_CODEPAGE_437; + return OFStringEncodingCodepage437; case 850: - return OF_STRING_ENCODING_CODEPAGE_850; + return OFStringEncodingCodepage850; case 858: - return OF_STRING_ENCODING_CODEPAGE_858; + return OFStringEncodingCodepage858; case 1251: - return OF_STRING_ENCODING_WINDOWS_1251; + return OFStringEncodingWindows1251; case 1252: - return OF_STRING_ENCODING_WINDOWS_1252; + return OFStringEncodingWindows1252; default: @throw [OFInvalidEncodingException exception]; } } @@ -86,17 +84,17 @@ if (self != [OFWin32ConsoleStdIOStream class]) return; if ((fd = _fileno(stdin)) >= 0) - of_stdin = [[OFWin32ConsoleStdIOStream alloc] + OFStdIn = [[OFWin32ConsoleStdIOStream alloc] of_initWithFileDescriptor: fd]; if ((fd = _fileno(stdout)) >= 0) - of_stdout = [[OFWin32ConsoleStdIOStream alloc] + OFStdOut = [[OFWin32ConsoleStdIOStream alloc] of_initWithFileDescriptor: fd]; if ((fd = _fileno(stderr)) >= 0) - of_stderr = [[OFWin32ConsoleStdIOStream alloc] + OFStdErr = [[OFWin32ConsoleStdIOStream alloc] of_initWithFileDescriptor: fd]; } - (instancetype)of_initWithFileDescriptor: (int)fd { @@ -122,22 +120,21 @@ } return self; } -- (size_t)lowlevelReadIntoBuffer: (void *)buffer_ - length: (size_t)length +- (size_t)lowlevelReadIntoBuffer: (void *)buffer_ length: (size_t)length { void *pool = objc_autoreleasePoolPush(); char *buffer = buffer_; - of_char16_t *UTF16; + OFChar16 *UTF16; size_t j = 0; if (length > UINT32_MAX) @throw [OFOutOfRangeException exception]; - UTF16 = of_alloc(length, sizeof(of_char16_t)); + UTF16 = OFAllocMemory(length, sizeof(OFChar16)); @try { DWORD UTF16Len; OFMutableData *rest = nil; size_t i = 0; @@ -147,11 +144,11 @@ @throw [OFReadFailedException exceptionWithObject: self requestedLength: length * 2 errNo: EIO]; } else { - of_string_encoding_t encoding; + OFStringEncoding encoding; OFString *string; size_t stringLen; if (!ReadConsoleA(_handle, (char *)UTF16, (DWORD)length, &UTF16Len, NULL)) @@ -172,45 +169,44 @@ UTF16Len = (DWORD)stringLen; memcpy(UTF16, string.UTF16String, stringLen); } if (UTF16Len > 0 && _incompleteUTF16Surrogate != 0) { - of_unichar_t c = + OFUnichar c = (((_incompleteUTF16Surrogate & 0x3FF) << 10) | (UTF16[0] & 0x3FF)) + 0x10000; char UTF8[4]; size_t UTF8Len; - if ((UTF8Len = of_string_utf8_encode(c, UTF8)) == 0) + if ((UTF8Len = OFUTF8StringEncode(c, UTF8)) == 0) @throw [OFInvalidEncodingException exception]; if (UTF8Len <= length) { memcpy(buffer, UTF8, UTF8Len); j += UTF8Len; } else { if (rest == nil) rest = [OFMutableData data]; - [rest addItems: UTF8 - count: UTF8Len]; + [rest addItems: UTF8 count: UTF8Len]; } _incompleteUTF16Surrogate = 0; i++; } for (; i < UTF16Len; i++) { - of_unichar_t c = UTF16[i]; + OFUnichar c = UTF16[i]; char UTF8[4]; size_t UTF8Len; /* Missing high surrogate */ if ((c & 0xFC00) == 0xDC00) @throw [OFInvalidEncodingException exception]; if ((c & 0xFC00) == 0xD800) { - of_char16_t next; + OFChar16 next; if (UTF16Len <= i + 1) { _incompleteUTF16Surrogate = c; if (rest != nil) { @@ -236,58 +232,55 @@ 0x10000; i++; } - if ((UTF8Len = of_string_utf8_encode(c, UTF8)) == 0) + if ((UTF8Len = OFUTF8StringEncode(c, UTF8)) == 0) @throw [OFInvalidEncodingException exception]; if (j + UTF8Len <= length) { memcpy(buffer + j, UTF8, UTF8Len); j += UTF8Len; } else { if (rest == nil) rest = [OFMutableData data]; - [rest addItems: UTF8 - count: UTF8Len]; + [rest addItems: UTF8 count: UTF8Len]; } } if (rest != nil) - [self unreadFromBuffer: rest.items - length: rest.count]; + [self unreadFromBuffer: rest.items length: rest.count]; } @finally { - free(UTF16); + OFFreeMemory(UTF16); } objc_autoreleasePoolPop(pool); return j; } -- (size_t)lowlevelWriteBuffer: (const void *)buffer_ - length: (size_t)length +- (size_t)lowlevelWriteBuffer: (const void *)buffer_ length: (size_t)length { const char *buffer = buffer_; - of_char16_t *tmp; + OFChar16 *tmp; size_t i = 0, j = 0; if (length > SIZE_MAX / 2) @throw [OFOutOfRangeException exception]; if (_incompleteUTF8SurrogateLen > 0) { - of_unichar_t c; - of_char16_t UTF16[2]; + OFUnichar c; + OFChar16 UTF16[2]; ssize_t UTF8Len; size_t toCopy; DWORD UTF16Len, bytesWritten; - UTF8Len = -of_string_utf8_decode( + UTF8Len = -OFUTF8StringDecode( _incompleteUTF8Surrogate, _incompleteUTF8SurrogateLen, &c); - OF_ENSURE(UTF8Len > 0); + OFEnsure(UTF8Len > 0); toCopy = UTF8Len - _incompleteUTF8SurrogateLen; if (toCopy > length) toCopy = length; @@ -296,11 +289,11 @@ _incompleteUTF8SurrogateLen += toCopy; if (_incompleteUTF8SurrogateLen < (size_t)UTF8Len) return 0; - UTF8Len = of_string_utf8_decode( + UTF8Len = OFUTF8StringDecode( _incompleteUTF8Surrogate, _incompleteUTF8SurrogateLen, &c); if (UTF8Len <= 0 || c > 0x10FFFF) { assert(UTF8Len == 0 || UTF8Len < -4); @@ -329,11 +322,11 @@ } else { void *pool = objc_autoreleasePoolPush(); OFString *string = [OFString stringWithUTF16String: UTF16 length: UTF16Len]; - of_string_encoding_t encoding = + OFStringEncoding encoding = codepageToEncoding(GetConsoleOutputCP()); size_t nativeLen = [string cStringLengthWithEncoding: encoding]; if (nativeLen > UINT32_MAX) @@ -360,23 +353,23 @@ _incompleteUTF8SurrogateLen = 0; i += toCopy; } - tmp = of_alloc(length * 2, sizeof(of_char16_t)); + tmp = OFAllocMemory(length * 2, sizeof(OFChar16)); @try { DWORD bytesWritten; while (i < length) { - of_unichar_t c; + OFUnichar c; ssize_t UTF8Len; - UTF8Len = of_string_utf8_decode(buffer + i, length - i, + UTF8Len = OFUTF8StringDecode(buffer + i, length - i, &c); if (UTF8Len < 0 && UTF8Len >= -4) { - OF_ENSURE(length - i < 4); + OFEnsure(length - i < 4); memcpy(_incompleteUTF8Surrogate, buffer + i, length - i); _incompleteUTF8SurrogateLen = length - i; @@ -412,11 +405,11 @@ errNo: EIO]; } else { void *pool = objc_autoreleasePoolPush(); OFString *string = [OFString stringWithUTF16String: tmp length: j]; - of_string_encoding_t encoding = + OFStringEncoding encoding = codepageToEncoding(GetConsoleOutputCP()); size_t nativeLen = [string cStringLengthWithEncoding: encoding]; if (nativeLen > UINT32_MAX) @@ -439,11 +432,11 @@ exceptionWithObject: self requestedLength: j * 2 bytesWritten: bytesWritten * 2 errNo: 0]; } @finally { - free(tmp); + OFFreeMemory(tmp); } /* * We do not count in bytes when writing to the Win32 console. But * since any incomplete write is an exception here anyway, we can just @@ -489,14 +482,11 @@ return; csbi.wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY); - [color getRed: &red - green: &green - blue: &blue - alpha: NULL]; + [color getRed: &red green: &green blue: &blue alpha: NULL]; if (red >= 0.25) csbi.wAttributes |= FOREGROUND_RED; if (green >= 0.25) csbi.wAttributes |= FOREGROUND_GREEN; @@ -518,14 +508,11 @@ return; csbi.wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY); - [color getRed: &red - green: &green - blue: &blue - alpha: NULL]; + [color getRed: &red green: &green blue: &blue alpha: NULL]; if (red >= 0.25) csbi.wAttributes |= BACKGROUND_RED; if (green >= 0.25) csbi.wAttributes |= BACKGROUND_GREEN; @@ -591,19 +578,19 @@ csbi.dwCursorPosition.X = column; SetConsoleCursorPosition(_handle, csbi.dwCursorPosition); } -- (void)setCursorPosition: (of_point_t)position +- (void)setCursorPosition: (OFPoint)position { if (position.x < 0 || position.y < 0) @throw [OFInvalidArgumentException exception]; SetConsoleCursorPosition(_handle, (COORD){ position.x, position.y }); } -- (void)setRelativeCursorPosition: (of_point_t)position +- (void)setRelativeCursorPosition: (OFPoint)position { CONSOLE_SCREEN_BUFFER_INFO csbi; if (!GetConsoleScreenBufferInfo(_handle, &csbi)) return;