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
)
30 template <typename CHAR_TYPE
>
31 ACE_Arg_Shifter_T
<CHAR_TYPE
>::ACE_Arg_Shifter_T (int& argc
,
36 temp_ ((const CHAR_TYPE
**) temp
),
37 argv_ ((const CHAR_TYPE
**) argv
),
45 template <typename CHAR_TYPE
>
47 ACE_Arg_Shifter_T
<CHAR_TYPE
>::init ()
49 // If not provided with one, allocate a temporary array.
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_
));
56 const CHAR_TYPE
*[this->total_size_
]);
57 #endif /* ACE_HAS_ALLOC_HOOKS */
60 // Fill the temporary array.
62 for (int i
= 0; i
< this->total_size_
; i
++)
64 this->temp_
[i
] = this->argv_
[i
];
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)
82 ACE_Allocator::instance ()->free (this->temp_
);
85 #endif /* ACE_HAS_ALLOC_HOOKS */
88 template <typename 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_
];
100 template <typename 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())
111 // check to see if the flag is the argument
112 int const offset
= this->cur_arg_strncasecmp (flag
);
118 this->consume_arg ();
120 if (!this->is_parameter_next())
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 ())
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)
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 (' '))
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
)
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);
160 ++i
, ++j
, ++this->current_index_
)
161 this->argv_
[j
] = this->temp_
[this->current_index_
];
163 this->back_
-= number
;
170 template <typename CHAR_TYPE
>
172 ACE_Arg_Shifter_T
<CHAR_TYPE
>::ignore_arg (int number
)
176 // Keep unknowns at the head of the vector.
177 if (this->is_anything_left () >= number
)
181 i
++, this->current_index_
++, this->front_
++)
182 this->argv_
[this->front_
] = this->temp_
[this->current_index_
];
185 this->argc_
+= number
;
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
221 ACE_END_VERSIONED_NAMESPACE_DECL
223 #endif /* ACE_ATOMIC_OP_T_CPP */