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(char sep1, char sep2)
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 << sep1;
00130 Ref<DictionaryElement> el = elements->get(i);
00131 *buf << url_escape(el->getKey()) << sep2
00132 << url_escape(el->getValue());
00133 }
00134 return buf->toString();
00135 }
00136
00137 String Dictionary::encode()
00138 {
00139 return _encode('&', '=');
00140 }
00141
00142 String Dictionary::encodeSimple()
00143 {
00144 return _encode('/', '/');
00145 }
00146
00147 void Dictionary::decode(String url)
00148 {
00149 char *data = url.c_str();
00150 char *dataEnd = data + url.length();
00151 while (data < dataEnd)
00152 {
00153 char *ampPos = strchr(data, '&');
00154 if (!ampPos)
00155 {
00156 ampPos = dataEnd;
00157 }
00158 char *eqPos = strchr(data, '=');
00159 if(eqPos && eqPos < ampPos)
00160 {
00161 String key(data, eqPos - data);
00162 String value(eqPos + 1, ampPos - eqPos - 1);
00163 key = url_unescape(key);
00164 value = url_unescape(value);
00165
00166 put(key, value);
00167 }
00168 data = ampPos + 1;
00169 }
00170 }
00171
00172
00173
00174 void Dictionary::decodeSimple(String url)
00175 {
00176 String encoded;
00177 int pos;
00178 int last_pos = 0;
00179 do
00180 {
00181 pos = url.index(last_pos, '/');
00182 if (pos < last_pos + 1)
00183 break;
00184
00185 String key = url_unescape(url.substring(last_pos, pos - last_pos));
00186 last_pos = pos + 1;
00187 pos = url.index(last_pos, '/');
00188 if (pos == -1)
00189 pos = url.length();
00190 if (pos < last_pos + 1)
00191 break;
00192
00193 String value = url_unescape(url.substring(last_pos, pos - last_pos));
00194 last_pos = pos + 1;
00195 put(key, value);
00196 }
00197 while (last_pos < url.length());
00198 }
00199
00200 void Dictionary::clear()
00201 {
00202 elements->remove(0, elements->size());
00203 }
00204
00205 Ref<Dictionary> Dictionary::clone()
00206 {
00207 Ref<Dictionary> ret(new Dictionary());
00208 int len = elements->size();
00209 for (int i = 0; i < len; i++)
00210 {
00211 Ref<DictionaryElement> el = elements->get(i);
00212 ret->put(el->getKey(), el->getValue());
00213 }
00214 return ret;
00215 }
00216
00217 void Dictionary::merge(Ref<Dictionary> other)
00218 {
00219 if (other == nil)
00220 return;
00221
00222 Ref<Array<DictionaryElement> > other_el = other->getElements();
00223 int len = other_el->size();
00224 for (int i = 0; i < len; i++)
00225 {
00226 Ref<DictionaryElement> el = other_el->get(i);
00227 this->put(el->getKey(), el->getValue());
00228 }
00229 }
00230
00231 bool Dictionary::isSubsetOf(Ref<Dictionary> other)
00232 {
00233 int len = elements->size();
00234 for (int i = 0; i < len; i++)
00235 {
00236 Ref<DictionaryElement> el = elements->get(i);
00237 if (el->getValue() != other->get(el->getKey()))
00238 return 0;
00239 }
00240 return 1;
00241 }
00242 bool Dictionary::equals(Ref<Dictionary> other)
00243 {
00244 return (isSubsetOf(other) && other->isSubsetOf(Ref<Dictionary>(this)));
00245 }
00246
00247 Ref<Array<DictionaryElement> > Dictionary::getElements()
00248 {
00249 return elements;
00250 }