Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. #include <string>
  2. #include <cstring>
  3. #include <tuple>
  4. #include <istream>
  5. #include <cstdio>
  6.  
  7. #define CONFIG(X) X(int, test)
  8.  
  9. struct Conf {
  10. #define X(type, name) type name;
  11. CONFIG(X)
  12. #undef X
  13. };
  14.  
  15. template <class T>
  16. T Convert(std::string s);
  17.  
  18. template <>
  19. int Convert<int>(std::string s) {
  20. return std::stoi(s);
  21. };
  22.  
  23. Conf ParseConfig(std::istream& stream) {
  24. Conf c;
  25. std::string line;
  26. while (std::getline(stream, line)) {
  27. #define X(type, name) \
  28. if (line.length() > strlen(#name) + 1 && \
  29. strncmp(line.c_str(), #name, strlen(#name))) \
  30. c.name = Convert<type>(line.substr(strlen(#name) + 1)); \
  31. else
  32. CONFIG(X){};
  33. #undef X
  34. }
  35. return c;
  36. }
  37. Conf ParseConfig(int argc, char** argv) {
  38. Conf c;
  39. std::string line;
  40. for (int i = 1; i < argc - 1; i += 2) {
  41. line = std::string(argv[i]) + " " + std::string(argv[i + 1]);
  42. #define X(type, name) \
  43. if (line.length() > strlen(#name) + 1 && \
  44. strncmp(line.c_str(), #name, strlen(#name))) \
  45. c.name = Convert<type>(line.substr(strlen(#name) + 1)); \
  46. else
  47. CONFIG(X){};
  48. #undef X
  49. }
  50. return c;
  51. }
  52.  
  53. #undef CONFIG
  54.  
  55. int main(int argc, char** argv) {
  56. Conf test = ParseConfig(argc, argv);
  57. printf("test=\"%d\"\n", test.test);
  58. return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement