-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathconfigfileparser.h
More file actions
126 lines (105 loc) · 3.52 KB
/
configfileparser.h
File metadata and controls
126 lines (105 loc) · 3.52 KB
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
/*
This file is part of FlashMQ (https://www.flashmq.org)
Copyright (C) 2021-2023 Wiebe Cazemier
FlashMQ is free software: you can redistribute it and/or modify
it under the terms of The Open Software License 3.0 (OSL-3.0).
See LICENSE for license details.
*/
#ifndef CONFIGFILEPARSER_H
#define CONFIGFILEPARSER_H
#include <string>
#include <set>
#include <unordered_map>
#include <vector>
#include <memory>
#include <list>
#include <limits>
#include <regex>
#include "settings.h"
enum class ConfigParseLevel
{
Root,
Listen,
Bridge
};
template<typename T>
typename std::enable_if<std::is_signed<T>::value, long long>::type
value_to_int(const std::string &key, const std::string &value)
{
try
{
size_t len = 0;
long long newVal{std::stoll(value, &len)};
if (len != value.length())
{
throw std::exception();
}
return newVal;
}
catch (std::exception &ex)
{
throw ConfigFileException(formatString("%s's value of '%s' can't be parsed to a number", key.c_str(), value.c_str()));
}
}
template<typename T>
typename std::enable_if<std::is_unsigned<T>::value, long long unsigned>::type
value_to_int(const std::string &key, const std::string &value)
{
try
{
size_t len = 0;
long long unsigned newVal{std::stoull(value, &len)};
if (len != value.length())
{
throw std::exception();
}
return newVal;
}
catch (std::exception &ex)
{
throw ConfigFileException(formatString("%s's value of '%s' can't be parsed to a number", key.c_str(), value.c_str()));
}
}
template<typename T>
T value_to_int_ranged(const std::string &key, const std::string &value, const T min=std::numeric_limits<T>::min(), const T max=std::numeric_limits<T>::max())
{
const auto newVal{value_to_int<T>(key, value)};
if (newVal < min || newVal > max)
{
std::ostringstream oss;
oss << "Value '" << value << "' out of range for '" << key << "', which must be between ";
if (sizeof(T) == 1)
oss << static_cast<int>(min);
else
oss << min;
oss << " and ";
if (sizeof(T) == 1)
oss << static_cast<int>(max);
else
oss << max;
throw ConfigFileException(oss.str());
}
return static_cast<T>(newVal);
}
class ConfigFileParser
{
const std::string path;
std::set<std::string> validKeys;
std::set<std::string> validListenKeys;
std::set<std::string> validBridgeKeys;
const std::regex key_value_regex = std::regex("^([\\w\\-]+)\\s+(.+)$");
const std::regex block_regex_start = std::regex("^([a-zA-Z0-9_\\-]+) *\\{$");
const std::regex block_regex_end = std::regex("^\\}$");
Settings settings;
void static testCorrectNumberOfValues(const std::string &key, size_t expected_values, const std::vector<std::string> &values);
bool testKeyValidity(const std::string &key, const std::string &matchKey, const std::set<std::string> &validKeys) const;
public:
void static checkFileExistsAndReadable(const std::string &key, const std::string &pathToCheck, ssize_t max_size = std::numeric_limits<ssize_t>::max());
void static checkFileOrItsDirWritable(const std::string &filepath);
void static checkDirExists(const std::string &key, const std::string &dir);
ConfigFileParser(const std::string &path);
void loadFile(bool test);
std::list<std::string> readFileRecursively(const std::string &path) const;
const Settings &getSettings();
};
#endif // CONFIGFILEPARSER_H