Advertisement
Guest User

Untitled

a guest
Jun 17th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. #include "FileManager.h"
  2.  
  3. FileManager::FileManager()
  4. {
  5.     idFound = false;
  6. }
  7.  
  8. FileManager::~FileManager()
  9. {
  10. }
  11.  
  12. void FileManager::LoadContent(const char*fileName, std::vector<std::vector<std::string>> &attributes, std::vector<std::vector<std::string>> &content)
  13. {
  14.     attributes.clear();
  15.     content.clear();
  16.  
  17.     std::ifstream openFile(fileName);
  18.     if (openFile.is_open())
  19.     {
  20.         while (!openFile.eof())
  21.         {
  22.             std::string line;
  23.             std::getline(openFile, line);
  24.  
  25.             line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
  26.  
  27.             if (line.find("Load=") != std::string::npos)
  28.             {
  29.                 state = ATTRIBUTES;
  30.                 line.erase(0, line.find('=') + 1);
  31.                 tempAttributes.clear();
  32.             }
  33.             else
  34.             {
  35.                 state = CONTENTS;
  36.                 tempContents.clear();
  37.             }
  38.             line.erase(std::remove(line.begin(), line.end(), '['), line.end());
  39.             std::stringstream str(line);
  40.             while (str)
  41.             {
  42.                 std::getline(str, line, ']');
  43.                 if (line != "")
  44.                 {
  45.                     if (state == ATTRIBUTES)
  46.                     {
  47.                         tempAttributes.push_back(line);
  48.                     }
  49.                     else
  50.                     {
  51.                         tempContents.push_back(line);
  52.                     }
  53.                     std::cout << line << std::endl;
  54.                 }
  55.             }
  56.             if (state == CONTENTS && tempContents.size() > 0)
  57.             {
  58.                 attributes.push_back(tempAttributes);
  59.                 content.push_back(tempContents);
  60.             }
  61.         }
  62.     }
  63. }
  64.  
  65. void FileManager::LoadContent(const char*fileName, std::vector<std::vector<std::string>> &attributes, std::vector<std::vector<std::string>> &content, std::string id)
  66. {
  67.     attributes.clear();
  68.     content.clear();
  69.  
  70.     std::ifstream openFile(fileName);
  71.  
  72.     idFound = false;
  73.     if (openFile.is_open())
  74.     {
  75.         while (!openFile.eof())
  76.         {
  77.             std::string line;
  78.             std::getline(openFile, line);
  79.  
  80.             line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
  81.  
  82.             if(line.find("Start=[" + id + "]") != std::string::npos) // npos zwraca true jeśli nie znajdzie żadnego tekstu w pliku
  83.             {
  84.                 idFound = true;
  85.                 continue;
  86.             }
  87.             else if (line.find("End=") != std::string::npos && line.find(id) != std::string::npos)
  88.             {
  89.                 idFound = false;
  90.                 break;
  91.             }
  92.  
  93.             if (idFound)
  94.             {
  95.                 if (line.find("Load=") != std::string::npos)
  96.                 {
  97.                     state = ATTRIBUTES;
  98.                     line.erase(0, line.find('=') + 1);
  99.                     tempAttributes.clear();
  100.                 }
  101.                 else
  102.                 {
  103.                     state = CONTENTS;
  104.                     tempContents.clear();
  105.                 }
  106.                 line.erase(std::remove(line.begin(), line.end(), '['), line.end());
  107.                 std::stringstream str(line);
  108.                 while (str)
  109.                 {
  110.                     std::getline(str, line, ']');
  111.                     if (line != "")
  112.                     {
  113.                         if (state == ATTRIBUTES)
  114.                         {
  115.                             tempAttributes.push_back(line);
  116.                         }
  117.                         else
  118.                         {
  119.                             tempContents.push_back(line);
  120.                         }
  121.                         std::cout << line << std::endl;
  122.                     }
  123.                 }
  124.                 if (state == CONTENTS && tempContents.size()>0)
  125.                 {
  126.                     attributes.push_back(tempAttributes);
  127.                     content.push_back(tempContents);
  128.                 }
  129.             }
  130.         }
  131.     }
  132. }
  133.  
  134. void FileManager::UnloadContent()
  135. {
  136.     tempAttributes.clear();
  137.     tempContents.clear();
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement