iw4x-client/src/Utils/IO.cpp

75 lines
1.4 KiB
C++
Raw Normal View History

2016-07-11 11:14:58 -04:00
#include "STDInclude.hpp"
namespace Utils
{
namespace IO
{
bool FileExists(std::string file)
{
return std::ifstream(file).good();
}
void WriteFile(std::string file, std::string data)
{
2016-11-02 15:50:17 -04:00
auto pos = file.find_last_of("/\\");
if (pos != std::string::npos)
{
CreateDirectory(file.substr(0, pos));
}
2016-07-11 11:14:58 -04:00
std::ofstream stream(file, std::ios::binary);
if (stream.is_open())
{
stream.write(data.data(), data.size());
stream.close();
}
}
std::string ReadFile(std::string file)
{
std::string buffer;
if (FileExists(file))
{
std::streamsize size = 0;
std::ifstream stream(file, std::ios::binary);
if (!stream.is_open()) return buffer;
stream.seekg(0, std::ios::end);
size = stream.tellg();
stream.seekg(0, std::ios::beg);
if (size > -1)
{
buffer.clear();
buffer.resize((uint32_t)size);
stream.read(const_cast<char*>(buffer.data()), size);
}
stream.close();
}
return buffer;
}
2016-08-31 11:54:08 -04:00
2016-09-04 07:06:44 -04:00
bool CreateDirectory(std::string dir)
2016-08-31 11:54:08 -04:00
{
return std::experimental::filesystem::create_directories(dir);
}
2016-08-31 11:54:08 -04:00
std::vector<std::string> ListFiles(std::string dir)
{
std::vector<std::string> files;
2016-08-31 11:54:08 -04:00
for (auto& file : std::experimental::filesystem::directory_iterator(dir))
2016-08-31 11:54:08 -04:00
{
files.push_back(file.path().generic_string());
2016-08-31 11:54:08 -04:00
}
2016-09-04 07:06:44 -04:00
return files;
2016-08-31 11:54:08 -04:00
}
2016-07-11 11:14:58 -04:00
}
}