TRF Language Model
wb-option.cpp
Go to the documentation of this file.
1 // You may obtain a copy of the License at
2 //
3 // http://www.apache.org/licenses/LICENSE-2.0
4 //
5 // Unless required by applicable law or agreed to in writing, software
6 // distributed under the License is distributed on an "AS IS" BASIS,
7 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8 // See the License for the specific language governing permissions and
9 // limitations under the License.
10 //
11 // Copyright 2014-2015 Tsinghua University
12 // Author: wb.th08@gmail.com (Bin Wang), ozj@tsinghua.edu.cn (Zhijian Ou)
13 //
14 // All h, cpp, cc, and script files (e.g. bat, sh, pl, py) should include the above
15 // license declaration. Different coding language may use different comment styles.
16 
17 
18 #include "wb-option.h"
19 
20 namespace wb
21 {
23  {
24  m_strOtherHelp = "";
25  m_bOutputValues = true;
26  m_bMustCommand = true;
27  }
29  {
30  for (int i = 0; i < m_allocedBufs.GetNum(); i++) {
31  free(m_allocedBufs[i]);
32  }
34  }
35  void Option::Add(ValueType t, const char* pLabel, void *pAddress, const char* pDocMsg /* = NULL */)
36  {
37  m_opts.Add({ t, pLabel, pAddress, pDocMsg });
38  }
40  {
41  lout << "[Usage]:" << endl;
42  lout << m_strOtherHelp.c_str() << endl;
43  for (int i = 0; i < m_opts.GetNum(); i++)
44  {
45  lout << "-" << m_opts[i].pLabel << "\t" << m_opts[i].pDocMsg << " [default=";
46  Print(m_opts[i].type, m_opts[i].pAddress);
47  lout << "]" << endl;
48  }
49  }
50  void Option::Print(ValueType type, void* pAddress)
51  {
52  switch (type)
53  {
54  case wbOPT_TRUE:
55  lout << *((bool*)(pAddress));
56  break;
57  case wbOPT_FALSE:
58  lout << *((bool*)(pAddress));
59  break;
60  case wbOPT_INT:
61  lout << *((int*)(pAddress));
62  break;
63  case wbOPT_FLOAT:
64  lout << *((float*)(pAddress));
65  break;
66  case wbOPT_STRING:
67  lout << *((char**)(pAddress));
68  break;
69  }
70  }
72  {
73  for (int i = 0; i < m_opts.GetNum(); i++)
74  {
75  lout << "-" << m_opts[i].pLabel << " =\t";
76  Print(m_opts[i].type, m_opts[i].pAddress);
77  lout << endl;
78  }
79  }
80  void Option::Parse(const char *plabel, const char *pvalue)
81  {
82  int nOpt = -1;
83  Opt_Struct *pOpt = NULL;
84  for (int i = 0; i < m_opts.GetNum(); i++) {
85  if (0 == strcmp(plabel + 1, m_opts[i].pLabel)) {
86  nOpt = i;
87  pOpt = &m_opts[i];
88  break;
89  }
90  }
91 
92  if (!pOpt) { // unknown label
93  if (strcmp(plabel, "-?") == 0) { // output the usage
94  PrintUsage();
95  exit(0);
96  }
97  else if (strcmp(plabel, "-log") == 0) { // revise the log file
98  if (pvalue) {
99  lout.ReFile(pvalue, true);
100  }
101  else {
102  lout_error("no legal file name after -log");
103  }
104  }
105  else if (strcmp(plabel, "--") == 0) { // read from the config file
106  if (pvalue) {
107  Parse(pvalue);
108  }
109  else {
110  lout_error("no configuration file name after --");
111  }
112  }
113  else {
114  lout_error("Unknown label: " << plabel);
115  }
116  }
117  else {
118  if (pOpt->type != wbOPT_TRUE && pOpt->type != wbOPT_FALSE && !pvalue)
119  {
120  lout_error(plabel << " no corresponding value");
121  exit(0);
122  }
123 
124  switch (pOpt->type)
125  {
126  case wbOPT_TRUE:
127  *((bool*)(pOpt->pAddress)) = true;
128  break;
129  case wbOPT_FALSE:
130  *((bool*)(pOpt->pAddress)) = false;
131  break;
132  case wbOPT_INT:
133  *((int*)(pOpt->pAddress)) = atoi(pvalue);
134  break;
135  case wbOPT_FLOAT:
136  *((float*)(pOpt->pAddress)) = atof(pvalue);
137  break;
138  case wbOPT_STRING:
139  /* for string, we allocate new memory */
140  m_allocedBufs.Add() = strdup(pvalue);
141  *((char**)(pOpt->pAddress)) = m_allocedBufs.End();
142  break;
143  }
144  }
145  }
146  int Option::Parse(int argc /* = __argc */, char **argv /* = __argv */)
147  {
148  if (argc == 1 && m_bMustCommand) {
149  PrintUsage();
150  lout << "press any key to continue..." << endl;
151  getch();
152  exit(0);
153  }
154 
155  register int CommandCur = 0;
156  register char *CommandPtr = NULL;
157  register int nOpt = 0;
158  register Opt_Struct *pOpt = NULL;
159 
160  int nActiveOptNum = 0;
161  for (CommandCur = 1; CommandCur < argc;)
162  {
163  char *pLabel = argv[CommandCur];
164  char *pValue = NULL;
165  if (CommandCur + 1 < argc) {
166  pValue = argv[CommandCur + 1];
167  if (*pValue == '-') {
168  pValue = NULL;
169  }
170  }
171 
172  Parse(pLabel, pValue);
173 
174  nActiveOptNum++;
175  if (pValue)
176  CommandCur += 2;
177  else
178  CommandCur += 1;
179  }
180 
181  if (m_bOutputValues)
182  PrintValue();
183 
184  return nActiveOptNum;
185  }
186  int Option::Parse(const char *optfile)
187  {
188  ifstream file(optfile);
189 
190  int nActiveOptNum = 0;
191  const int maxlength = 1024 * 4;
192  char strline[maxlength];
193  while (file.getline(strline, maxlength)) {
194  char *pLabel = strtok(strline, "= \t");
195  char *pContent = strtok(NULL, "= \t");
196  if (pLabel == NULL)
197  continue;
198  Parse(pLabel, pContent);
199  nActiveOptNum++;
200  }
201  if (nActiveOptNum == 0 && m_bMustCommand) {
202  PrintUsage();
203  lout << "press any key to continue..." << endl;
204  getch();
205  exit(0);
206  }
207  if (m_bOutputValues)
208  PrintValue();
209 
210  return nActiveOptNum;
211  }
212 
213 }
bool m_bMustCommand
setting &#39;true&#39; means that, report error when no option input.
Definition: wb-option.h:60
void ReFile(const char *path, bool bNew=true)
relocate the log file. The defualt log file is "program name".log
Definition: wb-log.cpp:68
ValueType type
value type
Definition: wb-option.h:42
is true if exist
Definition: wb-option.h:33
Array< char * > m_allocedBufs
if read from file, we may need to allocate memory for string.
Definition: wb-option.h:62
#define lout_error(x)
Definition: wb-log.h:183
void Parse(const char *plabel, const char *pvalue)
parse a single option, "pvalue" can be NULL
Definition: wb-option.cpp:80
T & End()
Get the value at the tail of array.
Definition: wb-vector.h:256
void PrintValue()
output values
Definition: wb-option.cpp:71
~Option()
destructor
Definition: wb-option.cpp:28
structure of the value
Definition: wb-option.h:41
set false if exist
Definition: wb-option.h:34
Option()
constructor
Definition: wb-option.cpp:22
integer
Definition: wb-option.h:35
string m_strOtherHelp
extra help information, which will be output in PrintUsage
Definition: wb-option.h:58
void PrintUsage()
output usage
Definition: wb-option.cpp:39
bool m_bOutputValues
if output value after get options from the command line or file
Definition: wb-option.h:59
void Clean()
Clean the array. Just set the top of array to -1 and donot release the memory.
Definition: wb-vector.h:258
int GetNum() const
Get Array number.
Definition: wb-vector.h:240
void Add(T t)
Add a value to the tail of array.
Definition: wb-vector.h:242
void Add(ValueType t, const char *pLabel, void *pAddress, const char *pDocMsg=NULL)
Add a option.
Definition: wb-option.cpp:35
Array< Opt_Struct > m_opts
all the options
Definition: wb-option.h:57
Log lout
the defination is in wb-log.cpp
Definition: wb-log.cpp:22
void Print(ValueType type, void *pAddress)
output a value
Definition: wb-option.cpp:50
Define the option class.
ValueType
define the value type
Definition: wb-option.h:32
define all the code written by Bin Wang.
Definition: wb-file.cpp:21
void * pAddress
value memory address
Definition: wb-option.h:44