]> git.stg.codes - ssmd.git/blob - 3rdparty/snmp++/src/integer.cpp
Initial adding
[ssmd.git] / 3rdparty / snmp++ / src / integer.cpp
1 /*_############################################################################
2   _## 
3   _##  integer.cpp  
4   _##
5   _##  SNMP++v3.2.25
6   _##  -----------------------------------------------
7   _##  Copyright (c) 2001-2010 Jochen Katz, Frank Fock
8   _##
9   _##  This software is based on SNMP++2.6 from Hewlett Packard:
10   _##  
11   _##    Copyright (c) 1996
12   _##    Hewlett-Packard Company
13   _##  
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. 
25   _##  
26   _##  Stuttgart, Germany, Thu Sep  2 00:07:47 CEST 2010 
27   _##  
28   _##########################################################################*/
29 /*===================================================================
30
31   Copyright (c) 1999
32   Hewlett-Packard Company
33
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.
44
45   I N T E G E R . C P P
46
47   SMI INTEGER CLASS IMPLEMTATION
48
49   DESIGN + AUTHOR: Jeff Meyer
50
51   DESCRIPTION:
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 $";
55
56 #include <stdio.h>             // for sprintf()
57 #include "snmp_pp/integer.h"   // header file for gauge class
58
59 #ifdef SNMP_PP_NAMESPACE
60 namespace Snmp_pp {
61 #endif
62
63 // constructor no value
64 SnmpUInt32::SnmpUInt32() : valid_flag(true), m_changed(true)
65 {
66   smival.value.uNumber = 0;
67   smival.syntax = sNMP_SYNTAX_UINT32;
68 }
69
70 // constructor with value
71 SnmpUInt32::SnmpUInt32 (const unsigned long i)
72   : valid_flag(true), m_changed(true)
73 {
74   smival.value.uNumber = i;
75   smival.syntax = sNMP_SYNTAX_UINT32;
76 }
77
78 // copy constructor
79 SnmpUInt32::SnmpUInt32(const SnmpUInt32 &c) : valid_flag(true), m_changed(true)
80 {
81   smival.value.uNumber=c.smival.value.uNumber;
82   smival.syntax = sNMP_SYNTAX_UINT32;
83 }
84
85 // overloaded assignment
86 SnmpUInt32& SnmpUInt32::operator=(const unsigned long i)
87 {
88   smival.value.uNumber = i;
89   valid_flag = true;
90   m_changed = true;
91   return *this;
92 }
93
94 // general assignment from any Value
95 SnmpSyntax& SnmpUInt32::operator=(const SnmpSyntax &in_val)
96 {
97   if (this == &in_val) return *this; // handle assignement from itself
98
99   valid_flag = false;           // will get set true if really valid
100   if (in_val.valid())
101   {
102     switch (in_val.get_syntax())
103     {
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;
111           valid_flag = true;
112           break;
113     }
114   }
115   m_changed = true;
116   return *this;
117 }
118
119 // overloaded assignment
120 SnmpUInt32& SnmpUInt32::operator=(const SnmpUInt32 &uli)
121 {
122   if (this == &uli) return *this;  // check for self assignment
123
124   smival.value.uNumber = uli.smival.value.uNumber;
125   valid_flag = uli.valid_flag;
126   m_changed = true;
127   return *this;
128 }
129
130 // ASCII format return
131 const char *SnmpUInt32::get_printable() const
132 {
133   if (m_changed == false) return output_buffer;
134
135   SnmpUInt32 *nc_this = PP_CONST_CAST(SnmpUInt32*, this);
136   sprintf(nc_this->output_buffer, "%lu", smival.value.uNumber);
137
138   nc_this->m_changed = false;
139
140   return output_buffer;
141 }
142
143 // Return the space needed for serialization
144 int SnmpUInt32::get_asn1_length() const
145 {
146   if (smival.value.uNumber < 0x80)
147     return 3;
148   else if (smival.value.uNumber < 0x8000)
149     return 4;
150   else if (smival.value.uNumber < 0x800000)
151     return 5;
152   else if (smival.value.uNumber < 0x80000000)
153     return 6;
154   return 7;
155 }
156
157 //====================================================================
158 //  INT 32 Implementation
159 //====================================================================
160
161 // constructor no value
162 SnmpInt32::SnmpInt32() : valid_flag(true), m_changed(true)
163 {
164   smival.value.sNumber = 0;
165   smival.syntax = sNMP_SYNTAX_INT32;
166 }
167
168 // constructor with value
169 SnmpInt32::SnmpInt32(const long i) : valid_flag(true), m_changed(true)
170 {
171   smival.value.sNumber = i;
172   smival.syntax = sNMP_SYNTAX_INT32;
173 }
174
175 // constructor with value
176 SnmpInt32::SnmpInt32(const SnmpInt32 &c) : valid_flag(true), m_changed(true)
177 {
178   smival.value.sNumber = c.smival.value.sNumber;
179   smival.syntax = sNMP_SYNTAX_INT32;
180 }
181
182 // overloaded assignment
183 SnmpInt32& SnmpInt32::operator=(const long i)
184 {
185   smival.value.sNumber = (unsigned long) i;
186   valid_flag = true;
187   m_changed = true;
188   return *this;
189 }
190
191 // overloaded assignment
192 SnmpInt32& SnmpInt32::operator=(const SnmpInt32 &uli)
193 {
194   if (this == &uli) return *this;  // check for self assignment
195
196   smival.value.sNumber = uli.smival.value.sNumber;
197   valid_flag = uli.valid_flag;
198   m_changed = true;
199   return *this;
200 }
201
202 // general assignment from any Value
203 SnmpSyntax& SnmpInt32::operator=(const SnmpSyntax &in_val)
204 {
205   if (this == &in_val) return *this; // handle assignement from itself
206
207   valid_flag = false;           // will get set true if really valid
208   if (in_val.valid())
209   {
210     switch (in_val.get_syntax())
211     {
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;
219           valid_flag = true;
220           break;
221     }
222   }
223   m_changed = true;
224   return *this;
225 }
226
227 // ASCII format return
228 const char *SnmpInt32::get_printable() const
229 {
230   if (m_changed == false) return output_buffer;
231
232   SnmpInt32 *nc_this = PP_CONST_CAST(SnmpInt32*, this);
233   sprintf(nc_this->output_buffer, "%ld", (long)smival.value.sNumber);
234
235   nc_this->m_changed = false;
236
237   return output_buffer;
238 }
239
240 // Return the space needed for serialization
241 int SnmpInt32::get_asn1_length() const
242 {
243   if ((smival.value.sNumber <   0x80) &&
244       (smival.value.sNumber >= -0x80))
245     return 3;
246   else if ((smival.value.sNumber <   0x8000) &&
247            (smival.value.sNumber >= -0x8000))
248     return 4;
249   else if ((smival.value.sNumber <   0x800000) &&
250            (smival.value.sNumber >= -0x800000))
251     return 5;
252   return 6;
253 }
254
255 #ifdef SNMP_PP_NAMESPACE
256 }; // end of namespace Snmp_pp
257 #endif