-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_file.h
178 lines (127 loc) · 5.81 KB
/
config_file.h
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* author: VogelPapaFinn
* version: 2.02
*/
#pragma once
#include<fstream>
#include<string>
#include<vector>
#include<optional>
/*
* Hint:
* There are no attributes without a group. You cant find them in the [default] group.
* But you don't need to care about this, just handle them like they don't have a group.
* The lib does the job there!
*/
enum class state
{
closed,
opened
};
class config_file
{
public:
/// Constructor
/// Opens the file 'file' or creates it, if it does not exists.
config_file(const std::string& file);
/// get_line_vector
/// Returns the vector that contains the lines of the file you just opened.
std::vector<std::string> get_line_vector();
/// file_valid
/// Checks if the 'file' is working.
/// Returns 'true' if it the file doesnt contain any errors.
static bool file_valid(const std::string& file);
/// open
/// Opens the file 'file' but does NOT create it if it doesnt exist.
/// Returns 'true' if successful.
bool open(const std::string& file);
/// save
/// Saves everything in the file. But doesnt clean it up
void save();
/// close
/// Saves the file, cleans it up and closes the stream.
void close();
/// write
/// Writes the attribute 'name' in the group 'group' and assigns the value 'value'.
/// Creates a new group if 'group' does not exist.
/// Returns 'true' if successful.
bool write(const std::string& name, const std::string& value);
/// write
/// Writes the attribute 'name' in the group default and assigns the value 'value'.
/// Returns 'true' if successful.
bool write(const std::string& name, const std::string& value, const std::string& group);
/// write_list
/// Writes the attribute 'name' in the group 'group' and assigns the list 'value'.
/// Creates a new group if 'group' does not exist.
/// Returns 'true' if successful.
bool write_list(const std::string& name, const std::vector<std::string>& value);
/// write
/// Writes the attribute 'name' in the group default and assigns the list 'value'.
/// Returns 'true' if successful.
bool write_list(const std::string& name, const std::vector<std::string>& value, const std::string& group);
/// get
/// Returns the value of the attribute 'name' in the group 'group'
/// as std::string. Have fun with casting! :)
/// Returns std::nullopt when not successful.
[[nodiscard]] std::optional<std::string> get(const std::string& name, const std::string& group) const;
/// get
/// Returns the value of the attribute 'name' in the group default
/// as std::string. Have fun with casting! :)
/// Returns std::nullopt when not successful.
[[nodiscard]] std::optional<std::string> get(const std::string& name) const;
/// get_list
/// Returns the list-value of the attribute 'name' in the group 'group'
/// as std::vector.
/// Returns empty vector when not successful.
[[nodiscard]] std::vector<std::string> get_list(const std::string& name, const std::string& group) const;
/// get_list
/// Returns the list-value of the attribute 'name' in the group 'group'
/// as std::vector.
/// Returns empty vector when not successful.
[[nodiscard]] std::vector<std::string> get_list(const std::string& name) const;
/// get_raw_list
/// Returns the list-value as string of the attribute 'name' in the group 'group'
/// Returns std::nullopt when not successful.
[[nodiscard]] std::optional<std::string> get_raw_list(const std::string& name) const;
/// get_raw_list
/// Returns the list-value as string of the attribute 'name' in the group default
/// Returns std::nullopt when not successful.
[[nodiscard]] std::optional<std::string> get_raw_list(const std::string& name, const std::string& group) const;
/// remove
/// Removes the attribute 'name' from the group 'group'.
void remove(const std::string& name, const std::string& group);
/// remove
/// Removes the attribute 'name' from the group default.
void remove(const std::string& name);
/// remove
/// Removes the group 'group'. 'move' indicates if the attributes in the group
/// should get moved to the default group.
void remove(const std::string& group, bool move);
/// remove_from_group
/// Removes the attribute 'name' from the group 'group'.
void remove_from_group(const std::string& name, const std::string& old_group);
/// move
/// Moves the attribute 'name' from the group 'old_group' to the group 'new_group'.
void move(const std::string& name, const std::string& old_group, const std::string& new_group);
/// move
/// Moves the attribute 'name' from the group default to the group 'new_group'.
void move(const std::string& name, const std::string& new_group);
/// find
/// Checks if the group 'group' exists.
/// Returns the number of the line or '-1' if it does not exist.
int find(const std::string& group) const;
/// find
/// Checks if the attribute 'name' in the group 'group' exists.
/// Returns the number of the line or '-1' if it does not exist.
int find(const std::string& name, const std::string& group) const;
private:
void read_lines(); // Read all lines
std::string vector_to_string(const std::vector<std::string>& vector); // Translates vector to string
static std::string translate_code(const std::string& line); // Translates code to file
static std::string translate_file(const std::string& line); // Translates file to code
void smooth(); // Smoothes the code
std::string file_;
std::vector<std::string> line_vector_; // Contains all the lines from the file
state current_state_; // Current state
std::fstream fstream_;
};