Back to index

salome-med  6.5.0
ParaMEDMEMTest.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "ParaMEDMEMTest.hxx"
00021 #include <cppunit/TestAssert.h>
00022 
00023 #include <sstream>
00024 #include <cmath>
00025 #include <list>
00026 #include <stdexcept>
00027 #include <stdlib.h>
00028 
00029 //================================================================================
00039 //================================================================================
00040 
00041 std::string ParaMEDMEMTest::getResourceFile( const std::string& filename )
00042 {
00043   std::string resourceFile = "";
00044 
00045   if ( getenv("top_srcdir") ) {
00046     // we are in 'make check' step
00047     resourceFile = getenv("top_srcdir");
00048     resourceFile += "/resources/";
00049   }
00050   else if ( getenv("MED_ROOT_DIR") ) {
00051     // use MED_ROOT_DIR env.var
00052     resourceFile = getenv("MED_ROOT_DIR");
00053     resourceFile += "/share/salome/resources/med/";
00054   }
00055   resourceFile += filename;
00056   return resourceFile;
00057 }
00058 
00059 
00060 //================================================================================
00065 //================================================================================
00066 
00067 std::string ParaMEDMEMTest::getTmpDirectory()
00068 {
00069   std::string path;
00070 
00071   std::list<std::string> dirs;
00072   if ( getenv("TMP") )    dirs.push_back( getenv("TMP" ));
00073   if ( getenv("TMPDIR") ) dirs.push_back( getenv("TMPDIR" ));
00074   dirs.push_back( "/tmp" );
00075 
00076   std::string tmpd = "";
00077   for ( std::list<std::string>::iterator dir = dirs.begin(); dir != dirs.end() && tmpd == "" ; ++dir ) {
00078     if ( access( dir->data(), W_OK ) == 0 ) {
00079       tmpd = dir->data();
00080     }
00081   }
00082 
00083   if ( tmpd == "" )
00084     throw std::runtime_error("Can't find writable temporary directory. Set TMP environment variable");
00085 
00086   return tmpd;
00087 }
00088 
00089 //================================================================================
00098 //================================================================================
00099 std::string ParaMEDMEMTest::makeTmpFile( const std::string& tmpfile, const std::string& srcfile )
00100 {
00101   std::string tmpf = getTmpDirectory() + "/" + tmpfile;
00102   if ( srcfile != "" ) {
00103     std::string cmd  = "cp " + srcfile + " " + tmpf + " ; chmod +w " + tmpf;
00104     system( cmd.c_str() );
00105   }
00106   return tmpf;
00107 }
00108 
00109 
00114 ParaMEDMEMTest_TmpFilesRemover::~ParaMEDMEMTest_TmpFilesRemover()
00115 {
00116   std::set<std::string>::iterator it = myTmpFiles.begin();
00117   for (; it != myTmpFiles.end(); it++) {
00118     if (access((*it).data(), F_OK) == 0)
00119       remove((*it).data());
00120   }
00121   myTmpFiles.clear();
00122   //cout << "~ParaMEDMEMTest_TmpFilesRemover()" << endl;
00123 }
00124 
00125 bool ParaMEDMEMTest_TmpFilesRemover::Register(const std::string theTmpFile)
00126 {
00127   return (myTmpFiles.insert(theTmpFile)).second;
00128 }