]> git.mxchange.org Git - simgear.git/commitdiff
Initial work on simgear::Dir, replacement for PLIB ulDir functions.
authorJames Turner <zakalawe@mac.com>
Thu, 15 Jul 2010 08:40:46 +0000 (09:40 +0100)
committerJames Turner <zakalawe@mac.com>
Thu, 15 Jul 2010 08:40:46 +0000 (09:40 +0100)
projects/VC90/SimGear.vcproj
simgear/misc/Makefile.am
simgear/misc/sg_dir.cxx [new file with mode: 0644]
simgear/misc/sg_dir.hxx [new file with mode: 0644]

index 30b7b85d1524247e22f1f8b274efda9e06a047c4..24484e8c833d9ff914876f80e9165670e8002fe6 100644 (file)
                                RelativePath="..\..\simgear\misc\sg_path.hxx"
                                >
                        </File>
+                       <File
+                               RelativePath="..\..\simgear\misc\sg_dir.cxx"
+                               >
+                       </File>
+                       <File
+                               RelativePath="..\..\simgear\misc\sg_dir.hxx"
+                               >
+                       </File>
                        <File
                                RelativePath="..\..\simgear\misc\sgstream.cxx"
                                >
index dd4e6e416bcbd1cb8c9c8f3eb47c4231adca62da..0e0fc94993a0833de1f8dcea4dd5ed0ec4674ae8 100644 (file)
@@ -12,7 +12,8 @@ include_HEADERS = \
        zfstream.hxx \
        interpolator.hxx \
        stdint.hxx \
-       PathOptions.hxx
+       PathOptions.hxx \
+       sg_dir.hxx
 
 libsgmisc_a_SOURCES = \
        sg_path.cxx \
@@ -22,7 +23,8 @@ libsgmisc_a_SOURCES = \
        texcoord.cxx \
        zfstream.cxx \
        interpolator.cxx \
-       PathOptions.cxx
+       PathOptions.cxx \
+       sg_dir.cxx
 
 #noinst_PROGRAMS = tabbed_value_test swap_test
 
diff --git a/simgear/misc/sg_dir.cxx b/simgear/misc/sg_dir.cxx
new file mode 100644 (file)
index 0000000..e877509
--- /dev/null
@@ -0,0 +1,153 @@
+// Written by James Turner, started July 2010.
+//
+// Copyright (C) 2010  Curtis L. Olson - http://www.flightgear.org/~curt
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Library General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Library General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+//
+// $Id$
+
+
+#include <simgear/misc/sg_dir.hxx>
+
+#ifdef _WIN32
+#  define WIN32_LEAN_AND_MEAN
+#  include <windows.h>
+#else
+#  include <sys/types.h>
+#  include <dirent.h>
+#endif
+
+#include <simgear/debug/logstream.hxx>
+
+#include <iostream>
+
+namespace simgear
+{
+
+Dir::Dir(const SGPath& path) :
+  _path(path)
+{
+}
+
+Dir::Dir(const Dir& rel, const SGPath& relPath) :
+  _path(rel.file(relPath.str()))
+{
+}
+
+PathList Dir::children(int types, const std::string& nameFilter) const
+{
+  PathList result;
+  if (types == 0) {
+    types = TYPE_FILE | TYPE_DIR | NO_DOT_OR_DOTDOT;
+  }
+  
+#ifdef _WIN32
+  std::string search(_path.str());
+  if (nameFilter.empty()) {
+    search += "\\*"; // everything
+  } else {
+    search += "\\*" + nameFilter;
+  }
+  
+  WIN32_FIND_DATA fData;
+  HANDLE find = FindFirstFile(search.c_str(), &fData);
+  if (find == INVALID_HANDLE_VALUE) {
+    SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: FindFirstFile failed:" << _path.str());
+    return result;
+  }
+  
+  bool done = false;
+  for (bool done = false; !done; done = (FindNextFile(find, &fData) == 0)) {
+    if (fData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
+      if (!(types & INCLUDE_HIDDEN)) {
+        continue;
+      }
+    }
+    
+    if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
+      if (!(types & TYPE_DIR)) {
+        continue;
+      }
+       } else if ((fData.dwFileAttributes & FILE_ATTRIBUTE_DEVICE) ||
+                               (fData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
+       {
+               continue; // always ignore device files
+    } else if (!(types & TYPE_FILE)) {
+       continue;
+    }
+
+    result.push_back(file(fData.cFileName));
+  }
+
+  FindClose(find);
+#else
+  DIR* dp = opendir(_path.c_str());
+  if (!dp) {
+    SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: opendir failed:" << _path.str());
+    return result;
+  }
+  
+  while (true) {
+    struct dirent* entry = readdir(dp);
+    if (!entry) {
+      break; // done iteration
+    }
+    
+    // skip hidden files (names beginning with '.') unless requested
+    if (!(types & INCLUDE_HIDDEN) && (entry->d_name[0] == '.')) {
+      continue;
+    }
+        
+    if (entry->d_type == DT_DIR) {
+      if (!(types & TYPE_DIR)) {
+        continue;
+      }
+      
+      if (types & NO_DOT_OR_DOTDOT) {
+        if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
+          continue;
+        }
+      }
+    } else if (entry->d_type == DT_REG) {
+      if (!(types & TYPE_FILE)) {
+        continue;
+      }
+    } else {
+      continue; // ignore char/block devices, fifos, etc
+    }
+    
+    if (!nameFilter.empty()) {
+      if (strstr(entry->d_name, nameFilter.c_str()) == NULL) {
+        continue;
+      }
+    }
+    
+  // passed all criteria, add to our result vector
+    result.push_back(file(entry->d_name));
+  }
+  
+  closedir(dp);
+#endif
+  return result;
+}
+
+SGPath Dir::file(const std::string& name) const
+{
+  SGPath childPath = _path;
+  childPath.append(name);
+  return childPath;  
+}
+
+} // of namespace simgear
diff --git a/simgear/misc/sg_dir.hxx b/simgear/misc/sg_dir.hxx
new file mode 100644 (file)
index 0000000..5ff51c9
--- /dev/null
@@ -0,0 +1,62 @@
+
+// Written by James Turner, started July 2010.
+//
+// Copyright (C) 2010  Curtis L. Olson - http://www.flightgear.org/~curt
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Library General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Library General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+//
+// $Id$
+
+
+#ifndef _SG_DIR_HXX
+#define _SG_DIR_HXX
+
+#include <sys/types.h>
+
+#include <simgear/compiler.h>
+#include <string>
+
+#include <simgear/math/sg_types.hxx>
+#include <simgear/misc/sg_path.hxx>
+
+namespace simgear
+{
+  typedef std::vector<SGPath> PathList;
+
+  class Dir
+  {
+  public:
+    Dir(const SGPath& path);
+    Dir(const Dir& rel, const SGPath& relPath);
+    
+    enum FileTypes
+    {
+      TYPE_FILE = 1,
+      TYPE_DIR = 2,
+      NO_DOT_OR_DOTDOT = 1 << 12,
+      INCLUDE_HIDDEN = 1 << 13
+    };
+    
+    PathList children(int types = 0, const std::string& nameGlob = "") const;
+    
+    SGPath file(const std::string& name) const;
+  private:
+    mutable SGPath _path;
+  };
+} // of namespace simgear
+
+#endif // _SG_DIR_HXX
+
+