Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
簡易csv読み取り機
// copyright (c) 2020 M.K (a.k.a plasma-effect)
// Distributed under the Boost Software License, Version 1.0.
// (See http://www.boost.org/LICENSE_1_0.txt)
#pragma once
#include<vector>
#include<utility>
#include<array>
#include<optional>
#include<tuple>
#include<string>
#include<filesystem>
#include<fstream>
#include<boost/tokenizer.hpp>
#include<boost/format.hpp>
namespace plasma::csv_parser
{
namespace impl
{
template<class T>struct parse_i_base;
template<class T>struct parse_i_base<std::optional<T>>
{
static constexpr std::size_t size = parse_i_base<T>::size;
static std::optional<T> single(std::vector<std::string>const& vec, std::size_t& idx)
{
if (vec[idx] == "")
{
++idx;
return std::nullopt;
}
else
{
return parse_i_base<T>::single(vec, idx);
}
}
};
template<class T, std::size_t I>struct parse_i_base<std::array<T, I>>
{
static constexpr std::size_t size = I * parse_i_base<T>::size;
static std::array<T, I> single(std::vector<std::string>const& vec, std::size_t& idx)
{
std::array<T, I> ret{};
for (auto& v : ret)
{
v = parse_i_base<T>::single(vec, idx);
}
return ret;
}
};
template<>struct parse_i_base<int>
{
static constexpr std::size_t size = 1;
static auto single(std::vector<std::string>const& vec, std::size_t& idx)
{
return std::stoi(vec[idx++]);
}
};
template<>struct parse_i_base<long>
{
static auto single(std::vector<std::string>const& vec, std::size_t& idx)
{
return std::stol(vec[idx++]);
}
};
template<>struct parse_i_base<unsigned long>
{
static auto single(std::vector<std::string>const& vec, std::size_t& idx)
{
return std::stoul(vec[idx++]);
}
};
template<>struct parse_i_base<long long>
{
static auto single(std::vector<std::string>const& vec, std::size_t& idx)
{
return std::stoll(vec[idx++]);
}
};
template<>struct parse_i_base<unsigned long long>
{
static constexpr std::size_t size = 1;
static auto single(std::vector<std::string>const& vec, std::size_t& idx)
{
return std::stoull(vec[idx++]);
}
};
template<>struct parse_i_base<float>
{
static constexpr std::size_t size = 1;
static auto single(std::vector<std::string>const& vec, std::size_t& idx)
{
return std::stof(vec[idx++]);
}
};
template<>struct parse_i_base<double>
{
static constexpr std::size_t size = 1;
static auto single(std::vector<std::string>const& vec, std::size_t& idx)
{
return std::stod(vec[idx++]);
}
};
template<>struct parse_i_base<long double>
{
static constexpr std::size_t size = 1;
static auto single(std::vector<std::string>const& vec, std::size_t& idx)
{
return std::stold(vec[idx++]);
}
};
template<>struct parse_i_base<std::string>
{
static constexpr std::size_t size = 1;
static auto single(std::vector<std::string>const& vec, std::size_t& idx)
{
return vec[idx++];
}
};
template<std::size_t I, class... Ts>struct parse_i;
template<std::size_t I, class T, class... Ts>struct parse_i<I, T, Ts...>
{
static constexpr auto size = parse_i_base<T>::size + parse_i<I + 1, Ts...>::size;
template<class Tuple>static auto run(Tuple& ret, std::vector<std::string>const& vec, std::size_t& idx)
{
std::get<I>(ret) = parse_i_base<T>::single(vec, idx);
return parse_i<I + 1, Ts...>::run(ret, vec, idx);
}
};
template<std::size_t I>struct parse_i<I>
{
static constexpr std::size_t size = 0;
template<class Tuple>static auto run(Tuple& ret, std::vector<std::string>const& vec, std::size_t& idx)
{
return ret;
}
};
}
template<class... Ts>std::vector<std::tuple<Ts...>> parse(std::filesystem::path path, std::size_t estimate = 0)
{
typedef boost::escaped_list_separator<char> separator_t;
typedef boost::tokenizer<separator_t> tokenizer_t;
separator_t separator;
std::fstream fst(path);
std::vector<std::string> vec;
std::vector<std::tuple<Ts...>> ret;
std::string line;
if (estimate != 0)
{
ret.reserve(estimate);
}
vec.reserve(impl::parse_i<0, Ts...>::size);
std::size_t line_count = 0;
while (++line_count, std::getline(fst, line))
{
tokenizer_t tokens(line, separator);
vec.clear();
vec.assign(tokens.begin(), tokens.end());
if (vec.size() == 0)
{
continue;
}
else if (vec.size() < impl::parse_i<0, Ts...>::size)
{
throw std::invalid_argument((boost::format(R"(line %1%: few elements (requires "%2%" but "%3%"))") % line_count % impl::parse_i<0, Ts...>::size % vec.size()).str());
}
else
{
ret.emplace_back();
std::size_t idx = 0;
impl::parse_i<0, Ts...>::run(ret.back(), vec, idx);
}
}
return ret;
}
}
#include<iostream>
#include"light_csv_parser.hpp"
int main()
{
auto result = plasma::csv_parser::parse<std::array<double, 4>, std::string>("./iris.data");
for (auto&& [ar, name] : result)
{
std::cout << ar[0] << " |" << ar[1] << " |" << ar[2] << " |" << ar[3] << " |" << name << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment