Debian Codegraph @

melder/melder_console.cpp

raw symbols SCIP
1 /* melder_console.cpp
2 *
3 * Copyright (C) 1992-2018,2020,2022,2024 Paul Boersma
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "melder.h"cxx . . $ `<file>/melder/melder.h`/
20 #ifdef _WIN32
21 #include <windows.h>
22 #include <fcntl.h>
23 #include <io.h>
24 #include <assert.h>
25 #endif
26
27 /*
28 MelderConsole is the interface through which Melder_casual writes to
29 the console (on stderr), and through which Melder_info writes to the
30 console (on stdout) if it does not write to the GUI.
31 */
32
33 namespace MelderConsolecxx . . $ MelderConsole/ {// reopen
34 /*
35 On Windows, the console ("command prompt") understands UTF-16,
36 independently of the selected code page, so we use UTF-16 by default.
37 We could use UTF-8 as well, but this will work only if the code page
38 is 65001 (i.e. one would have to type "chcp 65001" into the console).
39 In redirection cases (pipes or files), the encoding will often have
40 to be different.
41
42 On Unix-like platforms (MacOS and Linux), the console understands UTF-8,
43 so we use UTF-8 by default. Other programs are usually fine handling UTF-8,
44 so we are probably good even in the context of pipes or redirection.
45 */
46 MelderConsolecxx . . $ MelderConsole/::Encodingcxx . . $ MelderConsole/Encoding# encodingcxx . . $ MelderConsole/encoding. =
47 #if defined (_WIN32)
48 MelderConsole::Encoding::UTF16;
49 #else
50 MelderConsolecxx . . $ MelderConsole/::Encodingcxx . . $ MelderConsole/Encoding#::UTF8cxx . . $ MelderConsole/Encoding#UTF8.;
51 #endif
52 }
53
54 void MelderConsolecxx . . $ MelderConsole/::setEncodingcxx . . $ MelderConsole/setEncoding(b30133ee0cddd54b). (MelderConsolecxx . . $ MelderConsole/::Encodingcxx . . $ MelderConsole/Encoding# newEncoding) {
55 MelderConsolecxx . . $ MelderConsole/ :: encodingcxx . . $ MelderConsole/encoding. = newEncoding;
56 }
57
58 /*
59 stdout and stderr should be kept distinct. For instance, if you do
60 praat test.praat > out.txt
61 the output of Melder_info should go to the file `out.txt`,
62 whereas the output of Melder_casual should go to the console or terminal.
63
64 On MacOS and Linux this requirement is satisfied by default,
65 but on Windows satisfying this requirement involves some work.
66 */
67
68 FILE *Melder_stdoutcxx . . $ Melder_stdout., *Melder_stderrcxx . . $ Melder_stderr.;
69
70 void MelderConsole_initcxx . . $ MelderConsole_init(49f6e7a06ebc5aa8). () {
71 tracecxx . . $ `melder/melder_debug.h:60:10`! (U"init");
72 #if defined (_WIN32)
73 /*
74 Stdout and stderr are initialized automatically if we are redirected to a pipe or file.
75 Stdout and stderr are not initialized, however, if Praat is started from the console,
76 neither in GUI mode nor in console mode; in these latter cases,
77 we manually attach Melder_stdout and Melder_stderr to the calling console.
78 */
79 const bool stdoutHasBeenInitialized = ( _fileno (stdout) >= 0 ); // usual with pipe or file
80 if (! stdoutHasBeenInitialized) {
81 /*
82 Don't change the following four lines into
83 freopen ("CONOUT$", "w", stream);
84 because if you did that, the distinction between stdout and stderr would be lost.
85 */
86 const HANDLE osfStdoutHandle = GetStdHandle (STD_OUTPUT_HANDLE);
87 if (osfStdoutHandle) {
88 const int fileDescriptor = _open_osfhandle ((intptr_t) osfStdoutHandle, _O_TEXT);
89 Melder_assert (fileDescriptor != 0);
90 FILE *f = _fdopen (fileDescriptor, "w");
91 if (f) { // usually false under Cygwin
92 #if defined (__clang__)
93 Melder_stdout = f; // usual under MSYS with Clang
94 #else
95 *stdout = *f; // usual under GCC (MSYS or Cygwin for Windows)
96 #endif
97 }
98 }
99 };
100 const bool stderrHasBeenInitialized = ( _fileno (stderr) >= 0 ); // usual with pipe or file
101 if (! stderrHasBeenInitialized) {
102 /*
103 Don't change the following four lines into
104 freopen ("CONOUT$", "w", stream);
105 because if you did that, the distinction between stdout and stderr would be lost.
106 */
107 const HANDLE osfStderrHandle = GetStdHandle (STD_ERROR_HANDLE);
108 if (osfStderrHandle) {
109 const int fileDescriptor = _open_osfhandle ((intptr_t) osfStderrHandle, _O_TEXT);
110 Melder_assert (fileDescriptor != 0);
111 FILE *f = _fdopen (fileDescriptor, "w");
112 if (f) { // usually false under Cygwin
113 #if defined (__clang__)
114 Melder_stderr = f; // usual under MSYS with Clang
115 #else
116 *stderr = *f; // usual under GCC (MSYS or Cygwin for Windows)
117 #endif
118 }
119 }
120 };
121 #endif
122 if (! Melder_stdoutcxx . . $ Melder_stdout.)
123 Melder_stdoutcxx . . $ Melder_stdout. = stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!;
124 if (! Melder_stderrcxx . . $ Melder_stderr.)
125 Melder_stderrcxx . . $ Melder_stderr. = stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!;
126 }
127
128 void MelderConsolecxx . . $ MelderConsole/::writecxx . . $ MelderConsole/write(7004b68cc6c23ea2). (conststring32cxx . . $ conststring32# message, bool useStderr) {
129 if (! message)
130 return;
131 FILE *f = useStderr ? Melder_stderrcxx . . $ Melder_stderr. : Melder_stdoutcxx . . $ Melder_stdout.;
132 if (MelderConsolecxx . . $ MelderConsole/ :: encodingcxx . . $ MelderConsole/encoding. == Encodingcxx . . $ MelderConsole/Encoding#::UTF16cxx . . $ MelderConsole/Encoding#UTF16.) {
133 #if defined (_WIN32)
134 fflush (f);
135 int savedMode = _setmode (_fileno (f), _O_U16TEXT); // without line-break translation
136 #endif
137 fwprintf (f, L"%ls", Melder_peek32to16 (message)); // with line-break translation (Windows)
138 fflush (f);
139 #if defined (_WIN32)
140 _setmode (_fileno (f), savedMode);
141 #endif
142 } else if (MelderConsolecxx . . $ MelderConsole/ :: encodingcxx . . $ MelderConsole/encoding. == Encodingcxx . . $ MelderConsole/Encoding#::UTF8cxx . . $ MelderConsole/Encoding#UTF8.) {
143 for (const char32cxx . . $ char32# *p = & message [0]; *p != U'\0'; p ++) {
144 char32cxx . . $ char32# kar = *p;
145 if (kar <= 0x00'007F) {
146 fputc ((int) kar, f); // because fputc wants an int instead of a uint8 (guarded conversion)
147 } else if (kar <= 0x00'07FF) {
148 fputc (0xC0 | (kar >> 6), f);
149 fputc (0x80 | (kar & 0x00'003F), f);
150 } else if (kar <= 0x00'FFFF) {
151 fputc (0xE0 | (kar >> 12), f);
152 fputc (0x80 | ((kar >> 6) & 0x00'003F), f);
153 fputc (0x80 | (kar & 0x00'003F), f);
154 } else {
155 fputc (0xF0 | (kar >> 18), f);
156 fputc (0x80 | ((kar >> 12) & 0x00'003F), f);
157 fputc (0x80 | ((kar >> 6) & 0x00'003F), f);
158 fputc (0x80 | (kar & 0x00'003F), f);
159 }
160 }
161 fflush (f);
162 } else if (MelderConsolecxx . . $ MelderConsole/ :: encodingcxx . . $ MelderConsole/encoding. == Encodingcxx . . $ MelderConsole/Encoding#::ANSIcxx . . $ MelderConsole/Encoding#ANSI.) {
163 const integercxx . . $ integer# n = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (message);
164 for (integercxx . . $ integer# i = 0; i < n; i ++) {
165 /*
166 We convert Unicode to ISO 8859-1 by simple truncation. This loses information.
167 */
168 unsigned int kar = message [i] & 0x00'00FF;
169 fputc ((int) kar, f);
170 }
171 fflush (f);
172 } else {
173 // should not happen
174 }
175 }
176
177 /* End of file melder_console.cpp */