Btk
fs.hpp
1 #if !defined(_BTK_PLATFORM_FS_HPP_)
2 #define _BTK_PLATFORM_FS_HPP_
3 
4 #include <string_view>
5 #include <string>
6 
7 #include <cstring>
8 #include <cerrno>
9 #ifdef _WIN32
10  #include <direct.h>
11  #include <io.h>
12  #define BTK_GETCWD ::_getcwd
13  #define BTK_ACCESS ::_access
14  #define BTK_CHDIR ::_chdir
15  #define BTK_F_OK 00
16 #else
17  #include <unistd.h>
18  #define BTK_GETCWD ::getcwd
19  #define BTK_ACCESS ::access
20  #define BTK_CHDIR ::chdir
21  #define BTK_F_OK F_OK
22 #endif
23 #include "../exception.hpp"
24 namespace Btk{
30  inline std::string getcwd(){
31  char *buf = BTK_GETCWD(nullptr,0);
32  if(buf == nullptr){
33  throwRuntimeError(strerror(errno));
34  }
35  std::string s(buf);
36  free(buf);
37  return s;
38  }
39  inline std::string getcwd(size_t bufsize){
40  std::string s;
41  s.resize(bufsize);
42  if(BTK_GETCWD(s.data(),bufsize) == nullptr){
43  throwRuntimeError(strerror(errno));
44  }
45  return s;
46  }
54  inline bool exists(std::string_view fname){
55  return BTK_ACCESS(fname.data(),BTK_F_OK) == 0;
56  }
64  inline bool chdir(std::string_view path){
65  return BTK_CHDIR(path.data()) == 0;
66  }
67 
68 }
69 
70 
71 #endif // _BTK_PLATFORM_FS_HPP_
This header include many useful containers.
Definition: async.hpp:7
bool chdir(std::string_view path)
Change the current work dir.
Definition: fs.hpp:64
std::string getcwd()
Get current working dir.
Definition: fs.hpp:30
bool exists(std::string_view fname)
Check the file exists.
Definition: fs.hpp:54