]> git.tdb.fi Git - libs/core.git/blobdiff - source/fs/windows/dir.cpp
Use nullptr instead of 0 for pointers
[libs/core.git] / source / fs / windows / dir.cpp
index 151cc1b0944c4b455655c1f426da09c0ee81f89f..fde0200d029708eccd7476911e5d8dd3fa18c285 100644 (file)
@@ -1,6 +1,7 @@
-#include <shlobj.h>
+#include <windows.h>
 #include <msp/core/mutex.h>
 #include <msp/core/systemerror.h>
+#include <msp/strings/regex.h>
 #include "dir.h"
 
 using namespace std;
@@ -20,6 +21,40 @@ void rmdir(const Path &path)
                throw system_error("RemoveDirectory");
 }
 
+vector<string> list_filtered(const Path &path, const string &filter)
+{
+       Regex r_filter(filter);
+
+       vector<string> result;
+       WIN32_FIND_DATA entry;
+       string pattern = path.str()+"\\*";
+       HANDLE search_handle = FindFirstFileEx(pattern.c_str(), FindExInfoBasic, &entry, FindExSearchNameMatch, nullptr, 0);
+       if(search_handle==INVALID_HANDLE_VALUE)
+       {
+               DWORD err = GetLastError();
+               if(err==ERROR_FILE_NOT_FOUND)
+                       return result;
+               throw system_error("FindFirstFileEx");
+       }
+
+       bool more = true;
+       for(; more; more=FindNextFile(search_handle, &entry))
+       {
+               const char *fn = entry.cFileName;
+               if(fn[0]=='.' && (fn[1]==0 || (fn[1]=='.' && fn[2]==0)))
+                       continue;
+               if(r_filter.match(fn))
+                       result.push_back(fn);
+       }
+
+       DWORD err = GetLastError();
+       FindClose(search_handle);
+       if(err!=ERROR_NO_MORE_FILES)
+               throw system_error("FindNextFile");
+
+       return result;
+}
+
 /* Windows documentation says Get/SetCurrentDirectory use a global buffer and
 are not thread safe. */
 static Mutex &cwd_mutex()
@@ -57,23 +92,5 @@ void chdir(const Path &path)
                throw system_error("SetCurrentDirectory");
 }
 
-Path get_home_dir()
-{
-       char home[MAX_PATH];
-       if(SHGetFolderPath(0, CSIDL_PERSONAL, 0, 0, home)==S_OK)
-               return home;
-       return ".";
-}
-
-Path get_user_data_dir(const string &appname)
-{
-       if(appname.empty())
-               throw invalid_argument("get_user_data_dir");
-       char datadir[MAX_PATH];
-       if(SHGetFolderPath(0, CSIDL_LOCAL_APPDATA, 0, 0, datadir)==S_OK)
-               return Path(datadir)/appname;
-       return ".";
-}
-
 } // namespace FS
 } // namespace Msp