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

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

#1 fix handling

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