INSTINCT Code Coverage Report


Directory: src/
File: Nodes/DataLogger/Protocol/FileWriter.cpp
Date: 2025-02-07 16:54:41
Exec Total Coverage
Lines: 28 59 47.5%
Functions: 4 7 57.1%
Branches: 28 86 32.6%

Line Branch Exec Source
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
13 #include "internal/FlowManager.hpp"
14 #include "internal/ConfigManager.hpp"
15
16 #include <imgui.h>
17 #include "internal/gui/widgets/FileDialog.hpp"
18 #include "internal/gui/widgets/HelpMarker.hpp"
19
20 bool 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
34 112 std::filesystem::path NAV::FileWriter::getFilepath()
35 {
36 112 std::filesystem::path filepath{ _path };
37
1/2
✓ Branch 1 taken 112 times.
✗ Branch 2 not taken.
112 if (filepath.is_relative())
38 {
39
1/2
✓ Branch 1 taken 111 times.
✗ Branch 2 not taken.
112 filepath = flow::GetOutputPath();
40
1/2
✓ Branch 1 taken 112 times.
✗ Branch 2 not taken.
111 filepath /= _path;
41 }
42 112 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
57 16 void NAV::FileWriter::restore(json const& j)
58 {
59 LOG_TRACE("called");
60
61
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 if (j.contains("path"))
62 {
63 16 j.at("path").get_to(_path);
64 }
65
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 if (j.contains("fileType"))
66 {
67 16 j.at("fileType").get_to(_fileType);
68 }
69 16 }
70
71 48 bool NAV::FileWriter::initialize()
72 {
73
1/2
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
48 deinitialize();
74
75 LOG_TRACE("called");
76
77
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (_fileType == FileType::NONE)
78 {
79 LOG_ERROR("FileWriter needs the _fileType set in the child class.");
80 return false;
81 }
82
83
1/2
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
48 std::filesystem::path filepath = getFilepath();
84
85
11/22
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 48 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 7 times.
✓ Branch 7 taken 41 times.
✓ Branch 9 taken 7 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 7 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 7 times.
✓ Branch 16 taken 7 times.
✓ Branch 17 taken 41 times.
✓ Branch 19 taken 48 times.
✗ Branch 20 not taken.
✗ Branch 22 not taken.
✓ Branch 23 taken 48 times.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
48 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
90
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
48 if (_fileType == FileType::ASCII || _fileType == FileType::BINARY)
91 {
92 // Does not enable binary read/write, but disables OS dependant treatment of \n, \r
93
1/2
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
48 _filestream.open(filepath, std::ios_base::trunc | std::ios_base::binary);
94 }
95
96
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
48 if (!_filestream.good())
97 {
98 LOG_ERROR("Could not open file {}", filepath);
99 return false;
100 }
101
102 48 return true;
103 48 }
104
105 64 void NAV::FileWriter::deinitialize()
106 {
107 LOG_TRACE("called");
108
109 try
110 {
111
2/2
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 28 times.
64 if (_filestream.is_open())
112 {
113
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
36 _filestream.flush();
114
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
36 _filestream.close();
115 }
116 }
117 catch (...) // NOLINT(bugprone-empty-catch)
118 {
119 }
120
121 64 _filestream.clear();
122 64 }
123
124 const char* NAV::FileWriter::to_string(NAV::FileWriter::FileType type)
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 }
138