1 /**
2  * @file    ConversionOption.cpp
3  * @brief   Implementation of ConversionOption, the class encapsulating conversion options.
4  * @author  Frank Bergmann
5  *
6  * <!--------------------------------------------------------------------------
7  * This file is part of libSBML.  Please visit http://sbml.org for more
8  * information about SBML, and the latest version of libSBML.
9  *
10  * Copyright (C) 2020 jointly by the following organizations:
11  *     1. California Institute of Technology, Pasadena, CA, USA
12  *     2. University of Heidelberg, Heidelberg, Germany
13  *     3. University College London, London, UK
14  *
15  * Copyright (C) 2019 jointly by the following organizations:
16  *     1. California Institute of Technology, Pasadena, CA, USA
17  *     2. University of Heidelberg, Heidelberg, Germany
18  *
19  * Copyright (C) 2013-2018 jointly by the following organizations:
20  *     1. California Institute of Technology, Pasadena, CA, USA
21  *     2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
22  *     3. University of Heidelberg, Heidelberg, Germany
23  *
24  * Copyright (C) 2009-2013 jointly by the following organizations:
25  *     1. California Institute of Technology, Pasadena, CA, USA
26  *     2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
27  *
28  * Copyright (C) 2006-2008 by the California Institute of Technology,
29  *     Pasadena, CA, USA
30  *
31  * Copyright (C) 2002-2005 jointly by the following organizations:
32  *     1. California Institute of Technology, Pasadena, CA, USA
33  *     2. Japan Science and Technology Agency, Japan
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the GNU Lesser General Public License as published by
37  * the Free Software Foundation.  A copy of the license agreement is provided
38  * in the file named "LICENSE.txt" included with this software distribution
39  * and also available online as http://sbml.org/software/libsbml/license.html
40  * ------------------------------------------------------------------------ -->
41  */
42 
43 
44 #ifdef __cplusplus
45 
46 #include <sbml/conversion/ConversionOption.h>
47 #include <sbml/SBase.h>
48 
49 #include <algorithm>
50 #include <string>
51 #include <sstream>
52 
53 using namespace std;
54 LIBSBML_CPP_NAMESPACE_BEGIN
55 
ConversionOption(const std::string & key,const std::string value,ConversionOptionType_t type,const std::string description)56 ConversionOption::ConversionOption(const std::string& key, const std::string value,
57     ConversionOptionType_t type,
58     const std::string description) :
59     mKey(key)
60   , mValue(value)
61   , mType(type)
62   , mDescription(description)
63 {
64 }
65 
ConversionOption(const std::string & key,const char * value,const std::string description)66 ConversionOption::ConversionOption(const std::string& key, const char* value,
67   const std::string description) :
68     mKey(key)
69   , mValue(value)
70   , mType(CNV_TYPE_STRING)
71   , mDescription(description)
72 {
73 }
74 
ConversionOption(const std::string & key,bool value,const std::string description)75 ConversionOption::ConversionOption(const std::string& key, bool value,
76   const std::string description) :
77     mKey(key)
78   , mValue("")
79   , mType(CNV_TYPE_STRING)
80   , mDescription(description)
81 {
82   setBoolValue(value);
83 }
84 
ConversionOption(const std::string & key,double value,const std::string description)85 ConversionOption::ConversionOption(const std::string& key, double value,
86   const std::string description):
87     mKey(key)
88   , mValue("")
89   , mType(CNV_TYPE_STRING)
90   , mDescription(description)
91 {
92   setDoubleValue(value);
93 }
94 
ConversionOption(const std::string & key,float value,const std::string description)95 ConversionOption::ConversionOption(const std::string& key, float value,
96   const std::string description) :
97     mKey(key)
98   , mValue("")
99   , mType(CNV_TYPE_STRING)
100   , mDescription(description)
101 {
102   setFloatValue(value);
103 }
104 
ConversionOption(const std::string & key,int value,const std::string description)105 ConversionOption::ConversionOption(const std::string& key, int value,
106   const std::string description) :
107     mKey(key)
108   , mValue("")
109   , mType(CNV_TYPE_STRING)
110   , mDescription(description)
111 {
112       setIntValue(value);
113 }
114 
115 
ConversionOption(const ConversionOption & orig)116 ConversionOption::ConversionOption
117   (const ConversionOption& orig)
118   : mKey(orig.mKey)
119   , mValue(orig.mValue)
120   , mType(orig.mType)
121   , mDescription(orig.mDescription)
122 {
123 }
124 
125 
126 
127 ConversionOption&
operator =(const ConversionOption & rhs)128 ConversionOption::operator=(const ConversionOption& rhs)
129 {
130   if(&rhs!=this)
131   {
132     mDescription = rhs.mDescription;
133     mKey = rhs.mKey;
134     mType = rhs.mType;
135     mValue = rhs.mValue;
136   }
137   return *this;
138 }
139 
140 ConversionOption*
clone() const141 ConversionOption::clone() const
142 {
143   return new ConversionOption(*this);
144 }
145 
~ConversionOption()146 ConversionOption::~ConversionOption() {}
147 
148 const std::string&
getKey() const149 ConversionOption::getKey() const
150 {
151   return mKey;
152 }
153 
154 void
setKey(const std::string & key)155 ConversionOption::setKey(const std::string& key)
156 {
157   mKey = key;
158 }
159 
160 const std::string&
getValue() const161 ConversionOption::getValue() const
162 {
163   return mValue;
164 }
165 
166 void
setValue(const std::string & value)167 ConversionOption::setValue(const std::string& value)
168 {
169   mValue = value;
170 }
171 
172 const std::string&
getDescription() const173 ConversionOption::getDescription() const
174 {
175   return mDescription;
176 }
177 
178 void
setDescription(const std::string & description)179 ConversionOption::setDescription(const std::string& description)
180 {
181   mDescription = description;
182 }
183 
184 ConversionOptionType_t
getType() const185 ConversionOption::getType() const
186 {
187   return mType;
188 }
189 
190 void
setType(ConversionOptionType_t type)191 ConversionOption::setType(ConversionOptionType_t type)
192 {
193   mType = type;
194 }
195 
196 bool
getBoolValue() const197 ConversionOption::getBoolValue() const
198 {
199   string value = mValue;
200 #ifdef __BORLANDC__
201    std::transform(value.begin(), value.end(), value.begin(),  (int(*)(int))
202 std::tolower);
203 #else
204    std::transform(value.begin(), value.end(), value.begin(), ::tolower);
205 #endif
206   if (value == "true") return true;
207   if (value == "false") return false;
208 
209   stringstream str; str << mValue;
210   bool result; str >> result;
211   return result;
212 }
213 
214 void
setBoolValue(bool value)215 ConversionOption::setBoolValue(bool value)
216 {
217   mValue = (value ? "true" : "false");
218   setType(CNV_TYPE_BOOL);
219 }
220 
221 double
getDoubleValue() const222 ConversionOption::getDoubleValue() const
223 {
224   stringstream str; str << mValue;
225   double result; str >> result;
226   return result;
227 }
228 
229 void
setDoubleValue(double value)230 ConversionOption::setDoubleValue(double value)
231 {
232   stringstream str; str << value;
233   mValue = str.str();
234   setType(CNV_TYPE_DOUBLE);
235 }
236 
237 
238 float
getFloatValue() const239 ConversionOption::getFloatValue() const
240 {
241   stringstream str; str << mValue;
242   float result; str >> result;
243   return result;
244 }
245 
246 void
setFloatValue(float value)247 ConversionOption::setFloatValue(float value)
248 {
249   stringstream str; str << value;
250   mValue = str.str();
251   setType(CNV_TYPE_SINGLE);
252 }
253 
254 
255 int
getIntValue() const256 ConversionOption::getIntValue() const
257 {
258   stringstream str; str << mValue;
259   int result; str >> result;
260   return result;
261 }
262 
263 void
setIntValue(int value)264 ConversionOption::setIntValue(int value)
265 {
266   stringstream str; str << value;
267   mValue = str.str();
268   setType(CNV_TYPE_INT);
269 }
270 
271 
272 LIBSBML_EXTERN
273 ConversionOption_t*
ConversionOption_create(const char * key)274 ConversionOption_create(const char* key)
275 {
276   return new ConversionOption(key);
277 }
278 
279 LIBSBML_EXTERN
280 void
ConversionOption_free(ConversionOption_t * co)281 ConversionOption_free(ConversionOption_t* co)
282 {
283   delete co;
284 }
285 
286 LIBSBML_EXTERN
287 ConversionOption_t*
ConversionOption_clone(const ConversionOption_t * co)288 ConversionOption_clone(const ConversionOption_t* co)
289 {
290   if (co == NULL) return NULL;
291   return co->clone();
292 }
293 
294 LIBSBML_EXTERN
295 ConversionOption_t*
ConversionOption_createWithKeyAndType(const char * key,ConversionOptionType_t type)296 ConversionOption_createWithKeyAndType(const char* key, ConversionOptionType_t type)
297 {
298   return new ConversionOption(key, type);
299 }
300 
301 LIBSBML_EXTERN
302 const char*
ConversionOption_getKey(const ConversionOption_t * co)303 ConversionOption_getKey(const ConversionOption_t* co)
304 {
305   if (co == NULL) return NULL;
306   return co->getKey().c_str();
307 }
308 
309 LIBSBML_EXTERN
310 const char*
ConversionOption_getDescription(const ConversionOption_t * co)311 ConversionOption_getDescription(const ConversionOption_t* co)
312 {
313   if (co == NULL) return NULL;
314   return co->getDescription().c_str();
315 }
316 
317 LIBSBML_EXTERN
318 const char*
ConversionOption_getValue(const ConversionOption_t * co)319 ConversionOption_getValue(const ConversionOption_t* co)
320 {
321   if (co == NULL) return NULL;
322   return co->getValue().c_str();
323 }
324 
325 LIBSBML_EXTERN
326 int
ConversionOption_getBoolValue(const ConversionOption_t * co)327 ConversionOption_getBoolValue(const ConversionOption_t* co)
328 {
329   if (co == NULL) return 0;
330   return (int) co->getBoolValue();
331 }
332 
333 LIBSBML_EXTERN
334 int
ConversionOption_getIntValue(const ConversionOption_t * co)335 ConversionOption_getIntValue(const ConversionOption_t* co)
336 {
337   if (co == NULL) return 0;
338   return (int) co->getIntValue();
339 }
340 
341 LIBSBML_EXTERN
342 float
ConversionOption_getFloatValue(const ConversionOption_t * co)343 ConversionOption_getFloatValue(const ConversionOption_t* co)
344 {
345   if (co == NULL) return std::numeric_limits<float>::quiet_NaN();
346   return co->getFloatValue();
347 }
348 
349 LIBSBML_EXTERN
350 double
ConversionOption_getDoubleValue(const ConversionOption_t * co)351 ConversionOption_getDoubleValue(const ConversionOption_t* co)
352 {
353   if (co == NULL) return std::numeric_limits<double>::quiet_NaN();
354   return co->getDoubleValue();
355 }
356 
357 LIBSBML_EXTERN
358 ConversionOptionType_t
ConversionOption_getType(const ConversionOption_t * co)359 ConversionOption_getType(const ConversionOption_t* co)
360 {
361   if (co == NULL) return CNV_TYPE_STRING;
362   return co->getType();
363 }
364 
365 LIBSBML_EXTERN
366 void
ConversionOption_setKey(ConversionOption_t * co,const char * key)367 ConversionOption_setKey(ConversionOption_t* co, const char* key)
368 {
369   if (co == NULL) return;
370   co->setKey(key);
371 }
372 
373 LIBSBML_EXTERN
374 void
ConversionOption_setDescription(ConversionOption_t * co,const char * description)375 ConversionOption_setDescription(ConversionOption_t* co, const char* description)
376 {
377   if (co == NULL) return;
378   co->setDescription(description);
379 
380 }
381 
382 LIBSBML_EXTERN
383 void
ConversionOption_setValue(ConversionOption_t * co,const char * value)384 ConversionOption_setValue(ConversionOption_t* co, const char* value)
385 {
386   if (co == NULL) return;
387   co->setValue(value);
388 }
389 
390 LIBSBML_EXTERN
391 void
ConversionOption_setBoolValue(ConversionOption_t * co,int value)392 ConversionOption_setBoolValue(ConversionOption_t* co, int value)
393 {
394   if (co == NULL) return;
395   co->setBoolValue(value != 0);
396 }
397 
398 LIBSBML_EXTERN
399 void
ConversionOption_setIntValue(ConversionOption_t * co,int value)400 ConversionOption_setIntValue(ConversionOption_t* co, int value)
401 {
402   if (co == NULL) return;
403   co->setIntValue(value);
404 }
405 
406 LIBSBML_EXTERN
407 void
ConversionOption_setFloatValue(ConversionOption_t * co,float value)408 ConversionOption_setFloatValue(ConversionOption_t* co, float value)
409 {
410   if (co == NULL) return;
411   co->setFloatValue(value);
412 }
413 
414 LIBSBML_EXTERN
415 void
ConversionOption_setDoubleValue(ConversionOption_t * co,double value)416 ConversionOption_setDoubleValue(ConversionOption_t* co, double value)
417 {
418   if (co == NULL) return;
419   co ->setDoubleValue(value);
420 }
421 
422 LIBSBML_EXTERN
423 void
ConversionOption_setType(ConversionOption_t * co,ConversionOptionType_t type)424 ConversionOption_setType(ConversionOption_t* co, ConversionOptionType_t type)
425 {
426   if (co == NULL) return;
427   co->setType(type);
428 }
429 
430 
431 LIBSBML_CPP_NAMESPACE_END
432 
433 #endif  /* __cplusplus */
434