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