1 //========================================================================
2 //
3 // Win32Console.h
4 //
5 // This file is licensed under the GPLv2 or later
6 //
7 // Copyright (C) 2017 Adrian Johnson <ajohnson@redneon.com>
8 // Copyright (C) 2019 Albert Astals Cid <aacid@kde.org>
9 // Copyright (C) 2019 Oliver Sander <oliver.sander@tu-dresden.de>
10 //
11 // To see a description of the changes please see the Changelog file that
12 // came with your tarball or type make ChangeLog if you are building from git
13 //
14 //========================================================================
15 
16 #ifndef WIN32CONSOLE_H
17 #define WIN32CONSOLE_H
18 
19 // UTF-8 Support for win32 console
20 //
21 // Converts argc/argv to UTF-8. Supports UTF-8 stdout/stderr to win32 console.
22 // On other platforms this class is a no-op.
23 
24 #ifdef _WIN32
25 
26 // Ensure stdio.h is included before redefining stdio functions. We need to provide
27 // our own declarations for the redefined functions because win32 stdio.h functions
28 // have DLL export decorations.
29 #    include <cstdio>
30 
31 #    ifndef WIN32_CONSOLE_IMPL // don't redefine in Win32Console.cc so we can call original functions
32 #        define printf(...) win32_fprintf(stdout, __VA_ARGS__)
33 #        define fprintf(stream, ...) win32_fprintf(stream, __VA_ARGS__)
34 #        define puts(s) win32_fprintf(stdout, "%s\n", s)
35 #        define fputs(s, stream) win32_fprintf(stream, "%s", s)
36 #        define putc(c) win32_fprintf(stdout, "%c", c)
37 #        define putchar(c) win32_fprintf(stdout, "%c", c)
38 #        define fputc(c, stream) win32_fprintf(stream, "%c", c)
39 #        define fwrite(ptr, size, nmemb, stream) win32_fwrite(ptr, size, nmemb, stream)
40 #    endif
41 
42 extern "C" {
43 int win32_fprintf(FILE *stream, ...);
44 size_t win32_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
45 }
46 
47 class Win32Console
48 {
49 public:
50     Win32Console(int *argc, char **argv[]);
51     ~Win32Console();
52 
53 private:
54     int numArgs;
55     char **argList;
56     char **privateArgList;
57 };
58 
59 #else
60 
61 // On other platforms this class is a no-op.
62 
63 class Win32Console
64 {
65 public:
Win32Console(int * argc,char *** argv)66     Win32Console(int *argc, char ***argv) { }
67 };
68 
69 #endif // _WIN32
70 
71 #endif
72