00001
00002
00003
00004
00005
00006
00007
00008 #include "ibrdtn/data/Dictionary.h"
00009 #include "ibrdtn/data/SDNV.h"
00010 #include <map>
00011 #include <stdexcept>
00012 #include <string.h>
00013 #include <iostream>
00014
00015 using namespace std;
00016
00017 namespace dtn
00018 {
00019 namespace data
00020 {
00021 Dictionary::Dictionary()
00022 {
00023 }
00024
00025 Dictionary::Dictionary(const Dictionary &d)
00026 {
00027 _bytestream << d._bytestream.rdbuf();
00028 }
00029
00033 Dictionary& Dictionary::operator=(const Dictionary &d)
00034 {
00035 _bytestream.str("");
00036 _bytestream << d._bytestream.rdbuf();
00037 return (*this);
00038 }
00039
00040 Dictionary::~Dictionary()
00041 {
00042 }
00043
00044 size_t Dictionary::get(const std::string value) const
00045 {
00046 std::string bytes = _bytestream.str();
00047 const char *bytebegin = bytes.c_str();
00048 const char *bytepos = bytebegin;
00049 const char *byteend = bytebegin + bytes.length() + 1;
00050
00051 if (bytes.length() <= 0) return std::string::npos;
00052
00053 while (bytepos < byteend)
00054 {
00055 std::string dictstr(bytepos);
00056
00057 if (dictstr == value)
00058 {
00059 return bytepos - bytebegin;
00060 }
00061
00062 bytepos += dictstr.length() + 1;
00063 }
00064
00065 return std::string::npos;
00066 }
00067
00068 bool Dictionary::exists(const std::string value) const
00069 {
00070 std::string bytes = _bytestream.str();
00071 const char *bytepos = bytes.c_str();
00072 const char *byteend = bytepos + bytes.length();
00073
00074 if (bytes.length() <= 0) return false;
00075
00076 while (bytepos < byteend)
00077 {
00078 std::string dictstr(bytepos);
00079
00080 if (dictstr == value)
00081 {
00082 return true;
00083 }
00084
00085 bytepos += dictstr.length() + 1;
00086 }
00087
00088 return false;
00089 }
00090
00091 void Dictionary::add(const std::string value)
00092 {
00093 if (!exists(value))
00094 {
00095 _bytestream << value << '\0';
00096 }
00097 }
00098
00099 void Dictionary::add(const EID &eid)
00100 {
00101 add(eid.getScheme());
00102 add(eid.getNode() + eid.getApplication());
00103 }
00104
00105 void Dictionary::add(const list<EID> &eids)
00106 {
00107 list<EID>::const_iterator iter = eids.begin();
00108
00109 while (iter != eids.end())
00110 {
00111 add(*iter);
00112 iter++;
00113 }
00114 }
00115
00116 EID Dictionary::get(size_t scheme, size_t ssp)
00117 {
00118 char buffer[1024];
00119
00120 _bytestream.seekg(scheme);
00121 _bytestream.get(buffer, 1024, '\0');
00122 string scheme_str(buffer);
00123
00124 _bytestream.seekg(ssp);
00125 _bytestream.get(buffer, 1024, '\0');
00126 string ssp_str(buffer);
00127
00128 return EID(scheme_str, ssp_str);
00129 }
00130
00131 void Dictionary::clear()
00132 {
00133 _bytestream.str("");
00134 }
00135
00136 size_t Dictionary::getSize() const
00137 {
00138 return _bytestream.str().length();
00139 }
00140
00141 pair<size_t, size_t> Dictionary::getRef(const EID &eid) const
00142 {
00143 const string scheme = eid.getScheme();
00144 const string ssp = eid.getNode() + eid.getApplication();
00145 return make_pair(get(scheme), get(ssp));
00146 }
00147
00148 std::ostream &operator<<(std::ostream &stream, const dtn::data::Dictionary &obj)
00149 {
00150 dtn::data::SDNV length(obj.getSize());
00151 stream << length;
00152 stream << obj._bytestream.rdbuf();
00153
00154 return stream;
00155 }
00156
00157 std::istream &operator>>(std::istream &stream, dtn::data::Dictionary &obj)
00158 {
00159 dtn::data::SDNV length;
00160 stream >> length;
00161
00162 obj._bytestream.str("");
00163 char data[length.getValue()];
00164 stream.read(data, length.getValue());
00165 obj._bytestream.write(data, length.getValue());
00166
00167 return stream;
00168 }
00169 }
00170 }