Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 253 lines (214 sloc) 3.978 kb
a9461cd initializing repo from version 0.10.2 minus the example files
Julian authored
1 // directory.cpp
2 // Revision 9-jan-2005
3
4 #include "directory.h"
5
6 #include "error.h"
7 #include "showerror.h"
8
9 #include "trace.h"
10
11 #include <stdio.h>
12 #include <errno.h>
13
14 // We use the unix style even on windows under cygwin.
15 #if defined __unix__ || defined __linux__ || defined __NetBSD__ || \
16 defined __APPLE__
17 #define USE_UNIX
18 #endif
19
20 #ifdef USE_UNIX
21
22 #include <unistd.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <sys/utsname.h>
26 #include <glob.h>
27
28 #else
29
30 #include <windows.h>
31 #include <dir.h>
32 #include <dos.h>
33
34 #ifdef __MINGW32__
35 #include <io.h>
36 #else
37 using std::unlink;
38 #endif
39
40 #endif
41
42 // ********************* Directory::Internal *************************
43
44 class Directory::Internal {
45 public:
46 Internal ();
47 ~Internal ();
48 std::string findfirst (const std::string & str);
49 std::string findnext ();
50 void closesearch ();
51 private:
52 Internal (const Internal &); // Forbidden
53 Internal & operator = (const Internal &); // Forbidden
54
55 bool finding;
56 #ifdef USE_UNIX
57 glob_t g;
58 size_t n;
59 #else
60 WIN32_FIND_DATA fd;
61 HANDLE h;
62 #endif
63 };
64
65 Directory::Internal::Internal () :
66 finding (false)
67 {
68 }
69
70 Directory::Internal::~Internal ()
71 {
72 TRACEFUNC (tr, "Directory::Internal::~Internal");
73
74 if (finding)
75 closesearch ();
76 }
77
78 std::string Directory::Internal:: findfirst (const std::string & str)
79 {
80 if (finding)
81 closesearch ();
82 finding= true;
83 #ifdef USE_UNIX
84 glob (str.c_str (), 0, NULL, & g);
85 if (g.gl_pathc == 0)
86 {
87 closesearch ();
88 return std::string ();
89 }
90 n= 1;
91 return g.gl_pathv [0];
92 #else
93 h= FindFirstFile (str.c_str (), & fd);
94 if (h == INVALID_HANDLE_VALUE)
95 {
96 closesearch ();
97 return std::string ();
98 }
99 return fd.cFileName;
100 #endif
101 }
102
103 std::string Directory::Internal:: findnext ()
104 {
105 if (! finding)
106 return std::string ();
107 #ifdef USE_UNIX
108 if (n >= static_cast <size_t> (g.gl_pathc) )
109 {
110 closesearch ();
111 return std::string ();
112 }
113 ++n;
114 return g.gl_pathv [n - 1];
115 #else
116 if (! FindNextFile (h, & fd) )
117 {
118 closesearch ();
119 return std::string ();
120 }
121 return fd.cFileName;
122 #endif
123 }
124
125 void Directory::Internal::closesearch ()
126 {
127 #ifdef USE_UNIX
128 globfree (& g);
129 #else
130 if (h != INVALID_HANDLE_VALUE)
131 FindClose (h);
132 #endif
133 finding= false;
134 }
135
136 // ********************* Directory *************************
137
138 Directory::Directory () :
139 pin (new Internal)
140 {
141 }
142
143 Directory::~Directory ()
144 {
145 delete pin;
146 }
147
148 std::string Directory::findfirst (const std::string & str)
149 {
150 return pin->findfirst (str);
151 }
152
153 std::string Directory::findnext ()
154 {
155 return pin->findnext ();
156 }
157
158 // ********************* Other functions *************************
159
160 void remove_file (const std::string & filename)
161 {
162 if (unlink (filename.c_str () ) != 0)
163 {
164 switch (errno)
165 {
166 case ENOENT:
167 throw ErrFileNotFound;
168 default:
169 showlasterror ();
170 throw ErrOperatingSystem;
171 }
172 }
173 }
174
175 void rename_file (const std::string & orig, const std::string & dest)
176 {
177 if (rename (orig.c_str (), dest.c_str () ) != 0)
178 {
179 switch (errno)
180 {
181 case ENOENT:
182 throw ErrFileNotFound;
183 default:
184 showlasterror ();
185 throw ErrRenameFile;
186 }
187 }
188 }
189
190 void change_dir (const std::string & dirname)
191 {
192 if (chdir (dirname.c_str () ) != 0)
193 {
194 showlasterror ();
195 throw ErrOperatingSystem;
196 }
197 }
198
199 void make_dir (const std::string & dirname)
200 {
201 #ifdef USE_UNIX
202 int r= mkdir (dirname.c_str (), 0777);
203 #else
204 int r= mkdir (dirname.c_str () );
205 #endif
206 if (r != 0)
207 {
208 showlasterror ();
209 throw ErrOperatingSystem;
210 }
211 }
212
213 void remove_dir (const std::string & dirname)
214 {
215 if (rmdir (dirname.c_str () ) != 0)
216 {
217 showlasterror ();
218 throw ErrOperatingSystem;
219 }
220 }
221
222 void sleep_milisec (unsigned long n)
223 {
224 TRACEFUNC (tr, "sleep_milisec");
225
226 #ifdef USE_UNIX
227
228 if (n == 0)
229 {
230 usleep (0);
231 }
232 else
233 {
234 n*= 1000;
235 unsigned int sec= n / 1000000;
236 n%= 1000000;
237 TRMESSAGE (tr, util::to_string (sec) + " sec, " +
238 util::to_string (n) + " microsec");
239 if (sec > 0)
240 sleep (sec);
241 if (n > 0)
242 usleep (n);
243 }
244
245 #else
246
247 Sleep (static_cast <DWORD> (n) );
248
249 #endif
250 }
251
252 // End of directory.cpp
Something went wrong with that request. Please try again.