ObjFW  Diff

Differences From Artifact [b1b921ad96]:

To Artifact [e2adf5fa8a]:

  • File src/OFStdIOStream.m — part of check-in [3e1b6bccbc] at 2016-03-12 20:28:31 on branch trunk — Properly handle UTF-8 in Win32 console

    The previous way was to set the codepage to UTF-8, however, this does
    not work properly on some versions of Windows.

    Instead, this catches reads / writes on of_std* on the lowlevel,
    interprets it as UTF-8, converts it to / from UTF-16 and then uses
    ReadConsoleW() / WriteConsoleW().

    Surrogates being cut in the middle is not properly handled yet, this
    will be implemented in a follow up commit. (user: js, size: 4066) [annotate] [blame] [check-ins using]


22
23
24
25
26
27
28

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# undef __USE_XOPEN
#endif

#include <errno.h>
#include <unistd.h>

#import "OFStdIOStream.h"

#import "OFDate.h"
#import "OFApplication.h"

#import "OFOutOfRangeException.h"
#import "OFReadFailedException.h"
#import "OFWriteFailedException.h"

#ifdef OF_WINDOWS
# include <windows.h>
#endif

OFStdIOStream *of_stdin = nil;
OFStdIOStream *of_stdout = nil;
OFStdIOStream *of_stderr = nil;

#ifdef OF_WINDOWS
UINT originalConsoleCP;
UINT originalConsoleOutputCP;

static void
restoreCodepage(void)
{
	SetConsoleCP(originalConsoleCP);
	SetConsoleOutputCP(originalConsoleOutputCP);
}
#endif

@interface OFStdIOStream ()
- (instancetype)OF_initWithFileDescriptor: (int)fd;
@end

void
of_log(OFConstantString *format, ...)
{
	void *pool = objc_autoreleasePoolPush();
	OFDate *date;
	OFString *dateString, *me, *msg;
	va_list arguments;







>







<
<
<
<




<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







22
23
24
25
26
27
28
29
30
31
32
33
34
35
36




37
38
39
40
















41
42
43
44
45
46
47
# undef __USE_XOPEN
#endif

#include <errno.h>
#include <unistd.h>

#import "OFStdIOStream.h"
#import "OFStdIOStream+Private.h"
#import "OFDate.h"
#import "OFApplication.h"

#import "OFOutOfRangeException.h"
#import "OFReadFailedException.h"
#import "OFWriteFailedException.h"





OFStdIOStream *of_stdin = nil;
OFStdIOStream *of_stdout = nil;
OFStdIOStream *of_stderr = nil;

















void
of_log(OFConstantString *format, ...)
{
	void *pool = objc_autoreleasePoolPush();
	OFDate *date;
	OFString *dateString, *me, *msg;
	va_list arguments;
77
78
79
80
81
82
83

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
	[of_stderr writeFormat: @"[%@.%03d %@(%d)] %@\n", dateString,
				[date microsecond] / 1000, me, getpid(), msg];

	objc_autoreleasePoolPop(pool);
}

@implementation OFStdIOStream

+ (void)load
{
	of_stdin = [[OFStdIOStream alloc] OF_initWithFileDescriptor: 0];
	of_stdout = [[OFStdIOStream alloc] OF_initWithFileDescriptor: 1];
	of_stderr = [[OFStdIOStream alloc] OF_initWithFileDescriptor: 2];
}

#ifdef OF_WINDOWS
+ (void)initialize
{
	originalConsoleCP = GetConsoleCP();
	originalConsoleOutputCP = GetConsoleOutputCP();
	atexit(restoreCodepage);

	SetConsoleCP(CP_UTF8);
	SetConsoleOutputCP(CP_UTF8);
}
#endif

- init
{
	OF_INVALID_INIT_METHOD
}








>






<
<
<
<
<
<
<
<
<
<
<







58
59
60
61
62
63
64
65
66
67
68
69
70
71











72
73
74
75
76
77
78
	[of_stderr writeFormat: @"[%@.%03d %@(%d)] %@\n", dateString,
				[date microsecond] / 1000, me, getpid(), msg];

	objc_autoreleasePoolPop(pool);
}

@implementation OFStdIOStream
#ifndef OF_WINDOWS
+ (void)load
{
	of_stdin = [[OFStdIOStream alloc] OF_initWithFileDescriptor: 0];
	of_stdout = [[OFStdIOStream alloc] OF_initWithFileDescriptor: 1];
	of_stderr = [[OFStdIOStream alloc] OF_initWithFileDescriptor: 2];
}











#endif

- init
{
	OF_INVALID_INIT_METHOD
}