source: libcf++/trunk/src/bin.cpp@ 11

Last change on this file since 11 was 11, checked in by cheese, 9 years ago

#1 add new interfaces for bin and task

File size: 3.0 KB
Line 
1/**
2 * @file bin.cpp
3 * @author myusgun@gmail.com
4 * @brief bin
5 */
6#include "cf/bin.h"
7
8#include <stdlib.h>
9#include <string.h>
10
11cf::bin::bin(const cf::uint8_t * in,
12 const cf::size_t length)
13{
14 set(in, length);
15}
16
17cf::bin::bin(const std::string & in)
18 : mBin(in.begin(), in.end())
19{
20}
21
22cf::bin::bin(const bin & in)
23 : mBin(in.mBin)
24{
25}
26
27cf::void_t cf::bin::clear()
28{
29 mBin.clear();
30}
31
32cf::uint8_t * cf::bin::buffer() const
33{
34 return const_cast<cf::uint8_t *>(&mBin[0]);
35}
36
37cf::size_t cf::bin::size() const
38{
39 return mBin.size();
40}
41
42cf::bool_t cf::bin::empty() const
43{
44 return mBin.empty();
45}
46
47cf::void_t cf::bin::resize(const cf::size_t size)
48{
49 mBin.resize(size);
50}
51
52cf::void_t cf::bin::set(const cf::uint8_t * in,
53 const cf::size_t length)
54{
55 if (!in)
56 return;
57
58 resize(length);
59 memcpy(buffer(), in, length);
60}
61
62cf::void_t cf::bin::append(const cf::uint8_t * in,
63 const cf::size_t appendedSize)
64{
65 const cf::size_t position = size();
66
67 resize(size() + appendedSize);
68 memcpy(&buffer()[position], in, appendedSize);
69}
70
71cf::void_t cf::bin::append(const cf::bin & in)
72{
73 append(in.buffer(), in.size());
74}
75
76cf::bool_t cf::bin::equal(const cf::bin & in) const
77{
78 if (size() != in.size())
79 return false;
80
81 if (size() > 0)
82 {
83 if (memcmp(buffer(), in.buffer(), size()))
84 return false;
85 }
86
87 return true;
88}
89
90cf::bin & cf::bin::operator =(const cf::bin & in)
91{
92 mBin = in.mBin;
93 return *this;
94}
95
96cf::bin & cf::bin::operator =(const std::string & in)
97{
98 mBin.assign(in.begin(), in.end());
99 return *this;
100}
101
102cf::void_t cf::bin::operator +=(const cf::bin & in)
103{
104 append(in);
105}
106
107cf::bin cf::bin::operator +(const cf::bin & in) const
108{
109 bin out(in);
110
111 out.append(in);
112
113 return out;
114}
115
116cf::bool_t cf::bin::operator ==(const cf::bin & in) const
117{
118 return equal(in);
119}
120
121cf::bool_t cf::bin::operator !=(const cf::bin & in) const
122{
123 return !equal(in);
124}
125
126cf::uint8_t cf::bin::operator [](const cf::size_t index) const
127{
128 if (index > size())
129 THROW_EXCEPTION("invalid index");
130
131 return mBin[index];
132}
133
134cf::size_t cf::bin::find(const cf::uint8_t * in,
135 const cf::size_t length) const
136{
137 cf::size_t limit = size() - length;
138
139 if (limit < 0 || !in)
140 return -1;
141
142 for (cf::size_t iter = 0 ; iter <= limit ; iter++)
143 {
144 if (!memcmp(buffer() + iter, in, length))
145 return iter;
146 }
147
148 return -1;
149}
150
151std::string cf::bin::toString() const
152{
153 return std::string(mBin.begin(), mBin.end());
154}
155
156cf::void_t cf::bin::print(const cf::char_t * prefix,
157 const cf::size_t limit,
158 FILE * fp) const
159{
160 cf::size_t i, j;
161 cf::size_t realsize = size() < limit ? size() : limit;
162
163 fprintf(fp, "[bin][%s] %lu bytes\n", prefix, size());
164 for (i = 0 ; i < realsize ; i += 16)
165 {
166 fprintf(fp, "%06lx : ", (cf::size_t)i);
167
168 for (j = 0; j < 16; j++)
169 {
170 if (i + j < realsize)
171 fprintf(fp, "%02x ", buffer()[i + j]);
172 else
173 fprintf(fp, " ");
174 }
175 fprintf(fp, " ");
176
177 for (j = 0 ; j < 16 ; j++)
178 {
179#define IS_READABLE_CHAR(__c) (' ' <= __c && __c <= '~')
180 if (i+j < realsize)
181 fprintf(fp, "%c", IS_READABLE_CHAR(buffer()[i + j]) ? buffer()[i + j] : '.');
182 }
183 fprintf(fp, "\n");
184 }
185}
Note: See TracBrowser for help on using the repository browser.