Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[modules/smesh.git] / src / DriverUNV / UNV_Utilities.hxx
1 //  Copyright (C) 2003  CEA/DEN, EDF R&D
2 //
3 //
4 //
5 //  File   : VISU_DatConvertor.hxx
6 //  Author : Alexey PETROV
7 //  Module : VISU
8
9 #ifndef MED_Utilities_HeaderFile
10 #define MED_Utilities_HeaderFile
11
12 #include <iostream>     
13 #include <sstream>      
14 #include <string>
15 #include <stdexcept>
16
17
18 namespace UNV{
19   using namespace std;
20
21   class PrefixPrinter{
22     static int myCounter;
23   public:
24     PrefixPrinter();
25     ~PrefixPrinter();
26
27     static string GetPrefix();
28   };
29
30   /**
31    * @returns \p false when error occured, \p true otherwise.
32    * Adjusts the \p in_stream to the beginning of the
33    * dataset \p ds_name.
34    */
35   inline bool beginning_of_dataset(std::istream& in_file, const std::string& ds_name)
36   {
37     assert (in_file.good());
38     assert (!ds_name.empty());
39     
40     std::string olds, news;
41     
42     while(true){
43       in_file >> olds >> news;
44       /*
45        * a "-1" followed by a number means the beginning of a dataset
46        * stop combing at the end of the file
47        */
48       while( ((olds != "-1") || (news == "-1") ) && !in_file.eof() ){     
49         olds = news;
50         in_file >> news;
51       }
52       if(in_file.eof())
53         return false;
54       if (news == ds_name)
55         return true;
56     }
57     // should never end up here
58     return false;
59   }
60
61   /**
62    * Method for converting exponential notation
63    * from "D" to "e", for example
64    * \p 3.141592654D+00 \p --> \p 3.141592654e+00
65    * in order to make it readable for C++.
66    */
67   inline double D_to_e(std::string& number)
68   {
69     /* find "D" in string, start looking at 
70      * 6th element, to improve speed.
71      * We dont expect a "D" earlier
72      */
73     const int position = number.find("D",6);
74     if(position != std::string::npos){
75       number.replace(position, 1, "e"); 
76     }
77     return atof (number.c_str());
78   }
79
80 };
81
82
83 #ifndef MESSAGE
84
85 #define MESSAGE(msg) std::cout<<__FILE__<<"["<<__LINE__<<"]::"<<msg<<endl;
86
87 #define BEGMSG(msg) std::cout<<UNV::PrefixPrinter::GetPrefix()<<msg
88
89 #define ADDMSG(msg) std::cout<<msg
90
91 #endif
92
93
94 #ifndef EXCEPTION
95
96 #define EXCEPTION(TYPE, MSG) {\
97   std::ostringstream aStream;\
98   aStream<<__FILE__<<"["<<__LINE__<<"]::"<<MSG;\
99   throw TYPE(aStream.str());\
100 }
101
102 #endif
103
104 #endif