MeVisLabToolboxReference
MeVis/Foundation/Sources/MLImageFormatBase/mlImageFormatTag.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //----------------------------------------------------------------------------------
00005 
00010 //----------------------------------------------------------------------------------
00011 
00012 #ifndef __mlImageFormatTag_H
00013 #define __mlImageFormatTag_H
00014 
00015 // Resolve platform independencies.
00016 #ifndef __MLImageFormatBaseSystem_H
00017 #include "MLImageFormatBaseSystem.h"
00018 #endif
00019 
00020 #ifndef __mlModuleIncludes_H
00021 #include "mlModuleIncludes.h"
00022 #endif
00023 
00024 // Implement everything in ML namespace.
00025 ML_START_NAMESPACE
00026 
00027   //----------------------------------------------------------------------
00029   //----------------------------------------------------------------------
00030   class MLIMAGEFORMATBASE_EXPORT MLImageFormatTag {
00031     public:
00032 
00033       //------------------------------------------------------------------
00035 
00036       //------------------------------------------------------------------
00038       inline MLImageFormatTag() :
00039         _name (""),
00040         _value("")
00041       {}
00042 
00044       inline MLImageFormatTag(const MLImageFormatTag &tag) :
00045         _name (tag._name),
00046         _value(tag._value)
00047       {}
00048 
00050       inline MLImageFormatTag(const std::string &name,
00051                               const std::string &value) :
00052         _name (name),
00053         _value(value)
00054       {}
00055 
00057       inline MLImageFormatTag(const std::string &name, MLint value) :
00058         _name (name)
00059       {
00060         setValue(value);
00061       }
00062 
00064       inline MLImageFormatTag(const std::string &name, MLdouble value) :
00065         _name (name)
00066       {
00067         setValue(value);
00068       }
00069 
00071       inline MLImageFormatTag(const std::string &name, MLldouble value) :
00072         _name (name)
00073       {
00074         setValue(value);
00075       }
00076 
00078       inline MLImageFormatTag& operator=(const MLImageFormatTag &tag)
00079       {
00080         // Assign only if different.
00081         if (&tag != this){
00082           _name  = tag._name ;
00083           _value = tag._value;
00084         }
00085         return *this;
00086       }
00088 
00089 
00090       //------------------------------------------------------------------
00092 
00093       //------------------------------------------------------------------
00095       inline const std::string &getName()  const     { return _name;  }
00096 
00098       inline const std::string &getValue() const     { return _value; }
00099 
00101       inline void setName(const std::string &newName){ _name = newName; }
00103 
00104 
00105       //------------------------------------------------------------------
00107 
00108       //------------------------------------------------------------------
00110       inline void setValue(const std::string &newVal)
00111       {
00112         _value = newVal;
00113       }
00114 
00116       inline void setValue(MLint newVal)
00117       {
00118         char strBuf[128]="";
00119         MLsnprintf(strBuf, 127, "%I64Ld", newVal);
00120         _value = strBuf;
00121       }
00122 
00124       inline void setValue(MLdouble newVal)
00125       {
00126         char frmt[256]="";
00127         char bufferStr[512]="";
00128 
00129         // Create the format string with full number of digits.
00130         MLsnprintf(frmt, 255,  "%%.%dg", DBL_DIG);
00131 
00132         // Print the long double value with full number of digits.
00133         MLsnprintf(bufferStr, 511, frmt, newVal);
00134         _value = bufferStr;
00135       }
00136 
00138       inline void setValue(MLldouble newVal)
00139       {
00140         char frmt[256]="";
00141         char bufferStr[512]="";
00142 
00143         // Create the format string with full number of digits.
00144         MLsnprintf(frmt, 255,  "%%.%dLg", LDBL_DIG);
00145 
00146         // Print the long double value with full number of digits.
00147         MLsnprintf(bufferStr, 511, frmt, newVal);
00148         _value = bufferStr;
00149       }
00150 
00152       inline size_t getTagSize() const
00153       {
00154         return _name.size() + _value.size() + 2;
00155       }
00156 
00160       inline void writeNameAndValueIntoMemory(char *memPos) const
00161       {
00162         const size_t nameSize  = _name .size();
00163         const size_t valueSize = _value.size();
00164         memcpy(memPos           , _name .c_str(), nameSize ); memPos[nameSize            ] = '\0';
00165         memcpy(memPos+nameSize+1, _value.c_str(), valueSize); memPos[nameSize+1+valueSize] = '\0';
00166       }
00167 
00186       inline const char * setNameAndValueFromMemory(const char* mem, const char* lastValidMemByte)
00187       {
00188         // Get start and end of buffers to parse content safely.
00189         const char * bufSeek   = mem;
00190 
00191         // Search first terminating character.
00192         int         numTerms   = 0;
00193         const char *firstTerm  = NULL;
00194         const char *secondTerm = NULL;
00195         for (; (bufSeek <= lastValidMemByte) && (numTerms < 2); ++bufSeek){
00196           if (*bufSeek == 0){
00197             // Found character is a null-terminating character. Save it
00198             // as firstTerm if firstTerm is still NULL, otherwise save
00199             // it pointer to the second terminating character. Also
00200             // increase the number of found terminating characters.
00201             // We need 2.
00202             numTerms++;
00203             if (firstTerm && !secondTerm){ secondTerm = bufSeek; }
00204             if (!firstTerm              ){ firstTerm  = bufSeek; }
00205           }
00206         } // for
00207 
00208         // Do we have two terminators found? If yes, then assign both values
00209         // which is safe then.
00210         if ((2 == numTerms) && firstTerm && secondTerm){
00211           _name  = mem;
00212           _value = firstTerm+1;
00213         }
00214 
00215         // Return pointer to second termination null or NULL on failure.
00216         return secondTerm;
00217       }
00219 
00220 
00221 
00222       //------------------------------------------------------------------
00224 
00225       //------------------------------------------------------------------
00227       inline const std::string &getStringValue()     const
00228       {
00229         return _value;
00230       }
00231 
00233       inline MLint              getMLintValue()      const
00234       {
00235         // Print the long double value with full number of digits.
00236         MLint intVal=0;
00237         int numRead = MLsscanf(_value.c_str(), "%I64Ld", &intVal);
00238         return (numRead == 1) ? intVal : 0;
00239       }
00240 
00242       inline MLdouble           getDoubleValue()     const
00243       {
00244         // Scan for a double value.
00245         MLdouble dblVal=0;
00246         int numRead = sscanf(_value.c_str(), "%lf", &dblVal);
00247         return (numRead == 1) ? dblVal : 0;
00248       }
00249 
00251       inline MLldouble          getLongDoubleValue() const
00252       {
00253         // Scan for a long double value.
00254         MLldouble ldblVal=0;
00255         int numRead = sscanf(_value.c_str(), "%Lf", &ldblVal);
00256         return (numRead == 1) ? ldblVal : 0;
00257       }
00259 
00260     protected:
00262       std::string _name;
00263 
00265       std::string _value;
00266   };
00267 
00268 
00269 
00270   //----------------------------------------------------------------------
00272   //----------------------------------------------------------------------
00273   inline bool operator==(const MLImageFormatTag &tag1, const MLImageFormatTag &tag2)
00274   {
00275     // Assign only if different.
00276     return (tag1.getName()  == tag2.getName() ) &&
00277            (tag1.getValue() == tag2.getValue());
00278   }
00279 
00280   //----------------------------------------------------------------------
00282   //----------------------------------------------------------------------
00283   inline bool operator!=(const MLImageFormatTag &tag1, const MLImageFormatTag &tag2)
00284   {
00285     // Assign only if different.
00286     return (tag1.getName()  != tag2.getName() ) ||
00287            (tag1.getValue() != tag2.getValue());
00288   }
00289 
00290 
00291 ML_END_NAMESPACE
00292 
00293 #endif // of __mlImageFormat_H
00294