dictionary.cc

Go to the documentation of this file.
00001 /*MT*
00002     
00003     MediaTomb - http://www.mediatomb.cc/
00004     
00005     dictionary.cc - this file is part of MediaTomb.
00006     
00007     Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>,
00008                        Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>
00009     
00010     Copyright (C) 2006-2007 Gena Batyan <bgeradz@mediatomb.cc>,
00011                             Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
00012                             Leonhard Wimmer <leo@mediatomb.cc>
00013     
00014     MediaTomb is free software; you can redistribute it and/or modify
00015     it under the terms of the GNU General Public License version 2
00016     as published by the Free Software Foundation.
00017     
00018     MediaTomb is distributed in the hope that it will be useful,
00019     but WITHOUT ANY WARRANTY; without even the implied warranty of
00020     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021     GNU General Public License for more details.
00022     
00023     You should have received a copy of the GNU General Public License
00024     version 2 along with MediaTomb; if not, write to the Free Software
00025     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
00026     
00027     $Id: dictionary.cc 1294 2007-05-13 16:28:24Z lww $
00028 */
00029 
00031 
00032 #ifdef HAVE_CONFIG_H
00033     #include "autoconfig.h"
00034 #endif
00035 
00036 #include "dictionary.h"
00037 
00038 #include <string.h>
00039 #include "tools.h"
00040 
00041 using namespace zmm;
00042 
00043 DictionaryElement::DictionaryElement(String key, String value) : Object()
00044 {
00045     this->key = key;
00046     this->value = value;
00047 }
00048 
00049 void DictionaryElement::setKey(String key)
00050 {
00051     this->key = key;
00052 }
00053 
00054 void DictionaryElement::setValue(String value)
00055 {
00056     this->value = value;
00057 }
00058 
00059 String DictionaryElement::getKey()
00060 {
00061     return key;
00062 }
00063 
00064 String DictionaryElement::getValue()
00065 {
00066     return value;
00067 }
00068 
00069 
00070 
00071 Dictionary::Dictionary() : Object()
00072 {
00073     elements = Ref<Array<DictionaryElement> >(new Array<DictionaryElement>());
00074 }
00075 
00076 void Dictionary::put(String key, String value)
00077 {
00078     for (int i = 0; i < elements->size(); i++)
00079     {
00080         Ref<DictionaryElement> el = elements->get(i);
00081         if(el->getKey() == key)
00082         {
00083             el->setValue(value);
00084             return;
00085         }
00086     }
00087     Ref<DictionaryElement> newEl(new DictionaryElement(key, value));
00088     elements->append(newEl);
00089 }
00090 
00091 String Dictionary::get(String key)
00092 {
00093     for (int i = 0; i < elements->size(); i++)
00094     {
00095         Ref<DictionaryElement> el = elements->get(i);
00096         if (el->getKey() == key)
00097         {
00098             return el->getValue();
00099         }
00100     }
00101     return nil;
00102 }
00103 
00104 int Dictionary::size()
00105 {
00106     return elements->size();
00107 }
00108 
00109 void Dictionary::remove(String key)
00110 {
00111     for (int i = 0; i < elements->size(); i++)
00112     {
00113         Ref<DictionaryElement> el = elements->get(i);
00114         if (el->getKey() == key)
00115         {
00116             elements->remove(i, 1);
00117             return;
00118         }
00119     }
00120 }
00121 
00122 String Dictionary::encode()
00123 {
00124     Ref<StringBuffer> buf(new StringBuffer());
00125     int len = elements->size();
00126     for (int i = 0; i < len; i++)
00127     {
00128         if(i > 0)
00129             *buf << '&';
00130         Ref<DictionaryElement> el = elements->get(i);
00131         *buf << url_escape(el->getKey()) << '='
00132              << url_escape(el->getValue());
00133     }
00134     return buf->toString();
00135 }
00136 
00137 void Dictionary::decode(String url)
00138 {
00139     char *data = url.c_str();
00140     char *dataEnd = data + url.length();
00141     while (data < dataEnd)
00142     {
00143         char *ampPos = strchr(data, '&');
00144         if (!ampPos)
00145         {
00146             ampPos = dataEnd;
00147         }
00148         char *eqPos = strchr(data, '=');
00149         if(eqPos && eqPos < ampPos)
00150         {
00151             String key(data, eqPos - data);
00152             String value(eqPos + 1, ampPos - eqPos - 1);
00153             key = url_unescape(key);
00154             value = url_unescape(value);
00155 
00156             put(key, value);
00157         }
00158         data = ampPos + 1;
00159     }
00160 }
00161 
00162 void Dictionary::clear()
00163 {
00164     elements->remove(0, elements->size());
00165 }
00166 
00167 Ref<Dictionary> Dictionary::clone()
00168 {
00169     Ref<Dictionary> ret(new Dictionary());
00170     int len = elements->size();
00171     for (int i = 0; i < len; i++)
00172     {
00173         Ref<DictionaryElement> el = elements->get(i);
00174         ret->put(el->getKey(), el->getValue());
00175     }
00176     return ret;
00177 }
00178 
00179 bool Dictionary::isSubsetOf(Ref<Dictionary> other)
00180 {
00181     int len = elements->size();
00182     for (int i = 0; i < len; i++)
00183     {
00184         Ref<DictionaryElement> el = elements->get(i);
00185         if (el->getValue() != other->get(el->getKey()))
00186             return 0;
00187     }
00188     return 1;
00189 }
00190 bool Dictionary::equals(Ref<Dictionary> other)
00191 {
00192     return (isSubsetOf(other) && other->isSubsetOf(Ref<Dictionary>(this)));
00193 }
00194 
00195 Ref<Array<DictionaryElement> > Dictionary::getElements()
00196 {
00197     return elements;
00198 }

Generated on Sun Jul 8 22:29:43 2007 for MediaTomb by  doxygen 1.5.2