]> SALOME platform Git repositories - modules/geom.git/blob - src/XAO/XAO_XaoUtils.cxx
Salome HOME
Merge from BR_V7_main_Field branch (02/09/2013)
[modules/geom.git] / src / XAO / XAO_XaoUtils.cxx
1 // Copyright (C) 2013  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Frederic Pons (OpenCascade)
20
21 #include <sstream>
22
23 #include "XAO_Xao.hxx"
24 #include "XAO_XaoUtils.hxx"
25
26 using namespace XAO;
27
28
29 const std::string XaoUtils::intToString(const int& value)
30 {
31     std::ostringstream str;
32     str << value;
33     return str.str();
34 }
35
36 const int XaoUtils::stringToInt(const std::string& value)
37 throw(XAO_Exception)
38 {
39     int res;
40     std::istringstream convert(value);
41     if ( !(convert >> res) )
42         throw XAO_Exception(MsgBuilder() << "Cannot convert '" << value << "' to integer.");
43     return res;
44 }
45
46 const std::string XaoUtils::doubleToString(const double& value)
47 {
48     std::ostringstream str;
49     str << value;
50     return str.str();
51 }
52
53 const double XaoUtils::stringToDouble(const std::string& value)
54 throw(XAO_Exception)
55 {
56     double res;
57     std::istringstream convert(value);
58     if ( !(convert >> res) )
59         throw XAO_Exception(MsgBuilder() << "Cannot convert '" << value << "' to double.");
60     return res;
61 }
62
63 const std::string XaoUtils::booleanToString(const bool& value)
64 {
65     if (value)
66         return "true";
67     return "false";
68 }
69
70 const bool XaoUtils::stringToBoolean(const std::string& value)
71 throw(XAO_Exception)
72 {
73     if (value == "true" || value == "1")
74         return true;
75     if (value == "false" || value == "0")
76         return false;
77
78     throw XAO_Exception(MsgBuilder() << "Invalid boolean value: " << value);
79 }
80
81 const std::string XaoUtils::dimensionToString(const XAO::Dimension& dimension)
82 throw(XAO_Exception)
83 {
84     if (dimension == XAO::VERTEX)
85         return "vertex";
86     if (dimension == XAO::EDGE)
87         return "edge";
88     if (dimension == XAO::FACE)
89         return "face";
90     if (dimension == XAO::SOLID)
91         return "solid";
92     if (dimension == XAO::WHOLE)
93         return "whole";
94
95     throw XAO_Exception(MsgBuilder() << "Bad dimension: " << dimension);
96 }
97
98 const XAO::Dimension XaoUtils::stringToDimension(const std::string& dimension)
99 throw(XAO_Exception)
100 {
101     if (dimension == "vertex")
102         return XAO::VERTEX;
103     if (dimension == "edge")
104         return XAO::EDGE;
105     if (dimension == "face")
106         return XAO::FACE;
107     if (dimension == "solid")
108         return XAO::SOLID;
109     if (dimension == "whole")
110         return XAO::WHOLE;
111
112     throw XAO_Exception(MsgBuilder() << "Bad dimension: " << dimension);
113 }
114
115 const std::string XaoUtils::fieldTypeToString(const XAO::Type& type)
116 throw(XAO_Exception)
117 {
118     if (type ==XAO:: BOOLEAN)
119         return "boolean";
120     if (type == XAO::INTEGER)
121         return "integer";
122     if (type == XAO::DOUBLE)
123         return "double";
124     if (type == XAO::STRING)
125         return "string";
126
127     throw XAO_Exception(MsgBuilder() << "Bad type: " << type);
128 }
129
130 const XAO::Type XaoUtils::stringToFieldType(const std::string& type)
131 throw(XAO_Exception)
132 {
133     if (type == "boolean")
134         return XAO::BOOLEAN;
135     if (type == "integer")
136         return XAO::INTEGER;
137     if (type == "double")
138         return XAO::DOUBLE;
139     if (type == "string")
140         return XAO::STRING;
141
142     throw XAO_Exception(MsgBuilder() << "Bad type: " << type);
143 }
144
145 const std::string XaoUtils::shapeFormatToString(const XAO::Format& format)
146 throw(XAO_Exception)
147 {
148     if (format == XAO::BREP)
149         return "BREP";
150     if (format == XAO::STEP)
151         return "STEP";
152
153     throw XAO_Exception(MsgBuilder() << "Bad format: " << format);
154 }
155
156 const XAO::Format XaoUtils::stringToShapeFormat(const std::string& format)
157 throw(XAO_Exception)
158 {
159     if (format == "BREP")
160         return XAO::BREP;
161     if (format == "STEP")
162         return XAO::STEP;
163
164     throw XAO_Exception(MsgBuilder() << "Bad format: " << format);
165 }