Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. #ifndef FILEMANAGER_H
  2. #define FILEMANAGER_H
  3.  
  4. #include <string>
  5. #include <vector>
  6.  
  7. #include <boost\filesystem.hpp>
  8. #include <boost\filesystem\fstream.hpp>
  9.  
  10. #include "..\base\ManagerBase.h"
  11.  
  12. enum EFileErrorType
  13. {
  14.     FILE_NO_ERROR = 0,
  15.     FILE_ERROR_NOT_EXISTS = 1,
  16.     FILE_ERROR_LOAD = 2,
  17.     FILE_ERROR_SAVE = 3,
  18.     FILE_ERROR_RELOAD = 4
  19. };
  20.  
  21. class FileManager : public ManagerBase
  22. {
  23. public:
  24.     FileManager(const std::string& name, bool forceCreate = false);
  25.     ~FileManager();
  26.  
  27.     void create();
  28.     void load();
  29.     void save();
  30.     void reload();
  31.  
  32.     std::string searchString(const std::string& str);
  33.     void replaceString(const std::string& strOld, const std::string& strNew);
  34.  
  35.     std::vector<std::string> getContent() const { return m_content; }
  36.     void addContent(const std::string& str);
  37.  
  38.     int getLines() const { return m_content.size(); }
  39.     std::string getPath() const { return m_path.string(); }
  40.  
  41. private:
  42.     boost::filesystem::path m_path;
  43.     std::vector<std::string> m_content;
  44.  
  45. };
  46.  
  47. #endif  // FILEMANAGER_H
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. #include "FileManager.h"
  55.  
  56. #include <boost\algorithm\string.hpp>
  57.  
  58. FileManager::FileManager(const std::string& name, bool forceCreate/* = false*/)
  59. {
  60.     m_path = boost::filesystem::path(name);
  61.     if (!forceCreate && !boost::filesystem::exists(m_path))
  62.         m_lastError = FILE_ERROR_NOT_EXISTS;
  63.  
  64.     if (forceCreate && boost::filesystem::exists(m_path))
  65.         load();
  66.     else if (forceCreate && !boost::filesystem::exists(m_path))
  67.         create();
  68.     else if(!forceCreate && isGood())
  69.         load();
  70. }
  71.  
  72. FileManager::~FileManager()
  73. {
  74.     if (isGood())
  75.         save();
  76. }
  77.  
  78. void FileManager::create()
  79. {
  80.     boost::filesystem::ofstream fileHandle(m_path, std::ios_base::trunc);
  81.     fileHandle.close();
  82. }
  83.  
  84. void FileManager::load()
  85. {
  86.     boost::filesystem::ifstream fileHandle(m_path);
  87.     if (!fileHandle.is_open())
  88.     {
  89.         m_lastError = FILE_ERROR_LOAD;
  90.         return;
  91.     }
  92.  
  93.     for (std::string buffer; std::getline(fileHandle, buffer);)
  94.         m_content.push_back(buffer);
  95.  
  96.     fileHandle.close();
  97. }
  98.  
  99. void FileManager::save()
  100. {
  101.     boost::filesystem::ofstream fileHandle(m_path, std::ios_base::trunc);
  102.     if (!fileHandle.is_open())
  103.     {
  104.         m_lastError = FILE_ERROR_SAVE;
  105.         return;
  106.     }
  107.  
  108.     for (auto& it : m_content)
  109.         fileHandle << it << std::endl;
  110.  
  111.     fileHandle.close();
  112. }
  113.  
  114. void FileManager::reload()
  115. {
  116.     if (!isGood())
  117.     {
  118.         m_lastError = FILE_ERROR_RELOAD;
  119.         return;
  120.     }
  121.  
  122.     m_content.clear();
  123.     load();
  124. }
  125.  
  126. std::string FileManager::searchString(const std::string & str)
  127. {
  128.     for (auto& it : m_content)
  129.     {
  130.         if (boost::contains(it, str))
  131.             return it;
  132.     }
  133.  
  134.     return std::string();
  135. }
  136.  
  137. void FileManager::replaceString(const std::string & strOld, const std::string & strNew)
  138. {
  139.     for (auto& it : m_content)
  140.     {
  141.         if (boost::contains(it, strOld))
  142.         {
  143.             int index = &it - &m_content[0];
  144.             m_content[index] = strNew;
  145.         }
  146.     }
  147. }
  148.  
  149. void FileManager::addContent(const std::string & str)
  150. {
  151.     m_content.push_back(str);
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement