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

Argv_Type_Converter.cpp

Go to the documentation of this file.
00001 #include "ace_pch.h"
00002 // $Id: Argv_Type_Converter.cpp,v 1.1.1.1.2.1 2003/03/13 19:44:20 chad Exp $
00003 
00004 #include "ace/Argv_Type_Converter.h"
00005 
00006 #if !defined (__ACE_INLINE__)
00007 #include "ace/Argv_Type_Converter.inl"
00008 #endif  /* __ACE_INLINE__ */
00009 
00010 ACE_RCSID (ace,
00011            Argv_Type_Converter,
00012            "$Id: Argv_Type_Converter.cpp,v 1.1.1.1.2.1 2003/03/13 19:44:20 chad Exp $")
00013 
00014 #include "ace/OS_String.h"
00015 #include "ace/OS_Errno.h"
00016 
00017 #if defined (ACE_USES_WCHAR)
00018 ACE_Argv_Type_Converter::ACE_Argv_Type_Converter(int &argc, wchar_t** argv)
00019   : saved_argc_ (argc)
00020     , char_argv_ (0)
00021     , wchar_argv_ (argv)
00022     , before_pass_argc_ (argc)
00023     , original_type_ (1)
00024     , wchar_passed_ (0)
00025     , char_passed_ (0)
00026 {
00027   this->initialize ();
00028 
00029   for (int i = 0; i < argc; ++i)
00030     {
00031       this->char_argv_[i] =
00032         ACE_OS_String::strdup (ACE_TEXT_ALWAYS_CHAR (argv[i]));
00033     }
00034 }
00035 #endif  // ACE_USES_WCHAR
00036 
00037 
00038 ACE_Argv_Type_Converter::ACE_Argv_Type_Converter(int &argc, char** argv)
00039   : saved_argc_(argc)
00040   , char_argv_(argv)
00041 #if defined (ACE_USES_WCHAR)
00042     , wchar_argv_(0)
00043     , before_pass_argc_(argc)
00044     , original_type_(0)
00045     , wchar_passed_(0)
00046     , char_passed_(0)
00047 {
00048   this->initialize();
00049 
00050   for (int i = 0; i < argc; ++i)
00051     {
00052       this->wchar_argv_[i] =
00053         ACE_OS_String::strdup (ACE_TEXT_ANTI_TO_TCHAR (argv[i]));
00054     }
00055 }
00056 #else
00057 {
00058 }
00059 #endif  // ACE_USES_WCHAR
00060 
00061 ACE_Argv_Type_Converter::~ACE_Argv_Type_Converter (void)
00062 {
00063 #if defined (ACE_USES_WCHAR)
00064   // selectively delete the 'copy' of argv
00065   if (this->original_type_)
00066     {
00067       // if original type is wchar_t
00068       if (this->char_passed_)
00069         {
00070           this->align_wchar_with_char();
00071         }
00072       for (int i = 0; i < this->before_pass_argc_; ++i)
00073         {
00074           delete [] this->char_argv_[i];
00075         }
00076       delete [] this->char_argv_;
00077     }
00078   else
00079     {
00080       // if original type is char
00081       if (this->wchar_passed_) {
00082         this->align_char_with_wchar ();
00083       }
00084       for (int i = 0; i < this->before_pass_argc_; ++i)
00085         {
00086           delete [] this->wchar_argv_[i];
00087         }
00088       delete [] this->wchar_argv_;
00089     }
00090 #endif  // ACE_USES_WCHAR
00091 }
00092 
00093 #if defined (ACE_USES_WCHAR)
00094 void
00095 ACE_Argv_Type_Converter::initialize (void)
00096 {
00097   if (this->original_type_)
00098     {  // make a copy of argv in 'char' type
00099       // Create one more argv entry than original argc for the NULL.
00100       ACE_NEW (char_argv_, char*[this->saved_argc_ + 1]);
00101       this->char_argv_[saved_argc_] = 0;  // last entry of argv is
00102                                           // always a NULL
00103     }
00104   else
00105     {  // make a copy of argv in 'wchar_t' type
00106       ACE_NEW (this->wchar_argv_, wchar_t*[this->saved_argc_ + 1]);
00107       this->wchar_argv_[saved_argc_] = 0;
00108     }
00109 }
00110 
00111 
00112 void
00113 ACE_Argv_Type_Converter::align_char_with_wchar (void)
00114 {
00115   int wchar_argv_index = 0;
00116   wchar_t* match_argv = this->wchar_argv_[0];  // pick the initial entry
00117 
00118   while (wchar_argv_index < this->saved_argc_)
00119     {
00120       // if n'th entries of both argv lists are different
00121       if (ACE_OS_String::strcmp(this->char_argv_[wchar_argv_index],
00122                                 ACE_TEXT_ALWAYS_CHAR (match_argv)) != 0)
00123         {
00124           // loop through the wchar argv list entries that are after
00125           // wchar_argv_index
00126           for (int i = wchar_argv_index + 1; i < before_pass_argc_; ++i)
00127             {
00128               if (ACE_OS_String::strcmp (this->char_argv_[i],
00129                                          ACE_TEXT_ALWAYS_CHAR (match_argv))
00130                   == 0)
00131                 {
00132                   // swap the pointers in the char argv list
00133                   char* temp = this->char_argv_[wchar_argv_index];
00134                   this->char_argv_[wchar_argv_index] = this->char_argv_[i];
00135                   this->char_argv_[i] = temp;
00136                   break;
00137                 }
00138             }
00139         }
00140 
00141         // move to the next wchar argv list entry
00142         match_argv = this->wchar_argv_[++wchar_argv_index];
00143     }
00144 
00145   this->cleanup ();
00146 }
00147 
00148 void
00149 ACE_Argv_Type_Converter::align_wchar_with_char (void)
00150 {
00151   int char_argv_index = 0;
00152   char* match_argv = this->char_argv_[0];  // pick the initial entry
00153 
00154   while (char_argv_index < saved_argc_)
00155     {
00156       // if n'th entries of both argv lists are different
00157       if (ACE_OS_String::strcmp (
00158             ACE_TEXT_ALWAYS_CHAR (this->wchar_argv_[char_argv_index]),
00159             match_argv) != 0)
00160         {
00161           // loop through the wchar argv list entries that are after
00162           // wchar_argv_index
00163           for (int i = char_argv_index + 1; i < this->before_pass_argc_; ++i)
00164             {
00165               if (ACE_OS_String::strcmp (
00166                     ACE_TEXT_ALWAYS_CHAR(this->wchar_argv_[i]),
00167                     match_argv) == 0) {
00168                 // swap the pointers in the char argv list
00169                 wchar_t* temp = this->wchar_argv_[char_argv_index];
00170                 this->wchar_argv_[char_argv_index] = this->wchar_argv_[i];
00171                 this->wchar_argv_[i] = temp;
00172                 break;
00173               }
00174             }
00175         }
00176 
00177       // move to the next wchar argv list entry
00178       match_argv = this->char_argv_[++char_argv_index];
00179     }
00180 
00181   this->cleanup();
00182 }
00183 
00184 void
00185 ACE_Argv_Type_Converter::cleanup (void)
00186 {
00187   for (int i = this->saved_argc_; i < this->before_pass_argc_; ++i)
00188     {
00189       delete [] this->char_argv_[i];
00190       delete [] this->wchar_argv_[i];
00191 
00192       this->char_argv_[i] = 0;
00193       this->wchar_argv_[i] = 0;
00194     }
00195 
00196   this->before_pass_argc_ = this->saved_argc_;
00197 
00198   this->wchar_passed_ = 0;
00199   this->char_passed_ = 0;
00200 }
00201 #endif  // ACE_USES_WCHAR

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