Subversion Repositories HomeAutomation

Rev

Rev 1916 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  *
  3.  *  Copyright (C) 2010  Mattias Runge
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License along
  16.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  17.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18.  *
  19.  */
  20.  
  21. #include "Byteset.h"
  22.  
  23. #include "string.h"
  24.  
  25. #include <boost/lexical_cast.hpp>
  26.  
  27. namespace atom {
  28. namespace common {
  29.  
  30. Byteset::Byteset(unsigned int max_size)
  31. {
  32.     this->max_size_ = max_size;
  33.     this->bytes_ = new unsigned char[this->max_size_];
  34.    
  35.     this->Clear();
  36. }
  37.  
  38. Byteset::Byteset(const Byteset& set)
  39. {
  40.     this->max_size_ = set.size_;
  41.     this->size_ = this->max_size_;
  42.     this->bytes_ = new unsigned char[this->max_size_];
  43.  
  44.     memcpy(this->bytes_, set.bytes_, this->max_size_);
  45. }
  46.  
  47. Byteset::Byteset(std::string str)
  48. {
  49.     this->max_size_ = str.size() + 1;
  50.     this->size_ = this->max_size_;
  51.     this->bytes_ = new unsigned char[this->max_size_];
  52.    
  53.     memcpy(this->bytes_, str.data(), this->max_size_);
  54. }
  55.  
  56. Byteset::Byteset(std::string str, bool no_null_termination)
  57. {
  58.     this->max_size_ = str.size() + (no_null_termination ? 0 : 1);
  59.     this->size_ = this->max_size_;
  60.     this->bytes_ = new unsigned char[this->max_size_];
  61.    
  62.     memcpy(this->bytes_, str.data(), this->max_size_);
  63. }
  64.  
  65. Byteset::~Byteset()
  66. {
  67.     delete [] this->bytes_;
  68. }
  69.  
  70. unsigned char* Byteset::Get() const
  71. {
  72.     return this->bytes_;
  73. }
  74.  
  75. std::string Byteset::ToCharString()
  76. {
  77.     return std::string((char*)this->bytes_, this->size_);
  78. }
  79.  
  80. std::string Byteset::ToDebugString()
  81. {
  82.     std::string debug_string;
  83.    
  84.     for (unsigned int n = 0; n < this->size_; n++)
  85.     {
  86.         debug_string += boost::lexical_cast<std::string>((unsigned int)this->bytes_[n]) + ",";
  87.     }
  88.    
  89.     return debug_string;
  90. }
  91.  
  92. unsigned char& Byteset::operator[](unsigned int index)
  93. {
  94.     if (this->size_ < index + 1)
  95.     {
  96.         this->size_ = index + 1;
  97.     }
  98.    
  99.     return this->bytes_[index];
  100. }
  101.  
  102. unsigned int Byteset::GetMaxSize() const
  103. {
  104.     return this->max_size_;
  105. }
  106.  
  107. unsigned int Byteset::GetSize() const
  108. {
  109.     return this->size_;
  110. }
  111.  
  112. void Byteset::SetSize(unsigned int size)
  113. {
  114.     this->size_ = size;
  115. }
  116.  
  117. void Byteset::Append(unsigned char byte)
  118. {
  119.     this->bytes_[this->size_] = byte;
  120.     this->size_++;
  121. }
  122.  
  123. void Byteset::Clear()
  124. {
  125.     memset(this->bytes_, 0, this->max_size_);
  126.     this->size_ = 0;
  127. }
  128.  
  129. void Byteset::Fill(char c)
  130. {
  131.     memset(this->bytes_, c, this->max_size_);
  132.     this->size_ = 0;
  133. }
  134.    
  135. }; // namespace type
  136. }; // namespace atom
  137.