Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Arg_Shifter.cpp

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

Generated on Mon Jun 16 11:19:03 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002