1 // -*- c++ -*-
2 // Generated by assa-genesis
3 //------------------------------------------------------------------------------
4 // $Id: assa-hexdump.cpp,v 1.1 2006/07/23 02:47:57 vlg Exp $
5 //------------------------------------------------------------------------------
6 // assa-hexdump.cpp
7 //------------------------------------------------------------------------------
8 // Copyright (c) 2006 by Vladislav Grinchenko
9 //
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License
12 // as published by the Free Software Foundation; either version
13 // 2 of the License, or (at your option) any later version.
14 //------------------------------------------------------------------------------
15 //
16 // Date : Tue Jul 18 11:48:09 2006
17 //
18 //------------------------------------------------------------------------------
19
20 static const char help_msg[]=
21 " \n"
22 " NAME: \n"
23 " \n"
24 " assa-hexdump \n"
25 " \n"
26 " DESCRIPTION: \n"
27 " \n"
28 " Dump the contents of the file in hex to standard output. \n"
29 " \n"
30 " USAGE: \n"
31 " \n"
32 " shell> assa-hexdump [OPTIONS] INPUT-FILE \n"
33 " \n"
34 " OPTIONS: \n"
35 " \n"
36 " -h, --help - Print this messag \n"
37 " -v, --version - Print version number \n"
38 " \n"
39 " \n";
40 //------------------------------------------------------------------------------
41
42 #ifdef HAVE_CONFIG_H
43 # include "config.h"
44 #endif
45
46 #include <fstream>
47 #include <string>
48 using std::string;
49
50 #include <assa/Assure.h>
51 #include <assa/GenServer.h>
52 #include <assa/Singleton.h>
53 #include <assa/TimeVal.h>
54 #include <assa/MemDump.h>
55
56 class HexDump :
57 public ASSA::GenServer,
58 public ASSA::Singleton<HexDump>
59 {
60 public:
61 HexDump ();
62
63 virtual void init_service ();
64 virtual void process_events ();
65
66 private:
67 // An example of processing positional arguments
68 virtual void pos_arg (const char* arg_);
69
70 private:
71 string m_in_fname;
72 int m_pos_arg_count;
73 };
74
75
76 /* Useful definitions */
77
78 #define HEXDUMP HexDump::get_instance()
79 #define REACTOR HEXDUMP->get_reactor()
80
81
82 // Static declarations mandated by Singleton class
83 ASSA_DECL_SINGLETON(HexDump);
84
85 HexDump::
HexDump()86 HexDump () : m_pos_arg_count (0)
87 {
88 // ---Debugging---
89 rm_opt ('m', "mask" );
90 rm_opt ('d', "log-stdout" );
91 rm_opt ('D', "log-file" );
92 rm_opt ('z', "log-size" );
93
94 // ---Configuration---
95 rm_opt ('f', "config-file" );
96 rm_opt ('n', "instance" );
97 rm_opt ('p', "port" );
98
99 // ---Process bookkeeping---
100 rm_opt ('b', "daemon" );
101 rm_opt ('l', "pidfile" );
102 rm_opt ('L', "ommit-pidfile");
103
104 m_mask = 0;
105 }
106
107 void
108 HexDump::
pos_arg(const char * arg_)109 pos_arg (const char* arg_)
110 {
111 trace("HexDump::pos_arg");
112
113 switch(m_pos_arg_count) {
114 case 0:
115 m_in_fname = arg_;
116 break;
117
118 case 1:
119 default:
120 DL((ASSA::ASSAERR,"Error: unexpected argument '%s'\n", arg_));
121 DL((ASSA::ASSAERR,"Try `assa-hexdump --help` for more information.\n"));
122 Assure_exit (false);
123 }
124 m_pos_arg_count++;
125 }
126
127 void
128 HexDump::
init_service()129 init_service ()
130 {
131 trace("HexDump::init_service");
132
133 if (m_pos_arg_count != 1) {
134 DL((ASSA::ASSAERR,"Missing input file.\n"));
135 DL((ASSA::ASSAERR,"Try `assa-hexdump --help` for more information.\n"));
136 Assure_exit (false);
137 }
138 }
139
140 void
141 HexDump::
process_events()142 process_events ()
143 {
144 trace("HexDump::process_events");
145
146 char buf [4096];
147
148 std::ifstream instream (m_in_fname.c_str ());
149 if (!instream) {
150 DL((ASSA::ASSAERR,"Failed to open input file \"%s\".\n",
151 m_in_fname.c_str ()));
152 set_exit_value (1);
153 return;
154 }
155
156 while (!instream.eof ()) {
157 instream.read (buf, 4096);
158 ASSA::MemDump md (buf, instream.gcount ());
159 std::cout << md.getMemDump () << "\n";
160 }
161 instream.close ();
162 std::cout << std::flush;
163
164 // Shut the service down
165 m_reactor.stopReactor ();
166 DL((ASSA::APP,"Service stopped!\n"));
167 }
168
169
170
171 int
main(int argc,char * argv[])172 main (int argc, char* argv[])
173 {
174 static const char release[] = "VERSION";
175 int patch_level = 0;
176
177 HEXDUMP->set_version (release, patch_level);
178 HEXDUMP->set_author ("Vladislav Grinchenko");
179 HEXDUMP->set_flags (ASSA::GenServer::RMLOG);
180
181 HEXDUMP->init (&argc, argv, help_msg);
182
183 HEXDUMP->init_service ();
184 HEXDUMP->process_events ();
185
186
187 return HEXDUMP->get_exit_value ();
188 }
189
190