summaryrefslogtreecommitdiff
path: root/ace/Arg_Shifter.h
blob: cd1f5517344cc57a339ae9ddacfc51b026439c41 (plain)
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
// This may look like C, but it's really -*- C++ -*-
// $Id$

// ========================================================================
//
// = LIBRARY
//     ace
//
// = FILENAME
//     Arg_Shifter.h
//
// = AUTHOR
//     Seth Widoff
//
// ========================================================================

#ifndef ACE_ARG_SHIFTER_H
#define ACE_ARG_SHIFTER_H

#include "ace/OS.h"


class ACE_Export ACE_Arg_Shifter
{
  // = TITLE
  //    This ADT shifts known args to the back of the argv vector, so
  //    deeper levels of argument parsing can locate the yet
  //    unprocessed arguments at the beginning of the vector.
  //
  // = DESCRIPTION
  //    The <ACE_Arg_Shifter> copies the pointers of the argv vector
  //    into a temporary array. As the <ACE_Arg_Shifter> iterates over
  //    the temp, is places known arguments in the rear of the argv
  //    and unknown ones in the beginning. So, after having visited
  //    all the arguments in the temp vector, <ACE_Arg_Shifter> has
  //    placed all the unknown arguments in their original order at
  //    the front of argv.
public:
  // = Initialization and termination methods.
  ACE_Arg_Shifter (int& argc,
                   char** argv,
                   char** temp = 0);
  // Initialize the <ACE_Arg_Shifter> to the vector over which to
  // iterate, also providing the temporary array if the client doesn't
  // want the arg_shifter to dynamically allocate its own. If internal
  // dynamic allocation fails, the <ACE_Arg_Shifter> will set all the
  // indices to the end of the vector, forbidding iteration. Following
  // iteration over argv, the argc value will contain the number of
  // unconsumed arguments.

  ~ACE_Arg_Shifter (void);
  // Destructor.

  char* get_current (void) const;
  // Get the current head of the vector.

  char* get_current_parameter (int offset = 0);
  // After checking for a flag, use this method return the parameter
  // Safe to call without checking for current argument
  //
  // If offset > 0, returns current argument + offset
  //     if (offset == 2) ... "value" returns pointer to "lue"
  //     if (offset == 7) ... "value" returns 0
  //
  // If offset == 0
  //     consumes current argument
  //     if (is_parameter_next ()) is true
  //     then returns a pointer to the new current argument
  //     else returns 0
  //
  // If offset < 0, returns current argument with offset
  //     but offset counts from the back
  //     if (offset == -2) ... "value" returns pointer to "ue"
  //     if (offset == -9) ... "value" returns 0

  int cur_arg_strncasecmp (const char* flag);
  // Check if the current argument matches (case insensitive) <flag>
  //
  // ------------------------------------------------------------
  //
  // Case A: Perfect Match (case insensitive)
  // 0 is returned.
  //
  //     ie: when current_arg = "-foobar" or "-FOOBAR" or "-fooBAR"
  //         this->cur_arg_strncasecmp ("-FooBar);
  //         will return 0
  //
  // ------------------------------------------------------------
  //
  // Case B: Perfect Match (case insensitive) but the current_arg
  // is longer than the flag. Returns a number equal to the index
  // in the char* indicating the start of the extra characters
  //
  //     ie: when current_arg = "-foobar98023"
  //         this->cur_arg_strncasecmp ("-FooBar);
  //         will return 7
  //
  // Notice: this number will always be > 0
  //
  // ------------------------------------------------------------
  //
  // Case C: If neither of Case A or B is met (no match)
  // then -1 is returned

  int consume_arg (int number = 1);
  // Consume <number> argument(s) by sticking them/it on the end of
  // the vector.

  int ignore_arg (int number = 1);
  // Place <number> arguments in the same relative order ahead of the
  // known arguemnts in the vector.

  int is_anything_left (void) const;
  // Returns the number of args left to see in the vector.

  int is_option_next (void) const;
  // Returns 1 if there's a next item in the vector and it begins with
  // '-'.

  int is_parameter_next (void) const;
  // Returns 1 if there's a next item in the vector and it doesn't
  // begin with '-'.

  int num_ignored_args (void) const;
  // Returns the number of irrelevant args seen.

private:
  int& argc_;
  // The size of the argument vector.

  int total_size_;
  // The size of argv_.

  char** temp_;
  // The temporary array over which we traverse.

  char** argv_;
  // The array in which the arguments are reordered.

  int current_index_;
  // The element in <temp_> we're currently examining.

  int back_;
  // The index of <argv_> in which we'll stick the next unknown
  // argument.

  int front_;
  // The index of <argv_> in which we'll stick the next known
  // argument.
};

#endif /* ACE_ARG_SHIFTER_H */