src/DriverUNV/UNV_Utilities.hxx

Go to the documentation of this file.
00001 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00002 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00003 // 
00004 // This library is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public
00006 // License as published by the Free Software Foundation; either 
00007 // version 2.1 of the License.
00008 // 
00009 // This library is distributed in the hope that it will be useful 
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
00012 // Lesser General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Lesser General Public  
00015 // License along with this library; if not, write to the Free Software 
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00017 //
00018 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00019 
00020 #ifndef MED_Utilities_HeaderFile
00021 #define MED_Utilities_HeaderFile
00022 
00023 #include <iostream> 
00024 #include <sstream>  
00025 #include <fstream>
00026 #include <string>
00027 #include <stdexcept>
00028 #include <cassert>
00029 
00030 namespace UNV{
00031   using namespace std;
00032 
00033   class PrefixPrinter{
00034     static int myCounter;
00035   public:
00036     PrefixPrinter();
00037     ~PrefixPrinter();
00038 
00039     static string GetPrefix();
00040   };
00041 
00047   inline bool beginning_of_dataset(std::istream& in_file, const std::string& ds_name)
00048   {
00049     assert (in_file.good());
00050     assert (!ds_name.empty());
00051     
00052     std::string olds, news;
00053     
00054     while(true){
00055       in_file >> olds >> news;
00056       /*
00057        * a "-1" followed by a number means the beginning of a dataset
00058        * stop combing at the end of the file
00059        */
00060       while( ((olds != "-1") || (news == "-1") ) && !in_file.eof() ){   
00061      olds = news;
00062      in_file >> news;
00063       }
00064       if(in_file.eof())
00065      return false;
00066       if (news == ds_name)
00067      return true;
00068     }
00069     // should never end up here
00070     return false;
00071   }
00072 
00079   inline double D_to_e(std::string& number)
00080   {
00081     /* find "D" in string, start looking at 
00082      * 6th element, to improve speed.
00083      * We dont expect a "D" earlier
00084      */
00085     const int position = number.find("D",6);
00086     if(position != std::string::npos){
00087       number.replace(position, 1, "e"); 
00088     }
00089     return atof (number.c_str());
00090   }
00091   
00097   inline bool check_file(const std::string theFileName)
00098   {
00099     std::ifstream in_stream(theFileName.c_str());
00100     if (!in_stream)
00101       return false;
00102     std::string olds, news;
00103     while (!in_stream.eof()){
00104       olds = news;
00105       std::getline(in_stream, news, '\n');
00106     }
00107     return (olds == "    -1");
00108   }
00109 
00110 };
00111 
00112 
00113 #ifndef MESSAGE
00114 
00115 #define MESSAGE(msg) std::cout<<__FILE__<<"["<<__LINE__<<"]::"<<msg<<endl;
00116 
00117 #define BEGMSG(msg) std::cout<<UNV::PrefixPrinter::GetPrefix()<<msg
00118 
00119 #define ADDMSG(msg) std::cout<<msg
00120 
00121 #endif
00122 
00123 
00124 #ifndef EXCEPTION
00125 
00126 #define EXCEPTION(TYPE, MSG) {\
00127   std::ostringstream aStream;\
00128   aStream<<__FILE__<<"["<<__LINE__<<"]::"<<MSG;\
00129   throw TYPE(aStream.str());\
00130 }
00131 
00132 #endif
00133 
00134 #endif