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
|
/*
* libfud
* Copyright 2025 Dominick Allen
*
* 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.
*/
#ifndef FUD_CSV_HPP
#define FUD_CSV_HPP
#include "fud_file.hpp"
#include "fud_status.hpp"
#include "fud_string_view.hpp"
#include "fud_text.hpp"
#include "fud_utf8.hpp"
#include "fud_vector.hpp"
namespace fud {
using TextBuffer = Vector<std::byte>;
struct Csv {
/** \brief The number of lines of data in the CSV. */
size_t numLines{0};
/** \brief The number of columns in the CSV. */
size_t numColumns{0};
/** \brief Buffer for each line with numColumns of StringView. */
Vector<StringView> entries;
/** \brief Backing buffer for data. */
Vector<utf8> buffer;
/** \separator for each column */
Utf8 columnDelimiter{Ascii{','}};
Utf8 quoteCharacter{Ascii{'"'}};
/** \separator for each line */
NewlineRepr newlineDelimiter{NewlineRepr::Posix};
bool strictUtf8{true};
bool strictColumns{true};
bool strictQuote{false};
bool skipInitialSpace{false};
/** \brief Uses global Fud allocator for lines and backing buffer. */
static Csv makeDefault();
/** \brief Specify allocator to use for both lines and backing buffer. */
static Csv makeWithSingleAllocator(Allocator& allocator);
/** \brief Specify allocator. */
static Csv make(Allocator& entryAllocator, Allocator& bufferAllocator);
/** Consume and return the CSV. */
static FudStatus parseFromFilenameUnbuffered(
Csv& csv,
StringView filename,
OpenFlags flags = OpenFlags{},
Option<int> dirFdOption = NullOpt,
size_t maxExtraAttempts = 0);
static FudStatus parseFromFilenameBuffered(
Csv& csv,
TextBuffer&& buffer,
StringView filename,
OpenFlags flags = OpenFlags{},
Option<int> dirFdOption = NullOpt,
size_t maxExtraAttempts = 0);
// assumes file is at start
static FudStatus parseFromUnbufferedFile(Csv& csv, RegularFile&& file, size_t maxExtraAttempts);
// assumes file is at start
static FudStatus parseFromBufferedFile(Csv& csv, BufferedRegularFile& file, size_t maxExtraAttempts);
[[nodiscard]] Result<Option<StringView>, FudStatus> entry(size_t line, size_t column) const;
};
} // namespace fud
#endif
|