Document return values
[ACE_TAO.git] / ACE / ace / Arg_Shifter.cpp
blobef51e154d22dc2ff1bc7e997527413b13f037c3a
1 #ifndef ACE_ARG_SHIFTER_T_CPP
2 #define ACE_ARG_SHIFTER_T_CPP
4 #include "ace/Arg_Shifter.h"
5 #include "ace/OS_NS_string.h"
6 #include "ace/OS_NS_strings.h"
7 #include "ace/OS_Errno.h"
8 #include "ace/OS_Memory.h"
9 #if defined (ACE_HAS_ALLOC_HOOKS)
10 # include "ace/Malloc_Base.h"
11 #endif /* ACE_HAS_ALLOC_HOOKS */
13 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
15 template <typename CHAR_TYPE>
16 ACE_Arg_Shifter_T<CHAR_TYPE>::ACE_Arg_Shifter_T (int& argc,
17 const CHAR_TYPE** argv,
18 const CHAR_TYPE** temp)
19 : argc_ (argc),
20 total_size_ (argc),
21 temp_ (temp),
22 argv_ (argv),
23 current_index_ (0),
24 back_ (argc - 1),
25 front_ (0)
27 this->init ();
30 template <typename CHAR_TYPE>
31 ACE_Arg_Shifter_T<CHAR_TYPE>::ACE_Arg_Shifter_T (int& argc,
32 CHAR_TYPE** argv,
33 CHAR_TYPE** temp)
34 : argc_ (argc),
35 total_size_ (argc),
36 temp_ ((const CHAR_TYPE **) temp),
37 argv_ ((const CHAR_TYPE **) argv),
38 current_index_ (0),
39 back_ (argc - 1),
40 front_ (0)
42 this->init ();
45 template <typename CHAR_TYPE>
46 void
47 ACE_Arg_Shifter_T<CHAR_TYPE>::init ()
49 // If not provided with one, allocate a temporary array.
50 if (this->temp_ == 0)
51 #if defined (ACE_HAS_ALLOC_HOOKS)
52 this->temp_ = reinterpret_cast<const CHAR_TYPE **>
53 (ACE_Allocator::instance ()->malloc (sizeof (CHAR_TYPE*) * this->total_size_));
54 #else
55 ACE_NEW (this->temp_,
56 const CHAR_TYPE *[this->total_size_]);
57 #endif /* ACE_HAS_ALLOC_HOOKS */
58 if (this->temp_ != 0)
60 // Fill the temporary array.
61 this->argc_ = 0;
62 for (int i = 0; i < this->total_size_; i++)
64 this->temp_[i] = this->argv_[i];
65 this->argv_[i] = 0;
68 else
70 // Allocation failed, prohibit iteration.
71 this->current_index_ = this->argc_;
72 this->front_ = this->argc_;
76 template <typename CHAR_TYPE>
77 ACE_Arg_Shifter_T<CHAR_TYPE>::~ACE_Arg_Shifter_T ()
79 // Delete the temporary vector.
80 #if defined (ACE_HAS_ALLOC_HOOKS)
81 if (this->temp_)
82 ACE_Allocator::instance ()->free (this->temp_);
83 #else
84 delete [] temp_;
85 #endif /* ACE_HAS_ALLOC_HOOKS */
88 template <typename CHAR_TYPE>
89 const CHAR_TYPE *
90 ACE_Arg_Shifter_T<CHAR_TYPE>::get_current () const
92 const CHAR_TYPE * retval = 0;
94 if (this->is_anything_left ())
95 retval = this->temp_[current_index_];
97 return retval;
100 template <typename CHAR_TYPE>
101 const CHAR_TYPE *
102 ACE_Arg_Shifter_T<CHAR_TYPE>::get_the_parameter (const CHAR_TYPE *flag)
104 // the return 0's abound because this method
105 // would otherwise be a deep if { } else { }
107 // check to see if any arguments still exist
108 if (!this->is_anything_left())
109 return 0;
111 // check to see if the flag is the argument
112 int const offset = this->cur_arg_strncasecmp (flag);
113 if (offset == -1)
114 return 0;
116 if (offset == 0)
118 this->consume_arg ();
120 if (!this->is_parameter_next())
122 return 0;
125 // the parameter is in the middle somewhere...
126 return this->temp_[current_index_] + offset;
129 template <typename CHAR_TYPE>
131 ACE_Arg_Shifter_T<CHAR_TYPE>::cur_arg_strncasecmp (const CHAR_TYPE *flag)
133 if (!this->is_anything_left ())
134 return -1;
136 const size_t flag_length = ACE_OS::strlen (flag);
137 const CHAR_TYPE *arg = this->temp_[this->current_index_];
139 if (ACE_OS::strncasecmp (arg, flag, flag_length) != 0)
140 return -1;
142 const size_t arg_length = ACE_OS::strlen (arg);
143 size_t remaining = flag_length;
144 while (remaining < arg_length && arg[remaining] == CHAR_TYPE (' '))
145 ++remaining;
146 return (arg_length == flag_length) ? 0 : static_cast<int> (remaining);
149 template <typename CHAR_TYPE>
151 ACE_Arg_Shifter_T<CHAR_TYPE>::consume_arg (int number)
153 int retval = 0;
155 // Stick knowns at the end of the vector (consumed).
156 if (this->is_anything_left() >= number)
158 for (int i = 0, j = this->back_ - (number - 1);
159 i < number;
160 ++i, ++j, ++this->current_index_)
161 this->argv_[j] = this->temp_[this->current_index_];
163 this->back_ -= number;
164 retval = 1;
167 return retval;
170 template <typename CHAR_TYPE>
172 ACE_Arg_Shifter_T<CHAR_TYPE>::ignore_arg (int number)
174 int retval = 0;
176 // Keep unknowns at the head of the vector.
177 if (this->is_anything_left () >= number)
179 for (int i = 0;
180 i < number;
181 i++, this->current_index_++, this->front_++)
182 this->argv_[this->front_] = this->temp_[this->current_index_];
184 retval = 1;
185 this->argc_ += number;
188 return retval;
191 template <typename CHAR_TYPE>
193 ACE_Arg_Shifter_T<CHAR_TYPE>::is_anything_left () const
195 return this->total_size_ - this->current_index_;
198 template <typename CHAR_TYPE>
200 ACE_Arg_Shifter_T<CHAR_TYPE>::is_option_next () const
202 return this->is_anything_left () &&
203 this->temp_[this->current_index_][0] == '-';
206 template <typename CHAR_TYPE>
208 ACE_Arg_Shifter_T<CHAR_TYPE>::is_parameter_next () const
210 return this->is_anything_left ()
211 && this->temp_[this->current_index_][0] != '-';
214 template <typename CHAR_TYPE>
216 ACE_Arg_Shifter_T<CHAR_TYPE>::num_ignored_args () const
218 return this->front_;
221 ACE_END_VERSIONED_NAMESPACE_DECL
223 #endif /* ACE_ATOMIC_OP_T_CPP */