filesystem.h
Go to the documentation of this file.
1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
4 
5 #pragma once
6 
7 #if defined _WIN32 || defined WINCE
8 # include <windows.h>
9 const char dir_separators[] = "/\\";
10 
11 namespace
12 {
13  struct dirent
14  {
15  const char* d_name;
16  };
17 
18  struct DIR
19  {
20 #if defined(WINRT) || defined(_WIN32_WCE)
21  WIN32_FIND_DATAW data;
22 #else
23  WIN32_FIND_DATAA data;
24 #endif
25  HANDLE handle;
26  dirent ent;
27 #ifdef WINRT
28  DIR() { }
29  ~DIR()
30  {
31  if( ent.d_name )
32  delete[] ent.d_name;
33  }
34 #endif
35  };
36 
37  DIR* opendir( const char* path )
38  {
39  DIR* dir = new DIR;
40  dir->ent.d_name = 0;
41 #if defined(WINRT) || defined(_WIN32_WCE)
42  cv::String full_path = cv::String( path ) + "\\*";
43  wchar_t wfull_path[MAX_PATH];
44  size_t copied = mbstowcs( wfull_path, full_path.c_str(), MAX_PATH );
45  CV_Assert( (copied != MAX_PATH) && (copied != (size_t)-1) );
46  dir->handle = ::FindFirstFileExW( wfull_path, FindExInfoStandard,
47  &dir->data, FindExSearchNameMatch, NULL, 0 );
48 #else
49  dir->handle = ::FindFirstFileExA( (std::string( path ) + "\\*").c_str(),
50  FindExInfoStandard, &dir->data, FindExSearchNameMatch, NULL, 0 );
51 #endif
52  if( dir->handle == INVALID_HANDLE_VALUE )
53  {
54  /*closedir will do all cleanup*/
55  delete dir;
56  return 0;
57  }
58  return dir;
59  }
60 
61  dirent* readdir( DIR* dir )
62  {
63 #if defined(WINRT) || defined(_WIN32_WCE)
64  if( dir->ent.d_name != 0 )
65  {
66  if( ::FindNextFileW( dir->handle, &dir->data ) != TRUE )
67  return 0;
68  }
69  size_t asize = wcstombs( NULL, dir->data.cFileName, 0 );
70  CV_Assert( (asize != 0) && (asize != (size_t)-1) );
71  char* aname = new char[asize + 1];
72  aname[asize] = 0;
73  wcstombs( aname, dir->data.cFileName, asize );
74  dir->ent.d_name = aname;
75 #else
76  if( dir->ent.d_name != 0 )
77  {
78  if( ::FindNextFileA( dir->handle, &dir->data ) != TRUE )
79  return 0;
80  }
81  dir->ent.d_name = dir->data.cFileName;
82 #endif
83  return &dir->ent;
84  }
85 
86  void closedir( DIR* dir )
87  {
88  ::FindClose( dir->handle );
89  delete dir;
90  }
91 
92 
93 }
94 #else
95 # include <dirent.h>
96 # include <sys/stat.h>
97 const char dir_separators[] = "/";
98 #endif
99 
100 
101 #ifdef _WIN32
102 static const char native_separator = '\\';
103 #else
104 static const char native_separator = '/';
105 #endif
106 
107 static inline
108 bool isPathSeparator( char c )
109 {
110  return c == '/' || c == '\\';
111 }
112 
113 std::string join( const std::string& base, const std::string& path )
114 {
115  if( base.empty() )
116  return path;
117  if( path.empty() )
118  return base;
119 
120  bool baseSep = isPathSeparator( base[base.size() - 1] );
121  bool pathSep = isPathSeparator( path[0] );
123  if( baseSep && pathSep )
124  {
125  result = base + path.substr( 1 );
126  }
127  else if( !baseSep && !pathSep )
128  {
129  result = base + native_separator + path;
130  }
131  else
132  {
133  result = base + path;
134  }
135  return result;
136 }
137 
138 static bool wildcmp( const char *string, const char *wild )
139 {
140  // Based on wildcmp written by Jack Handy - <A href="mailto:jakkhandy@hotmail.com">jakkhandy@hotmail.com</A>
141  const char *cp = 0, *mp = 0;
142 
143  while( (*string) && (*wild != '*') )
144  {
145  if( (*wild != *string) && (*wild != '?') )
146  {
147  return false;
148  }
149 
150  wild++;
151  string++;
152  }
153 
154  while( *string )
155  {
156  if( *wild == '*' )
157  {
158  if( !*++wild )
159  {
160  return true;
161  }
162 
163  mp = wild;
164  cp = string + 1;
165  }
166  else if( (*wild == *string) || (*wild == '?') )
167  {
168  wild++;
169  string++;
170  }
171  else
172  {
173  wild = mp;
174  string = cp++;
175  }
176  }
177 
178  while( *wild == '*' )
179  {
180  wild++;
181  }
182 
183  return *wild == 0;
184 }
185 
186 
187 static bool isDir( const std::string& path, DIR* dir )
188 {
189 #if defined _WIN32 || defined WINCE
190  DWORD attributes;
191  BOOL status = TRUE;
192  if( dir )
193  attributes = dir->data.dwFileAttributes;
194  else
195  {
196  WIN32_FILE_ATTRIBUTE_DATA all_attrs;
197 #ifdef WINRT
198  wchar_t wpath[MAX_PATH];
199  size_t copied = mbstowcs( wpath, path.c_str(), MAX_PATH );
200  CV_Assert( (copied != MAX_PATH) && (copied != (size_t)-1) );
201  status = ::GetFileAttributesExW( wpath, GetFileExInfoStandard, &all_attrs );
202 #else
203  status = ::GetFileAttributesExA( path.c_str(), GetFileExInfoStandard, &all_attrs );
204 #endif
205  attributes = all_attrs.dwFileAttributes;
206  }
207 
208  return status && ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
209 #else
210  (void)dir; // avoid warnings about unused params
211  struct stat stat_buf;
212  if( 0 != stat( path.c_str(), &stat_buf ) )
213  return false;
214  int is_dir = S_ISDIR( stat_buf.st_mode );
215  return is_dir != 0;
216 #endif
217 }
218 
219 static void glob_rec( const std::string & directory,
220  const std::string & wildchart,
221  std::vector<std::string>& result,
222  bool recursive,
223  bool includeDirectories,
224  const std::string & pathPrefix
225 )
226 {
227  DIR *dir;
228 
229  if( (dir = opendir( directory.c_str() )) != 0 )
230  {
231  /* find all the files and directories within directory */
232  try
233  {
234  struct dirent *ent;
235  while( (ent = readdir( dir )) != 0 )
236  {
237  const char* name = ent->d_name;
238  if( (name[0] == 0) || (name[0] == '.' && name[1] == 0) || (name[0] == '.' && name[1] == '.' && name[2] == 0) )
239  continue;
240 
241  std::string path = join( directory, name );
242  std::string entry = join( pathPrefix, name );
243 
244  if( isDir( path, dir ) )
245  {
246  if( recursive )
247  glob_rec( path, wildchart, result, recursive, includeDirectories, entry );
248  if( !includeDirectories )
249  continue;
250  }
251 
252  if( wildchart.empty() || wildcmp( name, wildchart.c_str() ) )
253  result.push_back( entry );
254  }
255  }
256  catch( ... )
257  {
258  closedir( dir );
259  throw;
260  }
261  closedir( dir );
262  }
263  else
264  {
265  throw std::runtime_error( librealsense::to_string() << "could not open directory: " << directory.c_str() );
266  }
267 }
268 
269 static void glob(
270  const std::string & directory,
271  const std::string & spec,
272  std::function< void( std::string const & ) > fn,
273  bool recursive = true,
274  bool includeDirectories = false
275 )
276 {
277  std::vector< std::string > results;
278  glob_rec( directory, spec, results, recursive, includeDirectories, "" );
279  for( auto r : results )
280  fn( r );
281 }
282 
283 static
284 std::string get_parent( std::string const & path, std::string * basename = nullptr )
285 {
286  // Returns the parent and leaf for the given path:
287  // /foo/bar/ -> '/foo/bar' and '' (empty)
288  // blah/.. -> 'blah' and '..'
289  auto x = path.find_last_of( dir_separators );
290  if( x == std::string::npos )
291  return std::string();
292  if( basename )
293  *basename = path.substr( x + 1 );
294  return std::string( path, 0, x );
295 }
static bool isDir(const std::string &path, DIR *dir)
Definition: filesystem.h:187
typedef void(APIENTRY *GLDEBUGPROC)(GLenum source
GLuint const GLchar * name
std::string join(const std::string &base, const std::string &path)
Definition: filesystem.h:113
static bool wildcmp(const char *string, const char *wild)
Definition: filesystem.h:138
GLuint64 GLenum void * handle
Definition: glext.h:7785
GLsizei const GLchar *const * path
Definition: glext.h:4276
static void glob(const std::string &directory, const std::string &spec, std::function< void(std::string const &) > fn, bool recursive=true, bool includeDirectories=false)
Definition: filesystem.h:269
static void glob_rec(const std::string &directory, const std::string &wildchart, std::vector< std::string > &result, bool recursive, bool includeDirectories, const std::string &pathPrefix)
Definition: filesystem.h:219
static bool isPathSeparator(char c)
Definition: filesystem.h:108
GLsizei const GLchar *const * string
GLuint entry
Definition: glext.h:10991
::std_msgs::String_< std::allocator< void > > String
Definition: String.h:47
static std::string get_parent(std::string const &path, std::string *basename=nullptr)
Definition: filesystem.h:284
status
Defines return codes that SDK interfaces use. Negative values indicate errors, a zero value indicates...
const char dir_separators[]
Definition: filesystem.h:97
const GLubyte * c
Definition: glext.h:12690
GLdouble GLdouble r
GLdouble x
#define NULL
Definition: tinycthread.c:47
#define TRUE
Definition: tinycthread.c:50
static const char native_separator
Definition: filesystem.h:104
GLboolean * data
GLuint64EXT * result
Definition: glext.h:10921


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:14