aboutsummaryrefslogtreecommitdiff
path: root/include/openvic-dataloader
diff options
context:
space:
mode:
author George L. Albany <Megacake1234@gmail.com>2023-09-04 19:16:40 +0200
committer GitHub <noreply@github.com>2023-09-04 19:16:40 +0200
commit238ab9dfaa8ec7a48142154d227605ae367d53d1 (patch)
tree9833fa669651e04dea81b9b2b5875a8d21ea938e /include/openvic-dataloader
parent027828532837673a3631c8a9a8128f9b89a9db43 (diff)
parenta705b0223664f915d8bb00d4f89e05838da3e4bc (diff)
Merge pull request #11 from OpenVicProject/fix/more-csv-problems
Diffstat (limited to 'include/openvic-dataloader')
-rw-r--r--include/openvic-dataloader/csv/LineObject.hpp38
-rw-r--r--include/openvic-dataloader/csv/Parser.hpp9
-rw-r--r--include/openvic-dataloader/detail/ClassExport.hpp9
3 files changed, 51 insertions, 5 deletions
diff --git a/include/openvic-dataloader/csv/LineObject.hpp b/include/openvic-dataloader/csv/LineObject.hpp
index 8a9c2ec..c6d8e2a 100644
--- a/include/openvic-dataloader/csv/LineObject.hpp
+++ b/include/openvic-dataloader/csv/LineObject.hpp
@@ -1,13 +1,17 @@
#pragma once
+#include <algorithm>
+#include <cctype>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <initializer_list>
#include <optional>
+#include <ostream>
+#include <set>
#include <string>
#include <string_view>
-#include <tuple>
+#include <utility>
#include <vector>
#include <openvic-dataloader/detail/VectorConstexpr.hpp>
@@ -24,13 +28,13 @@ namespace ovdl::csv {
/// ;a;b;c -> 0,4+ == ""
///
/// If this is incorrect, please report an issue.
- class LineObject final : public std::vector<std::tuple<std::uint32_t, std::string>> {
+ class LineObject final : public std::vector<std::pair<std::uint32_t, std::string>> {
public:
// Stored position of value
using position_type = std::uint32_t;
// Value
using inner_value_type = std::string;
- using container_type = std::vector<std::tuple<position_type, inner_value_type>>;
+ using container_type = std::vector<std::pair<position_type, inner_value_type>>;
OVDL_VECTOR_CONSTEXPR LineObject() = default;
OVDL_VECTOR_CONSTEXPR LineObject(LineObject&) = default;
@@ -54,7 +58,7 @@ namespace ovdl::csv {
/// Special Functionality
/// Retrieves value, produces "" for empty values
constexpr std::string_view get_value_for(std::size_t position) const {
- if (position <= _prefix_end || position >= _suffix_end) return "";
+ if (position < _prefix_end || position >= _suffix_end) return "";
for (const auto& [pos, val] : *this) {
if (pos == position) return val;
}
@@ -62,7 +66,7 @@ namespace ovdl::csv {
}
/// Tries to retrieve reference, produces nullopt for empty values
constexpr std::optional<const std::reference_wrapper<const std::string>> try_get_string_at(std::size_t position) const {
- if (position <= _prefix_end || position > _suffix_end) return std::nullopt;
+ if (position < _prefix_end || position >= _suffix_end) return std::nullopt;
for (const auto& [pos, val] : *this) {
if (pos == position) return std::cref(val);
}
@@ -83,4 +87,28 @@ namespace ovdl::csv {
// Should be position after last value or position after last seperator
position_type _suffix_end = 0;
};
+
+ inline std::ostream& operator<<(std::ostream& stream, const LineObject& line) {
+ static const char SEP = ';';
+ LineObject::position_type sep_index = 0;
+ for (const auto& [pos, val] : line) {
+ while (sep_index < pos) {
+ stream << SEP;
+ sep_index++;
+ }
+ if (std::any_of(val.begin(), val.end(), [](char c) { return c == SEP || std::isspace(c); })) {
+ stream << '"' << val << '"';
+ } else {
+ stream << val;
+ }
+ }
+ return stream;
+ }
+
+ inline std::ostream& operator<<(std::ostream& stream, const std::vector<LineObject>& lines) {
+ for (const LineObject& line : lines) {
+ stream << line << '\n';
+ }
+ return stream;
+ }
} \ No newline at end of file
diff --git a/include/openvic-dataloader/csv/Parser.hpp b/include/openvic-dataloader/csv/Parser.hpp
index 0bd0670..fadaf3a 100644
--- a/include/openvic-dataloader/csv/Parser.hpp
+++ b/include/openvic-dataloader/csv/Parser.hpp
@@ -4,6 +4,12 @@
#include <openvic-dataloader/detail/BasicParser.hpp>
namespace ovdl::csv {
+ enum class EncodingType {
+ Windows1252,
+ Utf8
+ };
+
+ template<EncodingType Encoding = EncodingType::Windows1252>
class Parser final : public detail::BasicParser {
public:
Parser();
@@ -39,4 +45,7 @@ namespace ovdl::csv {
template<typename... Args>
constexpr void _run_load_func(detail::LoadCallback<BufferHandler, Args...> auto func, Args... args);
};
+
+ using Windows1252Parser = Parser<EncodingType::Windows1252>;
+ using Utf8Parser = Parser<EncodingType::Utf8>;
} \ No newline at end of file
diff --git a/include/openvic-dataloader/detail/ClassExport.hpp b/include/openvic-dataloader/detail/ClassExport.hpp
new file mode 100644
index 0000000..27098ed
--- /dev/null
+++ b/include/openvic-dataloader/detail/ClassExport.hpp
@@ -0,0 +1,9 @@
+#pragma once
+
+#ifdef _MSC_VER
+#define OVDL_EXPORT __declspec(dllexport)
+#elif defined(__GNUC__)
+#define OVDL_EXPORT __attribute__((visibility("default")))
+#else
+#define OVDL_EXPORT
+#endif \ No newline at end of file