oid.cpp

来自「JdonFramework need above jdk 1.4.0 This」· C++ 代码 · 共 767 行 · 第 1/2 页

CPP
767
字号
/*_############################################################################  _##   _##  oid.cpp    _##  _##  SNMP++v3.2.21a  _##  -----------------------------------------------  _##  Copyright (c) 2001-2006 Jochen Katz, Frank Fock  _##  _##  This software is based on SNMP++2.6 from Hewlett Packard:  _##    _##    Copyright (c) 1996  _##    Hewlett-Packard Company  _##    _##  ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.  _##  Permission to use, copy, modify, distribute and/or sell this software   _##  and/or its documentation is hereby granted without fee. User agrees   _##  to display the above copyright notice and this license notice in all   _##  copies of the software and any documentation of the software. User   _##  agrees to assume all liability for the use of the software;   _##  Hewlett-Packard and Jochen Katz make no representations about the   _##  suitability of this software for any purpose. It is provided   _##  "AS-IS" without warranty of any kind, either express or implied. User   _##  hereby grants a royalty-free license to any and all derivatives based  _##  upon this software code base.   _##    _##  Stuttgart, Germany, Tue Nov 21 22:12:16 CET 2006   _##    _##########################################################################*//*===================================================================  Copyright (c) 1999  Hewlett-Packard Company  ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.  Permission to use, copy, modify, distribute and/or sell this software  and/or its documentation is hereby granted without fee. User agrees  to display the above copyright notice and this license notice in all  copies of the software and any documentation of the software. User  agrees to assume all liability for the use of the software; Hewlett-Packard  makes no representations about the suitability of this software for any  purpose. It is provided "AS-IS" without warranty of any kind,either express  or implied. User hereby grants a royalty-free license to any and all  derivatives based upon this software code base.  O I D. C P P  OID CLASS IMPLEMENTATION  DESIGN + AUTHOR:         Peter E. Mellquist  DESCRIPTION:  This module contains the implementation of the oid class. This  includes all protected and public member functions. The oid class  may be compiled stand alone without the use of any other library.  LANGUAGE:                ANSI C++=====================================================================*/char oid_cpp_version[]="#(@) SNMP++ $Id: oid.cpp,v 1.13 2006/06/10 22:25:14 katz Exp $";//---------[ external C libaries used ]--------------------------------#include <stdio.h>                // standard io#if !(defined (CPU) && CPU == PPC603)#include <memory.h>               // memcpy's#endif#include <string.h>               // strlen, etc..#include <stdlib.h>               // standard library#include <ctype.h>                // isdigit#include <stdlib.h>               // malloc, free#include "snmp_pp/oid.h"                  // include def for oid class#ifdef SNMP_PP_NAMESPACEnamespace Snmp_pp {#endif#define  SNMPBUFFSIZE 11          // size of scratch buffer#define  SNMPCHARSIZE 11          // an individual oid instance as a string/* Borlands isdigit has a bug */#ifdef __BCPLUSPLUS__#define my_isdigit(c) ((c) >= '0' && (c) <= '9')#else#define my_isdigit isdigit#endif//=============[Oid::Oid(void)]============================================// constructor using no arguments// initialize octet ptr and string// ptr to nullOid::Oid() : iv_str(0), iv_part_str(0), m_changed(true){  smival.syntax = sNMP_SYNTAX_OID;  smival.value.oid.len = 0;  smival.value.oid.ptr = 0;}//=============[Oid::Oid(const char *dotted_string ]=====================// constructor using a dotted string//// do a string to oid using the string passed inOid::Oid(const char *oid_string, const bool is_dotted_oid_string)  : iv_str(0), iv_part_str(0), m_changed(true){  smival.syntax = sNMP_SYNTAX_OID;  smival.value.oid.len = 0;  smival.value.oid.ptr = 0;  if (is_dotted_oid_string)    StrToOid(oid_string, &smival.value.oid);  else    set_data(oid_string, oid_string ? strlen(oid_string) : 0);}//=============[Oid::Oid(const Oid &oid) ]================================// constructor using another oid object//// do an oid copy using the oid object passed inOid::Oid(const Oid &oid)  : iv_str(0), iv_part_str(0), m_changed(true){  smival.syntax = sNMP_SYNTAX_OID;  smival.value.oid.len = 0;  smival.value.oid.ptr = 0;  // allocate some memory for the oid  // in this case the size to allocate is the same size as the source oid  if (oid.smival.value.oid.len)  {    smival.value.oid.ptr = (SmiLPUINT32) new unsigned long[oid.smival.value.oid.len];    if (smival.value.oid.ptr)      OidCopy((SmiLPOID)&(oid.smival.value.oid), (SmiLPOID)&smival.value.oid);  }}//=============[Oid::Oid(const unsigned long *raw_oid, int oid_len) ]====// constructor using raw numeric form//// copy the integer values into the private memberOid::Oid(const unsigned long *raw_oid, int oid_len)  : iv_str(0), iv_part_str(0), m_changed(true){  smival.syntax = sNMP_SYNTAX_OID;  smival.value.oid.len = 0;  smival.value.oid.ptr = 0;  if (raw_oid && (oid_len > 0))  {    smival.value.oid.ptr = (SmiLPUINT32) new unsigned long[oid_len];    if (smival.value.oid.ptr)    {      smival.value.oid.len = oid_len;      for (int i=0; i < oid_len; i++)        smival.value.oid.ptr[i] = raw_oid[i];    }  }}#if 0// Constructor from standard C string array.Oid::Oid(const char *str, const unsigned int str_len)  : iv_str(0), iv_part_str(0), m_changed(true){  smival.syntax = sNMP_SYNTAX_OID;  smival.value.oid.len = 0;  smival.value.oid.ptr = 0;  set_data(str, str_len);}#endif//=============[Oid::~Oid]==============================================Oid::~Oid(){  delete_oid_ptr();  if (iv_str)      delete [] iv_str;        // free up the output string  if (iv_part_str) delete [] iv_part_str;   // free up the output string}//=============[Oid::operator = const char * dotted_string ]==============// assignment to a string operator overloaded//// free the existing oid// create the new oid from the string// return this objectOid& Oid::operator=(const char *dotted_oid_string){  delete_oid_ptr();  // assign the new value  StrToOid(dotted_oid_string, &smival.value.oid);  return *this;}//=============[Oid:: operator = const Oid &oid ]==========================// assignment to another oid object overloaded//// free the existing oid// create a new one from the object passed inOid& Oid::operator=(const Oid &oid){  if (this == &oid) return *this;  // protect against assignment from self  delete_oid_ptr();  // check for zero len on source  if (oid.smival.value.oid.len == 0)    return *this;  // allocate some memory for the oid  smival.value.oid.ptr = (SmiLPUINT32) new unsigned long[oid.smival.value.oid.len];  if (smival.value.oid.ptr)    OidCopy((SmiLPOID)&(oid.smival.value.oid), (SmiLPOID)&smival.value.oid);  return *this;}//==============[Oid:: operator += const char *a ]=========================// append operator, appends a string//// 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 char *a){  unsigned int n;  if (!a) return *this;  if (*a == '.') ++a;  n = (smival.value.oid.len * SNMPCHARSIZE) + (smival.value.oid.len)       + 1 + SAFE_UINT_CAST(strlen(a));  char *ptr = new char[n];  if (ptr)  {    /// @todo optimze this function (avoid conversion to string)    OidToStr(&smival.value.oid, n, ptr);    if (ptr[0])      STRCAT(ptr,".");    STRCAT(ptr,a);    delete_oid_ptr();    StrToOid(ptr, &smival.value.oid);    delete [] ptr;  }  return *this;}//=============[ int operator == oid,oid ]=================================// equivlence operator overloadedint operator==(const Oid &lhs, const Oid &rhs){  // ensure same len, then use nCompare  if (rhs.len() != lhs.len()) return 0;  return (lhs.nCompare(rhs.len(), rhs) == 0);}//==============[ operator<(Oid &x,Oid &y) ]=============================// less than < overloadedint operator<(const Oid &lhs, const Oid &rhs){  int result;  // call nCompare with the current  // Oidx, Oidy and len of Oidx  if((result = lhs.nCompare(rhs.len(), rhs))<0)  return 1;  if (result > 0)    return 0;  // if here, equivalent substrings, call the shorter one <  return (lhs.len() < rhs.len());}//==============[ operator==(Oid &x,char *) ]=============================// equivlence operator overloadedint operator==(const Oid &x, const char *dotted_oid_string){  Oid to(dotted_oid_string);   // create a temp oid object  return (x == to);   // compare using existing operator}//==============[ operator!=(Oid &x,char*) ]=============================// not equivlence operator overloadedint operator!=(const Oid &x, const char *dotted_oid_string){  Oid to(dotted_oid_string);  // create a temp oid object  return (x != to);  // compare using existing operator}//==============[ operator<(Oid &x,char*) ]=============================// less than < operator overloadedint operator<(const Oid &x, const char *dotted_oid_string){  Oid to(dotted_oid_string);  // create a temp oid object  return (x < to);  // compare using existing operator}//==============[ operator<=(Oid &x,char *) ]=============================// less than <= operator overloadedint operator<=(const Oid &x,char *dotted_oid_string){  Oid to(dotted_oid_string);  // create a temp oid object  return (x <= to);  // compare using existing operator}//==============[ operator>(Oid &x,char* ]=============================// greater than > operator overloadedint operator>(const Oid &x,const char *dotted_oid_string){  Oid to(dotted_oid_string);  // create a temp oid object  return (x > to);   // compare using existing operator}//==============[ operator>=(Oid &x,char*) ]=============================// greater than >= operator overloadedint operator>=(const Oid &x,const char *dotted_oid_string){  Oid to(dotted_oid_string);  // create a temp oid object  return (x >= to);   // compare using existing operator}//===============[Oid::set_data ]==---=====================================// copy data from raw form...void Oid::set_data(const unsigned long *raw_oid,                   const unsigned int oid_len){  if (smival.value.oid.len < oid_len)  {    delete_oid_ptr();    smival.value.oid.ptr = (SmiLPUINT32) new unsigned long[oid_len];    if (!smival.value.oid.ptr) return;  }  MEMCPY((SmiLPBYTE) smival.value.oid.ptr,         (SmiLPBYTE) raw_oid,         (size_t) (oid_len*sizeof(SmiUINT32)));  smival.value.oid.len = oid_len;  m_changed = true;}// Set the data from raw form.void Oid::set_data(const char *str, const unsigned int str_len){  if (smival.value.oid.len < str_len)  {    delete_oid_ptr();    smival.value.oid.ptr = (SmiLPUINT32) new unsigned long[str_len];    if (!smival.value.oid.ptr) return;  }  if ((!str) || (str_len == 0))    return;  for (unsigned int i=0; i<str_len; i++)    smival.value.oid.ptr[i] = str[i];  smival.value.oid.len = str_len;  m_changed = true;}//===============[Oid::trim(unsigned int) ]============================// trim off the n leftmost values of an oid// Note!, does not adjust actual space for// speedvoid Oid::trim(const unsigned long n){  // verify that n is legal  if ((n <= smival.value.oid.len) && (n > 0))  {    smival.value.oid.len -= n;    if (smival.value.oid.len == 0)      delete_oid_ptr();    m_changed = true;

⌨️ 快捷键说明

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