File: assa-hexdump.cpp

package info (click to toggle)
libassa 3.5.1-6
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 3,360 kB
  • ctags: 2,613
  • sloc: cpp: 15,703; sh: 12,083; makefile: 382; perl: 51
file content (189 lines) | stat: -rw-r--r-- 5,519 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// -*- c++ -*-
// Generated by assa-genesis
//------------------------------------------------------------------------------
// $Id: assa-hexdump.cpp,v 1.1 2006/07/23 02:47:57 vlg Exp $
//------------------------------------------------------------------------------
//                            assa-hexdump.cpp
//------------------------------------------------------------------------------
//  Copyright (c) 2006 by Vladislav Grinchenko 
//
//  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.      
//------------------------------------------------------------------------------
//
// Date   : Tue Jul 18 11:48:09 2006
//
//------------------------------------------------------------------------------

static const char help_msg[]=
"                                                                            \n"
" NAME:                                                                      \n"
"                                                                            \n"
"   assa-hexdump                                                             \n"
"                                                                            \n"
" DESCRIPTION:                                                               \n"
"                                                                            \n"
"   Dump the contents of the file in hex to standard output.                 \n"
"                                                                            \n"
" USAGE:                                                                     \n"
"                                                                            \n"
"   shell>  assa-hexdump [OPTIONS] INPUT-FILE                                \n"
"                                                                            \n"
" OPTIONS:                                                                   \n"
"                                                                            \n"
" -h, --help              - Print this message                               \n"
" -v, --version           - Print version number                             \n"
"                                                                            \n"
"                                                                           \n";
//------------------------------------------------------------------------------

#ifdef HAVE_CONFIG_H
#    include "config.h"
#endif

#include <fstream>
#include <string>
using std::string;

#include <assa/Assure.h>
#include <assa/GenServer.h>
#include <assa/Singleton.h>
#include <assa/TimeVal.h>
#include <assa/MemDump.h>

class HexDump :
    public ASSA::GenServer,
    public ASSA::Singleton<HexDump>
{
public:
    HexDump ();

    virtual void init_service ();
    virtual void process_events ();

private:
    // An example of processing positional arguments
    virtual void pos_arg (const char* arg_);

private:
    string  m_in_fname;
	int     m_pos_arg_count;
};


/* Useful definitions */

#define HEXDUMP  HexDump::get_instance()
#define REACTOR HEXDUMP->get_reactor()


// Static declarations mandated by Singleton class
ASSA_DECL_SINGLETON(HexDump);

HexDump::
HexDump () : m_pos_arg_count (0)
{
    // ---Debugging---
    rm_opt ('m', "mask"         );
    rm_opt ('d', "log-stdout"   );
    rm_opt ('D', "log-file"     );
    rm_opt ('z', "log-size"     );

    // ---Configuration---
    rm_opt ('f', "config-file"  );
    rm_opt ('n', "instance"     );
    rm_opt ('p', "port"         );

    // ---Process bookkeeping---
    rm_opt ('b', "daemon"       );
    rm_opt ('l', "pidfile"      );
    rm_opt ('L', "ommit-pidfile");

    m_mask = 0;
}

void
HexDump::
pos_arg (const char* arg_)
{
    trace("HexDump::pos_arg");

    switch(m_pos_arg_count) {
    case 0:
        m_in_fname = arg_;
        break;

    case 1:
    default:
        DL((ASSA::ASSAERR,"Error: unexpected argument '%s'\n", arg_));
        DL((ASSA::ASSAERR,"Try `assa-hexdump --help` for more information.\n"));
        Assure_exit (false);
    }
    m_pos_arg_count++;
}

void
HexDump::
init_service ()
{
    trace("HexDump::init_service");

	if (m_pos_arg_count != 1) {
        DL((ASSA::ASSAERR,"Missing input file.\n"));
        DL((ASSA::ASSAERR,"Try `assa-hexdump --help` for more information.\n"));
        Assure_exit (false);
	}
}

void
HexDump::
process_events ()
{
    trace("HexDump::process_events");
	
	char buf [4096];

	std::ifstream instream (m_in_fname.c_str ());
	if (!instream) {
        DL((ASSA::ASSAERR,"Failed to open input file \"%s\".\n", 
			m_in_fname.c_str ()));
		set_exit_value (1);
		return;
	}

	while (!instream.eof ()) {
		instream.read (buf, 4096);
		ASSA::MemDump md (buf, instream.gcount ());
		std::cout << md.getMemDump () << "\n";
	}
	instream.close ();
	std::cout << std::flush;

    // Shut the service down
    m_reactor.stopReactor ();
    DL((ASSA::APP,"Service stopped!\n"));
}



int
main (int argc, char* argv[])
{
    static const char release[] = "VERSION";
    int patch_level = 0;

    HEXDUMP->set_version (release, patch_level);
    HEXDUMP->set_author  ("Vladislav Grinchenko");
    HEXDUMP->set_flags   (ASSA::GenServer::RMLOG);

    HEXDUMP->init (&argc, argv, help_msg);
 
    HEXDUMP->init_service ();
    HEXDUMP->process_events ();


    return HEXDUMP->get_exit_value ();
}