Advertisement
Rapptz

config file v2

Jul 23rd, 2012
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <exception>
  6. #include <cctype>
  7. #include <fstream>
  8. #include <vector>
  9. #include <limits>
  10. #include <algorithm>
  11.  
  12. struct confi {
  13.     std::string fullname;
  14.     std::string favouritefruit;
  15.     bool needspeeling;
  16.     bool seedsremoved;
  17.     std::vector<std::string> otherfamily;
  18. };
  19.  
  20. void read_config(std::ifstream& in, confi& out) {
  21.     in.open("Config.txt");
  22.     std::string str;
  23.     out.needspeeling = false;
  24.     out.seedsremoved = false;
  25.     while(!in.eof()) {
  26.         while(getline(in,str)) {
  27.             std::string::size_type begin = str.find_first_not_of(" \f\t\v");
  28.             //Skips blank lines
  29.             if(begin == std::string::npos)
  30.                 continue;
  31.             //Skips #
  32.             if(std::string("#").find(str[begin]) != std::string::npos)
  33.                 continue;
  34.             std::string firstWord;
  35.             try {
  36.                 firstWord = str.substr(0,str.find(" "));
  37.             }
  38.             catch(std::exception& e) {
  39.                 firstWord = str.erase(str.find_first_of(" "),str.find_first_not_of(" "));
  40.             }
  41.             std::transform(firstWord.begin(),firstWord.end(),firstWord.begin(), ::toupper);
  42.             if(firstWord == "FULLNAME")
  43.                 out.fullname = str.substr(str.find(" ")+1,str.length());
  44.             if(firstWord == "FAVOURITEFRUIT")
  45.                 out.favouritefruit = str.substr(str.find(" ")+1,str.length());
  46.             if(firstWord == "NEEDSPEELING")
  47.                 out.needspeeling = true;
  48.             if(firstWord == "SEEDSREMOVED")
  49.                 out.seedsremoved = true;
  50.             if(firstWord == "OTHERFAMILY") {
  51.                 size_t found = str.find(",");
  52.                 if(found != std::string::npos) {
  53.                     out.otherfamily.push_back(str.substr(str.find_first_of(" ")+1,found-str.find_first_of(" ")-1));
  54.                     out.otherfamily.push_back(str.substr(found+2,str.length()));
  55.                 }
  56.             }
  57.                    
  58.         }
  59.     }
  60.     std::cout << "Full Name: " << out.fullname << std::endl;
  61.     std::cout << "Favourite Fruit: " << out.favouritefruit << std::endl;
  62.     std::cout << "Needs peeling?: ";
  63.     if(out.needspeeling == true)
  64.         std::cout << "True" << std::endl;
  65.     else
  66.         std::cout << "False" << std::endl;
  67.     std::cout << "Seeds removed?: ";
  68.     if(out.seedsremoved == true)
  69.         std::cout << "True" << std::endl;
  70.     else
  71.         std::cout << "False" << std::endl;
  72.     std::cout << "Other family members: " << out.otherfamily[0] << ", " << out.otherfamily[1] << std::endl;
  73. }
  74. int main() {
  75.     std::ifstream inp;
  76.     confi outp;
  77.     read_config(inp,outp);
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement