Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* MIT License
- *
- * Copyright (c) 2018 Justin Crawford <Justin@stacksmash.net>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
- #include "ConfigurationDriver.h"
- #define RGB(r, g, b) "\033[38;2;" #r ";" #g ";" #b "m"
- #define WHITE RGB(255, 255, 255)
- #define LINECOLOR RGB(0, 255, 169)
- #define SECTIONCOLOR RGB(86, 104, 239)
- #define IDENTIFIERCOLOR RGB(247, 255, 33)
- #define VALUECOLOR RGB(117, 232, 95)
- #define RESET "\033[0m"
- void PrintSection(const ConfigurationDriver::Section §ion)
- {
- size_t size = section.values.size();
- printf(SECTIONCOLOR "%s" RESET " - %zu items\n", section.GetName().data(), size);
- for (const auto &[key, value] : section.values)
- {
- if (size - 1 == 0)
- printf(LINECOLOR " └ " IDENTIFIERCOLOR "%s" WHITE " = " VALUECOLOR, key.c_str());
- else
- printf(LINECOLOR " ├ " IDENTIFIERCOLOR "%s" WHITE " = " VALUECOLOR, key.c_str());
- switch (value.type)
- {
- case ex_type::BOOLEAN:
- printf("%s", value.boolean ? "true" : "false");
- break;
- case ex_type::FLOATING:
- printf("%.08f", value.floating);
- break;
- case ex_type::INTEGER:
- printf("%ld", value.number);
- break;
- case ex_type::STRING:
- printf("\"%s\"", value.string.c_str());
- break;
- default:
- printf("(unknown)");
- break;
- }
- printf(RESET "\n");
- size--;
- }
- }
- int main(int argc, char **argv)
- {
- std::vector<std::string> args{argv, argv + argc};
- try
- {
- if (args.size() == 1)
- {
- printf("Must specify a file.\n");
- return EXIT_FAILURE;
- }
- std::experimental::filesystem::path filenamedeal = args[1];
- if (!std::experimental::filesystem::exists(filenamedeal))
- {
- printf("File does not exist.");
- return EXIT_FAILURE;
- }
- ConfigurationDriver config(args[1]);
- config.Parse();
- if (!config.good())
- {
- printf("Failed to parse the config file\n");
- return EXIT_FAILURE;
- }
- auto mysect = config.FindSection("m_mymodule");
- if (!mysect)
- {
- printf("config file does not have 'm_mymodule' section\n");
- return EXIT_FAILURE;
- }
- std::string mystring = *mysect->GetValue<std::string>("mystring");
- int myinteger = *mysect->GetValue<int>("myinteger");
- bool myboolean = *mysect->GetValue<bool>("myboolean");
- float myfloat = *mysect->GetValue<float>("myfloat");
- printf("Printing one section:\n");
- PrintSection(*mysect);
- printf("\nPrinting all sections:\n");
- for (const auto &[key, value] : config.values)
- {
- auto found = config.FindSection(key);
- if (found)
- PrintSection(*found);
- }
- }
- catch (std::experimental::filesystem::filesystem_error &err)
- {
- printf("Failed to parse config file: %s\n", err.what());
- return EXIT_FAILURE;
- }
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment