• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

ibrdtn/ibrdtn/data/Dictionary.cpp

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

Generated on Thu Nov 11 2010 09:49:47 for IBR-DTNSuite by  doxygen 1.7.1