From: James Turner Date: Mon, 9 Aug 2010 07:19:14 +0000 (+0100) Subject: Make SGPath cache stat() information, cheers Fred X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=c4b4c0ce59602a0b749e22b29d6ce5db6f654eae;p=simgear.git Make SGPath cache stat() information, cheers Fred * cache exists/isDir/isFile in SGPath, to avoid repeated calls. --- diff --git a/simgear/bucket/newbucket.cxx b/simgear/bucket/newbucket.cxx index 11995903..aea0466a 100644 --- a/simgear/bucket/newbucket.cxx +++ b/simgear/bucket/newbucket.cxx @@ -139,7 +139,7 @@ void SGBucket::set_bucket(const SGGeod& geod) } // Build the path name for this bucket -string SGBucket::gen_base_path() const { +std::string SGBucket::gen_base_path() const { // long int index; int top_lon, top_lat, main_lon, main_lat; char hem, pole; diff --git a/simgear/ephemeris/stardata.cxx b/simgear/ephemeris/stardata.cxx index 13df0cc1..a7858333 100644 --- a/simgear/ephemeris/stardata.cxx +++ b/simgear/ephemeris/stardata.cxx @@ -34,6 +34,8 @@ using std::getline; #endif +using std::string; + // Constructor SGStarData::SGStarData( const SGPath& path ) { diff --git a/simgear/misc/sg_dir.cxx b/simgear/misc/sg_dir.cxx index e912b2d1..b8a68991 100644 --- a/simgear/misc/sg_dir.cxx +++ b/simgear/misc/sg_dir.cxx @@ -29,8 +29,6 @@ # include #endif -#include - #include #include @@ -119,12 +117,12 @@ PathList Dir::children(int types, const std::string& nameFilter) const continue; } - struct stat s; - if (stat(file(entry->d_name).c_str(), &s)) { + SGPath f = file(entry->d_name); + if (!f.exists()) { continue; // stat() failed } - if (S_ISDIR(s.st_mode)) { + if (f.isDir()) { // directory handling if (!(types & TYPE_DIR)) { continue; @@ -135,7 +133,7 @@ PathList Dir::children(int types, const std::string& nameFilter) const continue; } } - } else if (S_ISREG(s.st_mode)) { + } else if (f.isFile()) { // regular file handling if (!(types & TYPE_FILE)) { continue; @@ -162,23 +160,7 @@ PathList Dir::children(int types, const std::string& nameFilter) const bool Dir::exists() const { -#ifdef _WIN32 - struct _stat buf ; - - if (_stat (_path.c_str(), &buf ) < 0) { - return false; - } - - return ((S_IFDIR & buf.st_mode ) !=0); -#else - struct stat buf ; - - if (stat(_path.c_str(), &buf ) < 0) { - return false ; - } - - return ((S_ISDIR(buf.st_mode )) != 0); -#endif + return _path.isDir(); } SGPath Dir::file(const std::string& name) const diff --git a/simgear/misc/sg_path.cxx b/simgear/misc/sg_path.cxx index bf0438cc..fc6856dd 100644 --- a/simgear/misc/sg_path.cxx +++ b/simgear/misc/sg_path.cxx @@ -33,6 +33,8 @@ #endif #include "sg_path.hxx" +using std::string; + /** * define directory path separators @@ -70,18 +72,38 @@ SGPath::fix() // default constructor SGPath::SGPath() - : path("") + : path(""), + _cached(false) { } // create a path based on "path" SGPath::SGPath( const std::string& p ) - : path(p) + : path(p), + _cached(false) { fix(); } +SGPath::SGPath(const SGPath& p) : + path(p.path), + _cached(p._cached), + _exists(p._exists), + _isDir(p._isDir), + _isFile(p._isFile) +{ +} + +SGPath& SGPath::operator=(const SGPath& p) +{ + path = p.path; + _cached = p._cached; + _exists = p._exists; + _isDir = p._isDir; + _isFile = p._isFile; + return *this; +} // destructor SGPath::~SGPath() { @@ -92,6 +114,7 @@ SGPath::~SGPath() { void SGPath::set( const string& p ) { path = p; fix(); + _cached = false; } @@ -106,6 +129,7 @@ void SGPath::append( const string& p ) { path += p; } fix(); + _cached = false; } //add a new path component to the existing path string @@ -123,6 +147,7 @@ void SGPath::concat( const string& p ) { path += p; } fix(); + _cached = false; } @@ -169,25 +194,54 @@ string SGPath::extension() const { } } -bool SGPath::exists() const +void SGPath::validate() const { -#ifdef _WIN32 - struct _stat buf; - - if (_stat(path.c_str(), &buf) < 0) { - return false; + if (_cached) { + return; } - return true; +#ifdef _WIN32 + struct _stat buf ; + + if (_stat (path.c_str(), &buf ) < 0) { + _exists = false; + } else { + _exists = true; + _isFile = ((S_IFREG & buf.st_mode ) !=0) + _isDir = ((S_IFDIR & buf.st_mode ) !=0) + } + #else struct stat buf ; - if (stat(path.c_str(), &buf) < 0) { - return false ; + if (stat(path.c_str(), &buf ) < 0) { + _exists = false; + } else { + _exists = true; + _isFile = ((S_ISREG(buf.st_mode )) != 0); + _isDir = ((S_ISDIR(buf.st_mode )) != 0); } - return true; #endif + _cached = true; +} + +bool SGPath::exists() const +{ + validate(); + return _exists; +} + +bool SGPath::isDir() const +{ + validate(); + return _exists && _isDir; +} + +bool SGPath::isFile() const +{ + validate(); + return _exists && _isFile; } #ifdef _WIN32 diff --git a/simgear/misc/sg_path.hxx b/simgear/misc/sg_path.hxx index 70f1b6c5..41b7d744 100644 --- a/simgear/misc/sg_path.hxx +++ b/simgear/misc/sg_path.hxx @@ -35,8 +35,6 @@ #include -using std::string; - #ifdef _MSC_VER typedef int mode_t; #endif @@ -53,18 +51,22 @@ class SGPath { private: - string path; + std::string path; public: /** Default constructor */ SGPath(); + SGPath(const SGPath& p); + + SGPath& operator=(const SGPath& p); + /** * Construct a path based on the starting path provided. * @param p initial path */ - SGPath( const string& p ); + SGPath( const std::string& p ); /** Destructor */ ~SGPath(); @@ -73,57 +75,57 @@ public: * Set path to a new value * @param p new path */ - void set( const string& p ); + void set( const std::string& p ); SGPath& operator= ( const char* p ) { this->set(p); return *this; } /** * Append another piece to the existing path. Inserts a path * separator between the existing component and the new component. * @param p additional path component */ - void append( const string& p ); + void append( const std::string& p ); /** * Append a new piece to the existing path. Inserts a search path * separator to the existing path and the new patch component. * @param p additional path component */ - void add( const string& p ); + void add( const std::string& p ); /** * Concatenate a string to the end of the path without inserting a * path separator. * @param p additional path suffix */ - void concat( const string& p ); + void concat( const std::string& p ); /** * Get the file part of the path (everything after the last path sep) * @return file string */ - string file() const; + std::string file() const; /** * Get the directory part of the path. * @return directory string */ - string dir() const; + std::string dir() const; /** * Get the base part of the path (everything but the extension.) * @return the base string */ - string base() const; + std::string base() const; /** * Get the extension part of the path (everything after the final ".") * @return the extension string */ - string extension() const; + std::string extension() const; /** * Get the path string * @return path string */ - string str() const { return path; } + std::string str() const { return path; } /** * Get the path string @@ -143,22 +145,30 @@ public: */ int create_dir(mode_t mode); + bool isFile() const; + bool isDir() const; private: void fix(); + void validate() const; + + mutable bool _cached; + mutable bool _exists; + mutable bool _isDir; + mutable bool _isFile; }; /** * Split a directory string into a list of it's parent directories. */ -string_list sgPathBranchSplit( const string &path ); +string_list sgPathBranchSplit( const std::string &path ); /** * Split a directory search path into a vector of individual paths */ -string_list sgPathSplit( const string &search_path ); +string_list sgPathSplit( const std::string &search_path ); #endif // _SG_PATH_HXX diff --git a/simgear/scene/material/matmodel.cxx b/simgear/scene/material/matmodel.cxx index 0ac77543..6bd1e189 100644 --- a/simgear/scene/material/matmodel.cxx +++ b/simgear/scene/material/matmodel.cxx @@ -28,7 +28,7 @@ #include #include -using std::map; + #include #include @@ -46,8 +46,8 @@ using std::map; #include "matmodel.hxx" using namespace simgear; - - +using std::string; +using std::map; //////////////////////////////////////////////////////////////////////// // Implementation of SGMatModel. //////////////////////////////////////////////////////////////////////// diff --git a/simgear/scene/model/SGText.cxx b/simgear/scene/model/SGText.cxx index 4649c153..92b0d994 100644 --- a/simgear/scene/model/SGText.cxx +++ b/simgear/scene/model/SGText.cxx @@ -30,6 +30,8 @@ #include #include +using std::string; + class SGText::UpdateCallback : public osg::NodeCallback { public: UpdateCallback( osgText::Text * aText, SGConstPropertyNode_ptr aProperty, double aScale, double aOffset, double aTruncate, double aNumeric, const char * aFormat ) : diff --git a/simgear/scene/model/modellib.cxx b/simgear/scene/model/modellib.cxx index b965ff3e..9e1acbe2 100644 --- a/simgear/scene/model/modellib.cxx +++ b/simgear/scene/model/modellib.cxx @@ -39,7 +39,7 @@ #include - +using std::string; using namespace simgear; osgDB::RegisterReaderWriterProxy g_readerWriter_XML_Proxy; diff --git a/simgear/scene/tgdb/TileEntry.hxx b/simgear/scene/tgdb/TileEntry.hxx index 1c620a41..e22ae643 100644 --- a/simgear/scene/tgdb/TileEntry.hxx +++ b/simgear/scene/tgdb/TileEntry.hxx @@ -150,7 +150,7 @@ public: class ModelLoadHelper { public: - virtual osg::Node *loadTileModel(const string& modelPath, bool cacheModel)=0; + virtual osg::Node *loadTileModel(const std::string& modelPath, bool cacheModel)=0; }; diff --git a/simgear/timing/sg_time.cxx b/simgear/timing/sg_time.cxx index 676251ba..d32302ee 100644 --- a/simgear/timing/sg_time.cxx +++ b/simgear/timing/sg_time.cxx @@ -59,6 +59,7 @@ #define DEGHR(x) ((x)/15.) #define RADHR(x) DEGHR(x*SGD_RADIANS_TO_DEGREES) +using std::string; static const double MJD0 = 2415020.0; static const double J2000 = 2451545.0 - MJD0;