aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/core/yql_csv.h
blob: 6e3e48b816fcfa0600f92db61236467bd95bb45e (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
#pragma once

#include <util/stream/input.h>
#include <util/stream/output.h>
#include <util/generic/vector.h>

namespace NYql {
namespace NUtils {

///////////////////////////////////////////////////////////////////////////////
// TCsvInputStream
///////////////////////////////////////////////////////////////////////////////
class TCsvInputStream {
public:
    TCsvInputStream(IInputStream& slave, char delimiter = ';');

    TVector<TString> ReadLine();
    TVector<TString> ReadLineWithEscaping();

private:
    IInputStream& Slave_;
    const char    Delim_;
};


///////////////////////////////////////////////////////////////////////////////
// TCsvInputBuffer
///////////////////////////////////////////////////////////////////////////////
class TCsvInputBuffer {
public:
    TCsvInputBuffer(const TStringBuf& buffer, char delimiter = ';');

    TVector<TString> ReadLine();
    TVector<TString> ReadLineWithEscaping();

private:
    TStringBuf Buffer_;
    const char Delim_;
};


///////////////////////////////////////////////////////////////////////////////
// TCsvOutputStream
///////////////////////////////////////////////////////////////////////////////
class TCsvOutputStream: public IOutputStream {
public:
    TCsvOutputStream(IOutputStream& slave, char delimiter = ';', bool quoteItems = true);

    // hack for output empty values
    inline TCsvOutputStream& operator<<(const TString& value) {
        DoWrite(value.data(), value.size());
        return *this;
    }

    // hack for output empty values
    inline TCsvOutputStream& operator<<(const char* value) {
        DoWrite(value, strlen(value));
        return *this;
    }

    inline TCsvOutputStream& operator<<(const TVector<TString>& values) {
        for(const TString& value: values) {
            (*this) << value;
        }
        (*this) << Endl;
        return *this;
    }

private:
    void DoWrite(const void* buf, size_t len) override final;
    void DoFlush() override;

private:
    IOutputStream& Slave_;
    bool WasNL_;
    const char Delim_;
    const bool QuoteItems_;
};

} // namspace NUtils
} // namspace NYql