Salome HOME
NPAL14921 ( memory limitation and Salome freeze )
[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 <fstream>
26 #include <string>
27 #include <stdexcept>
28 #include <cassert>
29
30 namespace UNV{
31   using namespace std;
32
33   class PrefixPrinter{
34     static int myCounter;
35   public:
36     PrefixPrinter();
37     ~PrefixPrinter();
38
39     static string GetPrefix();
40   };
41
42   /**
43    * @returns \p false when error occured, \p true otherwise.
44    * Adjusts the \p in_stream to the beginning of the
45    * dataset \p ds_name.
46    */
47   inline bool beginning_of_dataset(std::istream& in_file, const std::string& ds_name)
48   {
49     assert (in_file.good());
50     assert (!ds_name.empty());
51     
52     std::string olds, news;
53     
54     while(true){
55       in_file >> olds >> news;
56       /*
57        * a "-1" followed by a number means the beginning of a dataset
58        * stop combing at the end of the file
59        */
60       while( ((olds != "-1") || (news == "-1") ) && !in_file.eof() ){     
61         olds = news;
62         in_file >> news;
63       }
64       if(in_file.eof())
65         return false;
66       if (news == ds_name)
67         return true;
68     }
69     // should never end up here
70     return false;
71   }
72
73   /**
74    * Method for converting exponential notation
75    * from "D" to "e", for example
76    * \p 3.141592654D+00 \p --> \p 3.141592654e+00
77    * in order to make it readable for C++.
78    */
79   inline double D_to_e(std::string& number)
80   {
81     /* find "D" in string, start looking at 
82      * 6th element, to improve speed.
83      * We dont expect a "D" earlier
84      */
85     const int position = number.find("D",6);
86     if(position != std::string::npos){
87       number.replace(position, 1, "e"); 
88     }
89     return atof (number.c_str());
90   }
91   
92   /**
93    * @returns \p false when file is incorrect, \p true otherwise.
94    * Check file with name \p theFileName for correct terminate
95    * string, i.e. the next to the last line is equal to "    -1",
96    */
97   inline bool check_file(const std::string theFileName)
98   {
99     std::ifstream in_stream(theFileName.c_str());
100     if (!in_stream)
101       return false;
102     std::string olds, news;
103     while (!in_stream.eof()){
104       olds = news;
105       std::getline(in_stream, news, '\n');
106     }
107     return (olds == "    -1");
108   }
109
110 };
111
112
113 #ifndef MESSAGE
114
115 #define MESSAGE(msg) std::cout<<__FILE__<<"["<<__LINE__<<"]::"<<msg<<endl;
116
117 #define BEGMSG(msg) std::cout<<UNV::PrefixPrinter::GetPrefix()<<msg
118
119 #define ADDMSG(msg) std::cout<<msg
120
121 #endif
122
123
124 #ifndef EXCEPTION
125
126 #define EXCEPTION(TYPE, MSG) {\
127   std::ostringstream aStream;\
128   aStream<<__FILE__<<"["<<__LINE__<<"]::"<<MSG;\
129   throw TYPE(aStream.str());\
130 }
131
132 #endif
133
134 #endif