1 /**
2  * @file    ConversionProperties.cpp
3  * @brief   Implemenentation of ConversionProperties, the class encapsulating conversion configuration.
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 #ifdef __cplusplus
44 
45 #include <sbml/conversion/ConversionProperties.h>
46 #include <sbml/conversion/ConversionOption.h>
47 #include <sbml/util/util.h>
48 #include <sbml/SBase.h>
49 
50 #include <algorithm>
51 #include <string>
52 #include <sstream>
53 
54 using namespace std;
55 LIBSBML_CPP_NAMESPACE_BEGIN
56 
ConversionProperties(SBMLNamespaces * targetNS)57 ConversionProperties::ConversionProperties(SBMLNamespaces* targetNS)
58   : mTargetNamespaces(NULL)
59   , mOptions()
60 {
61   if (targetNS != NULL) mTargetNamespaces = targetNS->clone();
62 }
63 
ConversionProperties(const ConversionProperties & orig)64 ConversionProperties::ConversionProperties(const ConversionProperties& orig)
65   : mTargetNamespaces(NULL)
66   , mOptions()
67 {
68 
69   if (orig.mTargetNamespaces != NULL)
70     mTargetNamespaces = orig.mTargetNamespaces->clone();
71 
72   map<string, ConversionOption*>::const_iterator it;
73   for (it = orig.mOptions.begin(); it != orig.mOptions.end(); ++it)
74   {
75     mOptions.insert(pair<string, ConversionOption*>
76       ( it->second->getKey(), it->second->clone()));
77   }
78 
79 }
80 
81 ConversionProperties&
operator =(const ConversionProperties & rhs)82 ConversionProperties::operator=(const ConversionProperties& rhs)
83 {
84   if (&rhs == this)
85   {
86     return *this;
87   }
88 
89   // clear
90 
91   if (mTargetNamespaces != NULL)
92   {
93     delete mTargetNamespaces;
94     mTargetNamespaces = NULL;
95   }
96 
97   map<string, ConversionOption*>::iterator it1;
98   for (it1 = mOptions.begin(); it1 != mOptions.end(); ++it1)
99   {
100     if (it1->second != NULL)
101     {
102       delete it1->second;
103       it1->second=NULL;
104     }
105   }
106   mOptions.clear();
107 
108   // assign
109 
110   if (rhs.mTargetNamespaces != NULL)
111     mTargetNamespaces = rhs.mTargetNamespaces->clone();
112   else
113     mTargetNamespaces = NULL;
114 
115   map<string, ConversionOption*>::const_iterator it;
116   for (it = rhs.mOptions.begin(); it != rhs.mOptions.end(); ++it)
117   {
118     mOptions.insert(pair<string, ConversionOption*>
119       ( it->second->getKey(), it->second->clone()));
120   }
121 
122   return *this;
123 }
124 
125 ConversionProperties*
clone() const126 ConversionProperties::clone() const
127 {
128   return new ConversionProperties(*this);
129 }
130 
~ConversionProperties()131 ConversionProperties::~ConversionProperties()
132 {
133   if (mTargetNamespaces != NULL)
134   {
135     delete mTargetNamespaces;
136     mTargetNamespaces = NULL;
137   }
138 
139   map<string, ConversionOption*>::iterator it;
140   for (it = mOptions.begin(); it != mOptions.end(); ++it)
141   {
142     if (it->second != NULL)
143     {
144       delete it->second;
145       it->second=NULL;
146     }
147   }
148 
149 }
150 
151 SBMLNamespaces *
getTargetNamespaces() const152 ConversionProperties::getTargetNamespaces() const
153 {
154   return mTargetNamespaces;
155 }
156 
157 bool
hasTargetNamespaces() const158 ConversionProperties::hasTargetNamespaces() const
159 {
160   return mTargetNamespaces != NULL;
161 }
162 
163 
164 void
setTargetNamespaces(SBMLNamespaces * targetNS)165 ConversionProperties::setTargetNamespaces(SBMLNamespaces *targetNS)
166 {
167   if (mTargetNamespaces != NULL)
168   {
169       delete mTargetNamespaces;
170       mTargetNamespaces = NULL;
171   }
172   if (targetNS == NULL) return;
173 
174   mTargetNamespaces = targetNS->clone();
175 }
176 
177 const std::string&
getDescription(const std::string & key) const178 ConversionProperties::getDescription(const std::string& key) const
179 {
180   ConversionOption *option = getOption(key);
181   if (option != NULL) return option->getDescription();
182 
183 	static std::string empty = "";
184 
185 	return empty;
186 }
187 
188 ConversionOptionType_t
getType(const std::string & key) const189 ConversionProperties::getType(const std::string& key) const
190 {
191   ConversionOption *option = getOption(key);
192   if (option != NULL) return option->getType();
193 
194   return CNV_TYPE_STRING;
195 }
196 
197 
198 ConversionOption*
getOption(const std::string & key) const199 ConversionProperties::getOption(const std::string& key) const
200 {
201 
202   map<string, ConversionOption*>::const_iterator it;
203   for (it = mOptions.begin(); it != mOptions.end(); ++it)
204   {
205     if (it->second != NULL && it->second->getKey() == key)
206       return it->second;
207   }
208   return NULL;
209 }
210 
211 ConversionOption*
getOption(int index) const212 ConversionProperties::getOption(int index) const
213 {
214   map<string, ConversionOption*>::const_iterator it;
215   int count = 0;
216   for (it = mOptions.begin(); it != mOptions.end(); ++it)
217   {
218     if (count == index)
219       return it->second;
220 	count++;
221   }
222   return NULL;
223 }
224 
225 int
getNumOptions() const226 ConversionProperties::getNumOptions() const
227 {
228   return (int)mOptions.size();
229 }
230 
231 void
addOption(const ConversionOption & option)232 ConversionProperties::addOption(const ConversionOption &option)
233 {
234   ConversionOption *old = removeOption(option.getKey());
235   if (old != NULL) delete old;
236 
237   mOptions.insert(pair<string, ConversionOption*>(option.getKey(), option.clone()));
238 }
239 
240 void
addOption(const std::string & key,const std::string value,ConversionOptionType_t type,const std::string description)241 ConversionProperties::addOption(const std::string& key, const std::string value,
242     ConversionOptionType_t type,
243     const std::string description)
244 {
245   ConversionOption *old = removeOption(key);
246   if (old != NULL) delete old;
247 
248   mOptions.insert(pair<string, ConversionOption*>(key, new ConversionOption(key, value, type, description)));
249 }
250 void
addOption(const std::string & key,const char * value,const std::string description)251 ConversionProperties::addOption(const std::string& key, const char* value,
252     const std::string description)
253 {
254   ConversionOption *old = removeOption(key);
255   if (old != NULL) delete old;
256 
257   mOptions.insert(pair<string, ConversionOption*>(key, new ConversionOption(key, value, description)));
258 }
259 void
addOption(const std::string & key,bool value,const std::string description)260 ConversionProperties::addOption(const std::string& key, bool value,
261     const std::string description)
262 {
263   ConversionOption *old = removeOption(key);
264   if (old != NULL) delete old;
265 
266   mOptions.insert(pair<string, ConversionOption*>( key, new ConversionOption(key, value, description) ));
267 }
268 void
addOption(const std::string & key,double value,const std::string description)269 ConversionProperties::addOption(const std::string& key, double value,
270     const std::string description)
271 {
272   ConversionOption *old = removeOption(key);
273   if (old != NULL) delete old;
274 
275   mOptions.insert(pair<string, ConversionOption*>(key, new ConversionOption(key, value, description)));
276 }
277 void
addOption(const std::string & key,float value,const std::string description)278 ConversionProperties::addOption(const std::string& key, float value,
279     const std::string description)
280 {
281   ConversionOption *old = removeOption(key);
282   if (old != NULL) delete old;
283 
284   mOptions.insert(pair<string, ConversionOption*>(key, new ConversionOption(key, value, description)));
285 }
286 void
addOption(const std::string & key,int value,const std::string description)287 ConversionProperties::addOption(const std::string& key, int value,
288     const std::string description)
289 {
290   ConversionOption *old = removeOption(key);
291   if (old != NULL) delete old;
292 
293   mOptions.insert(pair<string, ConversionOption*>(key, new ConversionOption(key, value, description)));
294 }
295 
296 ConversionOption*
removeOption(const std::string & key)297 ConversionProperties::removeOption(const std::string& key)
298 {
299   ConversionOption* result = getOption(key);
300   if (result != NULL)
301     mOptions.erase(key);
302   return result;
303 }
304 
305 bool
hasOption(const std::string & key) const306 ConversionProperties::hasOption(const std::string& key) const
307 {
308   return (getOption(key) != NULL);
309 }
310 
311 const std::string&
getValue(const std::string & key) const312 ConversionProperties::getValue(const std::string& key) const
313 {
314   ConversionOption *option = getOption(key);
315   if (option != NULL) return option->getValue();
316 	static std::string empty = "";
317 
318 	return empty;
319 }
320 
321 void
setValue(const std::string & key,const std::string & value)322 ConversionProperties::setValue(const std::string& key, const std::string& value)
323 {
324   ConversionOption *option = getOption(key);
325   if (option != NULL) option->setValue(value);
326 }
327 
328 
329 bool
getBoolValue(const std::string & key) const330 ConversionProperties::getBoolValue(const std::string& key) const
331 {
332   ConversionOption *option = getOption(key);
333   if (option != NULL) return option->getBoolValue();
334   return false;
335 }
336 
337 void
setBoolValue(const std::string & key,bool value)338 ConversionProperties::setBoolValue(const std::string& key, bool value)
339 {
340   ConversionOption *option = getOption(key);
341   if (option != NULL) option->setBoolValue(value);
342 }
343 
344 double
getDoubleValue(const std::string & key) const345 ConversionProperties::getDoubleValue(const std::string& key) const
346 {
347   ConversionOption *option = getOption(key);
348   if (option != NULL) return option->getDoubleValue();
349   return std::numeric_limits<double>::quiet_NaN();
350 }
351 
352 void
setDoubleValue(const std::string & key,double value)353 ConversionProperties::setDoubleValue(const std::string& key, double value)
354 {
355   ConversionOption *option = getOption(key);
356   if (option != NULL) option->setDoubleValue(value);
357 }
358 
359 float
getFloatValue(const std::string & key) const360 ConversionProperties::getFloatValue(const std::string& key) const
361 {
362   ConversionOption *option = getOption(key);
363   if (option != NULL) return option->getFloatValue();
364   return std::numeric_limits<float>::quiet_NaN();
365 }
366 
367 void
setFloatValue(const std::string & key,float value)368 ConversionProperties::setFloatValue(const std::string& key, float value)
369 {
370   ConversionOption *option = getOption(key);
371   if (option != NULL) option->setFloatValue(value);
372 
373 }
374 
375 int
getIntValue(const std::string & key) const376 ConversionProperties::getIntValue(const std::string& key) const
377 {
378   ConversionOption *option = getOption(key);
379   if (option != NULL) return option->getIntValue();
380   return -1;
381 }
382 
383 void
setIntValue(const std::string & key,int value)384 ConversionProperties::setIntValue(const std::string& key, int value)
385 {
386   ConversionOption *option = getOption(key);
387   if (option != NULL) option->setIntValue(value);
388 
389 }
390 
391 
392 
393 LIBSBML_EXTERN
394 ConversionProperties_t*
ConversionProperties_create()395 ConversionProperties_create()
396 {
397   return new ConversionProperties();
398 }
399 
400 LIBSBML_EXTERN
401 ConversionProperties_t*
ConversionProperties_createWithSBMLNamespace(SBMLNamespaces_t * sbmlns)402 ConversionProperties_createWithSBMLNamespace(SBMLNamespaces_t* sbmlns)
403 {
404   return new ConversionProperties(sbmlns);
405 }
406 
407 LIBSBML_EXTERN
408 ConversionProperties_t*
ConversionProperties_clone(const ConversionProperties_t * cp)409 ConversionProperties_clone(const ConversionProperties_t* cp)
410 {
411   if (cp == NULL) return NULL;
412   return new ConversionProperties(*cp);
413 }
414 
415 LIBSBML_EXTERN
416 void
ConversionProperties_free(ConversionProperties_t * cp)417 ConversionProperties_free(ConversionProperties_t* cp)
418 {
419     delete cp;
420 }
421 
422 LIBSBML_EXTERN
423 int
ConversionProperties_getBoolValue(const ConversionProperties_t * cp,const char * key)424 ConversionProperties_getBoolValue(const ConversionProperties_t* cp, const char* key)
425 {
426   if (cp == NULL) return 0;
427   return cp->getBoolValue(key);
428 }
429 
430 LIBSBML_EXTERN
431 int
ConversionProperties_getIntValue(const ConversionProperties_t * cp,const char * key)432 ConversionProperties_getIntValue(const ConversionProperties_t* cp, const char* key)
433 {
434   if (cp == NULL) return -1;
435   return cp->getIntValue(key);
436 }
437 
438 LIBSBML_EXTERN
439 char*
ConversionProperties_getDescription(const ConversionProperties_t * cp,const char * key)440 ConversionProperties_getDescription(const ConversionProperties_t* cp, const char* key)
441 {
442   if (cp == NULL) return NULL;
443   return strdup(cp->getDescription(key).c_str());
444 }
445 
446 LIBSBML_EXTERN
447 double
ConversionProperties_getDoubleValue(const ConversionProperties_t * cp,const char * key)448 ConversionProperties_getDoubleValue(const ConversionProperties_t* cp, const char* key)
449 {
450   if (cp == NULL) return std::numeric_limits<double>::quiet_NaN();
451   return cp->getDoubleValue(key);
452 }
453 
454 LIBSBML_EXTERN
455 float
ConversionProperties_getFloatValue(const ConversionProperties_t * cp,const char * key)456 ConversionProperties_getFloatValue(const ConversionProperties_t* cp, const char* key)
457 {
458   if (cp == NULL) return std::numeric_limits<float>::quiet_NaN();
459   return cp->getFloatValue(key);
460 }
461 
462 LIBSBML_EXTERN
463 char*
ConversionProperties_getValue(const ConversionProperties_t * cp,const char * key)464 ConversionProperties_getValue(const ConversionProperties_t* cp, const char* key)
465 {
466   if (cp == NULL) return NULL;
467   return strdup(cp->getValue(key).c_str());
468 }
469 
470 LIBSBML_EXTERN
471 const ConversionOption_t*
ConversionProperties_getOption(const ConversionProperties_t * cp,const char * key)472 ConversionProperties_getOption(const ConversionProperties_t* cp, const char* key)
473 {
474   if (cp == NULL) return NULL;
475   return cp->getOption(key);
476 }
477 
478 LIBSBML_EXTERN
479 ConversionOptionType_t
ConversionProperties_getType(const ConversionProperties_t * cp,const char * key)480 ConversionProperties_getType(const ConversionProperties_t* cp, const char* key)
481 {
482   if (cp == NULL) return CNV_TYPE_STRING;
483   return cp->getType(key);
484 }
485 
486 LIBSBML_EXTERN
487 const SBMLNamespaces_t*
ConversionProperties_getTargetNamespaces(const ConversionProperties_t * cp)488 ConversionProperties_getTargetNamespaces(const ConversionProperties_t* cp)
489 {
490   if (cp == NULL) return NULL;
491   return cp->getTargetNamespaces();
492 }
493 
494 LIBSBML_EXTERN
495 int
ConversionProperties_hasOption(const ConversionProperties_t * cp,const char * key)496 ConversionProperties_hasOption(const ConversionProperties_t* cp, const char* key)
497 {
498   if (cp == NULL) return 0;
499   return (int)cp->hasOption(key);
500 }
501 
502 LIBSBML_EXTERN
503 int
ConversionProperties_hasTargetNamespaces(const ConversionProperties_t * cp)504 ConversionProperties_hasTargetNamespaces(const ConversionProperties_t* cp)
505 {
506   if (cp == NULL) return 0;
507   return (int)cp->hasTargetNamespaces();
508 }
509 
510 LIBSBML_EXTERN
511 void
ConversionProperties_setTargetNamespaces(ConversionProperties_t * cp,SBMLNamespaces_t * sbmlns)512 ConversionProperties_setTargetNamespaces(ConversionProperties_t* cp, SBMLNamespaces_t* sbmlns)
513 {
514   if (cp == NULL) return;
515   cp->setTargetNamespaces(sbmlns);
516 }
517 
518 LIBSBML_EXTERN
519 void
ConversionProperties_setBoolValue(ConversionProperties_t * cp,const char * key,int value)520 ConversionProperties_setBoolValue(ConversionProperties_t* cp, const char* key, int value)
521 {
522   if (cp == NULL) return;
523   cp->setBoolValue(key, (bool)value);
524 }
525 
526 LIBSBML_EXTERN
527 void
ConversionProperties_setIntValue(ConversionProperties_t * cp,const char * key,int value)528 ConversionProperties_setIntValue(ConversionProperties_t* cp, const char* key, int value)
529 {
530   if (cp == NULL) return;
531   cp->setIntValue(key, value);
532 }
533 
534 LIBSBML_EXTERN
535 void
ConversionProperties_setDoubleValue(ConversionProperties_t * cp,const char * key,double value)536 ConversionProperties_setDoubleValue(ConversionProperties_t* cp, const char* key, double value)
537 {
538   if (cp == NULL) return;
539   cp->setDoubleValue(key, value);
540 }
541 
542 LIBSBML_EXTERN
543 void
ConversionProperties_setFloatValue(ConversionProperties_t * cp,const char * key,float value)544 ConversionProperties_setFloatValue(ConversionProperties_t* cp, const char* key, float value)
545 {
546   if (cp == NULL) return;
547   cp->setFloatValue(key, value);
548 }
549 
550 LIBSBML_EXTERN
551 void
ConversionProperties_setValue(ConversionProperties_t * cp,const char * key,const char * value)552 ConversionProperties_setValue(ConversionProperties_t* cp, const char* key, const char* value)
553 {
554   if (cp == NULL) return;
555   cp->setValue(key, value);
556 }
557 
558 LIBSBML_EXTERN
559 void
ConversionProperties_addOption(ConversionProperties_t * cp,const ConversionOption_t * option)560 ConversionProperties_addOption(ConversionProperties_t* cp, const ConversionOption_t* option)
561 {
562   if (cp == NULL || option == NULL) return;
563   cp->addOption(*option);
564 }
565 
566 LIBSBML_EXTERN
567 void
ConversionProperties_addOptionWithKey(ConversionProperties_t * cp,const char * key)568 ConversionProperties_addOptionWithKey(ConversionProperties_t* cp, const char* key)
569 {
570   if (cp == NULL || key == NULL) return;
571   cp->addOption(key);
572 }
573 
574 LIBSBML_EXTERN
575 ConversionOption_t*
ConversionProperties_removeOption(ConversionProperties_t * cp,const char * key)576 ConversionProperties_removeOption(ConversionProperties_t* cp, const char* key)
577 {
578   if (cp == NULL || key == NULL) return NULL;
579   return cp->removeOption(key);
580 }
581 
582 LIBSBML_CPP_NAMESPACE_END
583 
584 #endif  /* __cplusplus */
585