feisty meow concerns codebase 2.140
bytedump.cpp
Go to the documentation of this file.
1/*****************************************************************************\
2* *
3* Name : dump_bytes *
4* Author : Chris Koeritz *
5* *
6* Purpose: *
7* *
8* Prints the files specified out in terms of their hex bytes. *
9* *
10*******************************************************************************
11* Copyright (c) 1991-$now By Author. This program is free software; you can *
12* redistribute it and/or modify it under the terms of the GNU General Public *
13* License as published by the Free Software Foundation; either version 2 of *
14* the License or (at your option) any later version. This is online at: *
15* http://www.fsf.org/copyleft/gpl.html *
16* Please send any updates to: fred@gruntose.com *
17\*****************************************************************************/
18
20#include <basis/astring.h>
25
26using namespace application;
27using namespace basis;
28using namespace loggers;
29using namespace filesystem;
30using namespace structures;
31using namespace textual;
32
34
36 // this is the size of the chunk we read from the files at a time. it is
37 // important to make this a multiple of 16, since that's the size of the line
38 // we use in the byte dumping.
39
40#define console program_wide_logger::get()
41
42int print_instructions_and_exit(char *program_name)
43{
45Usage:\n\t%s filename [filename]\n\n\
46Prints out (on standard output) a abyte dump of the files specified.\n\n",
47 program_name), ALWAYS_PRINT);
48 return 12;
49}
50
51int main(int argc, char *argv[])
52{
54
55 if (argc <= 1) return print_instructions_and_exit(argv[0]);
56 else {
57 int current_parameter = 0;
58/*
59 if (argv[1][0] == '-') {
60 if (argv[1][1] == 't') {
61 current_parameter++;
62 open_file_as_text = true;
63 } else if (argv[1][1] == 'b') {
64 current_parameter++;
65 open_file_as_text = false;
66 } else print_instructions_and_exit(argv[0]);
67 }
68*/
69 bool past_first_file = false;
70 while (++current_parameter < argc) {
71 if (past_first_file) {
72 // we're into the second file so start using some white space.
73 console.log(astring::empty_string(), ALWAYS_PRINT);
74 console.log(astring::empty_string(), ALWAYS_PRINT);
75 }
76 past_first_file = true; // set condition for next time.
77 astring name = argv[current_parameter];
78 byte_filer current(name, "rb");
79 if (!current.good()) {
80 console.log(astring("Cannot find the file named \"") + name + astring("\"."),
81 ALWAYS_PRINT);
82 continue;
83 }
85 // buffer plus some extra room.
86 bool printed_header = false;
87 int current_label = 0;
88 while (true) {
89 int bytes_read = current.read(buff, MAXIMUM_BYTEDUMP_BUFFER_SIZE);
90 if (bytes_read <= 0) break; // no contents.
91//console.log(astring(astring::SPRINTF, "read %d bytes", bytes_read));
92 if (!printed_header) {
93 console.log(name + ":", ALWAYS_PRINT);
94 console.log(astring::empty_string(), ALWAYS_PRINT); // blank line.
95 printed_header = true;
96 }
97 astring to_log = byte_formatter::text_dump(buff, bytes_read,
98 current_label);
99 if (to_log[to_log.end()] == '\n')
100 to_log.zap(to_log.end(), to_log.end());
101 console.log(to_log, ALWAYS_PRINT);
102 current_label += bytes_read;
103 }
104 }
105 }
106 return 0;
107}
108
#define console
Definition bytedump.cpp:40
int main(int argc, char *argv[])
Definition bytedump.cpp:51
int print_instructions_and_exit(char *program_name)
Definition bytedump.cpp:42
const int MAXIMUM_BYTEDUMP_BUFFER_SIZE
HOOPLE_STARTUP_CODE;.
Definition bytedump.cpp:35
Provides a dynamically resizable ASCII character string.
Definition astring.h:35
virtual void zap(int start, int end)
Deletes the characters between "start" and "end" inclusively.
Definition astring.cpp:524
static const astring & empty_string()
useful wherever empty strings are needed, e.g., function defaults.
Definition astring.cpp:128
int end() const
returns the index of the last (non-null) character in the string.
Definition astring.h:86
Provides file managment services using the standard I/O support.
Definition byte_filer.h:32
int read(basis::abyte *buffer, int buffer_size)
reads "buffer_size" bytes from the file into "buffer".
bool good()
returns true if the file seems to be in the appropriate desired state.
static void text_dump(basis::astring &output, const basis::abyte *location, basis::un_int length, basis::un_int label=0, const char *eol="\n")
prints out a block of memory in a human readable form.
#define SETUP_CONSOLE_LOGGER
< a macro that retasks the program-wide logger as a console_logger.
Provides macros that implement the 'main' program of an application.
Implements an application lock to ensure only one is running at once.
The guards collection helps in testing preconditions and reporting errors.
Definition array.h:30
unsigned char abyte
A fairly important unit which is seldom defined...
Definition definitions.h:51
A platform independent way to obtain the timestamp of a file.
A logger that sends to the console screen using the standard output device.
A dynamic container class that holds any kind of object via pointers.
Definition amorph.h:55