Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef FILEMANAGER_H
- #define FILEMANAGER_H
- #include <string>
- #include <vector>
- #include <boost\filesystem.hpp>
- #include <boost\filesystem\fstream.hpp>
- #include "..\base\ManagerBase.h"
- enum EFileErrorType
- {
- FILE_NO_ERROR = 0,
- FILE_ERROR_NOT_EXISTS = 1,
- FILE_ERROR_LOAD = 2,
- FILE_ERROR_SAVE = 3,
- FILE_ERROR_RELOAD = 4
- };
- class FileManager : public ManagerBase
- {
- public:
- FileManager(const std::string& name, bool forceCreate = false);
- ~FileManager();
- void create();
- void load();
- void save();
- void reload();
- std::string searchString(const std::string& str);
- void replaceString(const std::string& strOld, const std::string& strNew);
- std::vector<std::string> getContent() const { return m_content; }
- void addContent(const std::string& str);
- int getLines() const { return m_content.size(); }
- std::string getPath() const { return m_path.string(); }
- private:
- boost::filesystem::path m_path;
- std::vector<std::string> m_content;
- };
- #endif // FILEMANAGER_H
- #include "FileManager.h"
- #include <boost\algorithm\string.hpp>
- FileManager::FileManager(const std::string& name, bool forceCreate/* = false*/)
- {
- m_path = boost::filesystem::path(name);
- if (!forceCreate && !boost::filesystem::exists(m_path))
- m_lastError = FILE_ERROR_NOT_EXISTS;
- if (forceCreate && boost::filesystem::exists(m_path))
- load();
- else if (forceCreate && !boost::filesystem::exists(m_path))
- create();
- else if(!forceCreate && isGood())
- load();
- }
- FileManager::~FileManager()
- {
- if (isGood())
- save();
- }
- void FileManager::create()
- {
- boost::filesystem::ofstream fileHandle(m_path, std::ios_base::trunc);
- fileHandle.close();
- }
- void FileManager::load()
- {
- boost::filesystem::ifstream fileHandle(m_path);
- if (!fileHandle.is_open())
- {
- m_lastError = FILE_ERROR_LOAD;
- return;
- }
- for (std::string buffer; std::getline(fileHandle, buffer);)
- m_content.push_back(buffer);
- fileHandle.close();
- }
- void FileManager::save()
- {
- boost::filesystem::ofstream fileHandle(m_path, std::ios_base::trunc);
- if (!fileHandle.is_open())
- {
- m_lastError = FILE_ERROR_SAVE;
- return;
- }
- for (auto& it : m_content)
- fileHandle << it << std::endl;
- fileHandle.close();
- }
- void FileManager::reload()
- {
- if (!isGood())
- {
- m_lastError = FILE_ERROR_RELOAD;
- return;
- }
- m_content.clear();
- load();
- }
- std::string FileManager::searchString(const std::string & str)
- {
- for (auto& it : m_content)
- {
- if (boost::contains(it, str))
- return it;
- }
- return std::string();
- }
- void FileManager::replaceString(const std::string & strOld, const std::string & strNew)
- {
- for (auto& it : m_content)
- {
- if (boost::contains(it, strOld))
- {
- int index = &it - &m_content[0];
- m_content[index] = strNew;
- }
- }
- }
- void FileManager::addContent(const std::string & str)
- {
- m_content.push_back(str);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement