0.4.1
Loading...
Searching...
No Matches
FileReader.cpp
Go to the documentation of this file.
1// This file is part of INSTINCT, the INS Toolkit for Integrated
2// Navigation Concepts and Training by the Institute of Navigation of
3// the University of Stuttgart, Germany.
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9#include "FileReader.hpp"
10
11#include "util/Logger.hpp"
12
15
16#include <sstream>
17#include "util/StringUtil.hpp"
18
19#include <imgui.h>
22
23NAV::FileReader::GuiResult NAV::FileReader::guiConfig(const char* vFilters, const std::vector<std::string>& extensions, size_t id, const std::string& nameId)
24{
26
27 if (gui::widgets::FileDialogLoad(_path, "Select File", vFilters, extensions, flow::GetInputPath(), id, nameId))
28 {
29 if (!std::filesystem::exists(getFilepath()))
30 {
31 result = PATH_CHANGED_INVALID;
32 }
33 else
34 {
35 result = PATH_CHANGED;
36 }
37 }
38 ImGui::SameLine();
39 gui::widgets::HelpMarker(fmt::format("If a relative path is given, files will be searched inside {}.", flow::GetInputPath()).c_str());
40
41 return result;
42}
43
44std::filesystem::path NAV::FileReader::getFilepath()
45{
46 std::filesystem::path filepath{ _path };
47 if (filepath.is_relative())
48 {
49 filepath = flow::GetInputPath();
50 filepath /= _path;
51 }
52 return filepath;
53}
54
55[[nodiscard]] json NAV::FileReader::save() const
56{
57 LOG_TRACE("called");
58
59 json j;
60
61 j["path"] = _path;
62
63 return j;
64}
65
67{
68 LOG_TRACE("called");
69
70 if (j.contains("path"))
71 {
72 j.at("path").get_to(_path);
73 }
74}
75
77{
79
80 LOG_TRACE("called");
81
82 if (_path.empty())
83 {
84 return false;
85 }
86
87 std::filesystem::path filepath = getFilepath();
88
89 if (!std::filesystem::exists(filepath))
90 {
91 LOG_ERROR("File does not exist {}", filepath);
92 return false;
93 }
94 if (std::filesystem::is_directory(filepath))
95 {
96 LOG_ERROR("Path is a directory {}", filepath);
97 return false;
98 }
99
101
103 {
104 // Does not enable binary read/write, but disables OS dependant treatment of \n, \r
105 _filestream = std::ifstream(filepath, std::ios_base::in | std::ios_base::binary);
106 }
107 else
108 {
109 return false;
110 }
111
112 if (!_filestream.good())
113 {
114 LOG_ERROR("Could not open file {}", filepath);
115 return false;
116 }
117 _lineCnt = 0;
118
119 readHeader();
120
122 _dataStart = _filestream.tellg();
123
125 {
126 LOG_DEBUG("ASCII-File successfully initialized");
127 }
128 else if (_fileType == FileType::BINARY)
129 {
130 LOG_DEBUG("Binary-File successfully initialized");
131 }
132
133 return true;
134}
135
137{
138 LOG_TRACE("called");
139
140 _headerColumns.clear();
141
142 if (_filestream.is_open())
143 {
144 _filestream.close();
145 }
146
147 _filestream.clear();
148}
149
151{
152 LOG_TRACE("called");
153
154 auto filepath = getFilepath();
155
156 auto filestreamHeader = std::ifstream(filepath);
157 if (_filestream.good())
158 {
159 std::string line;
160 std::getline(filestreamHeader, line);
161 filestreamHeader.close();
162
163 auto n = std::ranges::count(line, ',');
164
165 if (n >= 3)
166 {
167 return FileType::ASCII;
168 }
169
170 return FileType::BINARY;
171 }
172
173 LOG_ERROR("Could not open file {}", filepath.string());
174 return FileType::NONE;
175}
176
178{
179 LOG_TRACE("called");
180
182 {
183 _headerColumns.clear();
184
185 // Read header line
186 std::string line;
187 getline(line);
188 // Remove any starting non text characters
189 line.erase(line.begin(), std::ranges::find_if(line, [](int ch) { return std::isalnum(ch); }));
190 // Convert line into stream
191 std::stringstream lineStream(line);
192 std::string cell;
193 // Split line at comma
194 while (std::getline(lineStream, cell, ','))
195 {
196 // Remove any trailing non text characters
197 cell.erase(std::ranges::find_if(cell, [](int ch) { return std::iscntrl(ch); }), cell.end());
198 _headerColumns.push_back(cell);
199 }
200 }
201}
202
204{
205 LOG_TRACE("called");
206
207 // Return to position
208 _filestream.clear();
209 _filestream.seekg(_dataStart, std::ios_base::beg);
211}
Config management for the Project.
File Chooser.
Abstract File Reader class.
Save/Load the Nodes.
nlohmann::json json
json namespace
Text Help Marker (?) with Tooltip.
Utility class for logging to console and file.
#define LOG_DEBUG
Debug information. Should not be called on functions which receive observations (spamming)
Definition Logger.hpp:67
#define LOG_ERROR
Error occurred, which stops part of the program to work, but not everything.
Definition Logger.hpp:73
#define LOG_TRACE
Detailled info to trace the execution of the program. Should not be called on functions which receive...
Definition Logger.hpp:65
Utility functions for working with std::strings.
bool initialize()
Initialize the file reader.
void restore(const json &j)
Restores the node from a json object.
std::string _path
Path to the file.
FileType
File Type Enumeration.
@ ASCII
Ascii text data.
@ BINARY
Binary data.
@ NONE
Not specified.
std::ifstream _filestream
File stream to read the file.
FileType _fileType
File Type.
std::filesystem::path getFilepath()
Returns the path of the file.
GuiResult
Results enum for the gui config.
@ PATH_UNCHANGED
No changes made.
@ PATH_CHANGED
The path changed and exists.
@ PATH_CHANGED_INVALID
The path changed but does not exist or is invalid.
std::vector< std::string > _headerColumns
Header Columns of a CSV file.
virtual FileType determineFileType()
Virtual Function to determine the File Type.
GuiResult guiConfig(const char *vFilters, const std::vector< std::string > &extensions, size_t id, const std::string &nameId)
ImGui config.
void resetReader()
Moves the read cursor to the start.
auto & getline(std::string &str)
Reads a line from the filestream.
json save() const
Saves the node into a json object.
virtual void readHeader()
Virtual Function to read the Header of a file.
std::streampos _dataStart
Start of the data in the file.
size_t _lineCnt
Line counter.
size_t _lineCntDataStart
Line counter data start.
void deinitialize()
Deinitialize the file reader.
std::filesystem::path GetInputPath()
Get the path where data files are searched.
bool FileDialogLoad(std::string &path, const char *vName, const char *vFilters, const std::vector< std::string > &extensions, const std::filesystem::path &startPath, size_t id, const std::string &nameId, const char *buttonText="Choose")
Shows an InputText and a button which opens a file dialog to select a path to load a file to.
void HelpMarker(const char *desc, const char *symbol="(?)")
Text Help Marker, e.g. '(?)', with Tooltip.