Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <iterator>
  4. #include <sys/stat.h>
  5. #include <dirent.h>
  6. #include <queue>
  7. #include <regex>
  8.  
  9. namespace stroll {
  10.  
  11. using namespace std;
  12.  
  13. typedef unsigned int uint;
  14. typedef vector<string> vstr;
  15.  
  16. void st(const string path1, const string path2, struct stat *st1, struct stat *st2){
  17. if(stat(path1.c_str(),st1) == -1){
  18. exit(-1);
  19. }
  20. if(stat(path2.c_str(),st2) == -1){
  21. exit(-1);
  22. }
  23. }
  24.  
  25. bool sortByMtime(const string path1, const string path2){
  26. struct stat st1, st2;
  27. st(path1,path2,&st1,&st2);
  28. return st1.st_mtime < st2.st_mtime;
  29. }
  30.  
  31. bool sortByMtimeDesc(const string path1, const string path2){
  32. struct stat st1, st2;
  33. st(path1,path2,&st1,&st2);
  34. return st1.st_mtime > st2.st_mtime;
  35. }
  36.  
  37. bool sortByLen(const string path1, const string path2){
  38. return path1.size() < path2.size();
  39. }
  40.  
  41. bool sortByLenDesc(const string path1, const string path2){
  42. return path1.size() > path2.size();
  43. }
  44.  
  45. bool sortByName(const string path1, const string path2){
  46. return path1 < path2;
  47. }
  48.  
  49. bool sortByNameDesc(const string path1, const string path2){
  50. return path1 > path2;
  51. }
  52.  
  53. class Stroll{
  54.  
  55. DIR *dir;
  56. struct dirent *dp;
  57. string topdir;
  58. string current;
  59. vstr files;
  60. vstr dirs;
  61. vstr no_path;
  62. vstr yes_path;
  63. vstr no_root;
  64. vstr yes_root;
  65. vstr no_file;
  66. vstr yes_file;
  67. bool (*sortby)(string, string);
  68.  
  69. public:
  70. bool read(){
  71. while(files.size()==0){
  72. string basedir = "";
  73. if(dirs.size()==0){
  74. return false;
  75. }else{
  76. basedir = dirs.front();
  77. dirs.erase(dirs.begin());
  78. }
  79.  
  80. dir = opendir(basedir.c_str());
  81. if(dir==NULL){
  82. return false;
  83. }
  84.  
  85. for(dp=readdir(dir);dp!=NULL;dp=readdir(dir)){
  86. if((string)dp->d_name=="." || (string)dp->d_name=="..")
  87. continue;
  88.  
  89. struct stat st;
  90. string filename = (string)dp->d_name;
  91. string path = basedir+"/"+filename;
  92. stat(path.c_str(),&st);
  93.  
  94. if((st.st_mode & S_IFMT) == S_IFREG){
  95. if(is_no_file(path) && is_yes_file(path)){
  96. files.push_back(path);
  97. }
  98. }else if((st.st_mode & S_IFMT) == S_IFDIR){
  99. if(is_no_path(path) && is_yes_path(path)){
  100. if(is_no_root(filename) && is_yes_root(filename)){
  101. dirs.push_back(path);
  102. }
  103. }
  104. }else{
  105. // Not supported
  106. continue;
  107. }
  108. }
  109. sort(dirs.begin(),dirs.end(),sortby);
  110. sort(files.begin(),files.end(),sortby);
  111. }
  112.  
  113. current = get_path();
  114. return true;
  115. }
  116.  
  117. bool is_no_root(string root){
  118. if(no_root.size()==0) return true;
  119. for(uint i=0;i<no_root.size();i++){
  120. regex re(no_root[i]);
  121. if( regex_search(root,re) ){
  122. return false;
  123. }
  124. }
  125. return true;
  126. }
  127.  
  128. bool is_yes_root(string root){
  129. if(yes_root.size()==0) return true;
  130. for(uint i=0;i<yes_root.size();i++){
  131. regex re(yes_root[i]);
  132. if( regex_search(root,re) ){
  133. return true;
  134. }
  135. }
  136. return false;
  137. }
  138.  
  139. bool is_no_path(string path){
  140. if(no_path.size()==0) return true;
  141. for(uint i=0;i<no_path.size();i++){
  142. regex re(no_path[i]);
  143. if( regex_search(path,re) ){
  144. return false;
  145. }
  146. }
  147. return true;
  148. }
  149.  
  150. bool is_yes_path(string path){
  151. if(yes_path.size()==0) return true;
  152. for(uint i=0;i<yes_path.size();i++){
  153. regex re(yes_path[i]);
  154. if( regex_search(path,re) ){
  155. return true;
  156. }
  157. }
  158. return false;
  159. }
  160.  
  161. bool is_no_file(string path){
  162. if(no_file.size()==0) return true;
  163. for(uint i=0;i<no_file.size();i++){
  164. regex re(no_file[i]);
  165. if( regex_search(path,re) ){
  166. return false;
  167. }
  168. }
  169. return true;
  170. }
  171.  
  172. bool is_yes_file(string path){
  173. if(yes_file.size()==0) return true;
  174. for(uint i=0;i<yes_file.size();i++){
  175. regex re(yes_file[i]);
  176. if( regex_search(path,re) ){
  177. return true;
  178. }
  179. }
  180. return false;
  181. }
  182.  
  183. string get_path(){
  184. string file = files.front();
  185. files.erase(files.begin());
  186. return file;
  187. }
  188.  
  189. string get(){
  190. return current;
  191. }
  192.  
  193. void set_sortby(bool (*sortby)(string,string)){
  194. this->sortby = sortby;
  195. }
  196.  
  197. void set_no_path(vstr no_path){
  198. this->no_path = no_path;
  199. }
  200.  
  201. void set_yes_path(vstr yes_path){
  202. this->yes_path = yes_path;
  203. }
  204.  
  205. void set_no_root(vstr no_root){
  206. this->no_root = no_root;
  207. }
  208.  
  209. void set_yes_root(vstr yes_root){
  210. this->yes_root = yes_root;
  211. }
  212.  
  213. void set_no_file(vstr no_file){
  214. this->no_file = no_file;
  215. }
  216.  
  217. void set_yes_file(vstr yes_file){
  218. this->yes_file = yes_file;
  219. }
  220.  
  221.  
  222. Stroll(string topdir,vstr no_path, vstr yes_path, vstr no_root, vstr yes_root, vstr no_file, vstr yes_file, bool (*sortby)(string,string)):topdir(topdir),no_path(no_path),yes_path(yes_path),no_root(no_root),yes_root(yes_root),no_file(no_file),yes_file(yes_file),sortby(sortby){
  223. dirs.push_back(topdir);
  224. }
  225.  
  226. Stroll(string topdir):topdir(topdir){
  227. dirs.push_back(topdir);
  228. sortby = sortByName;
  229. }
  230. };
  231.  
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement