<< Back to man.ChinaUnix.net

10.  OctetStr Class

Object Modeling Technique (OMT) view of the SNMP++ Octet String Class

 

10.1. The OctetStr Class

The SNMP++ Octet class allows for easy and safe usage of SMI octets. With the octet class, it is no longer needed to work with octets using internal  pointers and lengths. Using the SNMP++ OctetStr class, OctetStr objects can be easily instantiated, manipulated and destroyed without the overhead burden of managing memory and memory leaks. Like the ANSI C++ string class, the OctetStr class supports a variety of ways to construct OctetStr, assign them and use them with other SNMP++ classes. The OctetStr class interfaces with the SNMP++ variable binding (Vb) class making getting and setting the SMI value portion of a Vb object straight forward. The OctetStr class is fully portable and does not rely on additional SNMP libraries to be present.

 

10.2.  Overview of OctetStr Class Member Functions

 

OctetStr Class Member Functions

Description

Constructors

 

     OctetStr::OctetStr( void);

Construct a OctetStr with no data.

     OctetStr::OctetStr( const char* string);

Construct a OctetStr with a null terminated string.

     OctetStr::OctetStr( const unsigned char *s, unsigned long int i);

Construct a OctetStr with a pointer and a length.

     OctetStr::OctetStr( const OctetStr &octet);

Copy Constructor.

Destructor

 

     OctetStr::~OctetStr( );

Destroy an OctetStr object.


10.3.  Overview of OctetStr Class Member Functions Continued

OctetStr Class Member Functions

Description

Overloaded Operators

 

     OctetStr& operator = ( const char *string);   

Assign an OctetStr object a null terminated string.

     OctetStr& operator = ( const OctetStr& octet);

Assign an OctetStr another OctetStr.

     int operator == ( const OctetStr &lhs, const OctetStr &rhs);

Compare two OctetStr objects for equivalence.

     int operator == ( const OctetStr & lhs, const char *string);

Compare an OctetStr and a char * for equivalence.

     int operator != ( const OctetStr& lhs, const OctetStr& rhs);

Compare two OctetStr objects for not equivalence.

     int operator != ( const OctetStr& lhs, const char *string);

Compare an OctetStr and a char * for not equivalence.

     int operator < ( const OctetStr&lhs, const OctetStr& rhs);

Test if one OctetStr is less than another.

     int operator < ( const OctetStr &lhs, const char * string);

Test if one OctetStr is less than a char *.

     int operator <= ( const OctetStr &lhs, const OctetStr &rhs);

Test if one OctetStr is less than or equal to another OctetStr.

     int operator <= ( const OctetStr &lhs, const char * string);

Test if one OctetStr is less than or equal to a char *.

     int operator > ( const OctetStr& lhs, const OctetStr &rhs);

Test if one OctetStr is greater than another OctetStr.

     int operator > ( const OctetStr& lhs, const char * string);

Test if one OctetStr is greater than a char *.

     int operator >= ( const OctetStr& lhs, const OctetStr &rhs);

Test if one OctetStr is greater than or equal to another OctetStr.

     int operator >= ( const OctetStr& lhs, const char *);

Test if one OctetStr is greater than or equal to a char *.

    OctetStr& operator +=( const char * string);

Concatenate a string onto an OctetStr;

    OctetStr& operator +=( const unsigned char c);

Concatenate a single char onto an OctetStr.

    OctetStr& operator+=( const OctetStr &octetstr);

Concatenate a OctetStr object.

    unsigned char& operator[ ] ( int position i);

Allows array like access to an OctetStr.

Miscellaneous

 

     void set_data( const unsigned char *s, unsigned long l);

Set the data of an OctetStr using a pointer and length.

     int nCompare( const unsigned long n, const OctetStr &o);

Compare n elements from parameter o.

     unsigned long len();

Return the length of an OctetStr.

     int valid();

Return the validity of an OctetStr.

     unsigned char * data();

Returns pointer to internal data.

     char * get_printable();

Formats for output, calls hex dump if not ASCII.

     char * get_printable_hex();

Formats for output in hexadecimal format.

 

10.4.  Special features

When printing out an OctetStr object,  the char * or get_printable() member functions automatically invoke the get_printable_hex() member function if the octet string contains any character which is non ASCII. This allows the user to simply cast the OctetStr to a char * or fire the get_printable() member function and get nice output. The get_printable_hex() member function formats the OctetStr into a hexadecimal format.

10.5.  Some OctetStr Class Examples

// Octet Class Examples

#includeoctet.h

void octet_example()

{

   OctetStr octet1;                                                 // create an invalid un- initialized octet object

   OctetStr octet2( “Red Hook Extra Bitter Ale”);       // create an octet with a string

   OctetStr octet3( octet2);                                          // create an octet with another octet

   unsigned char raw_data[50];                                   // create some raw data

   OctetStr octet4( raw_data, 50);                                 // create an OctetStr using unsigned char data

  

   octet1 = “Sierra Nevada Pale Ale”;                         // assign one octet to another

   cout << octet1.get_printable();                                 // show octet1 as a null terminated string

   cout << octet4.get_printable_hex();                      // show octet4 as a hex string

   cout << (char *) octet1;                                             // same as get_printable()

   if ( octet1 == octet2)                                                    // compare two octets

      cout << “octet1 is equal to octet2”;

 

   octet2 += “WinterFest Ale”;                                    // concat a string to an Octet

   if ( octet2 >= octet3)

      cout << “octet2 greater than or equal to octet2”;

 

   octet2[4] = ‘b’;                                                              // modify an element of an OctetStr using [ ]’s

 

   cout << octet.len();                                                    // print out the length of an OctetStr

 

   unsigned char raw_data[100];

   octet1.set_data( raw_data, 100);                            // set the data of an to unsigned char data

 

   cout << (octet1.valid() )? “Octet1 is valid” : “Octet1 is Invalid”;  // get the validity of an OctetStr

  

  

 

};  // end octet example