0.4.1
Loading...
Searching...
No Matches
FileWriter.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 "FileWriter.hpp"
10
11#include "util/Logger.hpp"
12
15
16#include <imgui.h>
19
20bool NAV::FileWriter::guiConfig(const char* vFilters, const std::vector<std::string>& extensions, size_t id, const std::string& nameId)
21{
22 bool changesOccurred = false;
23
24 if (gui::widgets::FileDialogSave(_path, "Save File", vFilters, extensions, flow::GetOutputPath(), id, nameId))
25 {
26 changesOccurred = true;
27 }
28 ImGui::SameLine();
29 gui::widgets::HelpMarker(fmt::format("If a relative path is given, files will be stored inside {}.", flow::GetOutputPath()).c_str());
30
31 return changesOccurred;
32}
33
34std::filesystem::path NAV::FileWriter::getFilepath()
35{
36 std::filesystem::path filepath{ _path };
37 if (filepath.is_relative())
38 {
39 filepath = flow::GetOutputPath();
40 filepath /= _path;
41 }
42 return filepath;
43}
44
45[[nodiscard]] json NAV::FileWriter::save() const
46{
47 LOG_TRACE("called");
48
49 json j;
50
51 j["path"] = _path;
52 j["fileType"] = _fileType;
53
54 return j;
55}
56
58{
59 LOG_TRACE("called");
60
61 if (j.contains("path"))
62 {
63 j.at("path").get_to(_path);
64 }
65 if (j.contains("fileType"))
66 {
67 j.at("fileType").get_to(_fileType);
68 }
69}
70
72{
74
75 LOG_TRACE("called");
76
78 {
79 LOG_ERROR("FileWriter needs the _fileType set in the child class.");
80 return false;
81 }
82
83 std::filesystem::path filepath = getFilepath();
84
85 if (!std::filesystem::exists(filepath.parent_path()) && !std::filesystem::create_directories(filepath.parent_path()))
86 {
87 LOG_ERROR("Could not create directory '{}' for file '{}'", filepath.parent_path(), filepath);
88 }
89
91 {
92 // Does not enable binary read/write, but disables OS dependant treatment of \n, \r
93 _filestream.open(filepath, std::ios_base::trunc | std::ios_base::binary);
94 }
95
96 if (!_filestream.good())
97 {
98 LOG_ERROR("Could not open file {}", filepath);
99 return false;
100 }
101
102 return true;
103}
104
106{
107 LOG_TRACE("called");
108
109 try
110 {
111 if (_filestream.is_open())
112 {
113 _filestream.flush();
114 _filestream.close();
115 }
116 }
117 catch (...) // NOLINT(bugprone-empty-catch)
118 {
119 }
120
121 _filestream.clear();
122}
123
125{
126 switch (type)
127 {
128 case FileType::NONE:
129 return "None";
130 case FileType::ASCII:
131 return "CSV";
132 case FileType::BINARY:
133 return "Binary";
134 default:
135 return "Unkown";
136 }
137}
Config management for the Project.
File Chooser.
File Writer 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_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
std::string _path
Path to the file.
FileType
File Type.
@ ASCII
Ascii text data.
FileType _fileType
File Type.
static const char * to_string(FileType type)
Converts the provided type into string.
void deinitialize()
Deinitialize the file reader.
bool guiConfig(const char *vFilters, const std::vector< std::string > &extensions, size_t id, const std::string &nameId)
ImGui config.
std::filesystem::path getFilepath()
Returns the path of the file.
void restore(const json &j)
Restores the node from a json object.
json save() const
Saves the node into a json object.
bool initialize()
Initialize the file reader.
std::ofstream _filestream
File stream to write the file.
std::filesystem::path GetOutputPath()
Get the path where logs and outputs are stored.
bool FileDialogSave(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 save a file to.
void HelpMarker(const char *desc, const char *symbol="(?)")
Text Help Marker, e.g. '(?)', with Tooltip.