2 #include <msp/core/mutex.h>
3 #include <msp/core/systemerror.h>
4 #include <msp/strings/regex.h>
12 void mkdir(const Path &path, int)
14 if(!CreateDirectory(path.str().c_str(), NULL))
15 throw system_error("CreateDirectory");
18 void rmdir(const Path &path)
20 if(!RemoveDirectory(path.str().c_str()))
21 throw system_error("RemoveDirectory");
24 vector<string> list_filtered(const Path &path, const string &filter)
26 Regex r_filter(filter);
28 vector<string> result;
29 WIN32_FIND_DATA entry;
30 string pattern = path.str()+"\\*";
31 HANDLE search_handle = FindFirstFileEx(pattern.c_str(), FindExInfoBasic, &entry, FindExSearchNameMatch, nullptr, 0);
32 if(search_handle==INVALID_HANDLE_VALUE)
34 DWORD err = GetLastError();
35 if(err==ERROR_FILE_NOT_FOUND)
37 throw system_error("FindFirstFileEx");
41 for(; more; more=FindNextFile(search_handle, &entry))
43 const char *fn = entry.cFileName;
44 if(fn[0]=='.' && (fn[1]==0 || (fn[1]=='.' && fn[2]==0)))
46 if(r_filter.match(fn))
50 DWORD err = GetLastError();
51 FindClose(search_handle);
52 if(err!=ERROR_NO_MORE_FILES)
53 throw system_error("FindNextFile");
58 /* Windows documentation says Get/SetCurrentDirectory use a global buffer and
59 are not thread safe. */
60 static Mutex &cwd_mutex()
68 MutexLock lock(cwd_mutex());
72 DWORD len = GetCurrentDirectory(sizeof(buf), buf);
75 vector<char> buf2(len+1);
76 len = GetCurrentDirectory(buf2.size(), buf2.data());
83 throw system_error("GetCurrentDirectory");
88 void chdir(const Path &path)
90 MutexLock lock(cwd_mutex());
91 if(!SetCurrentDirectory(path.c_str()))
92 throw system_error("SetCurrentDirectory");