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 return *this;
00111 }
00112
00113 const uint8_t* Name::bytes() const {
00114 return _bytes;
00115 }
00116
00117 const size_t Name::length() const {
00118 return _length;
00119 }
00120
00121 bool Name::operator==(const Name& name) const {
00122
00123
00124 if (_bytes == NULL && name._bytes == NULL &&
00125 length() == name.length()) return true;
00126
00127
00128 if (_bytes == NULL || name._bytes == NULL) return false;
00129 if (name.length() != length()) return false;
00130 return (memcmp(name.bytes(), bytes(), length()) == 0);
00131 }
00132
00133 bool Name::operator!=(const Name& name) const {
00134 return !(*this == name);
00135 }
00136
00137 bool Name::isUnspecified() const {
00138 return *this == UNSPECIFIED;
00139 }
00140
00141 Name Name::random() {
00142 char name[17];
00143 srand( time(NULL) );
00144
00145 for (int i=0;i<16; i++)
00146 name[i] = ("abcdefghijklmnopqrstuvwxyz")[((unsigned)rand())%26];
00147 name[16] = 0;
00148
00149
00150 return Name( string(name) );
00151 }
00152
00153 string Name::toString() const {
00154 if (_hreadable) {
00155 char str[256];
00156 for (size_t i=0; i<length(); i++) str[i] = bytes()[i];
00157 str[length()] = 0;
00158 return string(str);
00159 } else {
00160 return string("<not readable>");
00161 }
00162 }
00163
00164 NodeID Name::toNodeId() const {
00165 if( bytes()==NULL || length()==0 ) return NodeID::UNSPECIFIED;
00166 return NodeID( Identifier::sha1(bytes(),length()) );
00167 }
00168
00169 SpoVNetID Name::toSpoVNetId() const {
00170 if( bytes()==NULL || length()==0 ) return SpoVNetID::UNSPECIFIED;
00171 return SpoVNetID( Identifier::sha1(bytes(),length()) );
00172 }
00173
00174 }