write_file.hpp 962 Bytes
Newer Older
1
2
3
4
5
6
7
#ifndef GUARD_OLC_WRITE_FILE_HPP
#define GUARD_OLC_WRITE_FILE_HPP

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

Chao Liu's avatar
rename  
Chao Liu committed
8
namespace online_compile {
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

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");
}

Chao Liu's avatar
rename  
Chao Liu committed
28
} // namespace online_compile
29
30

#endif