File: output.cpp

package info (click to toggle)
indigo 1.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 48,936 kB
  • sloc: ansic: 332,816; cpp: 169,470; python: 20,033; java: 13,701; cs: 9,979; asm: 8,475; sql: 6,743; xml: 6,354; javascript: 1,245; sh: 555; php: 506; makefile: 54
file content (376 lines) | stat: -rw-r--r-- 6,987 bytes parent folder | download
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/****************************************************************************
 * Copyright (C) from 2009 to Present EPAM Systems.
 *
 * This file is part of Indigo toolkit.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ***************************************************************************/

#include <errno.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

#include "base_c/defs.h"
#include "base_cpp/array.h"
#include "base_cpp/output.h"
#include "base_cpp/tlscont.h"

#ifndef va_copy
#define va_copy(d, s) ((d) = (s))
#endif

using namespace indigo;

IMPL_ERROR(Output, "output");

Output::Output()
{
}

Output::~Output()
{
}

void Output::writeBinaryInt(int value)
{
    // value = htonl(value);
    write(&value, sizeof(int));
}

void Output::writeBinaryDword(dword value)
{
    write(&value, sizeof(dword));
}

void Output::writeBinaryFloat(float value)
{
    write(&value, sizeof(float));
}

void Output::writeByte(byte value)
{
    write(&value, 1);
}

void Output::writeChar(char value)
{
    write(&value, 1);
}

void Output::writeBinaryWord(word value)
{
    // value = htons(value);
    write(&value, sizeof(word));
}

void Output::printf(const char* format, ...)
{
    va_list args;

    va_start(args, format);
    vprintf(format, args);
    va_end(args);
}

void Output::vprintf(const char* format, va_list args_orig)
{
    QS_DEF(Array<char>, str);
    if (str.size() < 2048)
        str.resize(2048);

    int n;
    while (true)
    {
        va_list args;

        // vsnprintf may change va_list argument that leads to segfault
        va_copy(args, args_orig);

#if defined(_WIN32) && !defined(__MINGW32__)
        n = _vsnprintf_l(str.ptr(), str.size(), format, getCLocale(), args);
#else
        n = vsnprintf(str.ptr(), str.size(), format, args);
#endif
        /* If that worked, return the string. */
        if (n > -1 && n < str.size())
            break;

        /* Else try again with more space. */
        int new_size;
        if (n > -1)                    /* glibc 2.1 */
            new_size = n + 1;          /* precisely what is needed */
        else                           /* glibc 2.0 */
            new_size = str.size() * 2; /* twice the old size */
        str.resize(new_size);
    }

    write(str.ptr(), n);
}

void Output::writeArray(const Array<char>& data)
{
    write(data.ptr(), data.size());
}

void Output::writeCR()
{
    writeChar('\n');
}

void Output::printfCR(const char* format, ...)
{
    va_list args;

    va_start(args, format);
    vprintf(format, args);
    va_end(args);

    writeCR();
}

void Output::writeString(const char* string)
{
    int n = (int)strlen(string);

    write(string, n);
}

void Output::writeStringCR(const char* string)
{
    writeString(string);
    writeCR();
}

void Output::writePackedShort(short value)
{
    byte low = value & 255;
    byte high = (value - low) >> 8;

    if (value < 128)
        writeByte(low);
    else
    {
        writeByte(high + 128);
        writeByte(low);
    }
}

void Output::writePackedUInt(unsigned int value)
{
    if (value == 0)
    {
        writeByte(0);
        return;
    }
    while (value > 0)
    {
        if (value >= 128)
            writeByte((value & 0x7F) | 0x80);
        else
            writeByte(value);
        value >>= 7;
    }
}

void Output::skip(int count)
{
    seek(count, SEEK_CUR);
}

FileOutput::FileOutput(Encoding filename_encoding, const char* filename)
{
    _file = openFile(filename_encoding, filename, "wb");

    if (_file == NULL)
        throw Error("can't open file %s. Error: %s", filename, strerror(errno));
}

FileOutput::FileOutput(const char* filename)
{
    _file = fopen(filename, "wb");

    if (_file == NULL)
        throw Error("can't open file %s. Error: %s", filename, strerror(errno));
}

FileOutput::FileOutput(bool append, const char* format, ...)
{
    char filename[1024];

    va_list args;

    va_start(args, format);
    vsnprintf(filename, sizeof(filename), format, args);
    va_end(args);

    if (append)
        _file = fopen(filename, "ab+");
    else
        _file = fopen(filename, "wb");

    if (_file == NULL)
        throw Error("can't open file %s. Error: %s", filename, strerror(errno));
}

FileOutput::~FileOutput()
{
    fclose(_file);
}

void FileOutput::write(const void* data, int size)
{
    if (size < 1)
        return;

    size_t res = fwrite(data, size, 1, _file);

    if (res != 1)
        throw Error("file write error in write()");
}

void FileOutput::flush()
{
    fflush(_file);
}

void FileOutput::seek(long long offset, int from)
{
#ifdef _WIN32
    _fseeki64(_file, offset, from);
#else
    fseeko(_file, offset, from);
#endif
}

long long FileOutput::tell()
{
#ifdef _WIN32
    return _ftelli64(_file);
#else
    return ftello(_file);
#endif
}

ArrayOutput::ArrayOutput(Array<char>& arr) : _arr(arr)
{
    _arr.clear();
}

ArrayOutput::~ArrayOutput()
{
}

void ArrayOutput::write(const void* data, int size)
{
    int old_size = _arr.size();

    _arr.resize(old_size + size);
    memcpy(_arr.ptr() + old_size, data, size);
}

long long ArrayOutput::tell()
{
    return _arr.size();
}

void ArrayOutput::flush()
{
}

void ArrayOutput::seek(long long offset, int from)
{
    throw Error("not implemented");
}

void ArrayOutput::clear()
{
    _arr.clear();
}

StandardOutput::StandardOutput()
{
    _count = 0;
}

StandardOutput::~StandardOutput()
{
}

void StandardOutput::write(const void* data, int size)
{
    if (size == 0)
        return;

    size_t res = fwrite(data, size, 1, stdout);

    if (res != 1)
        throw Error("error writing to standard output");

    _count += size;
}

void StandardOutput::seek(long long offset, int from)
{
    throw Error("can not seek in standard output");
}

long long StandardOutput::tell()
{
    return _count;
}

void StandardOutput::flush()
{
    fflush(stdout);
}

NullOutput::NullOutput()
{
}

NullOutput::~NullOutput()
{
}

void NullOutput::write(const void* data, int size)
{
}

void NullOutput::seek(long long offset, int from)
{
    throw Error("not implemented");
}

long long NullOutput::tell()
{
    throw Error("not implemented");
}

void NullOutput::flush()
{
}

namespace indigo
{
    void bprintf(Array<char>& buf, const char* format, ...)
    {
        va_list args;
        va_start(args, format);
        ArrayOutput output(buf);
        output.vprintf(format, args);
        output.writeChar(0);
        va_end(args);
    }
} // namespace indigo