Guest User

Untitled

a guest
Jan 22nd, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. /* MIT License
  2. *
  3. * Copyright (c) 2018 Justin Crawford <Justin@stacksmash.net>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. #include "ConfigurationDriver.h"
  24.  
  25. #define RGB(r, g, b) "\033[38;2;" #r ";" #g ";" #b "m"
  26. #define WHITE RGB(255, 255, 255)
  27. #define LINECOLOR RGB(0, 255, 169)
  28. #define SECTIONCOLOR RGB(86, 104, 239)
  29. #define IDENTIFIERCOLOR RGB(247, 255, 33)
  30. #define VALUECOLOR RGB(117, 232, 95)
  31. #define RESET "\033[0m"
  32.  
  33. void PrintSection(const ConfigurationDriver::Section &section)
  34. {
  35. size_t size = section.values.size();
  36. printf(SECTIONCOLOR "%s" RESET " - %zu items\n", section.GetName().data(), size);
  37.  
  38. for (const auto &[key, value] : section.values)
  39. {
  40. if (size - 1 == 0)
  41. printf(LINECOLOR " └ " IDENTIFIERCOLOR "%s" WHITE " = " VALUECOLOR, key.c_str());
  42. else
  43. printf(LINECOLOR " ├ " IDENTIFIERCOLOR "%s" WHITE " = " VALUECOLOR, key.c_str());
  44.  
  45. switch (value.type)
  46. {
  47. case ex_type::BOOLEAN:
  48. printf("%s", value.boolean ? "true" : "false");
  49. break;
  50. case ex_type::FLOATING:
  51. printf("%.08f", value.floating);
  52. break;
  53. case ex_type::INTEGER:
  54. printf("%ld", value.number);
  55. break;
  56. case ex_type::STRING:
  57. printf("\"%s\"", value.string.c_str());
  58. break;
  59. default:
  60. printf("(unknown)");
  61. break;
  62. }
  63. printf(RESET "\n");
  64. size--;
  65. }
  66. }
  67.  
  68. int main(int argc, char **argv)
  69. {
  70. std::vector<std::string> args{argv, argv + argc};
  71. try
  72. {
  73. if (args.size() == 1)
  74. {
  75. printf("Must specify a file.\n");
  76. return EXIT_FAILURE;
  77. }
  78.  
  79. std::experimental::filesystem::path filenamedeal = args[1];
  80. if (!std::experimental::filesystem::exists(filenamedeal))
  81. {
  82. printf("File does not exist.");
  83. return EXIT_FAILURE;
  84. }
  85.  
  86. ConfigurationDriver config(args[1]);
  87. config.Parse();
  88.  
  89. if (!config.good())
  90. {
  91. printf("Failed to parse the config file\n");
  92. return EXIT_FAILURE;
  93. }
  94.  
  95. auto mysect = config.FindSection("m_mymodule");
  96.  
  97. if (!mysect)
  98. {
  99. printf("config file does not have 'm_mymodule' section\n");
  100. return EXIT_FAILURE;
  101. }
  102.  
  103. std::string mystring = *mysect->GetValue<std::string>("mystring");
  104. int myinteger = *mysect->GetValue<int>("myinteger");
  105. bool myboolean = *mysect->GetValue<bool>("myboolean");
  106. float myfloat = *mysect->GetValue<float>("myfloat");
  107.  
  108. printf("Printing one section:\n");
  109. PrintSection(*mysect);
  110.  
  111. printf("\nPrinting all sections:\n");
  112. for (const auto &[key, value] : config.values)
  113. {
  114. auto found = config.FindSection(key);
  115. if (found)
  116. PrintSection(*found);
  117. }
  118. }
  119. catch (std::experimental::filesystem::filesystem_error &err)
  120. {
  121. printf("Failed to parse config file: %s\n", err.what());
  122. return EXIT_FAILURE;
  123. }
  124.  
  125. return EXIT_SUCCESS;
  126. }
Add Comment
Please, Sign In to add comment