From 14f63cbe67eb79b59c5dd9e235012fe0fd8f4db6 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 1 Aug 2013 10:28:12 +0000 Subject: [PATCH] From Ulrich Hertlein, "Based on the exchange on osg-users I went ahead and reworked shp/XBaseParser to avoid weird behaviour (closing stdin) and leaking file descriptors, as well as some const-ness." --- src/osgPlugins/shp/ESRIShapeReaderWriter.cpp | 2 +- src/osgPlugins/shp/XBaseParser.cpp | 16 ++++++++-------- src/osgPlugins/shp/XBaseParser.h | 5 +++-- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/osgPlugins/shp/ESRIShapeReaderWriter.cpp b/src/osgPlugins/shp/ESRIShapeReaderWriter.cpp index a00ea8b83..5a87720ea 100644 --- a/src/osgPlugins/shp/ESRIShapeReaderWriter.cpp +++ b/src/osgPlugins/shp/ESRIShapeReaderWriter.cpp @@ -67,7 +67,7 @@ class ESRIShapeReaderWriter : public osgDB::ReaderWriter osg::Geode * geode = sp.getGeode(); unsigned int i = 0; - ESRIShape::XBaseParser::ShapeAttributeListList::iterator it, end = xbp.getAttributeList().end(); + ESRIShape::XBaseParser::ShapeAttributeListList::const_iterator it, end = xbp.getAttributeList().end(); for (it = xbp.getAttributeList().begin(); it != end; ++it, ++i) { geode->getDrawable(i)->setUserData(it->get()); diff --git a/src/osgPlugins/shp/XBaseParser.cpp b/src/osgPlugins/shp/XBaseParser.cpp index 977c22508..b34380ab2 100644 --- a/src/osgPlugins/shp/XBaseParser.cpp +++ b/src/osgPlugins/shp/XBaseParser.cpp @@ -80,12 +80,12 @@ bool XBaseFieldDescriptor::read(int fd) } -XBaseParser::XBaseParser(const std::string fileName): +XBaseParser::XBaseParser(const std::string& fileName): _valid(false) { - int fd = 0; - if (fileName.empty() == false) + if (!fileName.empty()) { + int fd = 0; #ifdef WIN32 if( (fd = open( fileName.c_str(), O_RDONLY | O_BINARY )) < 0 ) #else @@ -93,11 +93,13 @@ XBaseParser::XBaseParser(const std::string fileName): #endif { perror( fileName.c_str() ); - return; + } + else + { + _valid = parse(fd); + close(fd); } } - - _valid = parse(fd); } bool XBaseParser::parse(int fd) @@ -208,8 +210,6 @@ bool XBaseParser::parse(int fd) delete [] record; - close (fd); - return true; } diff --git a/src/osgPlugins/shp/XBaseParser.h b/src/osgPlugins/shp/XBaseParser.h index 94fcfc0a3..8d44d3d80 100644 --- a/src/osgPlugins/shp/XBaseParser.h +++ b/src/osgPlugins/shp/XBaseParser.h @@ -62,9 +62,10 @@ class XBaseParser typedef std::vector< osg::ref_ptr > ShapeAttributeListList; - XBaseParser(const std::string fileName); + XBaseParser(const std::string& fileName); ~XBaseParser() {} - ShapeAttributeListList & getAttributeList() { return _shapeAttributeListList; } + + const ShapeAttributeListList & getAttributeList() const { return _shapeAttributeListList; } private: