This file is indexed.

/usr/include/ola/base/FlagsPrivate.h is in libola-dev 0.9.8-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * FlagsPrivate.h
 * Command line flag (option) handling.
 * Copyright (C) 2013 Simon Newton
 */

/**
 * @addtogroup flags
 * @{
 * @file FlagsPrivate.h
 * @brief Internal functionality for the flags.
 * @}
 */

#ifndef INCLUDE_OLA_BASE_FLAGSPRIVATE_H_
#define INCLUDE_OLA_BASE_FLAGSPRIVATE_H_

#include <getopt.h>
#include <string.h>
#include <ola/StringUtils.h>
#include <ola/base/Macro.h>
#include <map>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

namespace ola {

/**
 * @addtogroup flags
 * @{
 */

/**
 * @brief The interface for the Flag classes.
 */
class FlagInterface {
 public:
  virtual ~FlagInterface() {}

  /**
   * @brief Get the flag name
   */
  virtual const char* name() const = 0;

  /**
   * @brief Get the flag short option
   */
  virtual char short_opt() const = 0;

  /**
   * @brief Whether the flag requires an argument
   */
  virtual bool has_arg() const = 0;

  /**
   * @brief Get the flag argument type
   */
  virtual const char* arg_type() const = 0;

  /**
   * @brief Get the flag help string
   */
  virtual std::string help() const = 0;

  /**
   * @brief Check if the flag was present on the command line.
   * Good for switching behaviour when a flag is used.
   * @returns true if the flag was present, false otherwise
   */
  virtual bool present() const = 0;

  /**
   * @brief Set the flag value
   * @param input the input passed on the command line
   * @returns true on success, false otherwise
   */
  virtual bool SetValue(const std::string &input) = 0;
};

/**
 * @brief The common implementation.
 */
class BaseFlag : public FlagInterface {
 public:
  /**
   * @brief Create a new BaseFlag
   * @param arg_type the type of flag argument
   * @param short_opt the short option for the flag
   * @param help the help string for the flag
   */
  BaseFlag(const char *arg_type, const char *short_opt, const char *help)
      : m_arg_type(arg_type),
        m_short_opt(short_opt[0]),
        m_help(help),
        m_present(false) {
  }

  char short_opt() const { return m_short_opt; }
  const char* arg_type() const { return m_arg_type; }
  std::string help() const { return m_help; }
  bool present() const { return m_present; }

  /**
   * @brief Set that the flag was present on the command line
   */
  void MarkAsPresent() { m_present = true; }

 protected:
  void ReplaceUnderscoreWithHyphen(char *input);
  const char* NewCanonicalName(const char *name);

 private:
  const char *m_arg_type;
  char m_short_opt;
  const char *m_help;
  bool m_present;
};

/**
 * @brief A templated Flag class.
 * @tparam T the type of the flag.
 */
template <typename T>
class Flag : public BaseFlag {
 public:
  /**
   * @brief Create a new Flag
   * @param name the name of the flag
   * @param arg_type the type of flag argument
   * @param short_opt the short option for the flag
   * @param default_value the flag's default value
   * @param help the help string for the flag
   * @param has_arg if the flag should use an argument, only overrides
   *        Flag<bool>
   */
  Flag(const char *name, const char *arg_type, const char *short_opt,
       T default_value, const char *help,
       OLA_UNUSED const bool has_arg)
    : BaseFlag(arg_type, short_opt, help),
      m_name(name),
      m_default(default_value),
      m_value(default_value) {
    m_name = NewCanonicalName(name);
  }

  ~Flag() {
    delete[] m_name;
  }

  const char *name() const { return m_name; }
  bool has_arg() const { return true; }
  bool default_value() const { return m_default; }

  operator T() const { return m_value; }

  Flag &operator=(T v) {
    m_value = v;
    return *this;
  }

  bool SetValue(const std::string &input);

 private:
  const char *m_name;
  T m_default;
  T m_value;

  DISALLOW_COPY_AND_ASSIGN(Flag);
};

/**
 * @brief a bool flag
 */
template<>
class Flag<bool> : public BaseFlag {
 public:
  Flag(const char *name, const char *arg_type, const char *short_opt,
       bool default_value, const char *help, const bool has_arg)
    : BaseFlag(arg_type, short_opt, help),
      m_name(name),
      m_default(default_value),
      m_value(default_value),
      m_has_arg(has_arg) {
    if (!has_arg && default_value) {
      // prefix the long option with 'no'
      size_t prefix_size = strlen(NO_PREFIX);
      size_t name_size = strlen(name);
      char* new_name = new char[prefix_size + name_size + 1];
      memcpy(new_name, NO_PREFIX, prefix_size);
      memcpy(new_name + prefix_size, name, name_size);
      new_name[prefix_size + name_size] = 0;
      ReplaceUnderscoreWithHyphen(new_name);
      m_name = new_name;
    } else {
      m_name = NewCanonicalName(name);
    }
  }

  ~Flag() {
    delete[] m_name;
  }

  const char *name() const { return m_name; }
  bool has_arg() const { return m_has_arg; }
  bool default_value() const { return m_default; }

  operator bool() const { return m_value; }

  Flag &operator=(bool v) {
    m_value = v;
    return *this;
  }

  bool SetValue(const std::string &input) {
    MarkAsPresent();
    if (m_has_arg) {
      return ola::StringToBoolTolerant(input, &m_value);
    } else {
      m_value = !m_default;
      return true;
    }
  }

 private:
  const char *m_name;
  bool m_default;
  bool m_value;
  bool m_has_arg;

  static const char NO_PREFIX[];

  DISALLOW_COPY_AND_ASSIGN(Flag);
};

/**
 * @brief a string flag
 */
template<>
class Flag<std::string> : public BaseFlag {
 public:
  Flag(const char *name, const char *arg_type, const char *short_opt,
       std::string default_value, const char *help,
       OLA_UNUSED const bool has_arg)
    : BaseFlag(arg_type, short_opt, help),
      m_name(name),
      m_default(default_value),
      m_value(default_value) {
    m_name = NewCanonicalName(name);
  }

  ~Flag() {
    delete[] m_name;
  }

  const char *name() const { return m_name; }
  bool has_arg() const { return true; }
  std::string default_value() const { return m_default; }
  const char* arg_type() const { return "string"; }

  operator const char*() const { return m_value.c_str(); }
  operator std::string() const { return m_value; }
  std::string str() const { return m_value; }

  Flag &operator=(const std::string &v) {
    m_value = v;
    return *this;
  }

  bool SetValue(const std::string &input) {
    MarkAsPresent();
    m_value = input;
    return true;
  }

 private:
  const char *m_name;
  std::string m_default;
  std::string m_value;

  DISALLOW_COPY_AND_ASSIGN(Flag);
};

/**
 * @brief Used to set the value of a flag
 */
template <typename T>
bool Flag<T>::SetValue(const std::string &input) {
  MarkAsPresent();
  return ola::StringToInt(input, &m_value, true);
}


/**
 * @brief This class holds all the flags, and is responsbile for parsing the
 * command line.
 */
class FlagRegistry {
 public:
  FlagRegistry() {}

  void RegisterFlag(FlagInterface *flag);
  void ParseFlags(int *argc, char **argv);

  void SetFirstLine(const std::string &help);
  void SetDescription(const std::string &help);
  void DisplayUsage();
  void DisplayVersion();
  void GenManPage();

 private:
  typedef std::map<std::string, FlagInterface*> LongOpts;
  typedef std::map<char, FlagInterface*> ShortOpts;
  typedef std::map<int, FlagInterface*> FlagMap;
  // <flag, description>
  typedef std::pair<std::string, std::string> OptionPair;

  LongOpts m_long_opts;
  ShortOpts m_short_opts;
  std::string m_argv0;
  std::string m_first_line;
  std::string m_description;

  std::string GetShortOptsString() const;
  struct option *GetLongOpts(FlagMap *flag_map);
  void PrintFlags(std::vector<std::string> *lines);
  void PrintManPageFlags(std::vector<OptionPair> *lines);

  DISALLOW_COPY_AND_ASSIGN(FlagRegistry);
};

/**
 * @brief Get the global FlagRegistry.
 */
FlagRegistry *GetRegistry();

/**
 * @brief This class is responsible for registering a flag
 */
class FlagRegisterer {
 public:
  explicit FlagRegisterer(FlagInterface *flag) {
    GetRegistry()->RegisterFlag(flag);
  }

  FlagRegisterer(FlagInterface *flag, char *short_opt) {
    *short_opt = flag->short_opt();
    GetRegistry()->RegisterFlag(flag);
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(FlagRegisterer);
};

/** @} */

}  // namespace ola

/**
 * @cond HIDDEN_SYMBOLS
 */

/**
 * @brief Declare a flag which was defined in another file.
 */
#define DECLARE_flag(type, name) \
  namespace ola_flags { extern ola::Flag<type> FLAGS_##name; } \
  using ola_flags::FLAGS_##name;

/**
 * @brief Generic macro to define a flag
 */
#define DEFINE_flag(type, name, short_opt, default_value, help_str, \
                    has_arg) \
  namespace ola_flags { \
    ola::Flag<type> FLAGS_##name(#name, #type, #short_opt, default_value, \
                                 help_str, has_arg); \
    ola::FlagRegisterer flag_registerer_##name(&FLAGS_##name); \
  } \
  using ola_flags::FLAGS_##name

/**
 * @brief Generic macro to define a flag with a short option.
 */
#define DEFINE_flag_with_short(type, name, short_opt, default_value, help_str, \
                               has_arg) \
  namespace ola_flags { char flag_short_##short_opt = 0; } \
  namespace ola_flags { \
    ola::Flag<type> FLAGS_##name(#name, #type, #short_opt, default_value, \
                                 help_str, has_arg); \
    ola::FlagRegisterer flag_registerer_##name( \
        &FLAGS_##name, &flag_short_##short_opt); \
  } \
  using ola_flags::FLAGS_##name

/**
 * @endcond
 * End Hidden Symbols
 */

#endif  // INCLUDE_OLA_BASE_FLAGSPRIVATE_H_