aboutsummaryrefslogtreecommitdiffstats
path: root/util/stream/printf.cpp
blob: 0bc3aae988db12bbd621a3cf4e597031b0189a76 (plain) (blame)
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
#include "output.h"
#include "printf.h"

#include <util/generic/scope.h>
#include <util/memory/tempbuf.h>

size_t Printf(IOutputStream& out, const char* fmt, ...) {
    va_list lst;
    va_start(lst, fmt);

    Y_DEFER {
        va_end(lst);
    };

    return Printf(out, fmt, lst);
}

static inline size_t TryPrintf(void* ptr, size_t len, IOutputStream& out, const char* fmt, va_list params) {
    va_list lst;
    va_copy(lst, params);
    const int ret = vsnprintf((char*)ptr, len, fmt, lst);
    va_end(lst);

    if (ret < 0) {
        return len;
    }

    if ((size_t)ret < len) {
        out.Write(ptr, (size_t)ret);
    }

    return (size_t)ret;
}

size_t Printf(IOutputStream& out, const char* fmt, va_list params) {
    size_t guess = 0;

    while (true) {
        TTempBuf tmp(guess);
        const size_t ret = TryPrintf(tmp.Data(), tmp.Size(), out, fmt, params);

        if (ret < tmp.Size()) {
            return ret;
        }

        guess = Max(tmp.Size() * 2, ret + 1);
    }

    return 0;
}