"Fossies" - the Fresh Open Source Software Archive

Member "opensaf-5.21.09/src/imm/immnd/ImmAttrValue.cc" (14 Sep 2021, 10499 Bytes) of package /linux/misc/opensaf-5.21.09.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ImmAttrValue.cc" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 5.21.06_vs_5.21.09.

    1 /*      -*- OpenSAF  -*-
    2  *
    3  * (C) Copyright 2008 The OpenSAF Foundation
    4  *
    5  * This program is distributed in the hope that it will be useful, but
    6  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    7  * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed
    8  * under the GNU Lesser General Public License Version 2.1, February 1999.
    9  * The complete license can be accessed from the following location:
   10  * http://opensource.org/licenses/lgpl-license.php
   11  * See the Copying file included with the OpenSAF distribution for full
   12  * licensing terms.
   13  *
   14  * Author(s): Ericsson AB
   15  *
   16  */
   17 
   18 #include "imm/immnd/ImmAttrValue.h"
   19 #include "string.h"
   20 #include "immnd.h"
   21 
   22 ImmAttrValue::ImmAttrValue() : mValue(0), mValueSize(0) {}
   23 
   24 ImmAttrValue::ImmAttrValue(const ImmAttrValue& b)
   25     : mValue(NULL), mValueSize(b.mValueSize) {
   26   if (mValueSize) {
   27     mValue = new char[b.mValueSize];
   28     (void)::memcpy(mValue, b.mValue, b.mValueSize);
   29   }
   30 }
   31 
   32 ImmAttrValue::~ImmAttrValue() {
   33   delete[] mValue;
   34   mValue = 0;
   35   mValueSize = 0;
   36 }
   37 
   38 void ImmAttrValue::printSimpleValue() const {
   39   // printf("ImmAttrValue::printSimpleValue size: %u %p\n", mValueSize, mValue);
   40 }
   41 
   42 void ImmAttrValue::print() const { this->printSimpleValue(); }
   43 
   44 ImmAttrValue& ImmAttrValue::operator=(const ImmAttrValue& b) {
   45   if (this != &b) {
   46     delete[] mValue;
   47 
   48     mValueSize = b.mValueSize;
   49     if (mValueSize) {
   50       mValue = new char[b.mValueSize];
   51       (void)::memcpy(mValue, b.mValue, b.mValueSize);
   52     }
   53   }
   54 
   55   return *this;
   56 }
   57 
   58 void ImmAttrValue::setValue(const IMMSV_OCTET_STRING& in) {
   59   if (mValue) {
   60     if ((in.size == mValueSize) && (memcmp(mValue, in.buf, mValueSize) == 0)) {
   61       return;
   62     }  // Already equal
   63     delete[] mValue;
   64     mValue = 0;
   65   }
   66 
   67   mValueSize = (size_t)in.size;
   68   if (mValueSize) {
   69     mValue = new char[mValueSize];
   70     (void)::memcpy(mValue, in.buf, mValueSize);
   71   }
   72 }
   73 
   74 void ImmAttrValue::discardValues() {
   75   if (mValue) {
   76     delete[] mValue;
   77     mValue = 0;
   78     mValueSize = 0;
   79   }
   80 }
   81 
   82 void ImmAttrValue::setValue_int(int i) {
   83   if (mValue && mValueSize != sizeof(int)) {
   84     delete[] mValue;
   85     mValue = 0;
   86     mValueSize = 0;
   87   }
   88 
   89   if (!mValue) {
   90     mValueSize = sizeof(int);
   91     mValue = new char[mValueSize];
   92   }
   93 
   94   *((int*)mValue) = i;
   95 }
   96 
   97 int ImmAttrValue::getValue_int() const {
   98   if (mValueSize != sizeof(int)) {
   99     return 0;
  100   }
  101 
  102   return *((int*)mValue);
  103 }
  104 
  105 void ImmAttrValue::setValue_satimet(SaTimeT i) {
  106   if (mValue && mValueSize != sizeof(SaTimeT)) {
  107     delete[] mValue;
  108     mValue = 0;
  109     mValueSize = 0;
  110   }
  111 
  112   if (!mValue) {
  113     mValueSize = sizeof(SaTimeT);
  114     mValue = new char[mValueSize];
  115   }
  116   *(reinterpret_cast<SaTimeT*>(mValue)) = i;
  117 }
  118 
  119 SaTimeT ImmAttrValue::getValue_satimet() const {
  120   if (mValueSize != sizeof(SaTimeT)) {
  121     return 0;
  122   }
  123 
  124   return *(reinterpret_cast<SaTimeT*>(mValue));
  125 }
  126 
  127 void ImmAttrValue::setValueC_str(const char* str) {
  128   if (mValue) {
  129     if (str) {
  130       if (strncmp(mValue, str, mValueSize) == 0) {
  131         return;
  132       }  // Already equal
  133     }
  134     delete[] mValue;
  135     mValue = 0;
  136     mValueSize = 0;
  137   }
  138 
  139   if (str) {
  140     mValueSize = (unsigned int)strlen(str) + 1;
  141     mValue = new char[mValueSize];
  142     strncpy(mValue, str, mValueSize);
  143   }
  144 }
  145 
  146 const char* ImmAttrValue::getValueC_str() const { return mValue; }
  147 
  148 void ImmAttrValue::copyValueToEdu(IMMSV_EDU_ATTR_VAL* out,
  149                                   SaImmValueTypeT t) const {
  150   if (!mValueSize) {
  151     memset(out, 0, sizeof(IMMSV_EDU_ATTR_VAL));
  152     return;
  153   }
  154 
  155   osafassert(mValue);
  156 
  157   switch (t) {
  158     case SA_IMM_ATTR_SAINT32T:
  159       out->val.saint32 = *((SaInt32T*)mValue);
  160       return;
  161     case SA_IMM_ATTR_SAUINT32T:
  162       out->val.sauint32 = *((SaUint32T*)mValue);
  163       return;
  164     case SA_IMM_ATTR_SAINT64T:
  165       out->val.saint64 = *((SaInt64T*)mValue);
  166       return;
  167     case SA_IMM_ATTR_SAUINT64T:
  168       out->val.sauint64 = *((SaUint64T*)mValue);
  169       return;
  170     case SA_IMM_ATTR_SATIMET:
  171       out->val.satime = *((SaTimeT*)mValue);
  172       return;
  173     case SA_IMM_ATTR_SAFLOATT:
  174       out->val.safloat = *((SaFloatT*)mValue);
  175       return;
  176     case SA_IMM_ATTR_SADOUBLET:
  177       out->val.sadouble = *((SaDoubleT*)mValue);
  178       return;
  179 
  180     case SA_IMM_ATTR_SASTRINGT:
  181     case SA_IMM_ATTR_SAANYT:
  182     case SA_IMM_ATTR_SANAMET:
  183       out->val.x.size = mValueSize;
  184       out->val.x.buf = (char*)malloc(mValueSize);
  185       memcpy(out->val.x.buf, mValue, mValueSize);
  186 
  187       break;
  188 
  189     default:
  190       abort();
  191   }
  192 }
  193 
  194 unsigned int ImmAttrValue::extraValues() const  // virtual
  195 {
  196   return 0;
  197 }
  198 
  199 bool ImmAttrValue::isMultiValued() const  // virtual
  200 {
  201   return false;
  202 }
  203 
  204 void ImmAttrValue::removeValue(const IMMSV_OCTET_STRING& match)  // virtual
  205 {
  206   if ((mValueSize == match.size) &&
  207       (bcmp((const void*)mValue, (const void*)match.buf, mValueSize) == 0)) {
  208     this->discardValues();
  209   }
  210 }
  211 
  212 bool ImmAttrValue::hasMatchingValue(
  213     const IMMSV_OCTET_STRING& match) const  // virtual
  214 {
  215   if (mValueSize != match.size) return false;
  216 
  217   return bcmp((const void*)mValue, (const void*)match.buf, mValueSize) == 0;
  218 }
  219 
  220 bool ImmAttrValue::hasDuplicates() const  // virtual
  221 {
  222   return false; /* Single value can not have duplicates. */
  223 }
  224 
  225 ImmAttrMultiValue::ImmAttrMultiValue() : ImmAttrValue(), mNext(NULL) {}
  226 
  227 ImmAttrMultiValue::ImmAttrMultiValue(const ImmAttrMultiValue& b)
  228     : ImmAttrValue(b) {
  229   if (b.mNext) {
  230     mNext = new ImmAttrMultiValue(*(b.mNext));
  231   } else {
  232     mNext = NULL;
  233   }
  234 }
  235 
  236 ImmAttrMultiValue::ImmAttrMultiValue(const ImmAttrValue& b) : ImmAttrValue(b) {
  237   mNext = NULL;
  238 }
  239 
  240 ImmAttrMultiValue::~ImmAttrMultiValue() {
  241   if (mValue) {
  242     delete[] mValue;
  243     mValue = 0;
  244     mValueSize = 0;
  245   }
  246   if (mNext) {
  247     delete mNext;
  248     mNext = 0;
  249   }
  250 }
  251 
  252 ImmAttrMultiValue& ImmAttrMultiValue::operator=(const ImmAttrMultiValue& b) {
  253   if (this != &b) {
  254     delete[] mValue;
  255     mValue = 0;
  256     mValueSize = b.mValueSize;
  257     if (mValueSize) {
  258       mValue = new char[b.mValueSize];
  259       (void)::memcpy(mValue, b.mValue, b.mValueSize);
  260     }
  261   }
  262 
  263   if (mNext) {
  264     delete mNext;
  265     mNext = NULL;
  266   }
  267 
  268   if (b.mNext) {
  269     mNext = new ImmAttrMultiValue(*(b.mNext));
  270   }
  271   return *this;
  272 }
  273 
  274 void ImmAttrMultiValue::discardValues()  // virtual
  275 {
  276   if (mValue) {
  277     delete[] mValue;
  278     mValue = 0;
  279     mValueSize = 0;
  280   }
  281 
  282   if (mNext) {
  283     delete mNext;
  284     mNext = NULL;
  285   }
  286 }
  287 
  288 void ImmAttrMultiValue::removeValue(const IMMSV_OCTET_STRING& match)  // virtual
  289 {
  290   // Remove ALL matching occurences of the value `match'
  291   bool tryRemoveHead = true;
  292   do {
  293     while (!mValueSize && mNext) {  // Empty head => shift up an extra.
  294       ImmAttrMultiValue* tmp = mNext;
  295 
  296       mValue = tmp->mValue;
  297       tmp->mValue = NULL;
  298 
  299       mValueSize = tmp->mValueSize;
  300       tmp->mValueSize = 0;
  301 
  302       mNext = tmp->mNext;
  303       tmp->mNext = NULL;
  304       delete tmp;
  305     }
  306 
  307     if (mValueSize && (mValueSize == match.size) &&
  308         (bcmp((const void*)mValue, (const void*)match.buf, mValueSize) == 0)) {
  309       // match!
  310       delete[] mValue;
  311       mValue = 0;
  312       mValueSize = 0;
  313       // Head is now empty because it matched.
  314     } else {
  315       tryRemoveHead = false;
  316     }
  317 
  318   } while (tryRemoveHead);
  319 
  320   // We now have a head that does not match, possibly an empty head.
  321   // If head is empty then there can not be any tail.
  322   osafassert(mValueSize || !mNext);
  323 
  324   if (mNext) {
  325     mNext->removeValue(match);  // TODO: make non-recursive!
  326 
  327     if (!(mNext->mValueSize)) {
  328       ImmAttrMultiValue* tmp = mNext;
  329       mNext = tmp->mNext;
  330       tmp->mNext = NULL;
  331       delete tmp;
  332     }
  333   }
  334 }
  335 
  336 bool ImmAttrMultiValue::hasMatchingValue(
  337     const IMMSV_OCTET_STRING& match) const  // virtual
  338 {
  339   if ((mValueSize == match.size) &&
  340       bcmp((const void*)mValue, (const void*)match.buf, mValueSize) == 0) {
  341     return true;
  342   }
  343 
  344   // TODO: make non-recursive.
  345   return mNext ? (mNext->hasMatchingValue(match)) : false;
  346 }
  347 
  348 bool ImmAttrMultiValue::hasDuplicates() const  // virtual
  349 {
  350   IMMSV_OCTET_STRING match = {mValueSize, mValue};
  351 
  352   return mNext ? (mNext->hasMatchingValue(match) || mNext->hasDuplicates())
  353                : false;
  354 }
  355 
  356 // Note ImmAttrMultiValue::setValue(const IMMSV_OCTET_STRING& in)
  357 // is inherited non virtual from ImmAttrValue
  358 
  359 void ImmAttrMultiValue::setExtraValue(const IMMSV_OCTET_STRING& in) {
  360   // NOTE: values need not be unique, this is a "bag".
  361   ImmAttrMultiValue* next = new ImmAttrMultiValue();
  362   next->setValue(in);
  363   // Push the new value on over old (stack model)
  364   next->mNext = mNext;
  365   mNext = next;
  366 }
  367 
  368 void ImmAttrMultiValue::setExtraValueC_str(const char* str) {
  369   // NOTE: values need not be unique, this is a "bag".
  370   ImmAttrMultiValue* next = new ImmAttrMultiValue();
  371   next->setValueC_str(str);
  372   // Push the new value on over old (stack model)
  373   next->mNext = mNext;
  374   mNext = next;
  375 }
  376 
  377 bool ImmAttrMultiValue::hasExtraValueC_str(const char* str) const {
  378   const ImmAttrMultiValue* mval = this;
  379   while (mval) {
  380     if (strncmp(str, mval->mValue, mval->mValueSize) == 0) {
  381       return true;
  382     }
  383     mval = mval->mNext;
  384   }
  385   return false;
  386 }
  387 
  388 bool ImmAttrMultiValue::removeExtraValueC_str(const char* str) {
  389   ImmAttrMultiValue* mval = this;
  390   while (mval->mNext) {
  391     if (strncmp(str, mval->mNext->mValue, mval->mNext->mValueSize) == 0) {
  392       ImmAttrMultiValue* tmp = mval->mNext;
  393       mval->mNext = mval->mNext->mNext;
  394       tmp->mNext = NULL;
  395       delete tmp;
  396       return true;
  397     }
  398     mval = mval->mNext;
  399   }
  400   return false;
  401 }
  402 
  403 void ImmAttrMultiValue::copyExtraValuesToEdu(IMMSV_EDU_ATTR_VAL_LIST** amvOut,
  404                                              SaImmValueTypeT t) const {
  405   IMMSV_EDU_ATTR_VAL_LIST** prevp = amvOut;
  406   ImmAttrMultiValue* tmpNext = mNext;
  407   while (tmpNext) {
  408     *prevp =
  409         (IMMSV_EDU_ATTR_VAL_LIST*)calloc(1, sizeof(IMMSV_EDU_ATTR_VAL_LIST));
  410 
  411     tmpNext->copyValueToEdu(&((*prevp)->n), t);
  412     (*prevp)->next = NULL;
  413     prevp = &((*prevp)->next);
  414     tmpNext = tmpNext->mNext;
  415   }
  416 }
  417 
  418 unsigned int ImmAttrMultiValue::extraValues() const  // virtual
  419 {
  420   unsigned int extras = 0;
  421   ImmAttrMultiValue* mval = mNext;
  422   while (mval) {
  423     ++extras;
  424     mval = mval->mNext;
  425   }
  426   return extras;
  427 }
  428 
  429 bool ImmAttrMultiValue::isMultiValued() const  // virtual
  430 {
  431   return true;
  432 }
  433 
  434 void ImmAttrMultiValue::printMultiValue() const {
  435   this->printSimpleValue();
  436   // printf("ImmAttrMultiValue::printMultiValue() mNext: %p\n", mNext);
  437   if (mNext) mNext->printMultiValue();
  438 }
  439 
  440 void ImmAttrMultiValue::print() const { this->printMultiValue(); }