summaryrefslogtreecommitdiffstats
path: root/source/core/slang-writer.cpp
blob: 4c5df0c8a2fdfa4efd92ec389e32e8e8cfffd993 (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
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
#include "slang-writer.h"

#include "platform.h"
#include "slang-string-util.h"

// Includes to allow us to control console
// output when writing assembly dumps.
#include <fcntl.h>
#ifdef _WIN32
#   include <io.h>
#else
#   include <unistd.h>
#endif

#include <stdarg.h>

namespace Slang
{
static const Guid IID_ISlangWriter = SLANG_UUID_ISlangWriter;
static const Guid IID_ISlangUnknown = SLANG_UUID_ISlangUnknown;

/* !!!!!!!!!!!!!!!!!!!!!!!!! WriterHelper !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/

SlangResult WriterHelper::print(const char* format, ...)
{
    va_list args;
    va_start(args, format);
    SlangResult res = m_writer->writeVaList(format, args);

    if (res == SLANG_E_NOT_IMPLEMENTED)
    {
        StringBuilder builder;
        StringUtil::append(format, args, builder);

        // Write if there is anything to write
        res = (builder.Length()) ? m_writer->write(builder.Buffer(), builder.Length()) : SLANG_OK;
    }

    va_end(args);
    return res;
}

SlangResult WriterHelper::put(const char* text)
{
    return m_writer->write(text, ::strlen(text));
}

/* !!!!!!!!!!!!!!!!!!!!!!!!! BaseWriter !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/

ISlangUnknown* BaseWriter::getInterface(const Guid& guid)
{
    return (guid == IID_ISlangUnknown || guid == IID_ISlangWriter) ? static_cast<ISlangWriter*>(this) : nullptr;
}

/* !!!!!!!!!!!!!!!!!!!!!!!!! CallbackWriter !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/

SlangResult CallbackWriter::write(const char* chars, size_t numChars)
{
    if (numChars > 0)
    {
        // Make sure zero terminated
        StringBuilder builder;
        builder.Append(chars, numChars);

        m_callback(builder.Buffer(), (void*)m_data);
    }

    return SLANG_OK;
}

/* !!!!!!!!!!!!!!!!!!!!!!!!! FileWriter !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/

FileWriter::~FileWriter()
{
    if ((m_flags & WriterFlag::IsUnowned) == 0)
    {
        fclose(m_file);
    }
}

SlangResult FileWriter::writeVaList(const char* format, va_list args)
{
    // http://www.cplusplus.com/reference/cstdio/vfprintf/
    ::vfprintf(m_file, format, args);

    if (m_flags & WriterFlag::AutoFlush)
    {
        ::fflush(m_file);
    }

    return SLANG_OK;
}

SlangResult FileWriter::write(const char* text, size_t numChars)
{
    const size_t numWritten = ::fwrite(text, sizeof(char), numChars, m_file);
    if (m_flags & WriterFlag::AutoFlush)
    {
        ::fflush(m_file);
    }
    return numChars == numWritten ? SLANG_OK : SLANG_FAIL;
}

void FileWriter::flush()
{
    ::fflush(m_file);
}

/* static */bool FileWriter::isConsole(FILE* file)
{
    const int stdoutFileDesc = _fileno(file);
    return _isatty(stdoutFileDesc) != 0;
}

SlangResult FileWriter::setMode(SlangWriterMode mode)
{
    switch (mode)
    {
    case SLANG_WRITER_MODE_BINARY:
    {
#ifdef _WIN32
        int stdoutFileDesc = _fileno(m_file);
        _setmode(stdoutFileDesc, _O_BINARY);
        return SLANG_OK;
#else
        break;
#endif
    }
    default: break;
    }
    return SLANG_FAIL;
}

/* !!!!!!!!!!!!!!!!!!!!!!!!! StringWriter !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/

SlangResult StringWriter::writeVaList(const char* format, va_list args)
{
    StringUtil::append(format, args, *m_builder);
    return SLANG_OK;
}

SlangResult StringWriter::write(const char* chars, size_t numChars)
{
    if (numChars > 0)
    {
        m_builder->Append(chars, numChars);
    }
    return SLANG_OK;
}

}