Arg_Shifter.cpp

Go to the documentation of this file.
00001 #ifndef ACE_ARG_SHIFTER_T_CPP
00002 #define ACE_ARG_SHIFTER_T_CPP
00003 
00004 #include "ace/Arg_Shifter.h"
00005 #include "ace/OS_NS_string.h"
00006 #include "ace/OS_NS_strings.h"
00007 #include "ace/OS_Errno.h"
00008 #include "ace/OS_Memory.h"
00009 
00010 ACE_RCSID (ace,
00011            Arg_Shifter,
00012            "Arg_Shifter.cpp,v 4.29 2006/06/09 11:10:20 jwillemsen Exp")
00013 
00014 
00015 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00016 
00017 template <typename CHAR_TYPE>
00018 ACE_Arg_Shifter_T<CHAR_TYPE>::ACE_Arg_Shifter_T (int& argc,
00019                                                  const CHAR_TYPE** argv,
00020                                                  const CHAR_TYPE** temp)
00021   : argc_ (argc),
00022     total_size_ (argc),
00023     temp_ (temp),
00024     argv_ (argv),
00025     current_index_ (0),
00026     back_ (argc - 1),
00027     front_ (0)
00028 {
00029   this->init ();
00030 }
00031 
00032 template <typename CHAR_TYPE>
00033 ACE_Arg_Shifter_T<CHAR_TYPE>::ACE_Arg_Shifter_T (int& argc,
00034                                                  CHAR_TYPE** argv,
00035                                                  CHAR_TYPE** temp)
00036   : argc_ (argc),
00037     total_size_ (argc),
00038     temp_ ((const CHAR_TYPE **) temp),
00039     argv_ ((const CHAR_TYPE **) argv),
00040     current_index_ (0),
00041     back_ (argc - 1),
00042     front_ (0)
00043 {
00044   this->init ();
00045 }
00046 
00047 template <typename CHAR_TYPE>
00048 void
00049 ACE_Arg_Shifter_T<CHAR_TYPE>::init (void)
00050 {
00051   // If not provided with one, allocate a temporary array.
00052   if (this->temp_ == 0)
00053     ACE_NEW (this->temp_,
00054              const CHAR_TYPE *[this->total_size_]);
00055 
00056   if (this->temp_ != 0)
00057     {
00058       // Fill the temporary array.
00059       this->argc_ = 0;
00060       for (int i = 0; i < this->total_size_; i++)
00061         {
00062           this->temp_[i] = this->argv_[i];
00063           this->argv_[i] = 0;
00064         }
00065     }
00066   else
00067     {
00068       // Allocation failed, prohibit iteration.
00069       this->current_index_ = this->argc_;
00070       this->front_ = this->argc_;
00071     }
00072 }
00073 
00074 template <typename CHAR_TYPE>
00075 ACE_Arg_Shifter_T<CHAR_TYPE>::~ACE_Arg_Shifter_T (void)
00076 {
00077   // Delete the temporary vector.
00078   delete [] temp_;
00079 }
00080 
00081 template <typename CHAR_TYPE>
00082 const CHAR_TYPE *
00083 ACE_Arg_Shifter_T<CHAR_TYPE>::get_current (void) const
00084 {
00085   const CHAR_TYPE * retval = 0;
00086 
00087   if (this->is_anything_left ())
00088     retval =  this->temp_[current_index_];
00089 
00090   return retval;
00091 }
00092 
00093 template <typename CHAR_TYPE>
00094 const CHAR_TYPE *
00095 ACE_Arg_Shifter_T<CHAR_TYPE>::get_the_parameter (const CHAR_TYPE *flag)
00096 {
00097   // the return 0's abound because this method
00098   // would otherwise be a deep if { } else { }
00099 
00100   // check to see if any arguments still exist
00101   if (!this->is_anything_left())
00102     return 0;
00103 
00104   // check to see if the flag is the argument
00105   int const offset = this->cur_arg_strncasecmp (flag);
00106   if (offset == -1)
00107     return 0;
00108 
00109   if (offset == 0)
00110     {
00111       this->consume_arg ();
00112 
00113       if (!this->is_parameter_next())
00114         {
00115           return 0;
00116         }
00117     }
00118   // the paramter is in the middle somewhere...
00119   return this->temp_[current_index_] + offset;
00120 }
00121 
00122 template <typename CHAR_TYPE>
00123 int
00124 ACE_Arg_Shifter_T<CHAR_TYPE>::cur_arg_strncasecmp (const CHAR_TYPE *flag)
00125 {
00126   // Check for a current argument
00127   if (this->is_anything_left())
00128     {
00129       size_t const flag_length = ACE_OS::strlen (flag);
00130 
00131       // Check for presence of the flag
00132       if (ACE_OS::strncasecmp(this->temp_[current_index_],
00133                               flag,
00134                               flag_length) == 0)
00135         {
00136           if (ACE_OS::strlen(temp_[current_index_]) ==
00137               flag_length)
00138             {
00139               // match and lengths are equal
00140               return 0;
00141             }
00142           else
00143             {
00144               // matches, with more info to boot!
00145               size_t const remaining = ACE_OS::strspn
00146                 (this->temp_[current_index_] + flag_length,
00147                 ACE_LIB_TEXT (" ")) + flag_length;
00148               return static_cast<int> (remaining);
00149             }
00150         }
00151     }
00152   // failure
00153   return -1;
00154 }
00155 
00156 template <typename CHAR_TYPE>
00157 int
00158 ACE_Arg_Shifter_T<CHAR_TYPE>::consume_arg (int number)
00159 {
00160   int retval = 0;
00161 
00162   // Stick knowns at the end of the vector (consumed).
00163   if (this->is_anything_left() >= number)
00164     {
00165       for (int i = 0, j = this->back_ - (number - 1);
00166            i < number;
00167            ++i, ++j, ++this->current_index_)
00168         this->argv_[j] = this->temp_[this->current_index_];
00169 
00170       this->back_ -= number;
00171       retval = 1;
00172     }
00173 
00174   return retval;
00175 }
00176 
00177 template <typename CHAR_TYPE>
00178 int
00179 ACE_Arg_Shifter_T<CHAR_TYPE>::ignore_arg (int number)
00180 {
00181   int retval = 0;
00182 
00183   // Keep unknowns at the head of the vector.
00184   if (this->is_anything_left () >= number)
00185     {
00186       for (int i = 0;
00187            i < number;
00188            i++, this->current_index_++, this->front_++)
00189         this->argv_[this->front_] = this->temp_[this->current_index_];
00190 
00191       retval = 1;
00192       this->argc_ += number;
00193     }
00194 
00195   return retval;
00196 }
00197 
00198 template <typename CHAR_TYPE>
00199 int
00200 ACE_Arg_Shifter_T<CHAR_TYPE>::is_anything_left (void) const
00201 {
00202   return this->total_size_ - this->current_index_;
00203 }
00204 
00205 template <typename CHAR_TYPE>
00206 int
00207 ACE_Arg_Shifter_T<CHAR_TYPE>::is_option_next (void) const
00208 {
00209   return this->is_anything_left () &&
00210     this->temp_[this->current_index_][0] == '-';
00211 }
00212 
00213 template <typename CHAR_TYPE>
00214 int
00215 ACE_Arg_Shifter_T<CHAR_TYPE>::is_parameter_next (void) const
00216 {
00217   return this->is_anything_left ()
00218     && this->temp_[this->current_index_][0] != '-';
00219 }
00220 
00221 template <typename CHAR_TYPE>
00222 int
00223 ACE_Arg_Shifter_T<CHAR_TYPE>::num_ignored_args (void) const
00224 {
00225   return this->front_;
00226 }
00227 
00228 ACE_END_VERSIONED_NAMESPACE_DECL
00229 
00230 #endif /* ACE_ATOMIC_OP_T_CPP */

Generated on Thu Nov 9 09:41:45 2006 for ACE by doxygen 1.3.6