Back to index

salome-med  6.5.0
MEDSPLITTERTest_Utils.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 // File      : MEDSPLITTERTest_TmpFilesRemover.hxx
00020 // Created   : Mon Jul  6 14:08:12 2009
00021 // Author    : Edward AGAPOV (eap)
00022 
00023 #include "MEDSPLITTERTest_Utils.hxx"
00024 
00025 #include <MEDMEM_STRING.hxx>
00026 
00027 #include <cppunit/TestAssert.h>
00028 #include <list>
00029 #include <cstdlib>
00030 
00031 #ifdef WIN32
00032 #include <windows.h>
00033 #endif
00034 
00035 using namespace std;
00036 
00037 namespace MEDSPLITTERTest_Utils
00038 {
00039 
00040 string getResourceFile( const string& filename )
00041 {
00042   string resourceFile = "";
00043 
00044   if ( getenv("top_srcdir") ) {
00045     // we are in 'make check' step
00046     resourceFile = getenv("top_srcdir");
00047     resourceFile += "/resources/";
00048   }
00049   else if ( getenv("MED_ROOT_DIR") ) {
00050     // use MED_ROOT_DIR env.var
00051     resourceFile = getenv("MED_ROOT_DIR");
00052     resourceFile += "/share/salome/resources/med/";
00053   }
00054   resourceFile += filename;
00055 #ifndef WIN32
00056   if (access(resourceFile.data(), F_OK) != 0)
00057 #else
00058   if (  GetFileAttributes (  resourceFile.c_str()  ) == 0xFFFFFFFF  )
00059     if (  GetLastError () == ERROR_FILE_NOT_FOUND  )
00060 #endif
00061     CPPUNIT_FAIL(MEDMEM::STRING("Resource file ")<< filename << " not found");
00062   
00063   return resourceFile;
00064 }
00065 
00066 
00067 //================================================================================
00071 //================================================================================
00072 
00073 string getTmpDirectory()
00074 {
00075   string path;
00076 
00077   list<string> dirs;
00078   if ( getenv("TMP") ) dirs.push_back( getenv("TMP" ));
00079   if ( getenv("TMPDIR") ) dirs.push_back( getenv("TMPDIR" ));
00080   dirs.push_back( "/tmp" );
00081 #ifndef WIN32
00082   for ( list<string>::iterator dir = dirs.begin(); dir != dirs.end(); ++dir )
00083     if ( access( dir->data(), W_OK ) == 0 )
00084       return *dir;
00085 #endif
00086 
00087   CPPUNIT_FAIL("Can't find writable temporary directory. Set TMP environment variable correctly");
00088   return "";
00089 }
00090 
00091 
00092 //================================================================================
00101 //================================================================================
00102 std::string makeTmpFile( const std::string& tmpfile, const std::string& srcfile )
00103 {
00104   std::string tmpf = getTmpDirectory() + "/" + tmpfile;
00105   if ( srcfile != "" ) {
00106     std::string cmd  = "cp " + srcfile + " " + tmpf + " ; chmod +w " + tmpf;
00107     system( cmd.c_str() );
00108   }
00109   return tmpf;
00110 }
00111 }
00112 
00113 // to automatically remove temporary files from disk
00114 bool MEDSPLITTERTest_TmpFilesRemover::Register(const string theTmpFile, const bool and_remove)
00115 {
00116 //   if ( and_remove && access( theTmpFile.data(), F_OK ) == 0 )
00117 //     remove(theTmpFile.data());
00118   return (myTmpFiles.insert(theTmpFile)).second;
00119 }
00120 
00121 MEDSPLITTERTest_TmpFilesRemover::~MEDSPLITTERTest_TmpFilesRemover()
00122 {
00123   set< string>::iterator it = myTmpFiles.begin();
00124   for (; it != myTmpFiles.end(); it++) {
00125 #ifndef WIN32
00126     if (access((*it).data(), F_OK) == 0)
00127 #else 
00128 
00129 #endif
00130       remove((*it).data());
00131 
00132   }
00133   myTmpFiles.clear();
00134 }
00135