OpenTracker

An Open Architecture for Reconfigurable Tracking based on XML | Contact

StringTable.cxx

Go to the documentation of this file.
00001 /* ========================================================================
00002  * Copyright (c) 2006,
00003  * Institute for Computer Graphics and Vision
00004  * Graz University of Technology
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are
00009  * met:
00010  *
00011  * Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the following disclaimer.
00013  *
00014  * Redistributions in binary form must reproduce the above copyright
00015  * notice, this list of conditions and the following disclaimer in the
00016  * documentation and/or other materials provided with the distribution.
00017  *
00018  * Neither the name of the Graz University of Technology nor the names of
00019  * its contributors may be used to endorse or promote products derived from
00020  * this software without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00023  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00024  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00025  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00026  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00027  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00028  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00029  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00030  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  * ========================================================================
00034  * PROJECT: OpenTracker
00035  * ======================================================================== */
00042 /* ======================================================================= */
00043 
00044 #include "../tool/disable4786.h"
00045 #include "StringTable.h"
00046 #include <stdlib.h>
00047 #include <stdio.h>
00048 
00049 //using namespace std;
00050 
00051 // emtpy string to be returned, if key is not in the map
00052 
00053 const std::string empty("");
00054 
00055 // initializes the map
00056 
00057 namespace ot {
00058 
00059     StringTable::StringTable() : map()
00060     {}
00061 
00062     // copy constructor
00063     StringTable::StringTable(const StringTable& table ) : map(table.map)
00064     {}
00065 
00066     // clears the map
00067 
00068     StringTable::~StringTable()
00069     {
00070         map.clear();
00071     }
00072 
00073     // returns a value to a given key
00074 
00075     const std::string & StringTable::get( const std::string & key )
00076     {
00077         StringMap::iterator it = map.find( key );
00078         if( it == map.end())
00079             return empty;
00080         return (*it).second;
00081     }
00082 
00083     // stores a key, value pair
00084 
00085     void StringTable::put( const std::string & key, const std::string & value )
00086     {
00087         map[key] = value;
00088     }
00089 
00090     // removes a key, value pair
00091 
00092     void StringTable::remove( const std::string & key )
00093     {
00094         StringMap::iterator it = map.find( key );
00095         if( it == map.end())
00096             return;
00097         map.erase( it );
00098     }
00099 
00100     // tests for a given key
00101 
00102     int StringTable::containsKey( const std::string & key )
00103     {
00104         StringMap::iterator it = map.find( key );
00105         if( it == map.end())
00106             return 0;
00107         return 1;
00108     }
00109 
00110     // returns the size of the table
00111 
00112     unsigned StringTable::size()
00113     {
00114         return map.size();
00115     }
00116 
00117     // some put and get methods
00118 
00119     void StringTable::put(const std::string & key, const int value)
00120     {
00121         char buffer[20];
00122 
00123         sprintf( buffer, "%i", value );
00124         map[key] = buffer;
00125     }
00126 
00127     void StringTable::put(const std::string & key, const float value)
00128     {
00129         char buffer[20];
00130 
00131         sprintf( buffer, "%f", value );
00132         map[key] = buffer;
00133     }
00134 
00135     void StringTable::put(const std::string & key, const double value)
00136     {
00137         char buffer[30];
00138 
00139         sprintf( buffer, "%lf", value );
00140         map[key] = buffer;
00141     }
00142 
00143     void StringTable::put(const std::string & key, const int * value, int len)
00144     {
00145         char buffer[20];
00146         std::string strvalue;
00147 
00148         sprintf(buffer, "%i", value[0] );
00149         strvalue.append(buffer);
00150         for( int i = 1; i < len; i++ )
00151         {
00152             sprintf(buffer, " %i", value[i] );
00153             strvalue.append(buffer);
00154         }
00155         map[key] = strvalue;
00156     }
00157 
00158     void StringTable::put(const std::string & key, const float * value, int len)
00159     {
00160         char buffer[20];
00161         std::string strvalue;
00162 
00163         sprintf(buffer, "%f", value[0] );
00164         strvalue.append(buffer);
00165         for( int i = 1; i < len; i++ )
00166         {
00167             sprintf(buffer, " %f", value[i] );
00168             strvalue.append(buffer);
00169         }
00170         map[key] = strvalue;
00171     }
00172 
00173     void StringTable::put(const std::string & key, const double * value, int len)
00174     {
00175         char buffer[20];
00176         std::string strvalue;
00177 
00178         sprintf(buffer, "%lf", value[0] );
00179         strvalue.append(buffer);
00180         for( int i = 1; i < len; i++ )
00181         {
00182             sprintf(buffer, " %lf", value[i] );
00183             strvalue.append(buffer);
00184         }
00185         map[key] = strvalue;
00186     }
00187 
00188     int StringTable::get(const std::string & key, int * value, int len )
00189     {
00190         StringMap::iterator it = map.find( key );
00191         if( it == map.end())
00192             return 0;
00193 
00194         char * data = (char *)(*it).second.c_str();
00195         char * end = data;
00196         int count = 0;
00197         value[count++] = strtol( data, &end, 0 );
00198         while( end != data && count < len){
00199             data = end;
00200             value[count++] = strtol( data, &end, 0 );
00201         }
00202         return count;
00203     }
00204 
00205     int StringTable::get(const std::string & key, float * value, int len )
00206     {
00207         StringMap::iterator it = map.find( key );
00208         if( it == map.end())
00209             return 0;
00210 
00211         char * data = (char *)(*it).second.c_str();
00212         char * end = data;
00213         int count = 0;
00214         value[count++] = (float)strtod( data, &end );
00215         while( end != data && count < len){
00216             data = end;
00217             value[count++] = (float)strtod( data, &end );
00218         }
00219         return count;
00220     }
00221 
00222     int StringTable::get(const std::string & key, std::vector<float> & vector, int len )
00223     {
00224         StringMap::iterator it = map.find( key );
00225         if( it == map.end())
00226             return 0;
00227 
00228         float *array = (float*)malloc(len * sizeof(float));
00229         int count = get(key, array, len);
00230         copyA2V(array, len, vector);
00231         free(array);
00232         return count;
00233     }
00234 
00235     int StringTable::get(const std::string & key, double * value, int len )
00236     {
00237         StringMap::iterator it = map.find( key );
00238         if( it == map.end())
00239             return 0;
00240 
00241         char * data = (char *)(*it).second.c_str();
00242         char * end = data;
00243         int count = 0;
00244         value[count++] = strtod( data, &end );
00245         while( end != data && count < len){
00246             data = end;
00247             value[count++] = strtod( data, &end );
00248         }
00249         return count;
00250     }
00251 
00252     int KeyIterator::hasMoreKeys() const
00253     {
00254         return((int)(it != map.end()));
00255     }
00256 
00257     const std::string & KeyIterator::nextElement()
00258     {
00259         if( hasMoreKeys()){
00260             const std::string & res = (*it).first;
00261             it++;
00262             return res;
00263         }
00264         return empty;
00265     }
00266 
00267 } // namespace ot
00268 
00269 /* 
00270  * ------------------------------------------------------------
00271  *   End of StringTable.cxx
00272  * ------------------------------------------------------------
00273  *   Automatic Emacs configuration follows.
00274  *   Local Variables:
00275  *   mode:c++
00276  *   c-basic-offset: 4
00277  *   eval: (c-set-offset 'substatement-open 0)
00278  *   eval: (c-set-offset 'case-label '+)
00279  *   eval: (c-set-offset 'statement 'c-lineup-runin-statements)
00280  *   eval: (setq indent-tabs-mode nil)
00281  *   End:
00282  * ------------------------------------------------------------ 
00283  */

copyright (c) 2006 Graz University of Technology