cgv
Loading...
Searching...
No Matches
dir.cxx
1#include <cgv/utils/dir.h>
2#include <cgv/utils/file.h>
3
4#ifdef _WIN32
5#include <direct.h>
6#else
7#include <unistd.h>
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <fcntl.h>
11#include <iostream>
12#include <cstdlib>
13
14
15#endif
16
17namespace cgv {
18 namespace utils {
19 namespace dir {
20
21bool exists(const std::string& dir_name)
22{
23#ifdef _WIN32
24 void* handle = file::find_first(dir_name+"\\*.*");
25 bool ret = (bool)handle;
26 if (handle)
27 file::find_close(handle);
28 return ret;
29#else
30 struct stat myStat;
31 return ((stat(dir_name.c_str(), &myStat) == 0) && (((myStat.st_mode) & S_IFMT) == S_IFDIR));
32#endif
33}
34
35bool chdir(const std::string& dir_name)
36{
37#ifdef _WIN32
38 return _chdir(dir_name.c_str()) == 0;
39#else
40 return ::chdir(dir_name.c_str());
41// std::cerr << "Not Implemented\n" << std::endl;
42#endif
43 return false;
44}
45
46bool mkdir(const std::string& dir_name)
47{
48#ifdef _WIN32
49 return _mkdir(dir_name.c_str()) == 0;
50#else
51 return ::mkdir(dir_name.c_str(),S_IRWXU|S_IRWXG|S_IRWXO);
52// std::cerr << "Not Implemented\n" << std::endl;
53#endif
54}
55
56bool rmdir(const std::string& dir_name)
57{
58#ifdef _WIN32
59 return _rmdir(dir_name.c_str()) == 0;
60#else
61 return ::rmdir(dir_name.c_str());
62// std::cerr << "Not Implemented\n" << std::endl;
63#endif
64 return false;
65}
66
67
68std::string current()
69{
70#ifdef _WIN32
71 char buf[FILENAME_MAX];
72 char * pbuf = _getcwd(buf,FILENAME_MAX);
73 std::string s = (pbuf) ? buf : "";
74#else
75 char* r = getcwd(NULL,0);//buff,256);
76 std::string s="";
77 if (r == NULL) {
78 std::cerr<< "failed"<<std::endl;
79 }
80 else {
81 s = r;
82 free(r);
83 }
84#endif
85 return s;
86}
87
88bool recursive_glob(const std::string& dir_name, const std::string& sub_path, std::vector<std::string>& file_names, const std::string& filter, bool recursive, bool short_file_names, std::vector<std::string>* subdir_names)
89{
90 std::string search_path = dir_name + sub_path + filter;
91 void* handle = file::find_first(search_path);
92 while (handle != 0) {
93 std::string file_name = (short_file_names ? sub_path : dir_name + sub_path) + file::find_name(handle);
94 if (file::find_directory(handle)) {
95 if (file::find_name(handle) != "." && file::find_name(handle) != "..") {
96 if (subdir_names)
97 subdir_names->push_back(file_name);
98 if (recursive)
99 recursive_glob(dir_name, sub_path + file::find_name(handle) + "/", file_names, filter, true, short_file_names, subdir_names);
100 }
101 }
102 else {
103 file_names.push_back(file_name);
104 }
105 handle = file::find_next(handle);
106 }
107 if (recursive) {
108 search_path = dir_name + sub_path + "*";
109 void* handle = file::find_first(search_path);
110 while (handle != 0) {
111 std::string file_name = (short_file_names ? sub_path : dir_name + sub_path) + file::find_name(handle);
112 if (file::find_directory(handle) && file::find_name(handle) != "." && file::find_name(handle) != "..") {
113 if (subdir_names)
114 subdir_names->push_back(file_name);
115 if (recursive)
116 recursive_glob(dir_name, sub_path + file::find_name(handle) + "/", file_names, filter, true, short_file_names, subdir_names);
117 }
118 handle = file::find_next(handle);
119 }
120 }
121 return true;
122}
123
125bool glob(const std::string& dir_name, std::vector<std::string>& file_names, const std::string& filter, bool recursive, bool short_file_names, std::vector<std::string>* subdir_names)
126{
127 if (!exists(dir_name))
128 return false;
129 recursive_glob(dir_name+"/", "", file_names, filter, recursive, short_file_names, subdir_names);
130 return true;
131}
132 }
133 }
134}
the cgv namespace
Definition print.h:11