27 #ifdef DEBUG_BYTE_FORMAT
28 #define LOG(s) printf("%s\n", astring(s).s())
35 using namespace basis;
40 void byte_formatter::print_char(
abyte to_print,
astring &out,
char replace)
42 int temp = to_print % 128;
43 if (!parser_bits::is_printable_ascii(to_print)) out += replace;
44 else out += char(temp);
47 void byte_formatter::print_chars(
const abyte *to_print,
int len,
astring &out,
char replace)
49 for (
int i = 0; i < len; i++)
50 print_char(to_print[i], out, replace);
56 while (thresh >= 0x10) {
67 text_dump(to_return, location, length, label, eol);
74 text_dump(output, to_dump.
observe(), to_dump.
length(), label, eol);
80 text_dump(output, to_dump.
observe(), to_dump.
length(), label, eol);
94 for (
basis::un_int i = 0; i < length; i += entries_per_line) {
95 make_eight(i + label, to_return);
98 if (i + j >= length) {
102 int ord_of_current_char = *(location + i + j) & 0xFF;
103 if (ord_of_current_char < 0x10) to_return +=
'0';
111 if (k >= length) to_return +=
' ';
113 else print_char(*(location + k), to_return);
115 to_return +=
astring(
" |") + eol;
124 for (
int i = 0; i < dumped_form.
length(); i++) {
125 int indy = dumped_form.
find(
'\n', i);
130 if (i < dumped_form.
length() - 1) {
138 lines_found += dumped_form.
substring(i, indy - 1);
142 for (
int j = 0; j < lines_found.
length(); j++) {
146 int bar_one = s.
find(
'|', 0);
150 int bar_two = s.
find(
'|', bar_one + 1);
154 string_to_bytes(s2, this_part);
155 bytes_found += this_part;
161 void byte_formatter::bytes_to_string(
const abyte *to_convert,
int length,
astring &as_string,
162 bool space_delimited)
164 if (!to_convert || !length)
return;
170 if (space_delimited) pattern +=
" ";
173 for (
int i = 0; i < length; i++)
179 bool byte_formatter::in_hex_range(
char to_check)
182 return ( (to_check <=
'9') && (to_check >=
'0') )
183 || ( (to_check <=
'f') && (to_check >=
'a') )
184 || ( (to_check <=
'F') && (to_check >=
'A') );
187 void byte_formatter::string_to_bytes(
const char *to_convert,
byte_array &as_array)
190 const int len = int(strlen(to_convert));
193 enum states { FINDING_HEX, IGNORING_JUNK };
194 states state = IGNORING_JUNK;
200 for (
int i = 0; i < len; i++) {
202 case IGNORING_JUNK: {
203 if (in_hex_range(to_convert[i])) {
214 as_array +=
abyte(accumulator);
218 state = IGNORING_JUNK;
225 accumulator += string_manipulation::char_to_hex(to_convert[i]);
228 if (!in_hex_range(to_convert[i+1])) {
236 state = IGNORING_JUNK;
245 as_array +=
abyte(accumulator);
250 bool space_delimited)
252 bytes_to_string(to_convert.
observe(), to_convert.
length(), as_string,
257 { string_to_bytes(to_convert.
s(), as_array); }
261 #ifdef DEBUG_BYTE_FORMAT
262 FUNCDEF(
"bytes_to_shifted_string");
266 for (i = 0; i < splitter.
bits(); i += 7) {
268 for (
int j = i; j < i + 7; j++) {
270 if (j < splitter.
bits())
273 as_string += char(curr);
275 #ifdef DEBUG_BYTE_FORMAT
283 #ifdef DEBUG_BYTE_FORMAT
284 FUNCDEF(
"shifted_string_to_bytes");
288 for (
int i = 0; i < to_convert.
length(); i++) {
293 for (
int j = 0; j < 7; j++) {
296 abyte set_here = current & 0x80;
298 accumulator.
set_bit(i * 7 + j,
bool(set_here));
302 int remainder = accumulator.
bits() % 8;
303 accumulator.
resize(accumulator.
bits() - remainder);
306 #ifdef DEBUG_BYTE_FORMAT
309 if (accumulator.
bits() % 8)
310 deadly_error(
"byte_formatter", func,
"number of bits is erroneous.");
314 for (
int q = 0; q < accumulator.
bits() / 8; q++)
315 as_array += accumref[q];
317 #ifdef DEBUG_BYTE_FORMAT
a_sprintf is a specialization of astring that provides printf style support.
void reset(int number=0, const contents *initial_contents=NULL_POINTER)
Resizes this array and sets the contents from an array of contents.
const contents * observe() const
Returns a pointer to the underlying C array of data.
int length() const
Returns the current reported length of the allocated C array.
Provides a dynamically resizable ASCII character string.
const char * s() const
synonym for observe. the 's' stands for "string", if that helps.
bool substring(astring &target, int start, int end) const
a version that stores the substring in an existing "target" string.
int length() const
Returns the current length of the string.
int find(char to_find, int position=0, bool reverse=false) const
Locates "to_find" in "this".
A very common template for a dynamic array of bytes.
An array of bits with operations for manipulating and querying individual bits.
bool on(int position) const
returns true if the bit at "position" is set.
int bits() const
returns the number of bits in the vector.
void set_bit(int position, bool value)
sets the bit at "position" to a particular "value".
void resize(int size)
Changes the size of the bit_vector to "size" bits.
An array of strings with some additional helpful methods.
#define deadly_error(c, f, i)
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
The guards collection helps in testing preconditions and reporting errors.
unsigned char abyte
A fairly important unit which is seldom defined...
unsigned int un_int
Abbreviated name for unsigned integers.
bool negative(const type &a)
negative returns true if "a" is less than zero.
A dynamic container class that holds any kind of object via pointers.