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

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

#1 remove unused interface

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::void_t cf::bin::resize(const cf::size_t size)
43{
44 mBin.resize(size);
45}
46
47cf::void_t cf::bin::set(const cf::uint8_t * in,
48 const cf::size_t length)
49{
50 if (!in)
51 return;
52
53 resize(length);
54 memcpy(buffer(), in, length);
55}
56
57cf::void_t cf::bin::append(const cf::uint8_t * in,
58 const cf::size_t appendedSize)
59{
60 const cf::size_t position = size();
61
62 resize(size() + appendedSize);
63 memcpy(&buffer()[position], in, appendedSize);
64}
65
66cf::void_t cf::bin::append(const cf::bin & in)
67{
68 append(in.buffer(), in.size());
69}
70
71cf::bool_t cf::bin::equal(const cf::bin & in) const
72{
73 if (size() != in.size())
74 return false;
75
76 if (size() > 0)
77 {
78 if (memcmp(buffer(), in.buffer(), size()))
79 return false;
80 }
81
82 return true;
83}
84
85cf::bin & cf::bin::operator =(const cf::bin & in)
86{
87 mBin = in.mBin;
88 return *this;
89}
90
91cf::bin & cf::bin::operator =(const std::string & in)
92{
93 mBin.assign(in.begin(), in.end());
94 return *this;
95}
96
97cf::void_t cf::bin::operator +=(const cf::bin & in)
98{
99 append(in);
100}
101
102cf::bin cf::bin::operator +(const cf::bin & in) const
103{
104 bin out(in);
105
106 out.append(in);
107
108 return out;
109}
110
111cf::bool_t cf::bin::operator ==(const cf::bin & in) const
112{
113 return equal(in);
114}
115
116cf::bool_t cf::bin::operator !=(const cf::bin & in) const
117{
118 return !equal(in);
119}
120
121cf::uint8_t cf::bin::operator [](const cf::size_t index) const
122{
123 if (index > size())
124 THROW_EXCEPTION("invalid index");
125
126 return mBin[index];
127}
128
129cf::size_t cf::bin::find(const cf::uint8_t * in,
130 const cf::size_t length) const
131{
132 cf::size_t limit = size() - length;
133
134 if (limit < 0 || !in)
135 return -1;
136
137 for (cf::size_t iter = 0 ; iter <= limit ; iter++)
138 {
139 if (!memcmp(buffer() + iter, in, length))
140 return iter;
141 }
142
143 return -1;
144}
145
146std::string cf::bin::toString() const
147{
148 return std::string(mBin.begin(), mBin.end());
149}
150
151cf::void_t cf::bin::print(const cf::char_t * prefix,
152 const cf::size_t limit,
153 FILE * fp) const
154{
155 cf::size_t i, j;
156 cf::size_t realsize = size() < limit ? size() : limit;
157
158 fprintf(fp, "[bin][%s] %lu bytes\n", prefix, size());
159 for (i = 0 ; i < realsize ; i += 16)
160 {
161 fprintf(fp, "%06lx : ", (cf::size_t)i);
162
163 for (j = 0; j < 16; j++)
164 {
165 if (i + j < realsize)
166 fprintf(fp, "%02x ", buffer()[i + j]);
167 else
168 fprintf(fp, " ");
169 }
170 fprintf(fp, " ");
171
172 for (j = 0 ; j < 16 ; j++)
173 {
174#define IS_READABLE_CHAR(__c) (' ' <= __c && __c <= '~')
175 if (i+j < realsize)
176 fprintf(fp, "%c", IS_READABLE_CHAR(buffer()[i + j]) ? buffer()[i + j] : '.');
177 }
178 fprintf(fp, "\n");
179 }
180}
Note: See TracBrowser for help on using the repository browser.