0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ht_wordstream.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; version 3 of the
9  * License, or any later version.
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 
29 #include <Common/Compat.h>
30 #include <Common/Init.h>
31 #include <Common/Checksum.h>
32 #include <Common/FileUtils.h>
33 #include <Common/WordStream.h>
34 
35 #include <iostream>
36 
37 using namespace Hypertable;
38 using namespace Hypertable::Config;
39 using namespace std;
40 
41 namespace {
42 
43  const char *usage =
44  "\n"
45  "Usage: ht_wordstream [options] <words-per-record> [<word-file>]\n\n"
46  "Description:\n"
47  " This program is used to generate a stream of records, where each record\n"
48  " constists of one or more randomly chosen words concatenated together with\n"
49  " a ' ' character. The <words-per-record> argument controls how many words\n"
50  " each record should consist of, and the <word-file> argument can be used to\n"
51  " specify the file containing the source words, this argument defaults to\n"
52  " /usr/share/dict/words.\n\n"
53  "Options";
54 
55  struct MyPolicy : Config::Policy {
56  static void init_options() {
57  cmdline_desc(usage).add_options()
58  ("limit,l", i64(), "Limit on the number of records to output")
59  ("seed,s", i32()->default_value(1), "Pseudo-random number generator seed")
60  ;
61  cmdline_hidden_desc().add_options()
62  ("words-per-record", i32(), "Number of words for each output record.")
63  ("word-file", str()->default_value("/usr/share/dict/words"),
64  "Number of words for each output record.")
65  ;
66  cmdline_positional_desc().add("words-per-record", 1).add("word-file", -1);
67  }
68  };
69 }
70 
71 typedef Meta::list<MyPolicy, DefaultPolicy> Policies;
72 
73 int main(int argc, char **argv) {
74  try {
75  init_with_policies<Policies>(argc, argv);
76 
77  if (!has("words-per-record")) {
78  cout << cmdline_desc() << flush;
79  quick_exit(EXIT_SUCCESS);
80  }
81 
82  WordStreamPtr word_stream
83  = make_shared<WordStream>(get_str("word-file"), 1, 2, false);
84 
85  while (true)
86  cout << word_stream->next() << "\n";
87  }
88  catch (Exception &e) {
89  HT_ERROR_OUT << e << HT_END;
90  quick_exit(EXIT_FAILURE);
91  }
92 
93  cout << std::flush;
94  quick_exit(EXIT_SUCCESS);
95 }
Interface and base of config policy.
Definition: Config.h:149
A class generating a stream of words; the words are retrieved from a file and can be randomized...
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
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
bool has(const String &name)
Check existence of a configuration value.
Definition: Config.h:57
File system utility functions.
std::shared_ptr< WordStream > WordStreamPtr
Definition: WordStream.h:106
Po::typed_value< int64_t > * i64(int64_t *v=0)
Definition: Properties.h:182
Po::typed_value< int32_t > * i32(int32_t *v=0)
Definition: Properties.h:178
Compatibility Macros for C/C++.
Initialization helper for applications.
#define HT_END
Definition: Logger.h:220
#define HT_ERROR_OUT
Definition: Logger.h:301
Hypertable definitions
Implementation of checksum routines.
Meta::list< MyPolicy, DefaultPolicy > Policies
This is a generic exception class for Hypertable.
Definition: Error.h:314
int main(int argc, char **argv)
Desc & cmdline_hidden_desc()
Get the command line hidden options description (for positional options)
Definition: Config.cc:81
PositionalDesc & cmdline_positional_desc()
Get the command line positional options description.
Definition: Config.cc:90