datahash_util.h

Go to the documentation of this file.
00001 /*
00002  * datahash_util.h
00003  *
00004  * Copyright (c) 2003,2008 Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  * Utilities for dealing with datahashes.
00032  */
00033 
00034 #ifndef WAVEPACKET_DATAHASH_UTIL_H__
00035 #define WAVEPACKET_DATAHASH_UTIL_H__
00036 
00037 // includes --------------------------------------------------------------------
00038 #include "datahash.h"
00039 
00040 #include <sstream>
00041 
00042 
00043 /// \ingroup datahash
00044 /*@{*/
00045 
00046 
00047 ////////////////////////////////////////////////////////////////////////////////
00048 //
00049 //      Convenient typed accessors.
00050 //
00051 //      NOTE: these *all* assume there is a single value for the given key!
00052 //      If you need support for typed access to multi-valued keys you'll need
00053 //      to write that yourself.
00054 //
00055 ////////////////////////////////////////////////////////////////////////////////
00056 
00057 enum eDatahash_Flag {
00058         eDatahash_Required      = 1,            ///< field is required
00059         eDatahash_Optional      = 2,            ///< optional field
00060 
00061         // must be last!
00062         eDatahash_Invalid       = 0
00063 };
00064 
00065 
00066 const char * getString(IN const Datahash * hash, IN const char * key,
00067                                 IN eDatahash_Flag flag = eDatahash_Required);
00068 
00069 const char * getOptionalString(IN const Datahash * hash, IN const char * key,
00070                                 IN const char * default_value);
00071 
00072 long getLong(IN const Datahash * hash, IN const char * key);
00073 
00074 long getOptionalLong(IN const Datahash * hash, IN const char * key,
00075                                 IN long default_value);
00076 
00077 double getDouble(IN const Datahash * hash, IN const char * key);
00078 
00079 bool getBooleanValueFromString(IN const char * value);
00080 
00081 bool getBoolean(IN const Datahash * hash, IN const char * key);
00082 
00083 smart_ptr<Datahash> getSubhash(IN const Datahash * hash,
00084                                 IN const char * key_name,
00085                                 IN eDatahash_Flag flag = eDatahash_Required);
00086 
00087 
00088 // inline accessors (using the above)
00089 static inline short getShort(IN const Datahash * hash, IN const char * key)
00090 { return (short) getLong(hash, key); }
00091 
00092 static inline int getInt(IN const Datahash * hash, IN const char * key)
00093 { return (int) getLong(hash, key); }
00094 
00095 static inline byte_t getByte(IN const Datahash * hash, IN const char * key)
00096 { return (byte_t) getLong(hash, key); }
00097 
00098 static inline float getFloat(IN const Datahash * hash, IN const char * key)
00099 { return (float) getDouble(hash, key); }
00100 
00101 
00102 // generic setter (NOTE: clears any old values, and then sets new value)
00103 template <class T>
00104 void
00105 setValue
00106 (
00107 IN Datahash * hash,
00108 IN const char * key_name,
00109 IN const T& t
00110 )
00111 {
00112         std::ostringstream out;
00113         out << t;
00114 
00115         hash->remove(key_name);
00116         hash->insert(key_name, out.str().c_str());
00117 }
00118 
00119 
00120 void setTimestampAsDateString(IN Datahash * hash,
00121                         IN const char * key, long timestamp);
00122 
00123 long getTimestampFromDateString(IN const Datahash * hash,
00124                         IN const char * key);
00125 
00126 
00127 void getStringValues(IN const Datahash * hash,
00128                         IN const char * key,
00129                         OUT VecString& vec);
00130 
00131 
00132 // helper constructors
00133 smart_ptr<Datahash> getHashFromColonString(IN const char * s);
00134 
00135 
00136 #endif  // WAVEPACKET_DATAHASH_UTIL_H__
00137