1 | /* |
---|
2 | Copyright (c) 2006-2018 Elmar Pruesse <elmar.pruesse@ucdenver.edu> |
---|
3 | |
---|
4 | This file is part of SINA. |
---|
5 | SINA is free software: you can redistribute it and/or modify it under |
---|
6 | the terms of the GNU General Public License as published by the Free |
---|
7 | Software Foundation, either version 3 of the License, or (at your |
---|
8 | option) any later version. |
---|
9 | |
---|
10 | SINA is distributed in the hope that it will be useful, but WITHOUT ANY |
---|
11 | WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
12 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
13 | for more details. |
---|
14 | |
---|
15 | You should have received a copy of the GNU General Public License |
---|
16 | along with SINA. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | |
---|
18 | Additional permission under GNU GPL version 3 section 7 |
---|
19 | |
---|
20 | If you modify SINA, or any covered work, by linking or combining it |
---|
21 | with components of ARB (or a modified version of that software), |
---|
22 | containing parts covered by the terms of the |
---|
23 | ARB-public-library-license, the licensors of SINA grant you additional |
---|
24 | permission to convey the resulting work. Corresponding Source for a |
---|
25 | non-source form of such a combination shall include the source code |
---|
26 | for the parts of ARB used as well as that of the covered work. |
---|
27 | */ |
---|
28 | |
---|
29 | #ifndef _TEMPFILE_H_ |
---|
30 | #define _TEMPFILE_H_ |
---|
31 | |
---|
32 | #include <boost/filesystem.hpp> |
---|
33 | |
---|
34 | namespace sina { |
---|
35 | |
---|
36 | class TempFile : boost::noncopyable { |
---|
37 | public: |
---|
38 | TempFile(const boost::filesystem::path model = "sina-%%%%-%%%%-%%%%") |
---|
39 | : _path(boost::filesystem::temp_directory_path() |
---|
40 | / boost::filesystem::unique_path(model)) |
---|
41 | {} |
---|
42 | ~TempFile() { |
---|
43 | boost::filesystem::remove(_path); |
---|
44 | } |
---|
45 | operator const boost::filesystem::path() const { |
---|
46 | return _path; |
---|
47 | } |
---|
48 | const boost::filesystem::path& path() const { |
---|
49 | return _path; |
---|
50 | } |
---|
51 | void dump(std::ostream& out) const { |
---|
52 | out << "Dumping Tempfile " << this << std::endl; |
---|
53 | std::string sep = "----------------------"; |
---|
54 | out << sep << std::endl; |
---|
55 | boost::filesystem::ifstream file(*this); |
---|
56 | std::string line; |
---|
57 | while (getline(file, line).good()) { |
---|
58 | out << line << std::endl; |
---|
59 | } |
---|
60 | out << sep << std::endl; |
---|
61 | } |
---|
62 | std::string load() const { |
---|
63 | auto closer = [](FILE* fp){ if(fp) std::fclose(fp);}; |
---|
64 | auto fp = std::unique_ptr<FILE, decltype(closer)>( |
---|
65 | std::fopen(_path.native().c_str(), "r"), closer); |
---|
66 | std::string res; |
---|
67 | res.resize(boost::filesystem::file_size(_path)); |
---|
68 | auto len = std::fread(const_cast<char*>(res.data()), 1, res.size(), fp.get()); |
---|
69 | return res; |
---|
70 | } |
---|
71 | private: |
---|
72 | boost::filesystem::path _path; |
---|
73 | }; |
---|
74 | std::ostream& operator<<(std::ostream& out, const TempFile& t) { return out << t.path(); } |
---|
75 | |
---|
76 | } // namespace sina |
---|
77 | |
---|
78 | #endif // _TEMPFILE_H_ |
---|
79 | |
---|
80 | /* |
---|
81 | Local Variables: |
---|
82 | mode:c++ |
---|
83 | c-file-style:"stroustrup" |
---|
84 | c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) |
---|
85 | indent-tabs-mode:nil |
---|
86 | fill-column:99 |
---|
87 | End: |
---|
88 | */ |
---|
89 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : |
---|