Euphoria
os.cc
Go to the documentation of this file.
1 #include "base/os.h"
2 #include "base/stringutils.h"
3 
4 
5 // https://stackoverflow.com/questions/612097/how-can-i-get-the-list-of-files-in-a-directory-using-c-or-c
6 // look into using https://github.com/cxong/tinydir instead of current platform
7 // hack...?
8 
9 #include <iostream>
10 
12 // copied from https://stackoverflow.com/a/145309/180307
13 
14 #include <cstdio> /* defines FILENAME_MAX */
15 #include <utility>
16 #ifdef _MSC_VER
17 #include "windows.h"
18 #include <direct.h>
19 #define GET_CURRENT_DIR _getcwd
20 
21 #ifdef GetCurrentDirectory
22 #undef GetCurrentDirectory
23 #endif
24 #else
25 #include <dirent.h>
26 #include <unistd.h>
27 
28 
29 #define GET_CURRENT_DIR getcwd
30 #endif
31 
32 namespace eu
33 {
34  std::string
36  {
37  char current_directory[FILENAME_MAX];
38 
39  if(!GET_CURRENT_DIR(
40  static_cast<char*>(current_directory),
41  sizeof(current_directory)))
42  {
43  return "";
44  }
45 
46  current_directory[sizeof(current_directory) - 1] = 0;
47 
48  const std::string ret = static_cast<char*>(current_directory);
49  return ret;
50  }
51 
53 
54 #ifdef _MSC_VER
55 
56  DirectoryListing
57  list_directory(const std::string& path)
58  {
59  const std::string search_path = fmt::format("{}*.*", path);
60  WIN32_FIND_DATA fd;
61  HANDLE hFind = ::FindFirstFile(search_path.c_str(), &fd);
62 
63 
64  if(hFind != INVALID_HANDLE_VALUE)
65  {
66  DirectoryListing ret;
67  ret.valid = true;
68 
69  do
70  {
71  // read all (real) files in current folder, delete '!' read other 2
72  // default folder . and ..
73  if(!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
74  {
75  ret.files.push_back(fd.cFileName);
76  }
77  else // Put folders into vector
78  {
79  ret.directories.push_back(fd.cFileName);
80  }
81  } while(::FindNextFile(hFind, &fd));
82  ::FindClose(hFind);
83 
84  return ret;
85  }
86  else
87  {
88  DirectoryListing ret;
89  ret.valid = false;
90  return ret;
91  }
92  }
93 #else
94  DirectoryListing
95  list_directory(const std::string& path)
96  {
97  struct dirent* ent = nullptr;
98 
99  DIR* dir = opendir(path.c_str());
100  if(dir != nullptr)
101  {
102  DirectoryListing ret;
103  ret.valid = true;
104 
105  /* print all the files and directories within directory */
106  while((ent = readdir(dir)) != nullptr)
107  {
108  const std::string name = ent->d_name;
109  if(ent->d_type == DT_REG)
110  {
111  ret.files.emplace_back(name);
112  }
113  else if(ent->d_type == DT_DIR)
114  {
115  if(name == "." || name == "..")
116  {
117  }
118  else
119  {
120  ret.directories.emplace_back(name);
121  }
122  }
123  }
124 
125  closedir(dir);
126  return ret;
127  }
128  else
129  {
130  DirectoryListing ret;
131  ret.valid = false;
132  return ret;
133  }
134  }
135 #endif
136 
137  // todo(Gustav): move to stringutils
138  bool
139  ends_with(const std::string& str, char c)
140  {
141  const auto l = str.length();
142 
143  if(l == 0)
144  {
145  return false;
146  }
147 
148  return str[l - 1] == c;
149  }
150 
151  std::string
152  join_path(const std::string& left, const std::string& right)
153  {
155  {
156  return left + right;
157  }
158  else
159  {
160  return left + path_separator + right;
161  }
162  }
163 
164  std::string
165  get_extension(const std::string& path)
166  {
167  return get_last_string(path, '.').second;
168  }
169 
170  std::string
171  get_file_name_including_extension(const std::string& path)
172  {
173  const auto r = get_last_string(path, path_separator).second;
174  if(r.empty())
175  {
176  return r;
177  }
178  else
179  {
180  // skip leading path_separator
181  return r.substr(1);
182  }
183  }
184 
185  std::string
186  get_file_name_without_extension(const std::string& path)
187  {
188  return get_last_string(get_file_name_including_extension(path), '.').first;
189  }
190 
191 }
std::pair< std::string, std::string > get_last_string(const std::string &str, char sep)
for hello.dog gets .dog
Definition: stringutils.cc:17
constexpr unit3f right
Definition: vec3.h:133
constexpr unit3f left
Definition: vec3.h:134
Definition: assert.h:90
std::string get_current_directory()
Definition: os.cc:35
std::string get_extension(const std::string &path)
Definition: os.cc:165
constexpr char path_separator
Definition: os.h:23
bool ends_with(const std::string &str, char c)
Definition: os.cc:139
std::string get_file_name_including_extension(const std::string &path)
Definition: os.cc:171
std::string join_path(const std::string &left, const std::string &right)
Definition: os.cc:152
std::string get_file_name_without_extension(const std::string &path)
Definition: os.cc:186
DirectoryListing list_directory(const std::string &path)
Definition: os.cc:95
#define GET_CURRENT_DIR
Definition: os.cc:29
String utility functions.
std::vector< std::string > files
Definition: os.h:15
std::vector< std::string > directories
Definition: os.h:16