aboutsummaryrefslogtreecommitdiff
path: root/include/openvic-dataloader/csv/Parser.hpp
blob: 5807181653c09736a1116c4e5687a5b4e5bb7be7 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#pragma once

#include <openvic-dataloader/csv/LineObject.hpp>
#include <openvic-dataloader/detail/BasicParser.hpp>

namespace ovdl::csv {
   enum class EncodingType {
      /// Support for Windows-1252 ANSI standard
      Windows1252,
      /// Support for UTF-8 Unicode standard
      Utf8
   };

   /// Parser for CSV files
   /// @tparam Encoding The type of encoding used for this parser
   template<EncodingType Encoding = EncodingType::Windows1252>
   class Parser final : public detail::BasicParser {
   public:
      Parser();

      /// Makes a parse buffer from data to data+size
      /// @param data Initial character buffer memory position
      /// @param size End distance from data for buffer
      /// @return Parser that calls load_from_buffer(const char* data, std::size_t size)
      /// @sa load_from_buffer(const char* data, std::size_t size)
      static Parser from_buffer(const char* data, std::size_t size);
      /// Makes a parse buffer from data to end
      /// @param start Initial character buffer memory position
      /// @param end End character buffer memory position
      /// @return Parser that calls load_from_buffer(const char* start, const char* end)
      /// @sa load_from_buffer(const char* data, const char* end)
      static Parser from_buffer(const char* start, const char* end);
      /// Makes a parse buffer from a string_view
      /// @param string string_view to create a buffer from
      /// @return Parser that calls load_from_string(const std::string_view string)
      /// @sa load_from_string(const std::string_view string)
      static Parser from_string(const std::string_view string);
      /// Makes a parse buffer based on the file path
      /// @param path The file path to supply for the buffer
      /// @return Parser that calls load_from_file(const char* path)
      /// @sa load_from_file(const char* path)
      static Parser from_file(const char* path);
      /// Makes a parse buffer based on a filesystem::path
      /// @param path The file path to supply for the buffer
      /// @return Parser that calls load_from_file(const std::filesystem::path& path)
      /// @sa load_from_file(const std::filesystem::path& path)
      static Parser from_file(const std::filesystem::path& path);
      /// Makes a parse buffer based on any type that has a `const char* c_str()` function
      /// @param path The file path to supply for the buffer
      /// @return Parser that calls from_file(path.c_str())
      /// @sa from_file(const char* path)
      static Parser from_file(const detail::Has_c_str auto& path) {
         return from_file(path.c_str());
      }

      /// Loads a parse buffer from data to data+size
      /// @param data Initial character buffer memory position
      /// @param size End distance from data for buffer
      /// @return Parser& *this
      constexpr Parser& load_from_buffer(const char* data, std::size_t size);
      /// Loads a parse buffer from data to end
      /// @param start Initial character buffer memory position
      /// @param end End character buffer memory position
      /// @return Parser& *this
      constexpr Parser& load_from_buffer(const char* start, const char* end);
      /// Loads a parse buffer from a string_view
      /// @param string string_view to create a buffer from
      /// @return Parser& *this
      constexpr Parser& load_from_string(const std::string_view string);
      /// Loads a parse buffer based on the file path
      /// @param path The file path to supply for the buffer
      /// @return Parser& *this
      constexpr Parser& load_from_file(const char* path);
      /// Loads a parse buffer based on a filesystem::path
      /// @param path The file path to supply for the buffer
      /// @return Parser& *this
      Parser& load_from_file(const std::filesystem::path& path);

      /// Loads a parse buffer based on any type that has a `const char* c_str()` function
      /// @param path The file path to supply for the buffer
      /// @return Parser& *this
      constexpr Parser& load_from_file(const detail::Has_c_str auto& path) {
         return load_from_file(path.c_str());
      }

      /// Performs a CSV file parse over the buffer
      /// @return true Successfully parsed a CSV buffer
      /// @return false Failed to parse a CSV buffer
      /// @note Warnings still produce true
      bool parse_csv();

      /// Get a vector LineObjects
      /// @return const std::vector<csv::LineObject>&
      const std::vector<csv::LineObject>& get_lines() const;

      Parser(Parser&&);
      Parser& operator=(Parser&&);

      ~Parser();

   private:
      class BufferHandler;
      std::unique_ptr<BufferHandler> _buffer_handler;
      std::vector<csv::LineObject> _lines;

      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>;
}