/*****************************************************************************\
*                                                                             *
*  Name   : dump_bytes                                                        *
*  Author : Chris Koeritz                                                     *
*                                                                             *
*  Purpose:                                                                   *
*                                                                             *
*    Prints the files specified out in terms of their hex bytes.              *
*                                                                             *
*******************************************************************************
* Copyright (c) 1991-$now By Author.  This program is free software; you can  *
* redistribute it and/or modify it under the terms of the GNU General Public  *
* License as published by the Free Software Foundation; either version 2 of   *
* the License or (at your option) any later version.  This is online at:      *
*     http://www.fsf.org/copyleft/gpl.html                                    *
* Please send any updates to: fred@gruntose.com                               *
\*****************************************************************************/

#include <basis/istring.h>
#include <opsystem/byte_filer.h>
#include <loggers/console_logger.h>
#include <data_struct/static_memory_gremlin.h>
#include <textual/byte_format.h>

HOOPLE_STARTUP_CODE;

const int MAXIMUM_BYTEDUMP_BUFFER_SIZE = 32768;
  // this is the size of the chunk we read from the files at a time.  it is
  // important to make this a multiple of 16, since that's the size of the line
  // we use in the byte dumping.

#define console program_wide_logger()

int print_instructions_and_exit(char *program_name)
{
  console.log(istring(istring::SPRINTF, "\n\
Usage:\n\t%s filename [filename]\n\n\
Prints out (on standard output) a byte dump of the files specified.\n\n",
      program_name).s());
  return 12;
}

int main(int argc, char *argv[])
{
  SET_DEFAULT_CONSOLE_LOGGER;

  if (argc <= 1) return print_instructions_and_exit(argv[0]);
  else {
    int current_parameter = 0;
/*
    if (argv[1][0] == '-') {
      if (argv[1][1] == 't') {
        current_parameter++;
        open_file_as_text = true;
      } else if (argv[1][1] == 'b') {
        current_parameter++;
        open_file_as_text = false;
      } else print_instructions_and_exit(argv[0]);
    }
*/
    bool past_first_file = false;
    while (++current_parameter < argc) {
      if (past_first_file) {
        // we're into the second file so start using some white space.
        console.log("");
        console.log("");
      }
      past_first_file = true;  // set condition for next time.
      istring name = argv[current_parameter];
      byte_filer current(name, "rb");
      if (!current.good()) {
        console.log(istring("Cannot find the file named \"") + name
            + istring("\"."));
        continue;
      }
      byte buff[MAXIMUM_BYTEDUMP_BUFFER_SIZE + 10];
        // buffer plus some extra room.
      bool printed_header = false;
      int current_label = 0;
      while (true) {
        int bytes_read = current.read(buff, MAXIMUM_BYTEDUMP_BUFFER_SIZE);
        if (bytes_read <= 0) break;  // no contents.
//console.log(istring(istring::SPRINTF, "read %d bytes", bytes_read));
        if (!printed_header) {
          console.log(name + ":");
          console.log("");  // blank line.
          printed_header = true;
        }
        istring to_log = byte_format::text_dump(buff, bytes_read,
            current_label);
        if (to_log[to_log.end()] == '\n')
          to_log.zap(to_log.end(), to_log.end());
        console.log(to_log);
        current_label += bytes_read;
      }
    }
  }
  return 0;
}

#ifdef __BUILD_STATIC_APPLICATION__
  // static dependencies found by buildor_gen_deps.sh:
  #include <basis/array.cpp>
  #include <basis/byte_array.cpp>
  #include <basis/callstack_tracker.cpp>
  #include <basis/chaos.cpp>
  #include <basis/convert_utf.cpp>
  #include <basis/definitions.cpp>
  #include <basis/earth_time.cpp>
  #include <basis/guards.cpp>
  #include <basis/istring.cpp>
  #include <basis/log_base.cpp>
  #include <basis/memory_checker.cpp>
  #include <basis/mutex.cpp>
  #include <basis/object_base.cpp>
  #include <basis/outcome.cpp>
  #include <basis/packable.cpp>
  #include <basis/portable.cpp>
  #include <basis/sequence.cpp>
  #include <basis/set.cpp>
  #include <basis/utility.cpp>
  #include <basis/version_record.cpp>
  #include <data_struct/amorph.cpp>
  #include <data_struct/bit_vector.cpp>
  #include <data_struct/byte_hasher.cpp>
  #include <data_struct/configurator.cpp>
  #include <data_struct/hash_table.cpp>
  #include <data_struct/pointer_hash.cpp>
  #include <data_struct/stack.cpp>
  #include <data_struct/static_memory_gremlin.cpp>
  #include <data_struct/string_hash.cpp>
  #include <data_struct/string_hasher.cpp>
  #include <data_struct/string_table.cpp>
  #include <data_struct/symbol_table.cpp>
  #include <data_struct/table_configurator.cpp>
  #include <loggers/console_logger.cpp>
  #include <loggers/file_logger.cpp>
  #include <loggers/locked_logger.cpp>
  #include <loggers/null_logger.cpp>
  #include <loggers/program_wide_logger.cpp>
  #include <opsystem/byte_filer.cpp>
  #include <opsystem/command_line.cpp>
  #include <opsystem/critical_events.cpp>
  #include <opsystem/directory.cpp>
  #include <opsystem/filename.cpp>
  #include <opsystem/ini_config.cpp>
  #include <opsystem/ini_parser.cpp>
  #include <opsystem/path_configuration.cpp>
  #include <opsystem/rendezvous.cpp>
  #include <textual/byte_format.cpp>
  #include <textual/parser_bits.cpp>
  #include <textual/string_manipulation.cpp>
  #include <textual/tokenizer.cpp>
#endif // __BUILD_STATIC_APPLICATION__

