Merge branch 'release-2.140.101'
[feisty_meow.git] / nucleus / applications / utilities / bytedump.cpp
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
19 #include <application/hoople_main.h>
20 #include <basis/astring.h>
21 #include <filesystem/byte_filer.h>
22 #include <loggers/console_logger.h>
23 #include <structures/static_memory_gremlin.h>
24 #include <textual/byte_formatter.h>
25
26 using namespace application;
27 using namespace basis;
28 using namespace loggers;
29 using namespace filesystem;
30 using namespace structures;
31 using namespace textual;
32
33 ///HOOPLE_STARTUP_CODE;
34
35 const int MAXIMUM_BYTEDUMP_BUFFER_SIZE = 32768;
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
42 int print_instructions_and_exit(char *program_name)
43 {
44   console.log(astring(astring::SPRINTF, "\n\
45 Usage:\n\t%s filename [filename]\n\n\
46 Prints out (on standard output) a abyte dump of the files specified.\n\n",
47       program_name), ALWAYS_PRINT);
48   return 12;
49 }
50
51 int main(int argc, char *argv[])
52 {
53   SETUP_CONSOLE_LOGGER;
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       }
84       abyte buff[MAXIMUM_BYTEDUMP_BUFFER_SIZE + 10];
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