Back to index

salome-smesh  6.5.0
UNV_Utilities.hxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #ifndef MED_Utilities_HeaderFile
00024 #define MED_Utilities_HeaderFile
00025 
00026 #include "SMESH_DriverUNV.hxx"
00027 
00028 #include <iostream>     
00029 #include <sstream>      
00030 #include <fstream>
00031 #include <string>
00032 #include <stdexcept>
00033 #include <cassert>
00034 #include <cstdlib>
00035 
00036 namespace UNV{
00037   using namespace std;
00038 
00039   const size_t theMaxLineLen = 80;
00040 
00041   class MESHDRIVERUNV_EXPORT PrefixPrinter{
00042     static int myCounter;
00043   public:
00044     PrefixPrinter();
00045     ~PrefixPrinter();
00046 
00047     static string GetPrefix();
00048   };
00049 
00055   inline bool beginning_of_dataset(std::istream& in_file, const std::string& ds_name)
00056   {
00057     assert (in_file.good());
00058     assert (!ds_name.empty());
00059     
00060     std::string olds, news;
00061     
00062     in_file.seekg(0);
00063     while(true){
00064       in_file >> olds >> news;
00065       /*
00066        * a "-1" followed by a number means the beginning of a dataset
00067        * stop combing at the end of the file
00068        */
00069       while( ((olds != "-1") || (news == "-1") ) && !in_file.eof() ){     
00070         olds = news;
00071         in_file >> news;
00072       }
00073       if(in_file.eof())
00074       {
00075         in_file.clear();
00076         return false;
00077       }
00078       if (news == ds_name)
00079         return true;
00080     }
00081     // should never end up here
00082     return false;
00083   }
00084 
00091   inline double D_to_e(std::string& number)
00092   {
00093     /* find "D" in string, start looking at 
00094      * 6th element, to improve speed.
00095      * We dont expect a "D" earlier
00096      */
00097     const int position = number.find("D",6);
00098     if(position != std::string::npos){
00099       number.replace(position, 1, "e"); 
00100     }
00101     return atof (number.c_str());
00102   }
00103   
00109   inline bool check_file(const std::string theFileName)
00110   {
00111     std::ifstream in_stream(theFileName.c_str());
00112     if (!in_stream)
00113       return false;
00114     std::string olds, news;
00115     while (!in_stream.eof()){
00116       olds = news;
00117       std::getline(in_stream, news, '\n');
00118     }
00119     return (olds == "    -1");
00120   }
00121 
00129   inline std::string read_line(std::ifstream& in_stream, const bool next=true)
00130   {
00131     char line[theMaxLineLen];
00132     in_stream.getline( line, theMaxLineLen );
00133     if ( next )
00134       in_stream.getline( line, theMaxLineLen );
00135 
00136     std::string resLine = line;
00137     if ( resLine.size() > 0 && resLine[ resLine.size()-1 ] == '\r' )
00138       resLine.resize( resLine.size()-1 );
00139     return line;
00140   }
00141 };
00142 
00143 
00144 #ifndef MESSAGE
00145 
00146 #define MESSAGE(msg) std::cout<<__FILE__<<"["<<__LINE__<<"]::"<<msg<<endl;
00147 
00148 #define BEGMSG(msg) std::cout<<UNV::PrefixPrinter::GetPrefix()<<msg
00149 
00150 #define ADDMSG(msg) std::cout<<msg
00151 
00152 #endif
00153 
00154 
00155 #ifndef EXCEPTION
00156 
00157 #define EXCEPTION(TYPE, MSG) {\
00158   std::ostringstream aStream;\
00159   aStream<<__FILE__<<"["<<__LINE__<<"]::"<<MSG;\
00160   throw TYPE(aStream.str());\
00161 }
00162 
00163 #endif
00164 
00165 #endif