summaryrefslogtreecommitdiffstats
path: root/vespamalloc/src/vespamalloc/util/stream.cpp
blob: dc0692350d168425536e5ca0e4d624f3f4ad5b7b (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
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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "stream.h"
#include <algorithm>
#include <stdio.h>

namespace vespamalloc {

asciistream::asciistream() :
    _rPos(0),
    _wPos(0),
    _buffer(static_cast<char *>(malloc(1024))),
    _sz(1024)
{
}

asciistream::~asciistream()
{
    free(_buffer);
    _buffer = nullptr;
}

asciistream::asciistream(const asciistream & rhs) :
    _rPos(0),
    _wPos(rhs._wPos - rhs._rPos),
    _buffer(static_cast<char *>(malloc(_wPos+1))),
    _sz(_wPos)
{
    memcpy(_buffer, (rhs._buffer + rhs._rPos), _sz);
    _buffer[_wPos] = 0;
}

asciistream & asciistream::operator = (const asciistream & rhs)
{
    if (this != &rhs) {
        asciistream newStream(rhs);
        swap(newStream);
    }
    return *this;
}

void asciistream::swap(asciistream & rhs)
{
    std::swap(_rPos, rhs._rPos);
    std::swap(_wPos, rhs._wPos);
    std::swap(_buffer, rhs._buffer);
    std::swap(_sz, rhs._sz);
}

asciistream & asciistream::operator << (int32_t v)
{
    char tmp[16];
    int len = snprintf(tmp, sizeof(tmp), "%d", v);
    write(tmp, len);
    return *this;
}

asciistream & asciistream::operator << (uint32_t v)
{
    char tmp[16];
    int len = snprintf(tmp, sizeof(tmp), "%u", v);
    write(tmp, len);
    return *this;
}

asciistream & asciistream::operator << (int64_t v)
{
    char tmp[32];
    int len = snprintf(tmp, sizeof(tmp), "%ld", v);
    write(tmp, len);
    return *this;
}

asciistream & asciistream::operator << (uint64_t v)
{
    char tmp[32];
    int len = snprintf(tmp, sizeof(tmp), "%lu", v);
    write(tmp, len);
    return *this;
}

asciistream & asciistream::operator << (float v)
{
    char tmp[64];
    int len = snprintf(tmp, sizeof(tmp), "%g", v);
    write(tmp, len);
    return *this;
}

asciistream & asciistream::operator << (double v)
{
    char tmp[64];
    int len = snprintf(tmp, sizeof(tmp), "%g", v);
    write(tmp, len);
    return *this;
}

void asciistream::write(const void * buf, size_t len)
{
    if (_rPos == _wPos) {
        _rPos = _wPos = 0;
    }
    if ((_sz - _wPos) < len + 1) {
        _buffer = static_cast<char *>(realloc(_buffer, _sz * 2 + len));
        _sz = _sz * 2 + len + 1;
    }
    memcpy(_buffer + _wPos, buf, len);
    _wPos += len;
    _buffer[_wPos] = 0;
}

size_t asciistream::read(void * buf, size_t len)
{
    size_t available = _wPos - _rPos;
    size_t toRead(std::min(len, available));
    memcpy(buf, _buffer+_rPos, toRead);
    _rPos += toRead;
    return toRead;
}

}