Vince's CSV Parser
Loading...
Searching...
No Matches
csv_utility.hpp
1#pragma once
2#include "common.hpp"
3#include "csv_format.hpp"
4#include "csv_reader.hpp"
5#include "data_type.hpp"
6#include "string_view_stream.hpp"
7
8#include <memory>
9#include <sstream>
10#include <string>
11#include <type_traits>
12#include <unordered_map>
13
14namespace csv {
16 struct CSVFileInfo {
17 std::string filename;
18 std::vector<std::string> col_names;
19 char delim;
20 size_t n_rows;
21 size_t n_cols;
22 };
23
28
38 std::unique_ptr<std::istream> ss(new std::stringstream(std::string(in)));
39 return CSVReader(std::move(ss), format);
40 }
41
52 std::unique_ptr<std::istream> stream(new internals::StringViewStream(in));
53 return CSVReader(std::move(stream), format);
54 }
55
58 CSVFormat format;
59 format.header_row(-1);
60 return parse(in, format);
61 }
62
71 inline CSVReader operator ""_csv(const char* in, size_t n) {
72 return parse_unsafe(csv::string_view(in, n));
73 }
74
80 inline CSVReader operator ""_csv_no_header(const char* in, size_t n) {
81 CSVFormat format;
82 format.header_row(-1);
83 return parse_unsafe(csv::string_view(in, n), format);
84 }
86
89 std::unordered_map<std::string, DataType> csv_data_types(const std::string&);
90
94 inline CSVFileInfo get_file_info(const std::string& filename) {
95 CSVReader reader(filename);
96 CSVFormat format = reader.get_format();
97 for (auto it = reader.begin(); it != reader.end(); ++it);
98
99 return {
100 filename,
101 reader.get_col_names(),
102 format.get_delim(),
103 reader.n_rows(),
104 reader.get_col_names().size()
105 };
106 }
107
109 inline std::vector<std::string> get_col_names(
110 csv::string_view filename,
111 const CSVFormat& format = CSVFormat::guess_csv()) {
112 auto head = internals::get_csv_head(filename);
113 return parse_unsafe(head, format).get_col_names();
114 }
115
117 inline long long get_col_pos(csv::string_view filename, csv::string_view col_name,
118 const CSVFormat& format = CSVFormat::guess_csv()) {
119 auto col_names = get_col_names(filename, format);
120 return col_names.empty() ? CSV_NOT_FOUND :
121 std::distance(col_names.begin(), std::find(col_names.begin(), col_names.end(), col_name));
122 }
124}
Stores information about how to parse a CSV file.
static CSVFormat guess_csv()
CSVFormat preset for delimiter inference with header/n_cols inference enabled.
CSVFormat & header_row(int row)
Sets the header row.
Main class for parsing CSVs from files and in-memory sources.
CSVFormat get_format() const
Return the format of the original raw CSV.
CSV_CONST iterator end() const noexcept
A placeholder for the imaginary past-the-end row in a CSV.
CONSTEXPR size_t n_rows() const noexcept
Retrieves the number of rows that have been read so far.
std::vector< std::string > get_col_names() const
Return the CSV's column names as a vector of strings.
iterator begin()
Return an iterator to the first row in the reader.
Lightweight istream over csv::string_view with zero copy.
A standalone header file containing shared code.
Defines an object used to store CSV format settings.
Defines functionality needed for basic CSV parsing.
Implements data type parsing functionality.
The all encompassing namespace.
long long get_col_pos(csv::string_view filename, csv::string_view col_name, const CSVFormat &format=CSVFormat::guess_csv())
Find the position of a column in a CSV file or CSV_NOT_FOUND otherwise.
CSVReader parse_unsafe(csv::string_view in, CSVFormat format=CSVFormat::guess_csv())
Parse CSV from an in-memory view with zero copy.
CSVFileInfo get_file_info(const std::string &filename)
Get basic information about a CSV file.
CSVReader parse(csv::string_view in, const CSVFormat &format=CSVFormat::guess_csv())
Parse CSV from a string view, copying the input into an owned buffer.
std::vector< std::string > get_col_names(csv::string_view filename, const CSVFormat &format=CSVFormat::guess_csv())
Get the column names of a CSV file using just the first 500KB.
CSVReader parse_no_header(csv::string_view in)
Parses a CSV string with no headers.
constexpr int CSV_NOT_FOUND
Integer indicating a requested column wasn't found.
Definition common.hpp:296
std::unordered_map< std::string, DataType > csv_data_types(const std::string &filename)
Useful for uploading CSV files to SQL databases.
Definition csv_stat.cpp:228
nonstd::string_view string_view
The string_view class used by this library.
Definition common.hpp:135
Returned by get_file_info()
size_t n_cols
Number of columns in a CSV.
std::vector< std::string > col_names
CSV column names.
char delim
Delimiting character.
std::string filename
Filename.
size_t n_rows
Number of rows in a file.