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