Back to index

salome-med  6.5.0
MEDMEMTest_Utils.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
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 "MEDMEMTest_Utils.hxx"
00021 #include <cstdlib>
00022 #include <list>
00023 #include <stdexcept>
00024 
00025 #ifdef WNT
00026 #include <windows.h>
00027 #endif
00028 
00029 //================================================================================
00039 //================================================================================
00040 
00041 std::string 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 fixSlashes( resourceFile );
00057 }
00058 
00059 //================================================================================
00064 //================================================================================
00065 
00066 std::string getTmpDirectory()
00067 {
00068   std::string path;
00069 
00070   std::list<std::string> dirs;
00071   if ( getenv("TEMP") )   dirs.push_back( getenv("TEMP" ));
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 #ifdef WNT
00079     if ( GetFileAttributes(dir->data()) & FILE_ATTRIBUTE_DIRECTORY )
00080 #else
00081     if ( access( dir->data(), W_OK ) == 0 )
00082 #endif
00083       tmpd = dir->data();
00084   }
00085 
00086   if ( tmpd == "" )
00087     throw std::runtime_error("Can't find writable temporary directory. Set TMP environment variable");
00088 
00089   return tmpd;
00090 }
00091 
00092 //================================================================================
00101 //================================================================================
00102 
00103 std::string makeTmpFile( const std::string& tmpfile, const std::string& srcfile )
00104 {
00105   std::string tmpf = fixSlashes( getTmpDirectory() + "/" + tmpfile );
00106   if ( srcfile != "" ) {
00107 #ifdef WNT
00108     std::string cmd  = "copy " + fixSlashes( srcfile ) + " " + tmpf;
00109 #else
00110     std::string cmd  = "cp " + srcfile + " " + tmpf + " ; chmod +w " + tmpf;
00111 #endif
00112     system( cmd.c_str() );
00113   }
00114   return tmpf;
00115 }
00116 
00117 //================================================================================
00122 //================================================================================
00123 
00124 std::string fixSlashes( const std::string& path)
00125 {
00126 #ifdef WNT
00127   std::string fixedpath = path;
00128   for ( int i=0; i < path.size(); ++i )
00129     if (path[i] == '/')
00130       fixedpath[i] = '\\';
00131   return fixedpath;
00132 #else
00133   return path;
00134 #endif
00135 }