Guest User

ConfigExtractor.h

a guest
Oct 17th, 2021
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #ifndef CONFIGEXTRACTOR_H
  2. #define CONFIGEXTRACTOR_H
  3.  
  4. /* C system icnludes */
  5.  
  6. /* C++ system icnludes */
  7. #include <fstream>
  8. #include <iostream>
  9. #include <string>
  10. #include <unordered_map>
  11.  
  12. /* Third-party icnludes */
  13.  
  14. /* Forward Declaration */
  15.  
  16. typedef std::unordered_map<std::string, std::string> configData;
  17.  
  18. struct ConfigExtractor {
  19. public:
  20.     static configData readFromFile(const char *filePath) {
  21.         configData data;
  22.         std::fstream file(filePath);
  23.         if (!file.is_open()) {
  24.             std::cerr << "Unable to open config file: " << filePath << std::endl;
  25.         }
  26.         std::string configName, configValue;
  27.  
  28.         while (!file.eof()) { // eof -> end of file
  29.             getline(file, configName, ':');
  30.             getline(file, configValue);
  31.             data[configName] = configValue;
  32.         }
  33.         file.close();
  34.         return data;
  35.     }
  36.  
  37. private:
  38. };
  39.  
  40. #endif
Advertisement
Add Comment
Please, Sign In to add comment