m8
/
src
/
m8
/
writer.cc
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "m8/writer.hh"
#include "ob/string.hh"
#include "ob/term.hh"
namespace aec = OB::Term::ANSI_Escape_Codes;
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <filesystem>
namespace fs = std::filesystem;
Writer::Writer()
{
}
Writer::~Writer()
{
// if (fs::path(".m8/swp").empty())
// {
// fs::remove_all(".m8/swp");
// }
// if (fs::path(".m8").empty())
// {
// fs::remove_all(".m8");
// }
}
void Writer::open(std::string const& file_name)
{
file_name_ = file_name;
fs::path fp {file_name_};
file_tmp_ = ".m8/swp/" + OB::String::url_encode(fp) + file_ext_;
fs::path p {file_tmp_};
file_.open(p, std::ios::app);
if (! file_.is_open())
{
throw std::runtime_error("could not open the output file");
}
}
void Writer::write(std::string const& str)
{
if (file_name_.empty())
{
std::cout << str;
if (! str.empty() && str.back() != '\n')
{
std::cout << aec::wrap("%\n", aec::reverse) << std::flush;
}
}
else
{
file_ << str;
}
}
void Writer::flush()
{
if (file_name_.empty())
{
std::cout << std::flush;
}
else
{
file_ << std::flush;
}
}
void Writer::close()
{
if (file_.is_open())
{
file_.close();
}
}