Salome HOME
[CEA 13302] Policy for c++ files ? using namespaces in c++ header files. boost_ptr...
[modules/smesh.git] / src / DriverUNV / UNV_Utilities.hxx
1 // Copyright (C) 2007-2019  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, or (at your option) any later version.
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
23 #ifndef UNV_Utilities_HeaderFile
24 #define UNV_Utilities_HeaderFile
25
26 #include "SMESH_DriverUNV.hxx"
27
28 #include <iostream>     
29 #include <sstream>      
30 #include <fstream>
31 #include <string>
32 #include <stdexcept>
33 #include <cassert>
34 #include <cstdlib>
35
36 namespace UNV {
37
38   const size_t theMaxLineLen = 82; // 80 for text + 2 for "\r\n"
39
40   class MESHDRIVERUNV_EXPORT PrefixPrinter{
41     static int myCounter;
42   public:
43     PrefixPrinter();
44     ~PrefixPrinter();
45
46     static std::string GetPrefix();
47   };
48
49   /**
50    * @returns \p false when error occurred, \p true otherwise.
51    * Adjusts the \p in_stream to the beginning of the
52    * dataset \p ds_name.
53    */
54   inline bool beginning_of_dataset(std::istream& in_file, const std::string& ds_name)
55   {
56     assert (in_file.good());
57     assert (!ds_name.empty());
58
59     std::string olds, news;
60
61     in_file.seekg(0);
62     while(true)
63     {
64       in_file >> olds >> news;
65       /*
66        * a "-1" followed by a number means the beginning of a dataset
67        * stop combing at the end of the file
68        */
69       while( ((olds != "-1") || (news == "-1")))
70       {
71         olds = news;
72         in_file >> news;
73
74         if ( in_file.eof() || in_file.fail() )
75         {
76           in_file.clear();
77           return false;
78         }
79       }
80       if (news == ds_name)
81         return true;
82     }
83     // should never end up here
84     return false;
85   }
86
87   /**
88    * Method for converting exponential notation
89    * from "D" to "e", for example
90    * \p 3.141592654D+00 \p --> \p 3.141592654e+00
91    * in order to make it readable for C++.
92    */
93   inline double D_to_e(std::string& number)
94   {
95     /* find "D" in string, start looking at
96      * 6th element, to improve speed.
97      * We don't expect a "D" earlier
98      */
99     const size_t position = number.find("D",6);
100     if ( position != std::string::npos )
101       number.replace(position, 1, "e");
102
103     return atof (number.c_str());
104   }
105
106   /**
107    * @returns \p false when file is incorrect, \p true otherwise.
108    * Check file with name \p theFileName for correct terminate
109    * string, i.e. the next to the last line is equal to "    -1",
110    */
111   inline bool check_file(const std::string theFileName)
112   {
113     std::ifstream in_stream(theFileName.c_str());
114     if (!in_stream)
115       return false;
116     std::string olds, news;
117     while (!in_stream.eof()){
118       olds = news;
119       std::getline(in_stream, news, '\n');
120     }
121     return (olds == "    -1");
122   }
123
124   /*!
125    * \brief reads a whole line
126    *  \param in_stream - source stream
127    *  \param next - if true, first reads the current line up to the end
128    *  which is necessary after reading using >> operator
129    *  \retval std::string - the result line
130    */
131   inline std::string read_line(std::ifstream& in_stream, const bool next=true)
132   {
133     std::string resLine;
134     std::getline( in_stream, resLine );
135     if ( next )
136       std::getline( in_stream, resLine );
137
138     if ( resLine.size() > 0 && resLine[ resLine.size()-1 ] == '\r' )
139       resLine.resize( resLine.size()-1 );
140     return resLine;
141   }
142 };
143
144
145 #ifndef MESSAGE
146
147 #define MESSAGE(msg) std::cout<<__FILE__<<"["<<__LINE__<<"]::"<<msg<<endl;
148
149 #define BEGMSG(msg) std::cout<<UNV::PrefixPrinter::GetPrefix()<<msg
150
151 #define ADDMSG(msg) std::cout<<msg
152
153 #endif
154
155
156 #ifndef EXCEPTION
157
158 #define EXCEPTION(TYPE, MSG) {\
159   std::ostringstream aStream;\
160   aStream<<__FILE__<<"["<<__LINE__<<"]::"<<MSG;\
161   throw TYPE(aStream.str());\
162 }
163
164 #endif
165
166 #endif