]> git.tdb.fi Git - libs/core.git/blob - source/fs/unix/dir.cpp
Add move semantics to Variant
[libs/core.git] / source / fs / unix / dir.cpp
1 #include <unistd.h>
2 #include <dirent.h>
3 #include <sys/stat.h>
4 #include <msp/core/systemerror.h>
5 #include <msp/strings/regex.h>
6 #include "dir.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace FS {
12
13 void mkdir(const Path &path, int mode)
14 {
15         if(::mkdir(path.str().c_str(), mode)==-1)
16                 throw system_error("mkdir");
17 }
18
19 void rmdir(const Path &path)
20 {
21         if(::rmdir(path.str().c_str())==-1)
22                 throw system_error("rmdir");
23 }
24
25 vector<string> list_filtered(const Path &path, const string &filter)
26 {
27         Regex r_filter(filter);
28
29         vector<string> result;
30         DIR *dir = opendir(path.str().c_str());
31         if(!dir)
32                 throw system_error("opendir");
33
34         while(dirent *de = readdir(dir))
35         {
36                 const char *fn = de->d_name;
37                 if(fn[0]=='.' && (fn[1]==0 || (fn[1]=='.' && fn[2]==0)))
38                         continue;
39                 if(r_filter.match(fn))
40                         result.push_back(fn);
41         }
42         closedir(dir);
43
44         return result;
45 }
46
47 Path getcwd()
48 {
49         char buf[1024];
50         return ::getcwd(buf, sizeof(buf));
51 }
52
53 void chdir(const Path &path)
54 {
55         if(::chdir(path.str().c_str())==-1)
56                 throw system_error("chdir");
57 }
58
59 } // namespace FS
60 } // namespace Msp