Debian Codegraph @

src/3rdParty/salomesmesh/inc/UNV_Utilities.hxx

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