Commit f554d638 authored by thomas.forbriger's avatar thomas.forbriger
Browse files

[FEATURE] (libtfxx): provide hex dump function

parent 6269bb1e
tests/blitztest
tests/commandlinetest
tests/exceptiontest
tests/filetest
tests/fortraniotest
tests/fstest
tests/handletest
tests/misctest
tests/rangetest
tests/regextest
tests/stringtest
tests/fortranF77
......@@ -94,7 +94,7 @@ SRC=$(wildcard $(addsuffix /*.cc,$(DIRSTOINCLUDE)))
TESTS=tests/commandlinetest.cc tests/fortraniotest.cc \
tests/stringtest.cc tests/handletest.cc tests/regextest.cc \
tests/filetest.cc tests/rangetest.cc tests/fstest.cc \
tests/exceptiontest.cc
tests/exceptiontest.cc tests/misctest.cc
#tests/blitztest.cc
FTESTS=$(wildcard tests/*.f)
DOXYTXT=$(wildcard doxygen*.txt)
......
......@@ -26,6 +26,24 @@
* See the mainpage for a summary of all \ref main_modules.
*/
namespace tfxx {
}
/*! \namespace tfxx::util
* \brief Small unspecific utilities.
* \inrgoup group_util
*
* Small utilities of unspecific purpose.
* Some of them may be helpful for debugging code.
*
* \sa group_util
*
* \defgroup group_util Small utilities of unspecific purpose.
*
* Small utilities of unspecific purpose.
* Some of them may be helpful for debugging code.
*/
namespace util {
} // namespace util
} // namespace tfxx
/* ----- END OF doxygen.xtra.txt ----- */
......@@ -7,7 +7,26 @@
* \date 27/06/2016
*
* output hex dump of any structure (prototypes)
*
* \ingroup group_util
*
*
* ----
* 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 program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ----
*
* Copyright (c) 2016 by Thomas Forbriger (BFO Schiltach)
*
* REVISIONS and CHANGES
......@@ -22,12 +41,99 @@
#define TF_HEXDUMP_H_VERSION \
"TF_HEXDUMP_H V1.0"
#include<iomanip>
#include<iostream>
#include<ctype.h>
namespace tfxx {
namespace util {
/*! \brief output hex dump of any object.
* \ingroup group_util
*
* \param[in] v structure to dump
* \param[in] os output stream to which hex dump will be sent
* \param[in] c character to be used for non-printable characters
* \param[in] n number of bytes per line
*/
template<class C>
void hexdump(const C& v, std::ostream& os=std::cout,
const char& c='.', const unsigned int&n=16)
{
// provide pointer to object
const unsigned char* p=reinterpret_cast<const unsigned char *>(&v);
// character counter in object
unsigned short int ip=0;
// character counter in line
unsigned short int ic=0;
// size of object
const unsigned int size=sizeof(v);
// character array for printable characters
char *pc=new char[n];
// report size of object
os << "size of object: " << size << " bytes" << std::endl;
// memorize output stream flags
std::ostream::fmtflags flags=os.flags();
// cycle through all bytes in p
while (ip<size)
{
// start line with hex display of byte offset of first character
os << "0x";
os << std::hex << std::setfill('0') << std::setw(4)
<< std::nouppercase << ip;
// cycle through all characters in line
while ((ip < size) && (ic<n))
{
// extract numerical representation of current character
unsigned char ch=*p;
unsigned short int byte(ch);
// print hex value of current byte value
os << " ";
os << std::hex << std::setfill('0') << std::setw(2)
<< std::nouppercase << byte;
os.flags(flags);
// store printable representation of character
pc[ic]=c;
if (std::isprint(ch)) { pc[ic]=ch; }
// step to next byte in object
++p;
++ip;
++ic;
} // while ((ip < size) && (ic<n))
// restore state flags of output stream
os.flags(flags);
// forward line columns to position of printable characters
for (unsigned int i=ic; i<n; i++) { os << " "; }
// output printable representation of bytes
os << " ";
for (unsigned int i=0; i<ic; i++) { os << pc[i]; }
// end line
os << std::endl;
ic=0;
} // while (ip<size)
// delete temporary container of printable characters
delete[] pc;
} // void hexdump(const C& v, std::ostream& os=std::cout,
// const char& c='.', const int&n=16)
} // namespace util
} // namespace tfxx
/*! \brief produce hexdump to stdout, preceeded by object name.
* \ingroup group_util
*/
#define TFXX_hexdump(V) std::cout << "hex dump of object \"" \
<< #V << "\":" << std::endl; \
tfxx::util::hexdump(V);
#endif // TF_HEXDUMP_H_VERSION (includeguard)
/* ----- END OF hexdump.h ----- */
......@@ -71,7 +71,8 @@ clean: ;
STANDARDTEST=
# all that need the binary library
BINARYTEST=commandlinetest fortraniotest blitztest stringtest \
handletest regextest filetest rangetest fstest exceptiontest
handletest regextest filetest rangetest fstest exceptiontest \
misctest
# F77 part of the I/O test
FORTRANTEST=fortranF77
......
/*! \file misctest.cc
* \brief test miscellaneous functions and modules
*
* ----------------------------------------------------------------------------
*
* \author Thomas Forbriger
* \date 27/06/2016
*
* test miscellaneous functions and modules
*
* Copyright (c) 2016 by Thomas Forbriger (BFO Schiltach)
*
*
* ----
* 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 program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ----
*
* REVISIONS and CHANGES
* - 27/06/2016 V1.0 Thomas Forbriger
*
* ============================================================================
*/
#define MISCTEST_VERSION \
"MISCTEST V1.0 test miscellaneous functions and modules"
#include <iostream>
#include <tfxx/commandline.h>
#include <tfxx/hexdump.h>
using std::cout;
using std::cerr;
using std::endl;
int main(int iargc, char* argv[])
{
// define usage information
char usage_text[]=
{
MISCTEST_VERSION "\n"
"usage: misctest [--hexdump]" "\n"
" or: misctest --help|-h" "\n"
};
// define full help text
char help_text[]=
{
"--hexdump test hex dump function"
};
// define commandline options
using namespace tfxx::cmdline;
static Declare options[]=
{
// 0: print help
{"help",arg_no,"-"},
// 1: verbose mode
{"v",arg_no,"-"},
// 2: verbose mode
{"hexdump",arg_no,"-"},
{NULL}
};
// no arguments? print usage...
if (iargc<2)
{
cerr << usage_text << endl;
exit(0);
}
// collect options from commandline
Commandline cmdline(iargc, argv, options);
// help requested? print full help text...
if (cmdline.optset(0))
{
cerr << usage_text << endl;
cerr << help_text << endl;
exit(0);
}
/*
// dummy operation: print option settings
for (int iopt=0; iopt<2; iopt++)
{
cout << "option: '" << options[iopt].opt_string << "'" << endl;
if (cmdline.optset(iopt)) { cout << " option was set"; }
else { cout << "option was not set"; }
cout << endl;
cout << " argument (string): '" << cmdline.string_arg(iopt) << "'" << endl;
cout << " argument (int): '" << cmdline.int_arg(iopt) << "'" << endl;
cout << " argument (long): '" << cmdline.long_arg(iopt) << "'" << endl;
cout << " argument (float): '" << cmdline.float_arg(iopt) << "'" << endl;
cout << " argument (double): '" << cmdline.double_arg(iopt) << "'" << endl;
cout << " argument (bool): '";
if (cmdline.bool_arg(iopt))
{ cout << "true"; } else { cout << "false"; }
cout << "'" << endl;
}
while (cmdline.extra()) { cout << cmdline.next() << endl; }
// dummy operation: print rest of command line
while (cmdline.extra()) { cout << cmdline.next() << endl; }
*/
// hexdump
// -------
if (cmdline.optset(2))
{
int v=12;
char q[]="Hi there. This is a quite long character sequence.";
TFXX_hexdump(v);
std::cout << std::endl;
TFXX_hexdump(q);
std::cout << std::endl;
TFXX_hexdump(cmdline);
} // hexdump
}
/* ----- END OF misctest.cc ----- */
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment