Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/smesh.git] / src / DriverUNV / UNV_Utilities.hxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19
20 #ifndef MED_Utilities_HeaderFile
21 #define MED_Utilities_HeaderFile
22
23 #include <iostream>     
24 #include <sstream>      
25 #include <string>
26 #include <stdexcept>
27 #include <cassert>
28
29 namespace UNV{
30   using namespace std;
31
32   class PrefixPrinter{
33     static int myCounter;
34   public:
35     PrefixPrinter();
36     ~PrefixPrinter();
37
38     static string GetPrefix();
39   };
40
41   /**
42    * @returns \p false when error occured, \p true otherwise.
43    * Adjusts the \p in_stream to the beginning of the
44    * dataset \p ds_name.
45    */
46   inline bool beginning_of_dataset(std::istream& in_file, const std::string& ds_name)
47   {
48     assert (in_file.good());
49     assert (!ds_name.empty());
50     
51     std::string olds, news;
52     
53     while(true){
54       in_file >> olds >> news;
55       /*
56        * a "-1" followed by a number means the beginning of a dataset
57        * stop combing at the end of the file
58        */
59       while( ((olds != "-1") || (news == "-1") ) && !in_file.eof() ){     
60         olds = news;
61         in_file >> news;
62       }
63       if(in_file.eof())
64         return false;
65       if (news == ds_name)
66         return true;
67     }
68     // should never end up here
69     return false;
70   }
71
72   /**
73    * Method for converting exponential notation
74    * from "D" to "e", for example
75    * \p 3.141592654D+00 \p --> \p 3.141592654e+00
76    * in order to make it readable for C++.
77    */
78   inline double D_to_e(std::string& number)
79   {
80     /* find "D" in string, start looking at 
81      * 6th element, to improve speed.
82      * We dont expect a "D" earlier
83      */
84     const int position = number.find("D",6);
85     if(position != std::string::npos){
86       number.replace(position, 1, "e"); 
87     }
88     return atof (number.c_str());
89   }
90
91 };
92
93
94 #ifndef MESSAGE
95
96 #define MESSAGE(msg) std::cout<<__FILE__<<"["<<__LINE__<<"]::"<<msg<<endl;
97
98 #define BEGMSG(msg) std::cout<<UNV::PrefixPrinter::GetPrefix()<<msg
99
100 #define ADDMSG(msg) std::cout<<msg
101
102 #endif
103
104
105 #ifndef EXCEPTION
106
107 #define EXCEPTION(TYPE, MSG) {\
108   std::ostringstream aStream;\
109   aStream<<__FILE__<<"["<<__LINE__<<"]::"<<MSG;\
110   throw TYPE(aStream.str());\
111 }
112
113 #endif
114
115 #endif