1 /*_############################################################################
6 _## -----------------------------------------------
7 _## Copyright (c) 2001-2010 Jochen Katz, Frank Fock
9 _## This software is based on SNMP++2.6 from Hewlett Packard:
11 _## Copyright (c) 1996
12 _## Hewlett-Packard Company
14 _## ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
15 _## Permission to use, copy, modify, distribute and/or sell this software
16 _## and/or its documentation is hereby granted without fee. User agrees
17 _## to display the above copyright notice and this license notice in all
18 _## copies of the software and any documentation of the software. User
19 _## agrees to assume all liability for the use of the software;
20 _## Hewlett-Packard and Jochen Katz make no representations about the
21 _## suitability of this software for any purpose. It is provided
22 _## "AS-IS" without warranty of any kind, either express or implied. User
23 _## hereby grants a royalty-free license to any and all derivatives based
24 _## upon this software code base.
26 _## Stuttgart, Germany, Thu Sep 2 00:07:47 CEST 2010
28 _##########################################################################*/
29 /*===================================================================
32 Hewlett-Packard Company
34 ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
35 Permission to use, copy, modify, distribute and/or sell this software
36 and/or its documentation is hereby granted without fee. User agrees
37 to display the above copyright notice and this license notice in all
38 copies of the software and any documentation of the software. User
39 agrees to assume all liability for the use of the software; Hewlett-Packard
40 makes no representations about the suitability of this software for any
41 purpose. It is provided "AS-IS" without warranty of any kind,either express
42 or implied. User hereby grants a royalty-free license to any and all
43 derivatives based upon this software code base.
47 SMI INTEGER CLASS IMPLEMTATION
49 DESIGN + AUTHOR: Jeff Meyer
52 Class implemtation for SMI Integer classes.
53 =====================================================================*/
54 char integer_cpp_version[]="#(@) SNMP++ $Id: integer.cpp 1542 2009-05-29 11:38:48Z katz $";
56 #include <stdio.h> // for sprintf()
57 #include "snmp_pp/integer.h" // header file for gauge class
59 #ifdef SNMP_PP_NAMESPACE
63 // constructor no value
64 SnmpUInt32::SnmpUInt32() : valid_flag(true), m_changed(true)
66 smival.value.uNumber = 0;
67 smival.syntax = sNMP_SYNTAX_UINT32;
70 // constructor with value
71 SnmpUInt32::SnmpUInt32 (const unsigned long i)
72 : valid_flag(true), m_changed(true)
74 smival.value.uNumber = i;
75 smival.syntax = sNMP_SYNTAX_UINT32;
79 SnmpUInt32::SnmpUInt32(const SnmpUInt32 &c) : valid_flag(true), m_changed(true)
81 smival.value.uNumber=c.smival.value.uNumber;
82 smival.syntax = sNMP_SYNTAX_UINT32;
85 // overloaded assignment
86 SnmpUInt32& SnmpUInt32::operator=(const unsigned long i)
88 smival.value.uNumber = i;
94 // general assignment from any Value
95 SnmpSyntax& SnmpUInt32::operator=(const SnmpSyntax &in_val)
97 if (this == &in_val) return *this; // handle assignement from itself
99 valid_flag = false; // will get set true if really valid
102 switch (in_val.get_syntax())
104 case sNMP_SYNTAX_UINT32:
105 // case sNMP_SYNTAX_GAUGE32: .. indistinquishable from UINT32
106 case sNMP_SYNTAX_CNTR32:
107 case sNMP_SYNTAX_TIMETICKS:
108 case sNMP_SYNTAX_INT32: // implied cast int -> uint
109 smival.value.uNumber =
110 ((SnmpUInt32 &)in_val).smival.value.uNumber;
119 // overloaded assignment
120 SnmpUInt32& SnmpUInt32::operator=(const SnmpUInt32 &uli)
122 if (this == &uli) return *this; // check for self assignment
124 smival.value.uNumber = uli.smival.value.uNumber;
125 valid_flag = uli.valid_flag;
130 // ASCII format return
131 const char *SnmpUInt32::get_printable() const
133 if (m_changed == false) return output_buffer;
135 SnmpUInt32 *nc_this = PP_CONST_CAST(SnmpUInt32*, this);
136 sprintf(nc_this->output_buffer, "%lu", smival.value.uNumber);
138 nc_this->m_changed = false;
140 return output_buffer;
143 // Return the space needed for serialization
144 int SnmpUInt32::get_asn1_length() const
146 if (smival.value.uNumber < 0x80)
148 else if (smival.value.uNumber < 0x8000)
150 else if (smival.value.uNumber < 0x800000)
152 else if (smival.value.uNumber < 0x80000000)
157 //====================================================================
158 // INT 32 Implementation
159 //====================================================================
161 // constructor no value
162 SnmpInt32::SnmpInt32() : valid_flag(true), m_changed(true)
164 smival.value.sNumber = 0;
165 smival.syntax = sNMP_SYNTAX_INT32;
168 // constructor with value
169 SnmpInt32::SnmpInt32(const long i) : valid_flag(true), m_changed(true)
171 smival.value.sNumber = i;
172 smival.syntax = sNMP_SYNTAX_INT32;
175 // constructor with value
176 SnmpInt32::SnmpInt32(const SnmpInt32 &c) : valid_flag(true), m_changed(true)
178 smival.value.sNumber = c.smival.value.sNumber;
179 smival.syntax = sNMP_SYNTAX_INT32;
182 // overloaded assignment
183 SnmpInt32& SnmpInt32::operator=(const long i)
185 smival.value.sNumber = (unsigned long) i;
191 // overloaded assignment
192 SnmpInt32& SnmpInt32::operator=(const SnmpInt32 &uli)
194 if (this == &uli) return *this; // check for self assignment
196 smival.value.sNumber = uli.smival.value.sNumber;
197 valid_flag = uli.valid_flag;
202 // general assignment from any Value
203 SnmpSyntax& SnmpInt32::operator=(const SnmpSyntax &in_val)
205 if (this == &in_val) return *this; // handle assignement from itself
207 valid_flag = false; // will get set true if really valid
210 switch (in_val.get_syntax())
212 case sNMP_SYNTAX_INT32:
213 case sNMP_SYNTAX_UINT32: // implied cast uint -> int
214 // case sNMP_SYNTAX_GAUGE32: .. indistinquishable from UINT32
215 case sNMP_SYNTAX_CNTR32: // implied cast uint -> int
216 case sNMP_SYNTAX_TIMETICKS: // implied cast uint -> int
217 smival.value.sNumber =
218 ((SnmpInt32 &)in_val).smival.value.sNumber;
227 // ASCII format return
228 const char *SnmpInt32::get_printable() const
230 if (m_changed == false) return output_buffer;
232 SnmpInt32 *nc_this = PP_CONST_CAST(SnmpInt32*, this);
233 sprintf(nc_this->output_buffer, "%ld", (long)smival.value.sNumber);
235 nc_this->m_changed = false;
237 return output_buffer;
240 // Return the space needed for serialization
241 int SnmpInt32::get_asn1_length() const
243 if ((smival.value.sNumber < 0x80) &&
244 (smival.value.sNumber >= -0x80))
246 else if ((smival.value.sNumber < 0x8000) &&
247 (smival.value.sNumber >= -0x8000))
249 else if ((smival.value.sNumber < 0x800000) &&
250 (smival.value.sNumber >= -0x800000))
255 #ifdef SNMP_PP_NAMESPACE
256 }; // end of namespace Snmp_pp