changes here, changes there
This commit is contained in:
41
ml_io.cpp
41
ml_io.cpp
@@ -23,6 +23,14 @@ std::string read_file_contents(const std::string &filename) {
|
||||
return contents;
|
||||
}
|
||||
|
||||
int write_file_contents(const std::string &filename, const std::string &content) {
|
||||
std::ofstream f;
|
||||
f.open(filename.c_str());
|
||||
int result = (f << content) ? 1 : 0;
|
||||
f.close();
|
||||
return result;
|
||||
}
|
||||
|
||||
MlValue list_dir(const std::string &path) {
|
||||
std::vector<MlValue> entries;
|
||||
|
||||
@@ -50,13 +58,42 @@ bool is_path_dir(const std::string &path) {
|
||||
return (bool) S_ISDIR(buf.st_mode);
|
||||
}
|
||||
|
||||
int mk_dir(const std::string &path) {
|
||||
int mk_path_dir(const std::string &path) {
|
||||
int r = ::mkdir(path.c_str(), 0755);
|
||||
return r;
|
||||
}
|
||||
|
||||
int rm_dir(const std::string &path) {
|
||||
int rm_path_dir(const std::string &path) {
|
||||
int r = ::rmdir(path.c_str());
|
||||
return r;
|
||||
}
|
||||
|
||||
MlValue exec_system_cmd(const std::string &cmd) {
|
||||
// TODO handle reading of stderr
|
||||
// https://stackoverflow.com/questions/478898/how-do-i-execute-a-command-and-get-the-output-of-the-command-within-c-using-po
|
||||
// https://jineshkj.wordpress.com/2006/12/22/how-to-capture-stdin-stdout-and-stderr-of-child-program/
|
||||
|
||||
std::string cmd_output;
|
||||
int stat;
|
||||
char buffer[128];
|
||||
FILE *pipe = popen(cmd.c_str(), "r");
|
||||
if (!pipe)
|
||||
throw std::runtime_error("popen() failed!");
|
||||
try {
|
||||
while (!feof(pipe)) {
|
||||
if (fgets(buffer, 128, pipe) != nullptr)
|
||||
cmd_output += buffer;
|
||||
}
|
||||
} catch (...) {
|
||||
stat = pclose(pipe);
|
||||
throw;
|
||||
}
|
||||
stat = pclose(pipe);
|
||||
int cmd_retval = WEXITSTATUS(stat);
|
||||
|
||||
// TODO add helper function for this
|
||||
std::vector<MlValue> lst;
|
||||
lst.push_back(MlValue(cmd_retval));
|
||||
lst.push_back(MlValue::string(cmd_output));
|
||||
return lst;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user