⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oid.cpp

📁 SNMP++程序源码 for ll .8snmp++2_8.tar.Z 嵌入式linux环境下的SNMP开发代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// allocate some space for a max oid string// extract current string into space// concat new string// free up existing oid// make a new oid from string// delete allocated spaceOid& Oid::operator+=( const Oid &o){  SmiLPUINT32 new_oid;  if (o.smival.value.oid.len == 0)    return *this;  new_oid = (SmiLPUINT32) new unsigned long [ smival.value.oid.len + o.smival.value.oid.len];  if ( new_oid == 0 ){    if ( smival.value.oid.ptr ) {        delete [] smival.value.oid.ptr;        smival.value.oid.ptr = NULL;        smival.value.oid.len = 0;    }    return *this;  }    if (smival.value.oid.ptr){    MEMCPY((SmiLPBYTE) new_oid,	   (SmiLPBYTE) smival.value.oid.ptr,	   (size_t) (smival.value.oid.len*sizeof(SmiUINT32)));    delete [] smival.value.oid.ptr;  }    // out with the old, in with the new...  smival.value.oid.ptr = new_oid;  MEMCPY((SmiLPBYTE) &new_oid[smival.value.oid.len],         (SmiLPBYTE) o.smival.value.oid.ptr,         (size_t) (o.smival.value.oid.len*sizeof(SmiUINT32)));  smival.value.oid.len += o.smival.value.oid.len;  return *this;}//=============[Oid::get_printable ]====================================// return string value//// return string portion of the oid//char * Oid::get_printable(){   unsigned long n;   // the worst case char len of an oid can be..   // oid.len*3 + dots in between if each oid is XXXX   // so.. size = (len*4) + (len-1) + 1 , extra for a null   n = (smival.value.oid.len *SNMPCHARSIZE) + ( smival.value.oid.len -1) + 1 ;   if (n==0) n=1; // need at least 1 byte for a null string   // adjust the len of output array in case size was adjusted   if ( iv_str!=0)     delete [] iv_str;   // allocate some space for the output string   iv_str = (char *) new char[ n];   // convert to an output string   if ( iv_str !=0)     OidToStr(&smival.value.oid,n,iv_str);   return iv_str;};//==========[Oid::get_printable( unsigned int n) ]=========================// overloaded get_printable to get the n left most values// as a dotted stringchar * Oid::get_printable( const unsigned long n){  unsigned long index = 0;  unsigned long start;  unsigned totLen = 0;  char szNumber[SNMPBUFFSIZE];  unsigned long nz;  nz = (smival.value.oid.len *SNMPCHARSIZE) + ( smival.value.oid.len -1) + 1 ;  if (nz==0) nz=1; // need at least one byte for a null string  // delete the previous output string  if ( iv_str !=0)    delete [] iv_str;    // allocate some space for the output string  iv_str = (char *) new char[ nz];  if ( iv_str == 0)    return iv_str;    // init the string  iv_str[totLen] = 0;  // cannot ask for more then there is..  if (n>smival.value.oid.len)    return iv_str;  start = smival.value.oid.len - n;  // loop through and build up a string  for (index=start; index<smival.value.oid.len; index++)  {    // convert data element to a string    sprintf( szNumber,"%ld",smival.value.oid.ptr[index]);    // verify len is not over    if (totLen+STRLEN(szNumber)+1>=nz)      return iv_str;    // if not at end, pad with a dot    if (totLen!=0)      iv_str[totLen++] = '.';    // copy the string token into the main string    STRCPY(iv_str+totLen, szNumber);    // adjust the total len    totLen += STRLEN(szNumber);  }  return iv_str;};//==============[Oid::get_printable( unsigned int start, n) ]=============// return a dotted string starting at start,// going n positions to the left// NOTE, start is 1 based ( the first id is at position #1)char * Oid::get_printable( const unsigned long start,                           const unsigned long n){  unsigned long index = 0;  unsigned long totLen = 0;  char szNumber[SNMPBUFFSIZE];  unsigned long nz;  unsigned long my_start;  my_start = start;  nz = (smival.value.oid.len *SNMPCHARSIZE) + ( smival.value.oid.len -1) + 1 ;  if (nz==0) nz=1; // need at least one byte for a null string  // clean up the previous output string  if ( iv_str !=0)    delete [] iv_str;    // allocate some space for the output string  iv_str = (char *) new char[ nz];  if ( iv_str == 0)    return iv_str;    // init the string  iv_str[totLen] = 0;  my_start -=1;        // cannot ask for more then there is..  if ((my_start+n-1)>smival.value.oid.len)    return iv_str;        // loop through and build up a string  for (index=my_start; index<(my_start+n); index++)  {    // convert data element to a string    sprintf( szNumber,"%ld",smival.value.oid.ptr[index]);    // verify len is not over    if (totLen+STRLEN(szNumber)+1>=nz)      return iv_str;    // if not at end, pad with a dot    if (totLen!=0)      iv_str[totLen++] = '.';    // copy the string token into the main string    STRCPY(iv_str+totLen, szNumber);    // adjust the total len    totLen += STRLEN(szNumber);  }  return iv_str;};//=============[Oid::StrToOid( char *string, SmiLPOID dst) ]==============// convert a string to an oidint Oid::StrToOid( const char *string,                   SmiLPOID dstOid){  unsigned long index = 0;  unsigned long number = 0;  // make a temp buffer to copy the data into first  SmiLPUINT32 temp;  unsigned long nz;  if (string && *string)   {     nz = STRLEN( string);  }  else {     dstOid->len = 0;     dstOid->ptr = NULL;     return -1;  }  temp = (SmiLPUINT32) new unsigned long [ nz];  // return if can't get the mem  if ( temp == 0) return -1;  while (*string!=0 && index<nz)  {    // init the number for each token    number = 0;    // skip over the dot    if (*string=='.') string++;            // grab a digit token and convert it to a long int    while (isdigit(*string))      number=number*10 + *(string++)-'0';                // check for invalid chars    if (*string!=0 && *string!='.')      // Error: Invalid character in string    {      delete [] temp;      return -1;    }    // stuff the value into the array    temp[index] = number;    index++;  // bump the counter  }  // get some space for the real oid  dstOid->ptr = (SmiLPUINT32) new unsigned long [ index];  // return if can't get the mem needed  if( dstOid->ptr == 0)  {    delete [] temp;    return -1;  }  // copy in the temp data  MEMCPY((SmiLPBYTE) dstOid->ptr,         (SmiLPBYTE) temp,         (size_t) (index*sizeof(SmiUINT32)));  // set the len of the oid  dstOid->len = index;  // free up temp data  delete [] temp;  return (int) index;};//===============[Oid::OidCopy( source, destination) ]====================// Copy an oidint Oid::OidCopy( SmiLPOID srcOid,     // source oid                  SmiLPOID dstOid)     // destination oid{  // check source len ! zero  if (srcOid->len==0)    return -1;  // copy source to destination  MEMCPY((SmiLPBYTE) dstOid->ptr,         (SmiLPBYTE) srcOid->ptr,         (size_t) (srcOid->len*sizeof(SmiUINT32)));  //set the new len  dstOid->len = srcOid->len;  return (int) srcOid->len;};//===============[Oid::nCompare( n, Oid) ]=================================// compare the n leftmost values of two oids ( left-to_right )// // self == Oid then return 0, they are equal// self < Oid then return -1, <// self > Oid then return 1,  >int Oid::nCompare( const unsigned long n,                    const Oid &o) const{                unsigned long z;   unsigned long len = n;   int reduced_len = 0;      // 1st case they both are null   if (( len==0)&&( this->smival.value.oid.len==0)) return 0;  // equal      // verify that n is valid, must be >= 0   if ( len <=0) return 1;                         // ! equal   // only compare for the minimal length   if (len > this->smival.value.oid.len){     len = this->smival.value.oid.len;     reduced_len = 1;   }   if (len > o.smival.value.oid.len){     len = o.smival.value.oid.len;     reduced_len = 1;   }      z=0;   while(z<len)   {      if ( this->smival.value.oid.ptr[z] < o.smival.value.oid.ptr[z])        return -1;                              // less than      if ( this->smival.value.oid.ptr[z] > o.smival.value.oid.ptr[z])        return 1;                               // greater than      z++;       }              // if we truncated the len then these may not be equal   if (reduced_len){     if (this->smival.value.oid.len < o.smival.value.oid.len)       return -1;     if (this->smival.value.oid.len > o.smival.value.oid.len)       return 1;   }   return 0;                                 // equal      };//===============[Oid::nCompare( n, Oid) ]=================================// compare the n rightmost bytes (right-to-left)// returns 0, equal// returns -1, <// returns 1 , >int Oid::RnCompare( const unsigned long n, const Oid &o) const{   // oid to compare must have at least the same number   // of sub-ids to comparison else the argument Oid is    // less than THIS   if ( o.len() < n)      return -1;   // also can't compare argument oid for sub-ids which   // THIS does not have   if ( this->len() < n)      return -1;   int start = (int) this->len();   int end = (int) start - (int) n;   for ( int z=start;z< end;z--)   {       if ( o.smival.value.oid.ptr[z] < this->smival.value.oid.ptr[z])          return -1;       if ( o.smival.value.oid.ptr[z] > this->smival.value.oid.ptr[z])          return 1;   }   return 0;   // they are equal}; //================[ Oid::valid() ]========================================// is the Oid object valid// returns validityint Oid::valid() const{   return ( smival.value.oid.ptr ? TRUE : FALSE ) ;};//================[Oid::OidToStr ]=========================================// convert an oid to a stringint Oid::OidToStr( SmiLPOID srcOid,            // source oid                   unsigned long size,         // size of string                   char *string)               // pointer to string{  unsigned long index = 0;  unsigned totLen = 0;  char szNumber[SNMPBUFFSIZE];  // init the string  string[totLen] = 0;  // verify there is something to copy  if (srcOid->len==0)    return -1;  // loop through and build up a string  for (index=0; index<srcOid->len; index++)  {    // convert data element to a string    sprintf( szNumber,"%ld", srcOid->ptr[index]);    // verify len is not over    if (totLen+STRLEN(szNumber)+1>=size)      return -2;    // if not at end, pad with a dot    if (totLen!=0)      string[totLen++] = '.';    // copy the string token into the main string    STRCPY(string+totLen, szNumber);    // adjust the total len    totLen += STRLEN(szNumber);  }  return totLen+1;};//================[ general Value = operator ]========================SnmpSyntax& Oid::operator=( SnmpSyntax &val){  // protect against assignment from self  if ( this == &val )      return *this;  // blow away old value  smival.value.oid.len = 0;  if (smival.value.oid.ptr) {      delete [] smival.value.oid.ptr;      smival.value.oid.ptr = NULL;  }  // assign new value  if (val.valid()){    switch (val.get_syntax()){      case sNMP_SYNTAX_OID:        set_data( ((Oid &)val).smival.value.oid.ptr, 		  (unsigned int)((Oid &)val).smival.value.oid.len);        break;    }  }  return *this;};//================[ [] operator ]=====================================unsigned long& Oid::operator[](int position){  return smival.value.oid.ptr[position];}//================[ clone ]===========================================SnmpSyntax *Oid::clone() const{     return (SnmpSyntax *) new Oid(*this); };

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -