CUBRID Engine  latest
string_buffer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * string_buffer.cpp
21  */
22 
23 #include "string_buffer.hpp"
24 
25 #include <algorithm> // for std::min
26 #include <memory.h>
27 
28 void string_buffer::add_bytes (size_t len, const char *bytes)
29 {
30  assert (bytes != NULL);
31  m_ext_block.extend_to (m_len + len + 2);
32  memcpy (m_ext_block.get_ptr () + m_len, bytes, len);
33  m_len += len;
34  m_ext_block.get_ptr ()[m_len] = '\0';
35 }
36 
37 /*
38  * hex_dump : appends a buffer containing hex dump of input and optionally the ASCII content
39  *
40  * in : input string
41  * max_to_dump : maximum contents to dump from input
42  * line_size : number of characters to dump in each line (counting from input)
43  * print_ascii : if true, prints the ASCII content of input on the right of hex dump
44  */
45 void
46 string_buffer::hex_dump (const string_buffer &in, const size_t max_to_dump,
47  const size_t line_size, const bool print_ascii)
48 {
49  const char *buf = in.get_buffer ();
50  size_t buf_len = std::min (max_to_dump, in.len ());
51 
52  return hex_dump (buf, buf_len, line_size, print_ascii);
53 }
54 
55 void
56 string_buffer::hex_dump (const char *ptr, const size_t length, const size_t line_size, const bool print_ascii)
57 {
58  const char *ptr_line = ptr;
59 
60  this->operator() (" 0000: ");
61  for (size_t i = 0; i < length; i++)
62  {
63  this->operator() ("%02X ", (unsigned char) (*ptr++));
64  if (print_ascii == true
65  && (i % line_size == (line_size - 1) || i == length - 1))
66  {
67  const char *ptr_print;
68 
69  if (i % line_size != (line_size - 1))
70  {
71  std::string spaces (3 * (line_size - 1 - (i % line_size)), ' ');
72  this->operator() ("%s", spaces.c_str ());
73  }
74 
75  for (ptr_print = ptr_line; ptr_print < ptr; ptr_print++)
76  {
77  if (*ptr_print >= 32 && (unsigned char) *ptr_print < 128)
78  {
79  this->operator() ("%c", *ptr_print);
80  }
81  else
82  {
83  this->operator() (".");
84  }
85  }
86 
87  ptr_line += line_size;
88  }
89 
90  if (i % line_size == (line_size - 1) && i != length)
91  {
92  this->operator() ("\n %04d: ", i + 1);
93  }
94  }
95  this->operator() ("\n");
96 }
cubmem::extensible_block m_ext_block
int operator()(Args &&...args)
void hex_dump(const string_buffer &in, const size_t max_to_dump, const size_t line_size=16, const bool print_ascii=true)
#define assert(x)
#define min(a, b)
#define NULL
Definition: freelistheap.h:34
const char * get_buffer() const
size_t len() const
int i
Definition: dynamic_load.c:954
void extend_to(size_t total_bytes)
Definition: mem_block.hpp:346
void add_bytes(size_t len, const char *bytes)