0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
prepend_md5.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 #include <Common/Compat.h>
22 
23 #include <AsyncComm/Config.h>
24 
25 #include <Common/DynamicBuffer.h>
26 #include <Common/Init.h>
27 #include <Common/Logger.h>
28 #include <Common/md5.h>
29 #include <Common/SystemInfo.h>
30 
31 #include <cctype>
32 #include <cstdlib>
33 #include <cstring>
34 #include <iostream>
35 #include <string>
36 
37 using namespace Hypertable;
38 using namespace Hypertable::Config;
39 using namespace std;
40 
41 namespace {
42 
43  const char *usage =
44  "\nusage: prepend_md5\n\n"
45  "description:\n"
46  " This program reads the first field (delimited by '\\t') of each line\n"
47  " computes the MD5 checksum of the field and then outputs the line with\n"
48  " the checksum prepended to the line\n"
49  "\n"
50  "options";
51 
52  struct AppPolicy : Policy {
53  static void init_options() {
54  cmdline_desc(usage).add_options()
55  ("md5-chars", i32()->default_value(4), "Name of checksum characters to output")
56  ;
57  }
58  static void init() {
59  }
60  };
61 
62  typedef Meta::list<AppPolicy, DefaultCommPolicy> Policies;
63 
64 }
65 
69 int main(int argc, char **argv) {
70  char *ptr;
71  char hexdigits[33];
72 
73  char *line_buffer = new char [ 1024 * 1024 ];
74 
75  ios::sync_with_stdio(false);
76 
77  try {
78  init_with_policies<Policies>(argc, argv);
79 
80  cout << System::cpu_info() << endl;
81  cout << System::os_info() << endl;
82  cout << System::net_info() << endl;
83  cout << System::mem_stat() << endl;
84  cout << System::fs_stat() << endl;
85  exit(0);
86 
87  int32_t checksum_chars = get_i32("md5-chars");
88  if (checksum_chars < 1 || checksum_chars > 32) {
89  cerr << "Invalid value for md5-chars, must be from 1 to 32" << endl;
90  exit(EXIT_FAILURE);
91  }
92 
93  while (!cin.eof()) {
94 
95  cin.getline(line_buffer, 1024*1024);
96 
97  if (*line_buffer == 0)
98  continue;
99 
100  ptr = strchr(line_buffer, '\t');
101  if (ptr)
102  *ptr = 0;
103 
104  md5_string(line_buffer, hexdigits);
105  hexdigits[checksum_chars] = 0;
106 
107  if (ptr)
108  *ptr = '\t';
109 
110  cout << hexdigits << " " << line_buffer << "\n";
111  }
112  cout << flush;
113  }
114  catch (std::exception &e) {
115  cerr << "Error - " << e.what() << endl;
116  exit(EXIT_FAILURE);
117  }
118 }
Declarations for configuration properties.
Interface and base of config policy.
Definition: Config.h:149
static const CpuInfo & cpu_info()
Retrieves updated CPU information (see SystemInfo.h)
Definition: SystemInfo.cc:322
void init(int argc, char *argv[], const Desc *desc=NULL)
Initialize with default policy.
Definition: Init.h:95
STL namespace.
Desc & cmdline_desc(const char *usage)
A macro which definds global functions like get_bool(), get_str(), get_i16() etc. ...
Definition: Config.cc:72
A dynamic, resizable memory buffer.
Po::typed_value< int32_t > * i32(int32_t *v=0)
Definition: Properties.h:178
Logging routines and macros.
static const OsInfo & os_info()
Retrieves updated Operating system information (see SystemInfo.h)
Definition: SystemInfo.cc:347
Compatibility Macros for C/C++.
Initialization helper for applications.
void md5_string(const char *input, char output[33])
Calculates the hex string of MD5 of null terminated input.
Definition: md5.cc:384
static const FsStat & fs_stat()
Retrieves updated Filesystem statistics (see SystemInfo.h)
Definition: SystemInfo.cc:386
int main(int argc, char **argv)
Definition: prepend_md5.cc:69
Hypertable definitions
Meta::list< MyPolicy, DefaultPolicy > Policies
System information and statistics based on libsigar.
static const MemStat & mem_stat()
Retrieves updated Memory statistics (see SystemInfo.h)
Definition: SystemInfo.cc:339
md5 digest routines.
static const NetInfo & net_info()
Retrieves updated Network information (see SystemInfo.h)
Definition: SystemInfo.cc:360