File size: 7,144 Bytes
e5a8102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright 2022-2023 The Khronos Group Inc.
// Copyright 2022-2023 RasterGrid Kft.
// SPDX-License-Identifier: Apache-2.0


#include "command.h"
#include "platform_utils.h"
#include "version.h"
#include "ktx.h"
#include "sbufstream.h"
#include <stdio.h>
#include <filesystem>
#include <iostream>

#include <fmt/ostream.h>
#include <fmt/printf.h>

// -------------------------------------------------------------------------------------------------

#define QUOTE(x) #x
#define STR(x) QUOTE(x)

namespace ktx {

void Command::parseCommandLine(const std::string& name, const std::string& desc, int argc, char* argv[]) {
    commandName = name;
    commandDescription = desc;

    cxxopts::Options commandOpts(name, "");
    commandOpts.custom_help("[OPTION...]");
    commandOpts.set_width(CONSOLE_USAGE_WIDTH);
    initOptions(commandOpts); // virtual customization point

    cxxopts::ParseResult args;
    try {
        args = commandOpts.parse(argc, argv);
        processOptions(commandOpts, args); // virtual customization point

    } catch (const cxxopts::exceptions::parsing& ex) {
        fatal_usage("{}.", ex.what());
    }

#if defined(_WIN32) && defined(DEBUG)
    if (args["ld"].as<bool>())
        launchDebugger();
#endif
}

#if defined(_WIN32) && defined(DEBUG)
// For use when debugging stdin with Visual Studio which does not have a
// "wait for executable to be launched" choice in its debugger settings.
bool Command::launchDebugger()
{
    // Get System directory, typically c:\windows\system32
    std::wstring systemDir(MAX_PATH + 1, '\0');
    UINT nChars = GetSystemDirectoryW(&systemDir[0],
                                static_cast<UINT>(systemDir.length()));
    if (nChars == 0) return false; // failed to get system directory
    systemDir.resize(nChars);

    // Get process ID and create the command line
    DWORD pid = GetCurrentProcessId();
    std::wostringstream s;
    s << systemDir << L"\\vsjitdebugger.exe -p " << pid;
    std::wstring cmdLine = s.str();

    // Start debugger process
    STARTUPINFOW si;
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);

    PROCESS_INFORMATION pi;
    ZeroMemory(&pi, sizeof(pi));

    if (!CreateProcessW(NULL, &cmdLine[0], NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) return false;

    // Close debugger process handles to eliminate resource leak
    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);

    // Wait for the debugger to attach
    while (!IsDebuggerPresent()) Sleep(100);

    // Stop execution so the debugger can take over
    DebugBreak();
    return true;
}
#endif

std::string version(bool testrun) {
    return testrun ? STR(KTX_DEFAULT_VERSION) : STR(KTX_VERSION);
}

// -------------------------------------------------------------------------------------------------

InputStream::InputStream(const std::string& filepath, Reporter& report) :
        filepath(filepath) {
    if (filepath == "-") {
        #if defined(_WIN32)
        // Set "stdin" to binary mode
        const auto setmodeResult = _setmode(_fileno(stdin), _O_BINARY);
        if (setmodeResult == -1)
            report.fatal(rc::IO_FAILURE, "Failed to set stdin mode to binary: {}.", setmodeResult);
        // #else
        // std::freopen(nullptr, "rb", stdin);
        #endif

        // Read everything from stdin into memory to enable random access
        stdinBuffer << std::cin.rdbuf();
        activeStream = &stdinBuffer;
    } else {
        file.open(DecodeUTF8Path(filepath).c_str(), std::ios::binary | std::ios::in);
        if (!file)
            report.fatal(rc::IO_FAILURE, "Could not open input file \"{}\": {}.", filepath, errnoMessage());
        activeStream = &file;
    }
}

// -------------------------------------------------------------------------------------------------

OutputStream::OutputStream(const std::string& filepath, Reporter& report) :
        filepath(filepath) {

    if (filepath == "-") {
        #if defined(_WIN32)
        // Set "stdout" to binary mode
        const auto setmodeResult = _setmode(_fileno(stdout), _O_BINARY);
        if (setmodeResult == -1)
            report.fatal(rc::IO_FAILURE, "Failed to set stdout mode to binary: {}.", setmodeResult);
        // #else
        // std::freopen(nullptr, "wb", stdout);
        #endif
        file = stdout;
    } else {
#if defined(_WIN32)
        file = _wfopen(DecodeUTF8Path(filepath).c_str(), L"wb");
#else
        file = fopen(DecodeUTF8Path(filepath).c_str(), "wb");
#endif
        if (!file)
            report.fatal(rc::IO_FAILURE, "Could not open output file \"{}\": {}.", filepath, errnoMessage());
    }

    // TODO: Investigate and resolve the portability issue with the C++ streams. The issue will most likely
    //       be in StreambufStream's position reporting and seeking. Currently a fallback is implemented in C above.
    // if (filepath == "-") {
    //     #if defined(_WIN32)
    //     // Set "stdout" to binary mode
    //     const auto setmodeResult = _setmode(_fileno(stdout), _O_BINARY);
    //     if (setmodeResult == -1)
    //         report.fatal(rc::IO_FAILURE, "Failed to set stdout mode to binary: {}", setmodeResult);
    //     // #else
    //     // std::freopen(nullptr, "wb", stdout);
    //     #endif
    //     activeStream = &std::cout;
    // } else {
    //     file.open(DecodeUTF8Path(filepath).c_str(), std::ios::binary | std::ios::out);
    //     if (!file)
    //         report.fatal(rc::IO_FAILURE, "Could not open output file \"{}\": {}", filepath, errnoMessage());
    //     activeStream = &file;
    // }
}

OutputStream::~OutputStream() {
    if (file != stdout)
        fclose(file);
}

void OutputStream::write(const char* data, std::size_t size, Reporter& report) {
    const auto written = std::fwrite(data, 1, size, file);
    if (written != size)
        report.fatal(rc::IO_FAILURE, "Failed to write output file \"{}\": {}.", fmtOutFile(filepath), errnoMessage());
}

void OutputStream::writeKTX2(ktxTexture* texture, Reporter& report) {
    const auto ret = ktxTexture_WriteToStdioStream(texture, file);
    if (KTX_SUCCESS != ret) {
        if (file != stdout)
            std::filesystem::remove(DecodeUTF8Path(filepath).c_str());
        report.fatal(rc::IO_FAILURE, "Failed to write KTX file \"{}\": KTX error: {}.", filepath, ktxErrorString(ret));
    }

    // TODO: Investigate and resolve the portability issue with the C++ streams. The issue will most likely
    //       be in StreambufStream's position reporting and seeking. Currently a fallback is implemented in C above.
    // StreambufStream<std::streambuf*> stream(activeStream->rdbuf(), std::ios::in | std::ios::binary);
    // const auto ret = ktxTexture_WriteToStream(texture, stream.stream());
    //
    // if (KTX_SUCCESS != ret) {
    //     if (activeStream != &std::cout)
    //         std::filesystem::remove(DecodeUTF8Path(filepath).c_str());
    //     report.fatal(rc::IO_FAILURE, "Failed to write KTX file \"{}\": {}.", fmtOutFile(filepath), ktxErrorString(ret));
    // }
}

// -------------------------------------------------------------------------------------------------

} // namespace ktx