aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/core/yql_csv.cpp
blob: 259b6144f1bb55ca6d701d549868abd319b85d0a (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
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
#include "yql_csv.h"

#include <util/string/split.h>
#include <util/string/escape.h>

namespace {

const char QUOTE_CH = '"';
const char ESCAPE_CH = '\\';
const char NULL_CH = '\0';

class TCsvLineParser {
public:
    TCsvLineParser(const TStringBuf& line, char delimiter)
        : Cur_(line.begin())
        , End_(line.end())
        , Delim_(delimiter)
        , Prev_(NULL_CH) /* must differ from ESCAPE_CH */
    {
    }

    bool Next(TStringBuf& token) {
        if (Cur_ > End_) return false;

        const char* tokenStart = Cur_;
        while (Cur_ < End_ && (Prev_ == ESCAPE_CH || *Cur_ != QUOTE_CH) && *Cur_ != Delim_) Prev_ = *Cur_++;

        if (Cur_ == End_) {
            token = { tokenStart, Cur_ };
            Prev_ = NULL_CH;
            Cur_++;
            return true;
        }

        if (Prev_ != ESCAPE_CH && *Cur_ == QUOTE_CH) {
            Prev_ = *Cur_++;
            tokenStart = Cur_;
            while (Cur_ < End_) {
                // find non escaped quote char
                if (*Cur_ == QUOTE_CH && Prev_ != ESCAPE_CH) break;
                Prev_ = *Cur_++;
            }

            if (Cur_ == End_)
                ythrow yexception() << "expected closing \"";

            token = { tokenStart, Cur_ };
            Prev_ = *Cur_++; // skip closing quote char

            if (Cur_ != End_ && *Cur_ != Delim_)
                ythrow yexception() << "expected end of line or delimiter";

            Prev_ = *Cur_++; // move out of buffer or skip delimiter
            return true;
        } else if (*Cur_ == Delim_) {
            token = { tokenStart, Cur_ };
            Prev_ = *Cur_++;
            return true;
        }

        return false;
    }

private:
    const char* Cur_;
    const char* End_;
    const char  Delim_;
    char Prev_;
};

} // namspace

namespace NYql {
namespace NUtils {

///////////////////////////////////////////////////////////////////////////////
// TCsvInputStream
///////////////////////////////////////////////////////////////////////////////
TCsvInputStream::TCsvInputStream(IInputStream& slave, char delimiter)
    : Slave_(slave)
    , Delim_(delimiter)
{
}

TVector<TString> TCsvInputStream::ReadLine()
{
    TVector<TString> parts;
    TString line;

    if (Slave_.ReadLine(line)) {
        TCsvLineParser lineParser(line, Delim_);
        TStringBuf token;
        while (lineParser.Next(token)) {
            parts.push_back(UnescapeC(token.data(), token.size()));
        }
    }

    return parts;
}

TVector<TString> TCsvInputStream::ReadLineWithEscaping()
{
    TVector<TString> parts;
    TString line;

    if (Slave_.ReadLine(line)) {
        TCsvLineParser lineParser(line, Delim_);
        TStringBuf token;
        while (lineParser.Next(token)) {
            parts.push_back(TString(token));
        }
    }

    return parts;
}

///////////////////////////////////////////////////////////////////////////////
// TCsvInputStream
///////////////////////////////////////////////////////////////////////////////
TCsvInputBuffer::TCsvInputBuffer(const TStringBuf& buffer, char delimiter)
    : Buffer_(buffer)
    , Delim_(delimiter)
{
}

TVector<TString> TCsvInputBuffer::ReadLine()
{
    TVector<TString> parts;
    TStringBuf line;

    if (Buffer_.ReadLine(line)) {
        TCsvLineParser lineParser(line, Delim_);
        TStringBuf token;
        while (lineParser.Next(token)) {
            parts.push_back(UnescapeC(token.data(), token.size()));
        }
    }

    return parts;
}

TVector<TString> TCsvInputBuffer::ReadLineWithEscaping()
{
    TVector<TString> parts;
    TStringBuf line;

    if (Buffer_.ReadLine(line)) {
        TCsvLineParser lineParser(line, Delim_);
        TStringBuf token;
        while (lineParser.Next(token)) {
            parts.push_back(TString(token.data(), token.size()));
        }
    }

    return parts;
}


///////////////////////////////////////////////////////////////////////////////
// TCsvOutputStream
///////////////////////////////////////////////////////////////////////////////
TCsvOutputStream::TCsvOutputStream(IOutputStream& slave, char delimiter, bool quoteItems)
    : Slave_(slave)
    , WasNL_(true)
    , Delim_(delimiter)
    , QuoteItems_(quoteItems)
{
}

void TCsvOutputStream::DoWrite(const void* buf, size_t len)
{
    TStringBuf charBuf(reinterpret_cast<const char*>(buf), len);
    if (charBuf == TStringBuf("\n")) {
        WasNL_ = true;
        Slave_.Write(buf, len);
    } else {
        if (!WasNL_) Slave_.Write(Delim_);
        WasNL_ = false;
        if (QuoteItems_) {
            Slave_.Write(QUOTE_CH);
        }
        Slave_.Write(EscapeC(charBuf));
        if (QuoteItems_) {
            Slave_.Write(QUOTE_CH);
        }
    }
}

void TCsvOutputStream::DoFlush() {
    Slave_.Flush();
}

} // namspace NUtils
} // namspace NYql