REMOVED USER_STD macros, code formating, csv changes, few file functions
This commit is contained in:
62
ml_io.cpp
Normal file
62
ml_io.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
|
||||
#include "ml_io.h"
|
||||
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
|
||||
std::string read_file_contents(const std::string &filename) {
|
||||
std::ifstream f;
|
||||
f.open(filename.c_str());
|
||||
if (!f)
|
||||
throw std::runtime_error("could not open file");
|
||||
|
||||
f.seekg(0, std::ios::end);
|
||||
std::string contents;
|
||||
contents.reserve(f.tellg());
|
||||
f.seekg(0, std::ios::beg);
|
||||
contents.assign(std::istreambuf_iterator<char>(f),
|
||||
std::istreambuf_iterator<char>());
|
||||
f.close();
|
||||
|
||||
return contents;
|
||||
}
|
||||
|
||||
MlValue list_dir(const std::string &path) {
|
||||
std::vector<MlValue> entries;
|
||||
|
||||
DIR *dirp = opendir(path.c_str());
|
||||
if (dirp == NULL) {
|
||||
// handle error - probably not a dir or non existing path
|
||||
}
|
||||
struct dirent *dp;
|
||||
while ((dp = readdir(dirp)) != NULL) {
|
||||
entries.push_back(MlValue::string(dp->d_name));
|
||||
}
|
||||
closedir(dirp);
|
||||
|
||||
return MlValue(entries);
|
||||
}
|
||||
|
||||
bool is_path_file(const std::string &path) {
|
||||
std::ifstream ifile(path.c_str());
|
||||
return (bool) ifile;
|
||||
}
|
||||
|
||||
bool is_path_dir(const std::string &path) {
|
||||
struct stat buf;
|
||||
stat(path.c_str(), &buf);
|
||||
return (bool) S_ISDIR(buf.st_mode);
|
||||
}
|
||||
|
||||
int mk_dir(const std::string &path) {
|
||||
int r = ::mkdir(path.c_str(), 0755);
|
||||
return r;
|
||||
}
|
||||
|
||||
int rm_dir(const std::string &path) {
|
||||
int r = ::rmdir(path.c_str());
|
||||
return r;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user