oid.cpp
来自「JdonFramework need above jdk 1.4.0 This」· C++ 代码 · 共 767 行 · 第 1/2 页
CPP
767 行
}}//===============[Oid::operator += const unsigned int) ]====================// append operator, appends an int//Oid& Oid::operator+=(const unsigned long i){ Oid other(&i, 1); (*this) += other; return *this;}//===============[Oid::operator += const Oid) ]========================// append operator, appends an Oid//// 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 Oid &o){ SmiLPUINT32 new_oid; if (o.smival.value.oid.len == 0) return *this; new_oid = (SmiLPUINT32) new unsigned long[smival.value.oid.len + o.smival.value.oid.len]; if (new_oid == 0) { delete_oid_ptr(); return *this; } if (smival.value.oid.ptr) { MEMCPY((SmiLPBYTE) new_oid, (SmiLPBYTE) smival.value.oid.ptr, (size_t) (smival.value.oid.len*sizeof(SmiUINT32))); delete [] smival.value.oid.ptr; } // out with the old, in with the new... smival.value.oid.ptr = new_oid; MEMCPY((SmiLPBYTE) &new_oid[smival.value.oid.len], (SmiLPBYTE) o.smival.value.oid.ptr, (size_t) (o.smival.value.oid.len*sizeof(SmiUINT32))); smival.value.oid.len += o.smival.value.oid.len; m_changed = true; return *this;}//==============[Oid::get_printable(unsigned int start, n) ]=============// return a dotted string starting at start,// going n positions to the left// NOTE, start is 1 based (the first id is at position #1)const char *Oid::get_printable(const unsigned long start, const unsigned long n, char *&buffer) const{ if (!m_changed && (buffer == iv_str)) return buffer; unsigned long nz; unsigned long my_start = start - 1; unsigned long my_end = my_start + n; nz = (smival.value.oid.len * (SNMPCHARSIZE + 1)) + 1; if (buffer) delete [] buffer; // delete the previous output string buffer = new char[nz]; // allocate some space for the output string if (buffer == 0) return 0; buffer[0] = 0; // init the string // cannot ask for more than there is.. if ((my_start < 0) || (my_end > smival.value.oid.len)) return buffer; char *cur_ptr = buffer; bool first = true; // loop through and build up a string for (unsigned long index = my_start; index < my_end; ++index) { // if not at begin, pad with a dot if (first) first = false; else *cur_ptr++ = '.'; // convert data element to a string cur_ptr += sprintf(cur_ptr, "%lu", smival.value.oid.ptr[index]); } if (buffer == iv_str) { Oid *nc_this = PP_CONST_CAST(Oid*, this); nc_this->m_changed = false; } return buffer;}//=============[Oid::StrToOid(char *string, SmiLPOID dst) ]==============// convert a string to an oidint Oid::StrToOid(const char *str, SmiLPOID dstOid) const{ unsigned int index = 0; // make a temp buffer to copy the data into first SmiLPUINT32 temp; unsigned int nz; if (str && *str) { nz = SAFE_UINT_CAST(strlen(str)); } else { dstOid->len = 0; dstOid->ptr = 0; return -1; } temp = (SmiLPUINT32) new unsigned long[nz]; if (temp == 0) return -1; // return if can't get the mem while ((*str) && (index < nz)) { // skip over the dot if (*str == '.') ++str; // convert digits if (my_isdigit(*str)) { unsigned long number = 0; // grab a digit token and convert it to a long int while (my_isdigit(*str)) number = (number * 10) + *(str++) - '0'; // stuff the value into the array and bump the counter temp[index++] = number; // there must be a dot or end of string now if ((*str) && (*str != '.')) { delete [] temp; return -1; } } // check for other chars if ((*str) && (*str != '.')) { // found String -> converting it into an oid if (*str != '$') { delete [] temp; return -1; } // skip $ ++str; // copy until second $ while ((*str) && (*str != '$')) { temp[index] = (unsigned char)*str; ++str; ++index; } if (*str != '$') { delete [] temp; return -1; } // skip over the $ ++str; // there must be a dot or end of string now if ((*str) && (*str != '.')) { delete [] temp; return -1; } } } // get some space for the real oid dstOid->ptr = (SmiLPUINT32) new unsigned long[index]; // return if can't get the mem needed if(dstOid->ptr == 0) { delete [] temp; return -1; } // copy in the temp data MEMCPY((SmiLPBYTE) dstOid->ptr, (SmiLPBYTE) temp, (size_t) (index*sizeof(SmiUINT32))); // set the len of the oid dstOid->len = index; // free up temp data delete [] temp; return (int) index;}//===============[Oid::OidCopy(source, destination) ]====================// Copy an oidint Oid::OidCopy(SmiLPOID srcOid, SmiLPOID dstOid) const{ // check source len ! zero if (srcOid->len == 0) return -1; // copy source to destination MEMCPY((SmiLPBYTE) dstOid->ptr, (SmiLPBYTE) srcOid->ptr, (size_t) (srcOid->len*sizeof(SmiUINT32))); //set the new len dstOid->len = srcOid->len; return (int) srcOid->len;}//===============[Oid::nCompare(n, Oid) ]=================================// compare the n leftmost values of two oids (left-to_right )//// self == Oid then return 0, they are equal// self < Oid then return -1, <// self > Oid then return 1, >int Oid::nCompare(const unsigned long n, const Oid &o) const{ unsigned long length = n; bool reduced_len = false; // If both oids are too short, decrease len while ((smival.value.oid.len < length) && (o.smival.value.oid.len < length)) length--; if (length == 0) return 0; // equal // only compare for the minimal length if (length > smival.value.oid.len) { length = smival.value.oid.len; reduced_len = true; } if (length > o.smival.value.oid.len) { length = o.smival.value.oid.len; reduced_len = true; } unsigned long z = 0; while (z < length) { if (smival.value.oid.ptr[z] < o.smival.value.oid.ptr[z]) return -1; // less than if (smival.value.oid.ptr[z] > o.smival.value.oid.ptr[z]) return 1; // greater than ++z; } // if we truncated the len then these may not be equal if (reduced_len) { if (smival.value.oid.len < o.smival.value.oid.len) return -1; if (smival.value.oid.len > o.smival.value.oid.len) return 1; } return 0; // equal}//================[Oid::OidToStr ]=========================================// convert an oid to a stringint Oid::OidToStr(const SmiOID *srcOid, SmiUINT32 size, char *str) const{ unsigned totLen = 0; char szNumber[SNMPBUFFSIZE]; int cur_len; str[0] = 0; // init the string // verify there is something to copy if (srcOid->len == 0) return -1; // loop through and build up a string for (unsigned long index = 0; index < srcOid->len; ++index) { // convert data element to a string cur_len = sprintf(szNumber, "%lu", srcOid->ptr[index]); // verify len is not over if (totLen + cur_len + 1 >= size) return -2; // if not at begin, pad with a dot if (totLen) str[totLen++] = '.'; // copy the string token into the main string STRCPY(str + totLen, szNumber); // adjust the total len totLen += cur_len; } return totLen+1;}//================[ general Value = operator ]========================SnmpSyntax& Oid::operator=(const SnmpSyntax &val){ if (this == &val) return *this; // protect against assignment from self delete_oid_ptr(); // assign new value if (val.valid()) { switch (val.get_syntax()) { case sNMP_SYNTAX_OID: set_data(((Oid &)val).smival.value.oid.ptr, (unsigned int)((Oid &)val).smival.value.oid.len); break; } } return *this;}int Oid::get_asn1_length() const{ int length = 1; // for first 2 subids for (unsigned int i = 2; i < smival.value.oid.len; ++i) { unsigned long v = smival.value.oid.ptr[i]; if (v < 0x80) // 7 bits long subid length += 1; else if (v < 0x4000) // 14 bits long subid length += 2; else if (v < 0x200000) // 21 bits long subid length += 3; else if (v < 0x10000000) // 28 bits long subid length += 4; else // 32 bits long subid length += 5; } if (length < 128) return length + 2; else if (length < 256) return length + 3; return length + 4;}#ifdef SNMP_PP_NAMESPACE}; // end of namespace Snmp_pp#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?