oid.cpp
来自「HP公司的SNMP++的Win32版本源码」· C++ 代码 · 共 917 行 · 第 1/2 页
CPP
917 行
/*===================================================================
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:
Peter E. Mellquist
AUTHOR:
Peter E Mellquist
DATE:
June 15, 1999
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++
OPERATING SYSTEM(S):
MS-Windows Win32
BSD UNIX
=====================================================================*/
char oid_cpp_version[]="#(@)SNMP++ 2.8 $Header: oid.cpp,v 1.33 96/09/11 14:02:00 hmgr Exp $";
//---------[ external C libaries used ]--------------------------------
extern "C"
{
#include <stdio.h> // standard io
#include <memory.h> // memcpy's
#include <string.h> // strlen, etc..
#include <stdlib.h> // standard library
#include <ctype.h> // isdigit
#include <malloc.h> // malloc, free
}
#include "oid.h" // include def for oid class
#define SNMPBUFFSIZE 10 // size of scratch buffer
#define SNMPCHARSIZE 10 // an individual oid instance as a string
//=============[Oid::get_syntax(void)]====================================
SmiUINT32 Oid::get_syntax()
{ return sNMP_SYNTAX_OID; };
//=============[Oid::Oid(void)]============================================
// constructor using no arguments
// initialize octet ptr and string
// ptr to null
Oid::Oid( void)
{
smival.syntax = sNMP_SYNTAX_OID;
smival.value.oid.len = 0;
smival.value.oid.ptr = NULL;
iv_str=0;
};
//=============[Oid::Oid( const char *dotted_string ]=====================
// constructor using a dotted string
//
// do a string to oid using the string passed in
Oid::Oid( const char * dotted_oid_string)
{
smival.syntax = sNMP_SYNTAX_OID;
smival.value.oid.len = 0;
smival.value.oid.ptr = NULL;
StrToOid( (char *) dotted_oid_string, &smival.value.oid);
iv_str = 0;
};
//=============[Oid::Oid( const Oid &oid) ]================================
// constructor using another oid object
//
// do an oid copy using the oid object passed in
Oid::Oid ( const Oid &oid)
{
smival.syntax = sNMP_SYNTAX_OID;
smival.value.oid.len = 0;
smival.value.oid.ptr = NULL;
iv_str = 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 !=0)
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 member
Oid::Oid(const unsigned long *raw_oid, int oid_len)
{
smival.syntax = sNMP_SYNTAX_OID;
smival.value.oid.len = 0;
smival.value.oid.ptr = NULL;
iv_str = 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];
}
}
};
//=============[Oid::~Oid]==============================================
// destructor
//
// free up the descriptor space
Oid::~Oid()
{
// free up the octet deep memory
if ( smival.value.oid.ptr ) {
delete [] smival.value.oid.ptr;
smival.value.oid.ptr = NULL;
}
// free up the output string
if ( iv_str !=0)
delete [] iv_str;
};
//=============[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 object
Oid& Oid::operator=( const char *dotted_oid_string)
{
// delete the old value
if ( smival.value.oid.ptr ) {
delete [] smival.value.oid.ptr;
smival.value.oid.ptr = NULL;
}
smival.value.oid.len = 0;
// assign the new value
StrToOid( (char *) 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 in
Oid& Oid::operator=( const Oid &oid)
{
// protect against assignment from self
if ( this == &oid )
return *this;
// delete the old value
if ( smival.value.oid.ptr ) {
delete [] smival.value.oid.ptr;
smival.value.oid.ptr = NULL;
}
smival.value.oid.len = 0;
// check for zero len on source
if ( oid.smival.value.oid.len == 0) {
smival.value.oid.len = 0;
smival.value.oid.ptr = NULL;
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 !=0)
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 space
Oid& Oid::operator+=( const char *a)
{
unsigned long n;
if (!a)
return *this;
if ( *a=='.')
a++;
n = (smival.value.oid.len *SNMPCHARSIZE) + ( smival.value.oid.len) + 1 + STRLEN(a);
char *ptr = (char *) new char[ n];
if ( ptr !=0)
{
OidToStr(&smival.value.oid, n,ptr);
if (STRLEN(ptr))
STRCAT(ptr,".");
STRCAT(ptr,a);
if ( smival.value.oid.len !=0) {
delete [] smival.value.oid.ptr;
smival.value.oid.len = 0;
}
StrToOid( (char *) ptr, &smival.value.oid);
delete [] ptr;
}
return *this;
};
//=============[ int operator == oid,oid ]=================================
// equivlence operator overloaded
int operator==( const Oid &lhs, const Oid &rhs)
{
// ensure same len, then use nCompare
if (rhs.len() != lhs.len()) return 0;
if( lhs.nCompare( rhs.len(), rhs)==0) return 1; else return 0;
};
//==============[ operator!=( Oid &x,Oid &y) ]=============================
//not equivlence operator overloaded
int operator!=( const Oid &lhs,const Oid &rhs)
{
// just invert ==
return (!(lhs==rhs));
};
//==============[ operator<( Oid &x,Oid &y) ]=============================
// less than < overloaded
int 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;
else if (result > 0) return 0;
else{
// if here, equivalent substrings, call the shorter one <
if (lhs.len() < rhs.len()) return 1;
else return 0;
}
};
//==============[ operator<=( Oid &x,Oid &y) ]=============================
// less than <= overloaded
int operator<=( const Oid &x,const Oid &y)
{
if((x<y) || (x==y)) return 1;
else return 0;
};
//==============[ operator>( Oid &x,Oid &y) ]=============================
// greater than > overloaded
int operator>( const Oid &x,const Oid &y)
{
// just invert existing <=
if (!(x<=y)) return 1;
else return 0;
};
//==============[ operator>=( Oid &x,Oid &y) ]=============================
// greater than >= overloaded
int operator>=( const Oid &x,const Oid &y)
{
// just invert existing <
if(!(x<y)) return 1;
else return 0;
};
//==============[ operator==( Oid &x,char *) ]=============================
// equivlence operator overloaded
int operator==( const Oid &x,const char *dotted_oid_string)
{
// create a temp oid object
Oid to( dotted_oid_string);
// compare using existing operator
if(x == to) return 1; else return 0;
};
//==============[ operator!=( Oid &x,char*) ]=============================
// not equivlence operator overloaded
int operator!=( const Oid &x,const char *dotted_oid_string)
{
// create a temp oid object
Oid to( dotted_oid_string);
// compare using existing operator
if(x != to) return 1; else return 0;
};
//==============[ operator<( Oid &x,char*) ]=============================
// less than < operator overloaded
int operator<( const Oid &x,const char *dotted_oid_string)
{
// create a temp oid object
Oid to( dotted_oid_string);
// compare using existing operator
if(x < to) return 1; else return 0;
};
//==============[ operator<=( Oid &x,char *) ]=============================
// less than <= operator overloaded
int operator<=( const Oid &x,char *dotted_oid_string)
{
// create a temp oid object
Oid to( dotted_oid_string);
// compare using existing operator
if(x <= to) return 1; else return 0;
};
//==============[ operator>( Oid &x,char* ]=============================
// greater than > operator overloaded
int operator>( const Oid &x,const char *dotted_oid_string)
{
// create a temp oid object
Oid to( dotted_oid_string);
// compare using existing operator
if(x > to) return 1; else return 0;
};
//==============[ operator>=( Oid &x,char*) ]=============================
// greater than >= operator overloaded
int operator>=( const Oid &x,const char *dotted_oid_string)
{
// create a temp oid object
Oid to( dotted_oid_string);
// compare using existing operator
if(x >= to) return 1; else return 0;
};
//===============[Oid::oidval ]=============================================
// return the WinSnmp oid part
SmiLPOID Oid::oidval()
{
return (SmiLPOID) &smival.value.oid;
};
//===============[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){
if ( smival.value.oid.ptr) {
delete [] smival.value.oid.ptr;
smival.value.oid.ptr = NULL;
smival.value.oid.len = 0;
}
smival.value.oid.ptr = (SmiLPUINT32) new unsigned long [ oid_len];
if ( smival.value.oid.ptr ==0){
return;
}
}
MEMCPY((SmiLPBYTE) smival.value.oid.ptr,
(SmiLPBYTE) raw_oid,
(size_t) (oid_len*sizeof(SmiUINT32)));
smival.value.oid.len = oid_len;
};
//===============[Oid::len ]================================================
// return the len of the oid
unsigned long Oid::len() const
{
return smival.value.oid.len;
};
//===============[Oid::trim( unsigned int) ]============================
// trim off the n leftmost values of an oid
// Note!, does not adjust actual space for
// speed
void 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 [] smival.value.oid.ptr;
smival.value.oid.ptr = NULL;
}
}
};
//===============[Oid::operator += const unsigned int) ]====================
// append operator, appends an int
//
// 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 space
Oid& Oid::operator+=( const unsigned long i)
{
unsigned long n;
n = (smival.value.oid.len *SNMPCHARSIZE) + ( smival.value.oid.len -1) + 1 + 6; // extra for
char buffer[SNMPBUFFSIZE];
// allocate some temporary space
char *ptr = (char *) new char[ n];
if ( ptr != 0)
{
OidToStr(&smival.value.oid,n,ptr);
if (STRLEN(ptr))
STRCAT(ptr,".");
sprintf( buffer,"%ld",i);
STRCAT(ptr,buffer);
if ( smival.value.oid.ptr ) {
delete [] smival.value.oid.ptr;
smival.value.oid.ptr = NULL;
smival.value.oid.len = 0;
}
StrToOid( (char *) ptr, &smival.value.oid);
delete [] ptr;
}
return *this;
}
//===============[Oid::operator += const Oid) ]========================
// append operator, appends an Oid
//
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?