summaryrefslogtreecommitdiff
path: root/utils/Win32Console.h
blob: a1ef6efc0bcd46a2e495eb171ebf1ef2191c3fe4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
67
68
69
70
//========================================================================
//
// Win32Console.h
//
// This file is licensed under the GPLv2 or later
//
// Copyright (C) 2017 Adrian Johnson <ajohnson@redneon.com>
// Copyright (C) 2019 Albert Astals Cid <aacid@kde.org>
// Copyright (C) 2019 Oliver Sander <oliver.sander@tu-dresden.de>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================

#ifndef WIN32CONSOLE_H
#define WIN32CONSOLE_H

// UTF-8 Support for win32 console
//
// Converts argc/argv to UTF-8. Supports UTF-8 stdout/stderr to win32 console.
// On other platforms this class is a no-op.

#ifdef _WIN32

// Ensure stdio.h is included before redefining stdio functions. We need to provide
// our own declarations for the redefined functions because win32 stdio.h functions
// have DLL export decorations.
#include <cstdio>

#ifndef WIN32_CONSOLE_IMPL // don't redefine in Win32Console.cc so we can call original functions
#define printf(...)  win32_fprintf(stdout, __VA_ARGS__)
#define fprintf(stream, ...) win32_fprintf(stream, __VA_ARGS__)
#define puts(s) win32_fprintf(stdout, "%s\n", s)
#define fputs(s, stream) win32_fprintf(stream, "%s", s)
#define putc(c) win32_fprintf(stdout, "%c", c)
#define putchar(c) win32_fprintf(stdout, "%c", c)
#define fputc(c, stream) win32_fprintf(stream, "%c", c)
#define fwrite(ptr, size, nmemb, stream) win32_fwrite(ptr, size, nmemb, stream)
#endif

extern "C" {
  int win32_fprintf(FILE *stream, ...);
  size_t win32_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
}

class Win32Console
{
public:
  Win32Console(int *argc, char **argv[]);
  ~Win32Console();
private:
  int numArgs;
  char **argList;
  char **privateArgList;
};

#else

// On other platforms this class is a no-op.

class Win32Console
{
public:
  Win32Console(int *argc, char ***argv) {}
};

#endif // _WIN32

#endif