|
| 1 | +#ifndef BP_Dir_HH |
| 2 | +#define BP_Dir_HH |
| 3 | + |
| 4 | +#include <iostream> |
| 5 | +#include <istream> |
| 6 | +#include <fstream> |
| 7 | +#include <string> |
| 8 | +#include <cstdio> |
| 9 | +#include <sstream> |
| 10 | +#include "casm/BP_C++/BP_Parse.hh" |
| 11 | +#include "casm/BP_C++/BP_basic.hh" |
| 12 | +#include <errno.h> |
| 13 | +#include <dirent.h> |
| 14 | +#include <sys/stat.h> |
| 15 | +#include <unistd.h> |
| 16 | + |
| 17 | +namespace BP { |
| 18 | + |
| 19 | + /// \brief BP_Dir, a class for managing directories and files |
| 20 | + /// - list, create, copy, rename (move), delete directories |
| 21 | + /// - list, copy, rename (move), delete files |
| 22 | + /// - use absolute or relative filenames |
| 23 | + /// |
| 24 | + class BP_Dir { |
| 25 | + private: |
| 26 | + std::string _name; |
| 27 | + std::string _abs_name; |
| 28 | + std::string _joint; |
| 29 | + |
| 30 | + BP_Vec<std::string> files_list; |
| 31 | + BP_Vec<std::string> dirs_list; |
| 32 | + |
| 33 | + public: |
| 34 | + |
| 35 | + // // Initialize with current directory: |
| 36 | + // BP_Dir dir; |
| 37 | + // |
| 38 | + // // Get the current directory (just directory name): |
| 39 | + // cout << "The current directory: " << dir.name() << endl; |
| 40 | + // |
| 41 | + // // Get the current directory (include full path): |
| 42 | + // cout << "The current directory: " << dir.abs_name() << endl; |
| 43 | + // |
| 44 | + // |
| 45 | + // // get a list of files or directories in current directory: |
| 46 | + // BP_Vec<std::string> files = dir.files(); |
| 47 | + // BP_Vec<std::string> dirs = dir.dirs(); |
| 48 | + // |
| 49 | + // // or just use directly: |
| 50 | + // cout << "There are " << dir.files().size() << " files in this directory" << endl; |
| 51 | + // |
| 52 | + // // Change the current directory (of the BP_Dir object): |
| 53 | + // dir.cd( dir.dirs()[0]); |
| 54 | + // |
| 55 | + // // or directly: |
| 56 | + // dir.cd( "the/path/other_directory"); |
| 57 | + // |
| 58 | + // // to be more general you can do this: |
| 59 | + // dir.cd( "the" + dir.joint() + "path" + dir.joint() + "other_directory"); |
| 60 | + // |
| 61 | + // // Write a file in the new current directory using BP_Write: |
| 62 | + // BP_Write outfile(dir.abs_name("new_file.txt")); |
| 63 | + // outfile.newfile(); |
| 64 | + // outfile << "This is a new file" << endl; |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + BP_Dir(); |
| 69 | + |
| 70 | + std::string name() const { |
| 71 | + return _name; |
| 72 | + }; |
| 73 | + std::string abs_name() const { |
| 74 | + return _abs_name; |
| 75 | + }; |
| 76 | + |
| 77 | + void refresh(); |
| 78 | + bool cd(std::string s); |
| 79 | + bool mkdir(std::string s); |
| 80 | + bool rmdir(std::string s); |
| 81 | + bool rmdir(BP_Vec<std::string> s); |
| 82 | + bool recurs_rmdir(std::string s); |
| 83 | + bool recurs_rmdir(BP_Vec<std::string> s); |
| 84 | + bool rm(std::string s); |
| 85 | + bool rm(BP_Vec<std::string> s); |
| 86 | + bool rename(std::string s1, std::string s2); |
| 87 | + bool copy(std::string s1, std::string s2); |
| 88 | + bool copydir(std::string s1, std::string s2); |
| 89 | + bool cat(std::string s1, std::string s2, std::string s3); |
| 90 | + |
| 91 | + bool print(std::string s, std::ostream &sout) const; |
| 92 | + const BP_Vec<std::string> &files() const { |
| 93 | + return files_list; |
| 94 | + }; |
| 95 | + const BP_Vec<std::string> &dirs() const { |
| 96 | + return dirs_list; |
| 97 | + }; |
| 98 | + bool is_file(std::string s) const; |
| 99 | + bool is_dir(std::string s) const; |
| 100 | + bool exists(std::string s) const; |
| 101 | + |
| 102 | + void set_joint(std::string s) { |
| 103 | + _joint = s; |
| 104 | + }; |
| 105 | + std::string joint() const { |
| 106 | + return _joint; |
| 107 | + }; |
| 108 | + std::string without_path(std::string s) const; |
| 109 | + |
| 110 | + bool is_absolute_path(std::string s) const; |
| 111 | + void make_absolute_path(std::string &s) const; |
| 112 | + std::string abs_name(std::string s) const; |
| 113 | + |
| 114 | + |
| 115 | + private: |
| 116 | + bool generate_files_dirs(); |
| 117 | + bool remove_file(std::string s); |
| 118 | + bool remove_dir(std::string s); |
| 119 | + bool recurs_remove_dir(std::string s); |
| 120 | + bool change_directory(std::string s); |
| 121 | + |
| 122 | + }; |
| 123 | + |
| 124 | + |
| 125 | +} |
| 126 | + |
| 127 | +#endif // BP_Dir_HH |
| 128 | + |
0 commit comments