57tksystem *tksystem::g_tksystem =
nullptr;
61 if ( g_tksystem ==
nullptr ) {
70 glog.set_class(
"tksystem");
71 glog.set_method(
tkstring::form(
"getdir(%s, std::vector<tkstring> &_files)",_dir.data()));
75 if((dp = opendir(_dir.c_str())) ==
nullptr) {
81 while ((dirp = readdir(dp)) !=
nullptr) {
82 _files.emplace_back(
tkstring(dirp->d_name));
92 glog.set_class(
"tksystem");
98 if(path==name) path =
"./";
102 if((dp = opendir(path.c_str())) ==
nullptr) {
109 while ((dirp = readdir(dp)) !=
nullptr) {
110 if(!strcmp(dirp->d_name,name.data())) found =
true;
120 std::vector<tkstring> files;
122 for(
const auto &file: files) {
123 if(file.match(_pattern)) std::cout <<
" -- " << file << std::endl;
128 std::vector<tkstring> files;
130 for(
size_t ifile=0 ; ifile<files.size() ; ifile++) {
131 if(!files.at(ifile).match(_pattern)) {
132 files.erase(files.begin()+ifile);
141 if(_force)
return system(
tkstring::form(
"rm -rf %s",_dirname.data()));
147 if(_force)
return system(
tkstring::form(
"rm -f %s",_filename.data()));
153 if(_recursive)
return system(
tkstring::form(
"mkdir -p %s",_dirname.data()));
159 return system(
tkstring::form(
"mv %s %s",_from.data(),_to.data()));
165 return system(
tkstring::form(
"unzip %s -d %s/",_filename.data(),_outputdir.data()));
170 return system(
tkstring::form(
"ls %s %s",_option.data(),_dirname.data()));
176 if(!_replace_existing) command +=
"-N ";
177 command += _filename;
179 if(_use_filename) command +=
" -O ";
180 else command +=
" -P ";
181 command += _outputdir;
184 return system(command.data());
189 std::array<char, 128> buffer{};
191 std::unique_ptr<FILE,
decltype(&pclose)> pipe(popen(command.data(),
"r"), pclose);
193 throw std::runtime_error(
"popen() failed!");
195 while (fgets(buffer.data(), buffer.size(), pipe.get()) !=
nullptr) {
196 result += buffer.data();
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
static const char * form(const char *_format,...)
tkstring get_last_occurence(const char *_s1)
tkstring remove_last_occurence(const char *_s1)
Interface to the system (tested on linux and mac os)
int make_dir(const tkstring &_dirname, bool _recursive=true)
bool exists(tkstring _file_name)
int unzip_file(const tkstring &_filename, tkstring _outputdir="")
int remove_file(const tkstring &_filename, bool _force=false)
std::vector< tkstring > get_list_of_files(const tkstring &_dir, const tkstring &_pattern="*")
int remove_dir(const tkstring &_dirname, bool _force=false)
int ls_dir(const tkstring &_dirname, tkstring _option="-l")
int getdir(tkstring _dir, std::vector< tkstring > &_files)
int move(const tkstring &_from, const tkstring &_to)
void list_files(const tkstring &_dir, const tkstring &_pattern)
int download_file(const tkstring &_filename, const tkstring &_outputdir="", bool _replace_existing=false, bool _use_filename=false)
tkstring get_system_command_output(tkstring command)
tklog & error_v(tklog &log)
tklog & do_endl(tklog &log)