Salome HOME
2ef33528657721bac9b73af4cf065b372441da1c
[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_Exception.hxx"
24 #include "XAO_Xao.hxx"
25 #include "XAO_XaoUtils.hxx"
26
27 using namespace XAO;
28
29
30 const std::string XaoUtils::intToString(const int& value)
31 {
32     std::ostringstream str;
33     str << value;
34     return str.str();
35 }
36
37 const int XaoUtils::stringToInt(const std::string& value)
38 throw(XAO_Exception)
39 {
40     int res;
41     std::istringstream convert(value);
42     if ( !(convert >> res) )
43         throw XAO_Exception(MsgBuilder() << "Cannot convert '" << value << "' to integer.");
44     return res;
45 }
46
47 const std::string XaoUtils::doubleToString(const double& value)
48 {
49     std::ostringstream str;
50     str << value;
51     return str.str();
52 }
53
54 const double XaoUtils::stringToDouble(const std::string& value)
55 throw(XAO_Exception)
56 {
57     double res;
58     std::istringstream convert(value);
59     if ( !(convert >> res) )
60         throw XAO_Exception(MsgBuilder() << "Cannot convert '" << value << "' to double.");
61     return res;
62 }
63
64 const std::string XaoUtils::booleanToString(const bool& value)
65 {
66     if (value)
67         return "true";
68     return "false";
69 }
70
71 const bool XaoUtils::stringToBoolean(const std::string& value)
72 throw(XAO_Exception)
73 {
74     if (value == "true" || value == "1")
75         return true;
76     if (value == "false" || value == "0")
77         return false;
78
79     throw XAO_Exception(MsgBuilder() << "Invalid boolean value: " << value);
80 }
81
82 const std::string XaoUtils::dimensionToString(const XAO::Dimension& dimension)
83 throw(XAO_Exception)
84 {
85     if (dimension == XAO::VERTEX)
86         return "vertex";
87     if (dimension == XAO::EDGE)
88         return "edge";
89     if (dimension == XAO::FACE)
90         return "face";
91     if (dimension == XAO::SOLID)
92         return "solid";
93     if (dimension == XAO::WHOLE)
94         return "whole";
95
96     throw XAO_Exception(MsgBuilder() << "Bad dimension: " << dimension);
97 }
98
99 const XAO::Dimension XaoUtils::stringToDimension(const std::string& dimension)
100 throw(XAO_Exception)
101 {
102     if (dimension == "vertex")
103         return XAO::VERTEX;
104     if (dimension == "edge")
105         return XAO::EDGE;
106     if (dimension == "face")
107         return XAO::FACE;
108     if (dimension == "solid")
109         return XAO::SOLID;
110     if (dimension == "whole")
111         return XAO::WHOLE;
112
113     throw XAO_Exception(MsgBuilder() << "Bad dimension: " << dimension);
114 }
115
116 const std::string XaoUtils::fieldTypeToString(const XAO::Type& type)
117 throw(XAO_Exception)
118 {
119     if (type ==XAO:: BOOLEAN)
120         return "boolean";
121     if (type == XAO::INTEGER)
122         return "integer";
123     if (type == XAO::DOUBLE)
124         return "double";
125     if (type == XAO::STRING)
126         return "string";
127
128     throw XAO_Exception(MsgBuilder() << "Bad type: " << type);
129 }
130
131 const XAO::Type XaoUtils::stringToFieldType(const std::string& type)
132 throw(XAO_Exception)
133 {
134     if (type == "boolean")
135         return XAO::BOOLEAN;
136     if (type == "integer")
137         return XAO::INTEGER;
138     if (type == "double")
139         return XAO::DOUBLE;
140     if (type == "string")
141         return XAO::STRING;
142
143     throw XAO_Exception(MsgBuilder() << "Bad type: " << type);
144 }
145
146 const std::string XaoUtils::shapeFormatToString(const XAO::Format& format)
147 throw(XAO_Exception)
148 {
149     if (format == XAO::BREP)
150         return "BREP";
151     if (format == XAO::STEP)
152         return "STEP";
153
154     throw XAO_Exception(MsgBuilder() << "Bad format: " << format);
155 }
156
157 const XAO::Format XaoUtils::stringToShapeFormat(const std::string& format)
158 throw(XAO_Exception)
159 {
160     if (format == "BREP")
161         return XAO::BREP;
162     if (format == "STEP")
163         return XAO::STEP;
164
165     throw XAO_Exception(MsgBuilder() << "Bad format: " << format);
166 }