Salome HOME
Merge from V5_1_main 14/05/2010
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_ScalarVariable.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
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
20 //  File   : SALOMEDSImpl_ScalarVariable.cxx
21 //  Author : Roman NIKOLAEV, Open CASCADE S.A.S.
22 //  Module : SALOME
23 //
24 #include "SALOMEDSImpl_ScalarVariable.hxx"
25 #include "SALOMEDSImpl_GenericVariable.hxx"
26 #include <iostream>
27 #include <cstdlib>
28 #include <cstdio>
29
30 //============================================================================
31 /*! Function : SALOMEDSImpl_ScalarVariable
32  *  Purpose  : 
33  */
34 //============================================================================
35 SALOMEDSImpl_ScalarVariable::
36 SALOMEDSImpl_ScalarVariable(SALOMEDSImpl_GenericVariable::VariableTypes type,
37                             const std::string& theName):
38   SALOMEDSImpl_GenericVariable(type,theName)
39 {}
40
41 //============================================================================
42 /*! Function : ~SALOMEDSImpl_ScalarVariable()
43  *  Purpose  : 
44  */
45 //============================================================================
46 SALOMEDSImpl_ScalarVariable::~SALOMEDSImpl_ScalarVariable(){}
47
48 //============================================================================
49 /*! Function : 
50  *  Purpose  : 
51  */
52 //============================================================================
53 bool SALOMEDSImpl_ScalarVariable::setValue(const double theValue)
54 {
55   
56   if(myValue == theValue) 
57     return false;
58   
59   myValue = theValue;
60   return true;
61 }
62
63 //============================================================================
64 /*! Function : 
65  *  Purpose  : 
66  */
67 //============================================================================
68 bool SALOMEDSImpl_ScalarVariable::setStringValue(const std::string& theValue)
69 {
70   
71   if(myStrValue == theValue) 
72     return false;
73   
74   myStrValue = theValue;
75   return true;
76 }
77
78 //============================================================================
79 /*! Function : getValue()
80  *  Purpose  : 
81  */
82 //============================================================================
83 double SALOMEDSImpl_ScalarVariable::getValue() const
84 {
85   return myValue;
86 }
87
88 //============================================================================
89 /*! Function : getStringValue()
90  *  Purpose  : 
91  */
92 //============================================================================
93 std::string SALOMEDSImpl_ScalarVariable::getStringValue() const
94 {
95   return myStrValue;
96 }
97
98 //============================================================================
99 /*! Function : Save()
100  *  Purpose  : 
101  */
102 //============================================================================
103 std::string SALOMEDSImpl_ScalarVariable::Save() const{
104   char buffer[255];
105   switch(Type())
106     {
107     case SALOMEDSImpl_GenericVariable::REAL_VAR:
108       {
109         sprintf(buffer, "%.64e", myValue);
110         break;
111       }
112     case SALOMEDSImpl_GenericVariable::BOOLEAN_VAR:
113     case SALOMEDSImpl_GenericVariable::INTEGER_VAR:
114       {
115         sprintf(buffer, "%d", (int)myValue);
116         break;
117       }
118     case SALOMEDSImpl_GenericVariable::STRING_VAR:
119       {
120         sprintf(buffer, "\"%s\"", myStrValue.c_str());
121         break;
122       }
123     default:break;
124     }
125   return std::string(buffer);
126 }
127
128 //============================================================================
129 /*! Function : SaveToScript()
130  *  Purpose  : 
131  */
132 //============================================================================
133 std::string SALOMEDSImpl_ScalarVariable::SaveToScript() const
134 {
135   char buffer[255];
136   switch(Type())
137     {
138     case SALOMEDSImpl_GenericVariable::REAL_VAR:
139       {
140         sprintf(buffer, "%g", myValue);
141         break;
142       }
143     case SALOMEDSImpl_GenericVariable::INTEGER_VAR:
144       {
145         sprintf(buffer, "%d", (int)myValue);
146         break;
147       }
148     case SALOMEDSImpl_GenericVariable::BOOLEAN_VAR:
149       {
150         if((bool)myValue)
151           sprintf(buffer, "%s", "True");
152         else
153           sprintf(buffer, "%s", "False");
154         break;
155       }
156     case SALOMEDSImpl_GenericVariable::STRING_VAR:
157       {
158         sprintf(buffer, "\"%s\"", myStrValue.c_str());
159         break;
160       }
161     default:break;
162     }
163   return std::string(buffer);
164 }
165
166 //============================================================================
167 /*! Function : SaveType()
168  *  Purpose  : 
169  */
170 //============================================================================
171 std::string SALOMEDSImpl_ScalarVariable::SaveType() const{
172   char buffer[255];
173   sprintf(buffer, "%d", (int)Type());
174   return std::string(buffer);
175 }
176
177 //============================================================================
178 /*! Function : Load()
179  *  Purpose  : 
180  */
181 //============================================================================
182 void SALOMEDSImpl_ScalarVariable::Load(const std::string& theStrValue)
183 {
184   double aValue = atof(theStrValue.c_str());
185   setValue(aValue);
186 }