From e59d2d3e47f6b5290e309d12dda3f4e43e34064c Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 14 Oct 2011 19:28:01 +0100 Subject: [PATCH] Use new SimGear helpers to simplify some code. --- src/Lib/HGT/hgt.cxx | 41 ++++++++--------------------------- src/Lib/HGT/srtmbase.cxx | 6 +++++ src/Lib/HGT/srtmbase.hxx | 8 +++++-- src/Prep/DemChop/hgtchop.cxx | 5 +++-- src/Prep/DemChop/srtmchop.cxx | 33 +++++++--------------------- 5 files changed, 32 insertions(+), 61 deletions(-) diff --git a/src/Lib/HGT/hgt.cxx b/src/Lib/HGT/hgt.cxx index 66e887d1..2f5c7ada 100644 --- a/src/Lib/HGT/hgt.cxx +++ b/src/Lib/HGT/hgt.cxx @@ -53,24 +53,21 @@ using std::endl; using std::string; -TGHgt::TGHgt( int _res ) { +TGHgt::TGHgt( int _res ) +{ hgt_resolution = _res; data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE]; output_data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE]; - - remove_tmp_file = false; } -TGHgt::TGHgt( int _res, const SGPath &file ) { +TGHgt::TGHgt( int _res, const SGPath &file ) +{ hgt_resolution = _res; - data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE]; output_data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE]; - remove_tmp_file = false; - TGHgt::open( file ); } @@ -91,27 +88,22 @@ TGHgt::open ( const SGPath &f ) { if ( file_name.extension() == "zip" ) { // extract the .zip file to /tmp and point the file name // to the extracted file - SGPath tmp_dir_path = string( tempnam( 0, "hgt" ) ); - simgear::Dir tmp_dir(tmp_dir_path); + tmp_dir = simgear::Dir::tempDir("hgt"); - SGPath dummy = tmp_dir.file( "dummy" ); - dummy.create_dir( 0700 ); - cout << "Extracting " << file_name.str() << " to " << tmp_dir_path.str() << endl; - string command = "unzip -d \"" + tmp_dir_path.str() + "\" " + file_name.base(); + cout << "Extracting " << file_name.str() << " to " << tmp_dir.path().str() << endl; + string command = "unzip -d \"" + tmp_dir.path().str() + "\" " + file_name.base(); system( command.c_str() ); simgear::PathList files = tmp_dir.children(simgear::Dir::TYPE_FILE | simgear::Dir::NO_DOT_OR_DOTDOT); BOOST_FOREACH(const SGPath& file, files) { - string ext = file.extension(); - if ( ext == "HGT" || ext == "hgt" ) { + string ext = file.lower_extension(); + if ( ext == "hgt" ) { file_name = file; break; } } remove_tmp_file = true; - remove_file_name = file_name.str(); - cout << "Proceeding with " << file_name.str() << endl; } @@ -148,12 +140,6 @@ TGHgt::open ( const SGPath &f ) { bool TGHgt::close () { gzclose(fd); - - if ( remove_tmp_file ) { - unlink( remove_file_name.c_str() ); - rmdir( remove_file_name.dir().c_str() ); - } - return true; } @@ -196,13 +182,4 @@ TGHgt::~TGHgt() { // printf("class TGSrtmBase DEstructor called.\n"); delete [] data; delete [] output_data; - if ( remove_tmp_file ) { - simgear::Dir dir(remove_file_name.dir()); - simgear::PathList files = dir.children(simgear::Dir::TYPE_FILE | simgear::Dir::NO_DOT_OR_DOTDOT); - BOOST_FOREACH(const SGPath& file, files) { - unlink( file.c_str() ); - } - - rmdir( remove_file_name.dir().c_str() ); - } } diff --git a/src/Lib/HGT/srtmbase.cxx b/src/Lib/HGT/srtmbase.cxx index a57de5c2..5f483031 100644 --- a/src/Lib/HGT/srtmbase.cxx +++ b/src/Lib/HGT/srtmbase.cxx @@ -37,6 +37,12 @@ using std::cout; using std::endl; using std::string; +TGSrtmBase::~TGSrtmBase() +{ + if (remove_tmp_file) { + tmp_dir.remove(true /*recursive*/); + } +} // write out the area of data covered by the specified bucket. Data // is written out column by column starting at the lower left hand diff --git a/src/Lib/HGT/srtmbase.hxx b/src/Lib/HGT/srtmbase.hxx index d9015057..019806ad 100644 --- a/src/Lib/HGT/srtmbase.hxx +++ b/src/Lib/HGT/srtmbase.hxx @@ -31,11 +31,15 @@ #include #include -#include +#include class TGSrtmBase { protected: + TGSrtmBase() : remove_tmp_file(false) + {} + + ~TGSrtmBase(); // coordinates (in arc seconds) of south west corner double originx, originy; @@ -47,7 +51,7 @@ protected: double col_step, row_step; bool remove_tmp_file; - SGPath remove_file_name; + simgear::Dir tmp_dir; public: diff --git a/src/Prep/DemChop/hgtchop.cxx b/src/Prep/DemChop/hgtchop.cxx index d439176d..bd20744b 100644 --- a/src/Prep/DemChop/hgtchop.cxx +++ b/src/Prep/DemChop/hgtchop.cxx @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -67,8 +68,8 @@ int main(int argc, char **argv) { } SGPath sgp( work_dir ); - sgp.append( "dummy" ); - sgp.create_dir( 0755 ); + simgear::Dir workDir(sgp); + workDir.create(0755); TGHgt hgt(resolution, hgt_name); hgt.load(); diff --git a/src/Prep/DemChop/srtmchop.cxx b/src/Prep/DemChop/srtmchop.cxx index 0d15507b..699d4455 100644 --- a/src/Prep/DemChop/srtmchop.cxx +++ b/src/Prep/DemChop/srtmchop.cxx @@ -94,7 +94,6 @@ private: TGSrtmTiff::TGSrtmTiff( const SGPath &file ) { lkind = BottomLeft; tif = 0; - remove_tmp_file = false; data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE]; output_data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE]; opened = TGSrtmTiff::open( file ); @@ -103,7 +102,6 @@ TGSrtmTiff::TGSrtmTiff( const SGPath &file ) { TGSrtmTiff::TGSrtmTiff( const SGPath &file, LoadKind lk ) { lkind = lk; tif = 0; - remove_tmp_file = false; output_data = 0; if ( lkind == BottomLeft ) { data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE]; @@ -121,15 +119,6 @@ TGSrtmTiff::TGSrtmTiff( const SGPath &file, LoadKind lk ) { TGSrtmTiff::~TGSrtmTiff() { delete[] data; delete[] output_data; - if ( remove_tmp_file ) { - simgear::Dir dir(remove_file_name.dir()); - simgear::PathList files = dir.children(simgear::Dir::TYPE_FILE | simgear::Dir::NO_DOT_OR_DOTDOT); - BOOST_FOREACH(const SGPath& file, files) { - unlink( file.c_str() ); - } - - rmdir( remove_file_name.dir().c_str() ); - } if ( tif ) TIFFClose( tif ); } @@ -160,28 +149,22 @@ bool TGSrtmTiff::open( const SGPath &f ) { int x, y; pos_from_name( file_name.file(), prefix, x, y ); if ( ext == "zip" ) { - // extract the .zip file to /tmp and point the file name - // to the extracted file - SGPath tmp_dir_path = string( tempnam( 0, "srtm" ) ); - simgear::Dir tmp_dir(tmp_dir_path); - SGPath dummy = tmp_dir.file( "dummy" ); - dummy.create_dir( 0700 ); - cout << "Extracting " << file_name.str() << " to " << tmp_dir_path.str() << endl; - string command = "unzip -d \"" + tmp_dir_path.str() + "\" " + file_name.base(); + tmp_dir = simgear::Dir::tempDir("srtm"); + + cout << "Extracting " << file_name.str() << " to " << tmp_dir.path().str() << endl; + string command = "unzip -d \"" + tmp_dir.path().str() + "\" " + file_name.base(); system( command.c_str() ); simgear::PathList files = tmp_dir.children(simgear::Dir::TYPE_FILE | simgear::Dir::NO_DOT_OR_DOTDOT); BOOST_FOREACH(const SGPath& file, files) { - string ext = file.extension(); - if ( ext == "TIF" || ext == "tif" ) { + string ext = file.lower_extension(); + if ( (ext == "tif") || (ext == "tiff") ) { file_name = file; break; } } remove_tmp_file = true; - remove_file_name = file_name.str(); - cout << "Proceeding with " << file_name.str() << endl; } @@ -361,8 +344,8 @@ int main(int argc, char **argv) { string work_dir = argv[2]; SGPath sgp( work_dir ); - sgp.append( "dummy" ); - sgp.create_dir( 0755 ); + simgear::Dir workDir(sgp); + workDir.create( 0755 ); TGSrtmTiff hgt( hgt_name ); hgt.load();