-
Notifications
You must be signed in to change notification settings - Fork 302
/
os.cpp
84 lines (74 loc) · 2.65 KB
/
os.cpp
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
81
82
83
84
/***********************************************************************************
* Copyright (c) 2016, Johan Mabille, Loic Gouarin, Sylvain Corlay, Wolf Vollprecht *
* Copyright (c) 2016, QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
************************************************************************************/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "xeus-cling/xoptions.hpp"
#include "../xparser.hpp"
#include "os.hpp"
namespace xcpp
{
argparser writefile::get_options()
{
argparser argpars("file", XEUS_CLING_VERSION, argparse::default_arguments::none);
argpars.add_description("write file");
argpars.add_argument("-a", "--append")
.help("append")
.default_value(false)
.implicit_value(true);
argpars.add_argument("filename")
.help("filename")
.required();
// Add custom help (does not call `exit` avoiding to restart the kernel)
argpars.add_argument("-h", "--help")
.action([&](const std::string & /*unused*/)
{
std::cout << argpars.help().str();
})
.default_value(false)
.help("shows help message")
.implicit_value(true)
.nargs(0);
return argpars;
}
void writefile::operator()(const std::string& line, const std::string& cell)
{
auto argpars = get_options();
argpars.parse(line);
auto filename = argpars.get<std::string>("filename");
std::ofstream file;
// TODO: check permission rights
if (is_file_exist(filename.c_str()))
{
if (argpars["-a"] == true)
{
file.open(filename, std::ios::app);
std::cout << "Appending to " << filename << "\n";
}
else
{
file.open(filename);
std::cout << "Overwriting " << filename << "\n";
}
}
else
{
file.open(filename);
std::cout << "Writing " << filename << "\n";
}
file << cell << "\n";
file.close();
}
bool writefile::is_file_exist(const char* fileName)
{
std::ifstream infile(fileName);
return infile.good();
}
}