ARGV.cpp

Go to the documentation of this file.
00001 // ARGV.cpp,v 4.50 2006/06/09 11:11:06 jwillemsen Exp
00002 
00003 #ifndef ACE_ARGV_CPP
00004 #define ACE_ARGV_CPP
00005 
00006 #include "ace/Log_Msg.h"
00007 #include "ace/OS_NS_unistd.h"
00008 #include "ace/OS_NS_string.h"
00009 #include "ace/OS_Memory.h"
00010 
00011 #if !defined (__ACE_INLINE__)
00012 #include "ace/ARGV.inl"
00013 #endif /* __ACE_INLINE__ */
00014 
00015 ACE_RCSID(ace, ARGV, "ARGV.cpp,v 4.50 2006/06/09 11:11:06 jwillemsen Exp")
00016 
00017 // Open versioned namespace, if enabled by the user.
00018 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00019 
00020 ACE_ALLOC_HOOK_DEFINE (ACE_ARGV)
00021 
00022 template <typename CHAR_TYPE>
00023 void
00024 ACE_ARGV_T<CHAR_TYPE>::dump (void) const
00025 {
00026 #if defined (ACE_HAS_DUMP)
00027   ACE_TRACE ("ACE_ARGV_T::dump");
00028 
00029   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00030   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("argc_ = %d"), this->argc_));
00031 
00032   ACE_ARGV *this_obj = const_cast<ACE_ARGV *> (this);
00033 
00034   for (int i = 0; i < this->argc_; i++)
00035     ACE_DEBUG ((LM_DEBUG,
00036                 ACE_LIB_TEXT ("\nargv_[%i] = %s"),
00037                 i,
00038                 this_obj->argv ()[i]));
00039 
00040   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("\nbuf = %s\n"), this->buf_));
00041   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("\n")));
00042   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00043 #endif /* ACE_HAS_DUMP */
00044 }
00045 
00046 // Creates this->argv_ out of this->buf_.  New memory is allocated for
00047 // each element of the array.  This is used by the array-to-string
00048 // style constructor and for creating this->argv_ when in iterative
00049 // mode.
00050 
00051 template <typename CHAR_TYPE>
00052 int
00053 ACE_ARGV_T<CHAR_TYPE>::string_to_argv (void)
00054 {
00055   ACE_TRACE ("ACE_ARGV_T::string_to_argv");
00056 
00057   return ACE_OS::string_to_argv (this->buf_,
00058                                  this->argc_,
00059                                  this->argv_,
00060                                  this->substitute_env_args_);
00061 }
00062 
00063 template <typename CHAR_TYPE>
00064 int
00065 ACE_ARGV_T<CHAR_TYPE>::argv_to_string (CHAR_TYPE **argv, CHAR_TYPE *&buf)
00066 {
00067   return ACE_OS::argv_to_string (argv, buf);
00068 }
00069 
00070 template <typename CHAR_TYPE>
00071 ACE_ARGV_T<CHAR_TYPE>::ACE_ARGV_T (const CHAR_TYPE buf[],
00072                                    bool substitute_env_args)
00073   : substitute_env_args_ (substitute_env_args),
00074     iterative_ (false),
00075     argc_ (0),
00076     argv_ (0),
00077     buf_ (0),
00078     length_ (0),
00079     queue_ ()
00080 {
00081   ACE_TRACE ("ACE_ARGV_T::ACE_ARGV_T CHAR_TYPE[] to CHAR_TYPE *[]");
00082 
00083   if (buf == 0 || buf[0] == 0)
00084     return;
00085 
00086   // Make an internal copy of the string.
00087   ACE_NEW (this->buf_,
00088            CHAR_TYPE[ACE_OS::strlen (buf) + 1]);
00089   ACE_OS::strcpy (this->buf_, buf);
00090 
00091   // Create this->argv_.
00092   if (this->string_to_argv () == -1)
00093     ACE_ERROR ((LM_ERROR,
00094                 ACE_LIB_TEXT ("%p\n"),
00095                 ACE_LIB_TEXT ("string_to_argv")));
00096 }
00097 
00098 template <typename CHAR_TYPE>
00099 ACE_ARGV_T<CHAR_TYPE>::ACE_ARGV_T (CHAR_TYPE *argv[],
00100                                    bool substitute_env_args)
00101   : substitute_env_args_ (substitute_env_args),
00102     iterative_ (false),
00103     argc_ (0),
00104     argv_ (0),
00105     buf_ (0),
00106     length_ (0),
00107     queue_ ()
00108 {
00109   ACE_TRACE ("ACE_ARGV_T::ACE_ARGV_T CHAR_TYPE*[] to CHAR_TYPE[]");
00110 
00111   if (argv == 0 || argv[0] == 0)
00112     return;
00113 
00114   this->argc_ = ACE_OS::argv_to_string (argv, this->buf_, substitute_env_args);
00115 }
00116 
00117 template <typename CHAR_TYPE>
00118 ACE_ARGV_T<CHAR_TYPE>::ACE_ARGV_T (CHAR_TYPE *first_argv[],
00119                                    CHAR_TYPE *second_argv[],
00120                                    bool substitute_env_args)
00121   : substitute_env_args_ (substitute_env_args),
00122     iterative_ (false),
00123     argc_ (0),
00124     argv_ (0),
00125     buf_ (0),
00126     length_ (0),
00127     queue_ ()
00128 {
00129   ACE_TRACE ("ACE_ARGV_T::ACE_ARGV_T CHAR_TYPE*[] + CHAR_TYPE *[] to CHAR_TYPE[]");
00130 
00131   int first_argc;
00132   int second_argc;
00133 
00134   CHAR_TYPE *first_buf;
00135   CHAR_TYPE *second_buf;
00136 
00137   // convert the first argv to a string
00138   first_argc = this->argv_to_string (first_argv, first_buf);
00139 
00140   // convert the second argv to a string
00141   second_argc = this->argv_to_string (second_argv, second_buf);
00142 
00143   // Add the number of arguments in both the argvs.
00144   this->argc_ = first_argc + second_argc;
00145 
00146   size_t buf_len =
00147     ACE_OS::strlen (first_buf) + ACE_OS::strlen (second_buf) + 1;
00148 
00149   // Allocate memory to the lenght of the combined argv string.
00150   ACE_NEW (this->buf_,
00151            CHAR_TYPE[buf_len + 1]);
00152 
00153   // copy the first argv string to the buffer
00154   ACE_OS::strcpy (this->buf_, first_buf);
00155 
00156   // concatenate the second argv string to the buffer
00157   ACE_OS::strcat (this->buf_, second_buf);
00158 
00159   //   Delete the first and second buffers
00160 
00161   delete [] first_buf;
00162 
00163   delete [] second_buf;
00164 }
00165 
00166 template <typename CHAR_TYPE>
00167 ACE_ARGV_T<CHAR_TYPE>::ACE_ARGV_T (bool substitute_env_args)
00168   : substitute_env_args_ (substitute_env_args),
00169     iterative_ (true),
00170     argc_ (0),
00171     argv_ (0),
00172     buf_ (0),
00173     length_ (0),
00174     queue_ ()
00175 {
00176   ACE_TRACE ("ACE_ARGV_T::ACE_ARGV_T Iterative");
00177 
00178   // Nothing to do yet -- the user puts in arguments via add ()
00179 }
00180 
00181 template <typename CHAR_TYPE>
00182 int
00183 ACE_ARGV_T<CHAR_TYPE>::add (const CHAR_TYPE *next_arg)
00184 {
00185   // Only allow this to work in the "iterative" verion -- the
00186   // ACE_ARGVs created with the one argument constructor.
00187   if (!this->iterative_)
00188     {
00189       errno = EINVAL;
00190       return -1;
00191     }
00192 
00193   // Put the new argument at the end of the queue.
00194   if (this->queue_.enqueue_tail (const_cast <CHAR_TYPE *> (next_arg)) == -1)
00195     ACE_ERROR_RETURN ((LM_ERROR,
00196                        ACE_LIB_TEXT ("Can't add more to ARGV queue")),
00197                       -1);
00198 
00199   this->length_ += ACE_OS::strlen (next_arg);
00200 
00201   this->argc_++;
00202 
00203   // Wipe argv_ and buf_ away so that they will be recreated if the
00204   // user calls argv () or buf ().
00205   if (this->argv_ != 0)
00206     {
00207       for (int i = 0; this->argv_[i] != 0; i++)
00208         ACE_OS::free ((void *) this->argv_[i]);
00209 
00210       delete [] this->argv_;
00211       this->argv_ = 0;
00212     }
00213 
00214   delete [] this->buf_;
00215   this->buf_ = 0;
00216 
00217   return 0;
00218 }
00219 
00220 template <typename CHAR_TYPE>
00221 int
00222 ACE_ARGV_T<CHAR_TYPE>::add (CHAR_TYPE *argv[])
00223 {
00224   for (int i = 0; argv[i] != 0; i++)
00225     if (this->add (argv[i]) == -1)
00226       return -1;
00227 
00228   return 0;
00229 }
00230 
00231 // Free up argv_ and buf_
00232 
00233 template <typename CHAR_TYPE>
00234 ACE_ARGV_T<CHAR_TYPE>::~ACE_ARGV_T (void)
00235 {
00236   ACE_TRACE ("ACE_ARGV_T::~ACE_ARGV_T");
00237 
00238   if (this->argv_ != 0)
00239     for (int i = 0; this->argv_[i] != 0; i++)
00240       ACE_OS::free ((void *) this->argv_[i]);
00241 
00242   delete [] this->argv_;
00243   delete [] this->buf_;
00244 }
00245 
00246 // Create buf_ out of the queue_.  This is only used in the
00247 // "iterative" mode.
00248 
00249 template <typename CHAR_TYPE>
00250 int
00251 ACE_ARGV_T<CHAR_TYPE>::create_buf_from_queue (void)
00252 {
00253   ACE_TRACE ("ACE_ARGV_T::create_buf_from_queue");
00254 
00255   // If the are no arguments, don't do anything
00256   if (this->argc_ <= 0)
00257     return -1;
00258 
00259   delete [] this->buf_;
00260 
00261   ACE_NEW_RETURN (this->buf_,
00262                   CHAR_TYPE[this->length_ + this->argc_],
00263                   -1);
00264 
00265   // Get an iterator over the queue
00266   ACE_Unbounded_Queue_Iterator<CHAR_TYPE *> iter (this->queue_);
00267 
00268   CHAR_TYPE **arg = 0;
00269   CHAR_TYPE *ptr = this->buf_;
00270   size_t len;
00271   int more = 0;
00272 
00273   while (!iter.done ())
00274     {
00275       // Get next argument from the queue.
00276       iter.next (arg);
00277 
00278       more = iter.advance ();
00279 
00280       len = ACE_OS::strlen (*arg);
00281 
00282       // Copy the argument into buf_
00283       ACE_OS::memcpy ((void *) ptr,
00284                       (const void *) (*arg),
00285                       len * sizeof (CHAR_TYPE));
00286       // Move the pointer down.
00287       ptr += len;
00288 
00289       // Put in an argument separating space.
00290       if (more != 0)
00291         *ptr++ = ' ';
00292     }
00293 
00294   // Put in the NUL terminator
00295   *ptr = '\0';
00296 
00297   return 0;
00298 }
00299 
00300 // Close versioned namespace, if enabled by the user.
00301 ACE_END_VERSIONED_NAMESPACE_DECL
00302 
00303 #endif /* ACE_ARGV_CPP */

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