Salome HOME
0020523: String notebook support
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_ScalarVariable.cxx
1 // Copyright (C) 2008  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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
29 using namespace std;
30
31 //============================================================================
32 /*! Function : SALOMEDSImpl_ScalarVariable
33  *  Purpose  : 
34  */
35 //============================================================================
36 SALOMEDSImpl_ScalarVariable::
37 SALOMEDSImpl_ScalarVariable(SALOMEDSImpl_GenericVariable::VariableTypes type,
38                             const string& theName):
39   SALOMEDSImpl_GenericVariable(type,theName)
40 {}
41
42 //============================================================================
43 /*! Function : ~SALOMEDSImpl_ScalarVariable()
44  *  Purpose  : 
45  */
46 //============================================================================
47 SALOMEDSImpl_ScalarVariable::~SALOMEDSImpl_ScalarVariable(){}
48
49 //============================================================================
50 /*! Function : 
51  *  Purpose  : 
52  */
53 //============================================================================
54 bool SALOMEDSImpl_ScalarVariable::setValue(const double theValue)
55 {
56   
57   if(myValue == theValue) 
58     return false;
59   
60   myValue = theValue;
61   return true;
62 }
63
64 //============================================================================
65 /*! Function : 
66  *  Purpose  : 
67  */
68 //============================================================================
69 bool SALOMEDSImpl_ScalarVariable::setStringValue(const string& theValue)
70 {
71   
72   if(myStrValue == theValue) 
73     return false;
74   
75   myStrValue = theValue;
76   return true;
77 }
78
79 //============================================================================
80 /*! Function : getValue()
81  *  Purpose  : 
82  */
83 //============================================================================
84 double SALOMEDSImpl_ScalarVariable::getValue() const
85 {
86   return myValue;
87 }
88
89 //============================================================================
90 /*! Function : getStringValue()
91  *  Purpose  : 
92  */
93 //============================================================================
94 string SALOMEDSImpl_ScalarVariable::getStringValue() const
95 {
96   return myStrValue;
97 }
98
99 //============================================================================
100 /*! Function : Save()
101  *  Purpose  : 
102  */
103 //============================================================================
104 string SALOMEDSImpl_ScalarVariable::Save() const{
105   char buffer[255];
106   switch(Type())
107     {
108     case SALOMEDSImpl_GenericVariable::REAL_VAR:
109       {
110         sprintf(buffer, "%.64e", myValue);
111         break;
112       }
113     case SALOMEDSImpl_GenericVariable::BOOLEAN_VAR:
114     case SALOMEDSImpl_GenericVariable::INTEGER_VAR:
115       {
116         sprintf(buffer, "%d", (int)myValue);
117         break;
118       }
119     case SALOMEDSImpl_GenericVariable::STRING_VAR:
120       {
121         sprintf(buffer, "\"%s\"", myStrValue.c_str());
122         break;
123       }
124     default:break;
125     }
126   return string(buffer);
127 }
128
129 //============================================================================
130 /*! Function : SaveToScript()
131  *  Purpose  : 
132  */
133 //============================================================================
134 string SALOMEDSImpl_ScalarVariable::SaveToScript() const
135 {
136   char buffer[255];
137   switch(Type())
138     {
139     case SALOMEDSImpl_GenericVariable::REAL_VAR:
140       {
141         sprintf(buffer, "%g", myValue);
142         break;
143       }
144     case SALOMEDSImpl_GenericVariable::INTEGER_VAR:
145       {
146         sprintf(buffer, "%d", (int)myValue);
147         break;
148       }
149     case SALOMEDSImpl_GenericVariable::BOOLEAN_VAR:
150       {
151         if((bool)myValue)
152           sprintf(buffer, "%s", "True");
153         else
154           sprintf(buffer, "%s", "False");
155         break;
156       }
157     case SALOMEDSImpl_GenericVariable::STRING_VAR:
158       {
159         sprintf(buffer, "\"%s\"", myStrValue.c_str());
160         break;
161       }
162     default:break;
163     }
164   return string(buffer);
165 }
166
167 //============================================================================
168 /*! Function : SaveType()
169  *  Purpose  : 
170  */
171 //============================================================================
172 string SALOMEDSImpl_ScalarVariable::SaveType() const{
173   char buffer[255];
174   sprintf(buffer, "%d", (int)Type());
175   return string(buffer);
176 }
177
178 //============================================================================
179 /*! Function : Load()
180  *  Purpose  : 
181  */
182 //============================================================================
183 void SALOMEDSImpl_ScalarVariable::Load(const string& theStrValue)
184 {
185   double aValue = atof(theStrValue.c_str());
186   setValue(aValue);
187 }