00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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 }