Smallcase folders part 1
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
message(STATUS "·Configuring Config")
|
||||
|
||||
add_subdirectory(common)
|
||||
if (BUILD_SERVER)
|
||||
add_subdirectory(server)
|
||||
endif()
|
||||
if (BUILD_CLIENT)
|
||||
add_subdirectory(client)
|
||||
endif()
|
||||
@@ -0,0 +1,17 @@
|
||||
message(STATUS "··Configuring Client")
|
||||
|
||||
add_library(Config.Client
|
||||
Config.cpp
|
||||
)
|
||||
|
||||
add_library(Config::Client ALIAS Config.Client)
|
||||
|
||||
target_link_libraries(Config.Client
|
||||
PUBLIC
|
||||
Config::Common
|
||||
)
|
||||
|
||||
target_include_directories(Config.Client
|
||||
PUBLIC
|
||||
${INCLUDE_BASE_DIR}
|
||||
)
|
||||
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
namespace config::client
|
||||
{
|
||||
struct CollectorConfig
|
||||
{
|
||||
std::vector<std::vector<std::string>> disks{{{"R", "/"}}};
|
||||
};
|
||||
} // namespace config::client
|
||||
@@ -0,0 +1,24 @@
|
||||
#include "config/client/Config.h"
|
||||
#include "config/common/IniParser.h"
|
||||
#include <sstream>
|
||||
|
||||
namespace config::client
|
||||
{
|
||||
Config Config::load(const std::string &path)
|
||||
{
|
||||
Config cfg;
|
||||
IniParser ini;
|
||||
ini.load(path);
|
||||
|
||||
cfg.network.serverHost = ini.get("network", "server_host", "127.0.0.1");
|
||||
cfg.network.serverPort = std::stoi(ini.get("network", "server_port", "5005"));
|
||||
cfg.network.intervalMs = std::chrono::milliseconds(std::stoi(ini.get("network", "interval_ms", "1000")));
|
||||
|
||||
const std::string disks_line = ini.get("collector", "disks", "R:/");
|
||||
const std::vector<std::string> disks_vec = config::splitList(disks_line, ',');
|
||||
for (const std::string &disk : disks_vec)
|
||||
cfg.collector.disks.push_back(config::splitList(disk, ':'));
|
||||
|
||||
return cfg;
|
||||
}
|
||||
} // namespace config::client
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include "display/graphics/Color.h"
|
||||
#include "config/client/NetworkConfig.h"
|
||||
#include "config/client/CollectorConfig.h"
|
||||
|
||||
namespace config::client
|
||||
{
|
||||
class Config
|
||||
{
|
||||
public:
|
||||
static Config load(const std::string &path);
|
||||
|
||||
NetworkConfig network;
|
||||
CollectorConfig collector;
|
||||
};
|
||||
} // namespace config::client
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
|
||||
namespace config::client
|
||||
{
|
||||
struct NetworkConfig
|
||||
{
|
||||
std::string serverHost{"127.0.0.1"};
|
||||
int serverPort{5005};
|
||||
std::chrono::milliseconds intervalMs{std::chrono::milliseconds(1000)};
|
||||
};
|
||||
} // namespace config::client
|
||||
@@ -0,0 +1,12 @@
|
||||
message(STATUS "··Configuring Common")
|
||||
|
||||
add_library(Config.Common
|
||||
IniParser.cpp
|
||||
)
|
||||
|
||||
add_library(Config::Common ALIAS Config.Common)
|
||||
|
||||
target_include_directories(Config.Common
|
||||
PUBLIC
|
||||
${INCLUDE_BASE_DIR}
|
||||
)
|
||||
@@ -0,0 +1,77 @@
|
||||
#include "config/common/IniParser.h"
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
|
||||
namespace config
|
||||
{
|
||||
std::string trim(std::string s)
|
||||
{
|
||||
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int c)
|
||||
{ return !isspace(c); }));
|
||||
s.erase(std::find_if(s.rbegin(), s.rend(), [](int c)
|
||||
{ return !isspace(c); })
|
||||
.base(),
|
||||
s.end());
|
||||
return s;
|
||||
}
|
||||
|
||||
std::vector<std::string> splitList(const std::string &s, char delim)
|
||||
{
|
||||
std::vector<std::string> result;
|
||||
std::string item;
|
||||
std::istringstream ss(s);
|
||||
|
||||
while (std::getline(ss, item, delim))
|
||||
{
|
||||
item = trim(item);
|
||||
if (!item.empty())
|
||||
result.push_back(item);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool IniParser::load(const std::string &path)
|
||||
{
|
||||
std::ifstream f(path);
|
||||
if (!f)
|
||||
return false;
|
||||
|
||||
std::string line, section;
|
||||
while (std::getline(f, line))
|
||||
{
|
||||
line = trim(line);
|
||||
if (line.empty() || line[0] == '#' || line[0] == ';')
|
||||
continue;
|
||||
|
||||
if (line.front() == '[' && line.back() == ']')
|
||||
{
|
||||
section = line.substr(1, line.size() - 2);
|
||||
continue;
|
||||
}
|
||||
|
||||
auto pos = line.find('=');
|
||||
if (pos == std::string::npos)
|
||||
continue;
|
||||
|
||||
std::string key = trim(line.substr(0, pos));
|
||||
std::string val = trim(line.substr(pos + 1));
|
||||
data[section][key] = val;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string IniParser::get(const std::string §ion,
|
||||
const std::string &key,
|
||||
const std::string &def) const
|
||||
{
|
||||
auto s = data.find(section);
|
||||
if (s == data.end())
|
||||
return def;
|
||||
auto k = s->second.find(key);
|
||||
if (k == s->second.end())
|
||||
return def;
|
||||
return k->second;
|
||||
}
|
||||
} // namespace config
|
||||
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
namespace config
|
||||
{
|
||||
std::string trim(std::string s);
|
||||
std::vector<std::string> splitList(const std::string &s, char delim = ',');
|
||||
|
||||
class IniParser
|
||||
{
|
||||
public:
|
||||
bool load(const std::string &path);
|
||||
|
||||
std::string get(const std::string §ion,
|
||||
const std::string &key,
|
||||
const std::string &def = "") const;
|
||||
|
||||
private:
|
||||
using Section = std::unordered_map<std::string, std::string>;
|
||||
std::unordered_map<std::string, Section> data;
|
||||
};
|
||||
} // namespace config
|
||||
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "display/graphics/Color.h"
|
||||
|
||||
namespace config::server
|
||||
{
|
||||
struct BarStyleConfig
|
||||
{
|
||||
display::graphics::Color background;
|
||||
display::graphics::Color fill;
|
||||
};
|
||||
|
||||
} // namespace config::server
|
||||
@@ -0,0 +1,17 @@
|
||||
message(STATUS "··Configuring Server")
|
||||
|
||||
add_library(Config.Server
|
||||
Config.cpp
|
||||
)
|
||||
|
||||
add_library(Config::Server ALIAS Config.Server)
|
||||
|
||||
target_link_libraries(Config.Server
|
||||
PUBLIC
|
||||
Config::Common
|
||||
)
|
||||
|
||||
target_include_directories(Config.Server
|
||||
PUBLIC
|
||||
${INCLUDE_BASE_DIR}
|
||||
)
|
||||
@@ -0,0 +1,39 @@
|
||||
#include "Config/Server/Config.h"
|
||||
#include "Config/Common/IniParser.h"
|
||||
#include <sstream>
|
||||
|
||||
namespace config::server
|
||||
{
|
||||
static display::graphics::Color parseColor(const std::string &s)
|
||||
{
|
||||
int r, g, b;
|
||||
char c;
|
||||
std::stringstream ss(s);
|
||||
ss >> r >> c >> g >> c >> b;
|
||||
return {uint8_t(r), uint8_t(g), uint8_t(b)};
|
||||
}
|
||||
|
||||
Config Config::load(const std::string &path)
|
||||
{
|
||||
Config cfg;
|
||||
IniParser ini;
|
||||
ini.load(path);
|
||||
|
||||
cfg.display.rotation = std::stoi(ini.get("display", "rotation", "0"));
|
||||
cfg.display.refreshMs = std::chrono::milliseconds(std::stoi(ini.get("display", "refresh_ms", "1000")));
|
||||
|
||||
cfg.network.listenPort = std::stoi(ini.get("network", "listen_port", "5005"));
|
||||
cfg.network.intervalMs = std::chrono::milliseconds(std::stoi(ini.get("network", "interval_ms", "1000")));
|
||||
|
||||
cfg.textStyle.fontPath = ini.get("text", "font_path",
|
||||
"/usr/share/fonts/truetype/noto/NotoMono-Regular.ttf");
|
||||
cfg.textStyle.fontSize = std::stoi(ini.get("text", "font_size", "10"));
|
||||
|
||||
cfg.barStyle.background = parseColor(
|
||||
ini.get("bar", "background", "30,30,30"));
|
||||
cfg.barStyle.fill = parseColor(
|
||||
ini.get("bar", "fill", "0,180,0"));
|
||||
|
||||
return cfg;
|
||||
}
|
||||
} // namespace config::server
|
||||
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
#include "Config/Server/BarStyleConfig.h"
|
||||
#include "Config/Server/DisplayConfig.h"
|
||||
#include "Config/Server/NetworkConfig.h"
|
||||
#include "Config/Server/TextStyleConfig.h"
|
||||
|
||||
namespace config::server
|
||||
{
|
||||
class Config
|
||||
{
|
||||
public:
|
||||
static Config load(const std::string &path);
|
||||
|
||||
DisplayConfig display;
|
||||
NetworkConfig network;
|
||||
TextStyleConfig textStyle;
|
||||
BarStyleConfig barStyle;
|
||||
};
|
||||
} // namespace config::server
|
||||
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
|
||||
namespace config::server
|
||||
{
|
||||
struct DisplayConfig
|
||||
{
|
||||
int rotation = 0;
|
||||
std::chrono::milliseconds refreshMs = std::chrono::milliseconds(1000);
|
||||
};
|
||||
} // namespace config::server
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
|
||||
namespace config::server
|
||||
{
|
||||
struct NetworkConfig
|
||||
{
|
||||
std::string listenAddr = "0.0.0.0";
|
||||
int listenPort = 5005;
|
||||
std::chrono::milliseconds intervalMs = std::chrono::milliseconds(1000);
|
||||
};
|
||||
} // namespace config::server
|
||||
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace config::server
|
||||
{
|
||||
struct TextStyleConfig
|
||||
{
|
||||
std::string fontPath;
|
||||
int fontSize = 10;
|
||||
};
|
||||
} // namespace config::server
|
||||
Reference in New Issue
Block a user