write_file.hpp 952 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#ifndef GUARD_OLC_WRITE_FILE_HPP
#define GUARD_OLC_WRITE_FILE_HPP

#include <boost/filesystem.hpp>
#include <manage_ptr.hpp>
#include <fstream>

namespace olCompile {

using FilePtr = OLC_MANAGE_PTR(FILE*, std::fclose);

inline void WriteFile(const std::string& content, const boost::filesystem::path& name)
{
    // std::cerr << "Write file: " << name << std::endl;
    FilePtr f{std::fopen(name.string().c_str(), "w")};
    if(std::fwrite(content.c_str(), 1, content.size(), f.get()) != content.size())
        throw std::runtime_error("Failed to write to file");
}

inline void WriteFile(const std::vector<char>& content, const boost::filesystem::path& name)
{
    // std::cerr << "Write file: " << name << std::endl;
    FilePtr f{std::fopen(name.string().c_str(), "w")};
    if(std::fwrite(&content[0], 1, content.size(), f.get()) != content.size())
        throw std::runtime_error("Failed to write to file");
}

} // namespace olCompile

#endif