Subversion Repositories HomeAutomation

Rev

Rev 1642 | Rev 1952 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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