|
| 1 | +#include <figcone/config.h> |
| 2 | +#include <figcone/configreader.h> |
| 3 | +#include <figcone/shortmacros.h> //enables macros without FIGCONE_ prefix |
| 4 | +#include <filesystem> |
| 5 | +#include <iostream> |
| 6 | +#include <map> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +struct Host { |
| 10 | + std::string ip; |
| 11 | + int port; |
| 12 | +}; |
| 13 | + |
| 14 | +namespace figcone { |
| 15 | +template<> |
| 16 | +struct StringConverter<Host> { |
| 17 | + static std::optional<Host> fromString(const std::string& data) |
| 18 | + { |
| 19 | + auto delimPos = data.find(':'); |
| 20 | + if (delimPos == std::string::npos) |
| 21 | + return {}; |
| 22 | + auto host = Host{}; |
| 23 | + host.ip = data.substr(0, delimPos); |
| 24 | + host.port = std::stoi(data.substr(delimPos + 1, data.size() - delimPos - 1)); |
| 25 | + return host; |
| 26 | + } |
| 27 | +}; |
| 28 | +} //namespace figcone |
| 29 | + |
| 30 | +struct SharedAlbumCfg { |
| 31 | + std::filesystem::path dir; |
| 32 | + std::string name; |
| 33 | + std::vector<Host> hosts; |
| 34 | +}; |
| 35 | +struct PhotoViewerCfg { |
| 36 | + std::filesystem::path rootDir; |
| 37 | + std::vector<std::string> supportedFiles; |
| 38 | + std::vector<SharedAlbumCfg> sharedAlbums; |
| 39 | + std::map<std::string, std::string> envVars; |
| 40 | + |
| 41 | + using traits = figcone::FieldTraits<figcone::OptionalField<&PhotoViewerCfg::envVars>>; |
| 42 | +}; |
| 43 | + |
| 44 | +int main() |
| 45 | +{ |
| 46 | + auto cfgReader = figcone::ConfigReader{}; |
| 47 | + auto cfg = cfgReader.readYaml<PhotoViewerCfg>(R"( |
| 48 | + rootDir: ~/Photos |
| 49 | + supportedFiles: [ ".jpg", "png"] |
| 50 | + sharedAlbums: |
| 51 | + - |
| 52 | + dir: "summer_2019" |
| 53 | + name: "Summer 2019" |
| 54 | + hosts: ["127.0.0.1:8080"] |
| 55 | + )"); |
| 56 | + |
| 57 | + std::cout << "Launching PhotoViewer in directory " << cfg.rootDir << std::endl; |
| 58 | + |
| 59 | + if (!cfg.supportedFiles.empty()) |
| 60 | + std::cout << "Supported files:" << std::endl; |
| 61 | + for (const auto& file : cfg.supportedFiles) |
| 62 | + std::cout << " " << file << std::endl; |
| 63 | + |
| 64 | + if (!cfg.sharedAlbums.empty()) |
| 65 | + std::cout << "Shared albums:" << std::endl; |
| 66 | + for (const auto& album : cfg.sharedAlbums) { |
| 67 | + std::cout << " Album:" << album.name << std::endl; |
| 68 | + std::cout << " Hosts:" << std::endl; |
| 69 | + for (const auto& host : album.hosts) |
| 70 | + std::cout << " " << host.ip << ":" << host.port << std::endl; |
| 71 | + } |
| 72 | + |
| 73 | + return 0; |
| 74 | +} |
0 commit comments