⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 properties.cpp

📁 mysql-5.0.22.tar.gz源码包
💻 CPP
📖 第 1 页 / 共 2 页
字号:
Uint32 PropertiesImpl::getTotalItems() const {  int ret = 0;  for(unsigned int i = 0; i<items; i++)    if(content[i]->valueType == PropertiesType_Properties){      ret += ((Properties*)content[i]->value)->impl->getTotalItems();    } else {      ret ++;    }  return ret;}const char * PropertiesImpl::getProps(const char * name, 			 const PropertiesImpl ** impl) const {  const char * ret = name;  const char * tmp = strchr(name, Properties::delimiter);  if(tmp == 0){    * impl = this;    return ret;  } else {    Uint32 sz = tmp - name;    char * tmp2 = (char*)malloc(sz + 1);    memcpy(tmp2, name, sz);    tmp2[sz] = 0;    PropertyImpl * nvp = get(tmp2);    free(tmp2);    if(nvp == 0){      * impl = 0;      return 0;    }    if(nvp->valueType != PropertiesType_Properties){      * impl = 0;      return name;    }    return ((Properties*)nvp->value)->impl->getProps(tmp+1, impl);  }}const char * PropertiesImpl::getPropsPut(const char * name, 			    PropertiesImpl ** impl) {  const char * ret = name;  const char * tmp = strchr(name, Properties::delimiter);  if(tmp == 0){    * impl = this;    return ret;  } else {    Uint32 sz = tmp - name;    char * tmp2 = (char*)malloc(sz + 1);    memcpy(tmp2, name, sz);    tmp2[sz] = 0;        PropertyImpl * nvp = get(tmp2);    if(nvp == 0){      Properties   * tmpP  = new Properties();      PropertyImpl * tmpPI = new PropertyImpl(tmp2, tmpP);      PropertyImpl * nvp = put(tmpPI);      delete tmpP;      free(tmp2);      return ((Properties*)nvp->value)->impl->getPropsPut(tmp+1, impl);    }    free(tmp2);    if(nvp->valueType != PropertiesType_Properties){      * impl = 0;      return name;    }    return ((Properties*)nvp->value)->impl->getPropsPut(tmp+1, impl);  }}intmod4(unsigned int i){  int res = i + (4 - (i % 4));  return res;}Uint32PropertiesImpl::getPackedSize(Uint32 pLen) const {  Uint32 sz = 0;  for(unsigned int i = 0; i<items; i++){    if(content[i]->valueType == PropertiesType_Properties){      Properties * p = (Properties*)content[i]->value;      sz += p->impl->getPackedSize(pLen+strlen(content[i]->name)+1);    } else {       sz += 4; // Type      sz += 4; // Name Len      sz += 4; // Value Len      sz += mod4(pLen + strlen(content[i]->name)); // Name      switch(content[i]->valueType){      case PropertiesType_char:	sz += mod4(strlen((char *)content[i]->value));	break;      case PropertiesType_Uint32:	sz += mod4(4);	break;      case PropertiesType_Uint64:	sz += mod4(8);	break;      case PropertiesType_Properties:      default:	assert(0);      }    }  }  return sz;}struct CharBuf {  char * buffer;  Uint32 bufLen;  Uint32 contentLen;  CharBuf(){    buffer     = 0;    bufLen     = 0;    contentLen = 0;  }  ~CharBuf(){    free(buffer);  }    void clear() { contentLen = 0;}  bool add(const char * str, Uint32 strLen){    if(!expand(contentLen + strLen + 1))      return false;    memcpy(&buffer[contentLen], str, strLen);    contentLen += strLen;    buffer[contentLen] = 0;    return true;  }    bool add(char c){    return add(&c, 1);  }  bool expand(Uint32 newSize){    if(newSize >= bufLen){            char * tmp = (char*)malloc(newSize + 1024);      memset(tmp, 0, newSize + 1024);      if(tmp == 0)	return false;      if(contentLen > 0)	memcpy(tmp, buffer, contentLen);      if(buffer != 0)	free(buffer);      buffer = tmp;      bufLen = newSize + 1024;    }    return true;  }};boolPropertiesImpl::pack(Uint32 *& buf, const char * prefix, Uint32 pLen) const {  CharBuf charBuf;    for(unsigned int i = 0; i<items; i++){    const int strLenName      = strlen(content[i]->name);        if(content[i]->valueType == PropertiesType_Properties){      charBuf.clear();      if(!charBuf.add(prefix, pLen)){	properties->setErrno(E_PROPERTIES_ERROR_MALLOC_WHILE_PACKING,			     errno);	return false;      }            if(!charBuf.add(content[i]->name, strLenName)){	properties->setErrno(E_PROPERTIES_ERROR_MALLOC_WHILE_PACKING,			     errno);	return false;      }      if(!charBuf.add(Properties::delimiter)){	properties->setErrno(E_PROPERTIES_ERROR_MALLOC_WHILE_PACKING,			     errno);	return false;      }            if(!((Properties*)(content[i]->value))->impl->pack(buf, 							 charBuf.buffer,							 charBuf.contentLen)){		return false;      }      continue;    }        Uint32 valLenData  = 0;    Uint32 valLenWrite = 0;    Uint32 sz = 4 + 4 + 4 + mod4(pLen + strLenName);    switch(content[i]->valueType){    case PropertiesType_Uint32:      valLenData  = 4;      break;    case PropertiesType_Uint64:      valLenData  = 8;      break;    case PropertiesType_char:      valLenData  = strlen((char *)content[i]->value);      break;    case PropertiesType_Properties:      assert(0);    }    valLenWrite = mod4(valLenData);    sz += valLenWrite;        * (buf + 0) = htonl(content[i]->valueType);    * (buf + 1) = htonl(pLen + strLenName);    * (buf + 2) = htonl(valLenData);    char * valBuf  = (char*)(buf + 3);    char * nameBuf = (char*)(buf + 3 + (valLenWrite / 4));        memset(valBuf, 0, sz-12);    switch(content[i]->valueType){    case PropertiesType_Uint32:      * (Uint32 *)valBuf = htonl(* (Uint32 *)content[i]->value);      break;    case PropertiesType_Uint64:{      Uint64 val =  * (Uint64 *)content[i]->value;      Uint32 hi = (val >> 32);      Uint32 lo = (val & 0xFFFFFFFF);      * (Uint32 *)valBuf = htonl(hi);      * (Uint32 *)(valBuf + 4) = htonl(lo);    }      break;    case PropertiesType_char:      memcpy(valBuf, content[i]->value, strlen((char*)content[i]->value));      break;    case PropertiesType_Properties:      assert(0);    }    if(pLen > 0)      memcpy(nameBuf, prefix, pLen);    memcpy(nameBuf + pLen, content[i]->name, strLenName);        buf += (sz / 4);  }  return true;}boolPropertiesImpl::unpack(const Uint32 * buf, Uint32 &bufLen, Properties * top,		       int _items){  CharBuf charBuf;  while(_items > 0){    Uint32 tmp[3];         if(bufLen <= 12){      top->setErrno(E_PROPERTIES_BUFFER_TO_SMALL_WHILE_UNPACKING);      return false;    }    tmp[0] = ntohl(buf[0]);    tmp[1] = ntohl(buf[1]);    tmp[2] = ntohl(buf[2]);    buf    += 3;    bufLen -= 12;    PropertiesType pt   = (PropertiesType)tmp[0];    Uint32 nameLen      = tmp[1];    Uint32 valueLen     = tmp[2];    Uint32 nameLenRead  = mod4(nameLen);    Uint32 valueLenRead = mod4(valueLen);    Uint32 sz = nameLenRead + valueLenRead;    if(bufLen < sz){      top->setErrno(E_PROPERTIES_BUFFER_TO_SMALL_WHILE_UNPACKING);      return false;    }    if(!charBuf.expand(sz)){      top->setErrno(E_PROPERTIES_ERROR_MALLOC_WHILE_UNPACKING, errno);      return false;    }    memcpy(charBuf.buffer, buf, sz);    buf    += (sz / 4);    bufLen -= sz ;    char * valBuf  = charBuf.buffer;    char * nameBuf = charBuf.buffer + valueLenRead;        nameBuf[nameLen] = 0;    valBuf[valueLen] = 0;        bool res3 = false;    switch(pt){    case PropertiesType_Uint32:      res3 = top->put(nameBuf, ntohl(* (Uint32 *)valBuf), true);      break;    case PropertiesType_Uint64:{      Uint64 hi = ntohl(* (Uint32 *)valBuf);      Uint64 lo = ntohl(* (Uint32 *)(valBuf + 4));      res3 = top->put64(nameBuf, (hi << 32) + lo, true);    }      break;    case PropertiesType_char:      res3 = top->put(nameBuf, valBuf, true);      break;    case PropertiesType_Properties:      assert(0);    }    if(!res3){      return false;    }    _items--;  }  return true;}PropertyImpl::~PropertyImpl(){  free((char*)name);  switch(valueType){  case PropertiesType_Uint32:    delete (Uint32 *)value;    break;  case PropertiesType_Uint64:    delete (Uint64 *)value;    break;  case PropertiesType_char:    free((char *)value);    break;  case PropertiesType_Properties:    delete (Properties *)value;    break;  }}PropertyImpl *PropertyImpl::copyPropertyImpl(const PropertyImpl & org){  switch(org.valueType){  case PropertiesType_Uint32:    return new PropertyImpl(org.name, * (Uint32 *)org.value);  case PropertiesType_Uint64:    return new PropertyImpl(org.name, * (Uint64 *)org.value);    break;  case PropertiesType_char:    return new PropertyImpl(org.name, (char *)org.value);    break;  case PropertiesType_Properties:    return new PropertyImpl(org.name, (Properties *)org.value);    break;  default:    assert(0);  }  return 0;}PropertyImpl::PropertyImpl(const char * _name, Uint32 _value){  this->name = f_strdup(_name);  this->value = new Uint32;  * ((Uint32 *)this->value) = _value;  this->valueType = PropertiesType_Uint32;}PropertyImpl::PropertyImpl(const char * _name, Uint64 _value){  this->name = f_strdup(_name);  this->value = new Uint64;  * ((Uint64 *)this->value) = _value;  this->valueType = PropertiesType_Uint64;}PropertyImpl::PropertyImpl(const char * _name, const char * _value){  this->name = f_strdup(_name);  this->value = f_strdup(_value);  this->valueType = PropertiesType_char;}PropertyImpl::PropertyImpl(const char * _name, const Properties * _value){  this->name = f_strdup(_name);  this->value = new Properties(* _value);  this->valueType = PropertiesType_Properties;}const Uint32 E_PROPERTIES_OK                                      = 0;const Uint32 E_PROPERTIES_INVALID_NAME                            = 1;const Uint32 E_PROPERTIES_NO_SUCH_ELEMENT                         = 2;const Uint32 E_PROPERTIES_INVALID_TYPE                            = 3;const Uint32 E_PROPERTIES_ELEMENT_ALREADY_EXISTS                  = 4;const Uint32 E_PROPERTIES_ERROR_MALLOC_WHILE_PACKING              = 5;const Uint32 E_PROPERTIES_INVALID_VERSION_WHILE_UNPACKING         = 6;const Uint32 E_PROPERTIES_INVALID_BUFFER_TO_SHORT                 = 7;const Uint32 E_PROPERTIES_ERROR_MALLOC_WHILE_UNPACKING            = 8;const Uint32 E_PROPERTIES_INVALID_CHECKSUM                        = 9;const Uint32 E_PROPERTIES_BUFFER_TO_SMALL_WHILE_UNPACKING         = 10;/** * These are methods that used to be inline * * But Diab 4.1f could not compile -release with to many inlines */voidProperties::setErrno(Uint32 pErr, Uint32 osErr) const {  if(parent != 0){    parent->setErrno(pErr, osErr);    return ;  }    /**   * propErrno & osErrno used to be mutable,   * but diab didn't know what mutable meant.   */  *((Uint32*)&propErrno) = pErr;  *((Uint32*)&osErrno)   = osErr;}/** * Inlined get/put(name, no, ...) - methods */ boolProperties::put(const char * name, Uint32 no, Uint32 val, bool replace){  size_t tmp_len = strlen(name)+20;  char * tmp = (char*)malloc(tmp_len);  BaseString::snprintf(tmp, tmp_len, "%s_%d", name, no);  bool res = put(tmp, val, replace);  free(tmp);  return res;}boolProperties::put64(const char * name, Uint32 no, Uint64 val, bool replace){  size_t tmp_len = strlen(name)+20;  char * tmp = (char*)malloc(tmp_len);  BaseString::snprintf(tmp, tmp_len, "%s_%d", name, no);  bool res = put(tmp, val, replace);  free(tmp);  return res;}bool Properties::put(const char * name, Uint32 no, const char * val, bool replace){  size_t tmp_len = strlen(name)+20;  char * tmp = (char*)malloc(tmp_len);  BaseString::snprintf(tmp, tmp_len, "%s_%d", name, no);  bool res = put(tmp, val, replace);  free(tmp);  return res;}bool Properties::put(const char * name, Uint32 no, const Properties * val, 		bool replace){  size_t tmp_len = strlen(name)+20;  char * tmp = (char*)malloc(tmp_len);  BaseString::snprintf(tmp, tmp_len, "%s_%d", name, no);  bool res = put(tmp, val, replace);  free(tmp);  return res;}bool Properties::getTypeOf(const char * name, Uint32 no, 		      PropertiesType * type) const {  size_t tmp_len = strlen(name)+20;  char * tmp = (char*)malloc(tmp_len);  BaseString::snprintf(tmp, tmp_len, "%s_%d", name, no);  bool res = getTypeOf(tmp, type);  free(tmp);  return res;}bool Properties::contains(const char * name, Uint32 no) const {  size_t tmp_len = strlen(name)+20;  char * tmp = (char*)malloc(tmp_len);  BaseString::snprintf(tmp, tmp_len, "%s_%d", name, no);  bool res = contains(tmp);  free(tmp);  return res;}bool Properties::get(const char * name, Uint32 no, Uint32 * value) const{  size_t tmp_len = strlen(name)+20;  char * tmp = (char*)malloc(tmp_len);  BaseString::snprintf(tmp, tmp_len, "%s_%d", name, no);  bool res = get(tmp, value);  free(tmp);  return res;}bool Properties::get(const char * name, Uint32 no, Uint64 * value) const{  size_t tmp_len = strlen(name)+20;  char * tmp = (char*)malloc(tmp_len);  BaseString::snprintf(tmp, tmp_len, "%s_%d", name, no);  bool res = get(tmp, value);  free(tmp);  return res;}bool Properties::get(const char * name, Uint32 no, const char ** value) const {  size_t tmp_len = strlen(name)+20;  char * tmp = (char*)malloc(tmp_len);  BaseString::snprintf(tmp, tmp_len, "%s_%d", name, no);  bool res = get(tmp, value);  free(tmp);  return res;}bool Properties::get(const char * name, Uint32 no, const Properties ** value) const{  size_t tmp_len = strlen(name)+20;  char * tmp = (char*)malloc(tmp_len);  BaseString::snprintf(tmp, tmp_len, "%s_%d", name, no);  bool res = get(tmp, value);  free(tmp);  return res;}bool Properties::getCopy(const char * name, Uint32 no, char ** value) const {  size_t tmp_len = strlen(name)+20;  char * tmp = (char*)malloc(tmp_len);  BaseString::snprintf(tmp, tmp_len, "%s_%d", name, no);  bool res = getCopy(tmp, value);  free(tmp);  return res;}bool Properties::getCopy(const char * name, Uint32 no, Properties ** value) const {  size_t tmp_len = strlen(name)+20;  char * tmp = (char*)malloc(tmp_len);  BaseString::snprintf(tmp, tmp_len, "%s_%d", name, no);  bool res = getCopy(tmp, value);  free(tmp);  return res;}voidProperties::setCaseInsensitiveNames(bool value){  impl->setCaseInsensitiveNames(value);}boolProperties::getCaseInsensitiveNames() const {  return impl->m_insensitive;}template bool put(PropertiesImpl *, const char *, Uint32, bool);template bool put(PropertiesImpl *, const char *, Uint64, bool);template bool put(PropertiesImpl *, const char *, const char *, bool);template bool put(PropertiesImpl *, const char *, const Properties*, bool);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -