Changeset 2437 for source/ariba/Name.cpp


Ignore:
Timestamp:
Feb 17, 2009, 12:05:25 PM (15 years ago)
Author:
Christoph Mayer
Message:

-memory leaks und corruption gefixt

File:
1 edited

Legend:

Unmodified
Added
Removed
  • source/ariba/Name.cpp

    r2409 r2437  
    3939#include "Name.h"
    4040
    41 namespace ariba {
    42 const Name Name::UNSPECIFIED;
    43 }
    44 
    4541std::ostream& operator<<( std::ostream& s, const ariba::Name& n ) {
    4642        return s << n.toString();
    4743}
    4844
     45namespace ariba {
     46
     47const Name Name::UNSPECIFIED;
     48
     49void Name::init(const char* name, int len, bool copy, bool hreadable) {
     50
     51        // delete the old buffer
     52        if( _bytes != NULL ) {
     53                delete[] _bytes;
     54                _bytes = NULL;
     55                _length = 0;
     56        }
     57
     58        // alloc new stuff
     59        if (len == -1)
     60                len = strlen(name);
     61
     62        if (copy) {
     63
     64                if ((name!=NULL) && (len>0)){
     65                        _bytes = new uint8_t[len];
     66                        memcpy( _bytes, name, len );
     67                } else {
     68                        len = 0;
     69                        _bytes = NULL; 
     70                }
     71
     72        } else {
     73                _bytes = (uint8_t*)name;
     74        }
     75
     76        _copy = copy;
     77        _length = len;
     78        _hreadable = hreadable;
     79}
     80
     81Name::Name()
     82        : _bytes( NULL ), _length( 0 ), _copy( false ), _hreadable( false) {
     83}
     84
     85Name::Name(const char* name, int len, bool copy)
     86        : _bytes( NULL ), _length( 0 ), _copy( false ), _hreadable( false) {
     87        init(name, len, copy, len == -1);
     88}
     89
     90Name::Name(string name)
     91        : _bytes( NULL ), _length( 0 ), _copy( false ), _hreadable( false) {
     92        init(name.c_str(), name.length(), true, true);
     93}
     94
     95Name::Name(const Name& name)
     96        : _bytes( NULL ), _length( 0 ), _copy( false ), _hreadable( false) {
     97        init((const char*)name.bytes(), name.length(), true, name._hreadable);
     98}
     99
     100Name::~Name() {
     101        if (_copy && (_bytes!=NULL)){
     102                delete[] _bytes;
     103                _bytes = NULL;
     104                _length = 0;
     105        }
     106}
     107
     108const uint8_t* Name::bytes() const {
     109        return _bytes;
     110}
     111
     112const size_t Name::length() const {
     113        return _length;
     114}
     115
     116bool Name::operator==(const Name& name) const {
     117
     118        // unspecified Name objects
     119        if (_bytes == NULL && name._bytes == NULL &&
     120                length() == name.length()) return true;
     121       
     122        // specified name objects
     123        if (_bytes == NULL || name._bytes == NULL) return false;
     124        if (name.length() != length()) return false;
     125        return (memcmp(name.bytes(), bytes(), length()) == 0);
     126}
     127
     128bool Name::operator!=(const Name& name) const {
     129        return !(*this == name);
     130}
     131
     132bool Name::isUnspecified() const {
     133        return *this == UNSPECIFIED;
     134}
     135
     136Name Name::random() {
     137        char name[17];
     138        for (int i=0;i<16; i++)
     139                name[i] = ("abcdefghijklmnopqrstuvwxyz")[((unsigned)rand())%26];
     140        name[16] = 0;
     141
     142        // force use of the std::string ctor with this
     143        return Name( string(name) );
     144}
     145
     146string Name::toString() const {
     147        if (_hreadable) {
     148                char str[256];
     149                for (int i=0; i<length(); i++) str[i] = bytes()[i];
     150                str[length()] = 0;
     151                return string(str);
     152        } else {
     153                return string("<not readable>");
     154        }
     155}
     156
     157NodeID Name::toNodeId() const {
     158        if( bytes()==NULL || length()==0 ) return NodeID::UNSPECIFIED;
     159        return NodeID( Identifier::sha1(bytes(),length()) );
     160}
     161
     162SpoVNetID Name::toSpoVNetId() const {
     163        if( bytes()==NULL || length()==0 ) return SpoVNetID::UNSPECIFIED;
     164        return SpoVNetID( Identifier::sha1(bytes(),length()) );
     165}
     166
     167} // namespace ariba
Note: See TracChangeset for help on using the changeset viewer.