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
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "Name.h"
00040
00041 std::ostream& operator<<( std::ostream& s, const ariba::Name& n ) {
00042 return s << n.toString();
00043 }
00044
00045 namespace ariba {
00046
00047 const Name Name::UNSPECIFIED;
00048
00049 void Name::init(const char* name, int len, bool copy, bool hreadable) {
00050
00051
00052 if( _bytes != NULL ) {
00053 delete[] _bytes;
00054 _bytes = NULL;
00055 _length = 0;
00056 }
00057
00058
00059 if (len == -1)
00060 len = strlen(name);
00061
00062 if (copy) {
00063
00064 if ( (name!=NULL) && (len>0) ){
00065 _bytes = new uint8_t[len];
00066 memcpy( _bytes, name, len );
00067 } else {
00068 len = 0;
00069 _bytes = NULL;
00070 }
00071
00072 } else {
00073 _bytes = (uint8_t*)name;
00074 }
00075
00076 _copy = copy;
00077 _length = len;
00078 _hreadable = hreadable;
00079 }
00080
00081 Name::Name()
00082 : _bytes( NULL ), _length( 0 ), _copy( false ), _hreadable( false) {
00083 }
00084
00085 Name::Name(const char* name, int len, bool copy)
00086 : _bytes( NULL ), _length( 0 ), _copy( false ), _hreadable( false) {
00087 init(name, len, copy, len == -1);
00088 }
00089
00090 Name::Name(string name)
00091 : _bytes( NULL ), _length( 0 ), _copy( false ), _hreadable( false) {
00092 init(name.c_str(), name.length(), true, true);
00093 }
00094
00095 Name::Name(const Name& name)
00096 : _bytes( NULL ), _length( 0 ), _copy( false ), _hreadable( false) {
00097 init((const char*)name.bytes(), name.length(), true, name._hreadable);
00098 }
00099
00100 Name::~Name() {
00101 if (_copy && (_bytes!=NULL)){
00102 delete[] _bytes;
00103 _bytes = NULL;
00104 _length = 0;
00105 }
00106 }
00107
00108 Name& Name::operator=( const Name& name ) {
00109 init((const char*)name.bytes(), name.length(), true, name._hreadable);
00110 }
00111
00112 const uint8_t* Name::bytes() const {
00113 return _bytes;
00114 }
00115
00116 const size_t Name::length() const {
00117 return _length;
00118 }
00119
00120 bool Name::operator==(const Name& name) const {
00121
00122
00123 if (_bytes == NULL && name._bytes == NULL &&
00124 length() == name.length()) return true;
00125
00126
00127 if (_bytes == NULL || name._bytes == NULL) return false;
00128 if (name.length() != length()) return false;
00129 return (memcmp(name.bytes(), bytes(), length()) == 0);
00130 }
00131
00132 bool Name::operator!=(const Name& name) const {
00133 return !(*this == name);
00134 }
00135
00136 bool Name::isUnspecified() const {
00137 return *this == UNSPECIFIED;
00138 }
00139
00140 Name Name::random() {
00141 char name[17];
00142 srand( time(NULL) );
00143
00144 for (int i=0;i<16; i++)
00145 name[i] = ("abcdefghijklmnopqrstuvwxyz")[((unsigned)rand())%26];
00146 name[16] = 0;
00147
00148
00149 return Name( string(name) );
00150 }
00151
00152 string Name::toString() const {
00153 if (_hreadable) {
00154 char str[256];
00155 for (int i=0; i<length(); i++) str[i] = bytes()[i];
00156 str[length()] = 0;
00157 return string(str);
00158 } else {
00159 return string("<not readable>");
00160 }
00161 }
00162
00163 NodeID Name::toNodeId() const {
00164 if( bytes()==NULL || length()==0 ) return NodeID::UNSPECIFIED;
00165 return NodeID( Identifier::sha1(bytes(),length()) );
00166 }
00167
00168 SpoVNetID Name::toSpoVNetId() const {
00169 if( bytes()==NULL || length()==0 ) return SpoVNetID::UNSPECIFIED;
00170 return SpoVNetID( Identifier::sha1(bytes(),length()) );
00171 }
00172
00173 }