Salome HOME
updated copyright message
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_ScalarVariable.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, 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, or (at your option) any later version.
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 "Basics_Utils.hxx"
27 #include <iostream>
28 #include <cstdlib>
29 #include <cstdio>
30
31 #define OLDSTUDY_COMPATIBILITY
32
33 //============================================================================
34 /*! Function : SALOMEDSImpl_ScalarVariable
35  *  Purpose  : 
36  */
37 //============================================================================
38 SALOMEDSImpl_ScalarVariable::
39 SALOMEDSImpl_ScalarVariable(SALOMEDSImpl_GenericVariable::VariableTypes type,
40                             const std::string& theName):
41   SALOMEDSImpl_GenericVariable(type,theName),
42   myValue( 1e+100 )
43 {}
44
45 //============================================================================
46 /*! Function : ~SALOMEDSImpl_ScalarVariable()
47  *  Purpose  : 
48  */
49 //============================================================================
50 SALOMEDSImpl_ScalarVariable::~SALOMEDSImpl_ScalarVariable(){}
51
52 //============================================================================
53 /*! Function : 
54  *  Purpose  : 
55  */
56 //============================================================================
57 bool SALOMEDSImpl_ScalarVariable::setValue(const double theValue)
58 {
59   
60   if(myValue == theValue) 
61     return false;
62   
63   myValue = theValue;
64   return true;
65 }
66
67 //============================================================================
68 /*! Function : 
69  *  Purpose  : 
70  */
71 //============================================================================
72 bool SALOMEDSImpl_ScalarVariable::setStringValue(const std::string& theValue)
73 {
74   
75   if(myStrValue == theValue) 
76     return false;
77   
78   myStrValue = theValue;
79   return true;
80 }
81
82 //============================================================================
83 /*! Function : getValue()
84  *  Purpose  : 
85  */
86 //============================================================================
87 double SALOMEDSImpl_ScalarVariable::getValue() const
88 {
89   return myValue;
90 }
91
92 //============================================================================
93 /*! Function : getStringValue()
94  *  Purpose  : 
95  */
96 //============================================================================
97 std::string SALOMEDSImpl_ScalarVariable::getStringValue() const
98 {
99   return myStrValue;
100 }
101
102 //============================================================================
103 /*! Function : Save()
104  *  Purpose  : 
105  */
106 //============================================================================
107 std::string SALOMEDSImpl_ScalarVariable::Save() const{
108   Kernel_Utils::Localizer loc;
109
110   char buffer[255];
111   switch(Type())
112     {
113     case SALOMEDSImpl_GenericVariable::REAL_VAR:
114       {
115         sprintf(buffer, "%.64e", myValue);
116         break;
117       }
118     case SALOMEDSImpl_GenericVariable::BOOLEAN_VAR:
119     case SALOMEDSImpl_GenericVariable::INTEGER_VAR:
120       {
121         sprintf(buffer, "%d", (int)myValue);
122         break;
123       }
124     case SALOMEDSImpl_GenericVariable::STRING_VAR:
125       {
126         sprintf(buffer, "%s", myStrValue.c_str());
127         break;
128       }
129     default:break;
130     }
131   return std::string(buffer);
132 }
133
134 //============================================================================
135 /*! Function : SaveToScript()
136  *  Purpose  : 
137  */
138 //============================================================================
139 std::string SALOMEDSImpl_ScalarVariable::SaveToScript() const
140 {
141   Kernel_Utils::Localizer loc;
142
143   char buffer[255];
144   switch(Type())
145     {
146     case SALOMEDSImpl_GenericVariable::REAL_VAR:
147       {
148         sprintf(buffer, "%g", myValue);
149         break;
150       }
151     case SALOMEDSImpl_GenericVariable::INTEGER_VAR:
152       {
153         sprintf(buffer, "%d", (int)myValue);
154         break;
155       }
156     case SALOMEDSImpl_GenericVariable::BOOLEAN_VAR:
157       {
158         sprintf(buffer, "%s", ((bool)myValue) ? "True" : "False");
159         break;
160       }
161     case SALOMEDSImpl_GenericVariable::STRING_VAR:
162       {
163         sprintf(buffer, "\"%s\"", myStrValue.c_str());
164         break;
165       }
166     default:break;
167     }
168   return std::string(buffer);
169 }
170
171 //============================================================================
172 /*! Function : SaveType()
173  *  Purpose  : 
174  */
175 //============================================================================
176 std::string SALOMEDSImpl_ScalarVariable::SaveType() const{
177   char buffer[255];
178   sprintf(buffer, "%d", (int)Type());
179   return std::string(buffer);
180 }
181
182 //============================================================================
183 /*! Function : Load()
184  *  Purpose  : 
185  */
186 //============================================================================
187 void SALOMEDSImpl_ScalarVariable::Load(const std::string& theStrValue)
188 {
189   Kernel_Utils::Localizer loc;
190
191   std::string strCopy = theStrValue;
192   if ( Type() == SALOMEDSImpl_GenericVariable::STRING_VAR ) {
193 #ifdef OLDSTUDY_COMPATIBILITY
194     if (strCopy.size() > 1 && strCopy[0] == '\"' && strCopy[strCopy.size()-1] == '\"')
195       strCopy = strCopy.substr(1, strCopy.size()-2);
196 #endif // OLDSTUDY_COMPATIBILITY
197     setStringValue( strCopy );
198   }
199   else {
200 #ifdef OLDSTUDY_COMPATIBILITY
201     size_t dotpos = strCopy.find(',');
202     if (dotpos != std::string::npos)
203       strCopy.replace(dotpos, 1, ".");
204 #endif // OLDSTUDY_COMPATIBILITY
205     double aValue = atof(strCopy.c_str());
206     setValue(aValue);
207   }
208 }