Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <cstring>
- #include <tuple>
- #include <istream>
- #include <cstdio>
- #define CONFIG(X) X(int, test)
- struct Conf {
- #define X(type, name) type name;
- CONFIG(X)
- #undef X
- };
- template <class T>
- T Convert(std::string s);
- template <>
- int Convert<int>(std::string s) {
- return std::stoi(s);
- };
- Conf ParseConfig(std::istream& stream) {
- Conf c;
- std::string line;
- while (std::getline(stream, line)) {
- #define X(type, name) \
- if (line.length() > strlen(#name) + 1 && \
- strncmp(line.c_str(), #name, strlen(#name))) \
- c.name = Convert<type>(line.substr(strlen(#name) + 1)); \
- else
- CONFIG(X){};
- #undef X
- }
- return c;
- }
- Conf ParseConfig(int argc, char** argv) {
- Conf c;
- std::string line;
- for (int i = 1; i < argc - 1; i += 2) {
- line = std::string(argv[i]) + " " + std::string(argv[i + 1]);
- #define X(type, name) \
- if (line.length() > strlen(#name) + 1 && \
- strncmp(line.c_str(), #name, strlen(#name))) \
- c.name = Convert<type>(line.substr(strlen(#name) + 1)); \
- else
- CONFIG(X){};
- #undef X
- }
- return c;
- }
- #undef CONFIG
- int main(int argc, char** argv) {
- Conf test = ParseConfig(argc, argv);
- printf("test=\"%d\"\n", test.test);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement