00001
00002
00003 #include "ace/Argv_Type_Converter.h"
00004
00005 #if !defined (__ACE_INLINE__)
00006 #include "ace/Argv_Type_Converter.inl"
00007 #endif
00008
00009 ACE_RCSID (ace,
00010 Argv_Type_Converter,
00011 "Argv_Type_Converter.cpp,v 1.13 2006/05/31 14:22:17 schmidt Exp")
00012
00013 #include "ace/OS_NS_string.h"
00014 #include "ace/OS_Errno.h"
00015
00016 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00017
00018 #if defined (ACE_USES_WCHAR)
00019 ACE_Argv_Type_Converter::ACE_Argv_Type_Converter (int &argc, wchar_t** argv)
00020 : saved_argc_ (argc),
00021 char_argv_ (0),
00022 wchar_argv_ (argv),
00023 before_pass_argc_ (argc),
00024 original_type_ (1),
00025 wchar_passed_ (0),
00026 char_passed_ (0)
00027 {
00028 this->initialize ();
00029
00030 for (int i = 0; i < argc; ++i)
00031 this->char_argv_[i] = ACE_OS::strdup (ACE_TEXT_ALWAYS_CHAR (argv[i]));
00032 }
00033 #endif // ACE_USES_WCHAR
00034
00035
00036 ACE_Argv_Type_Converter::ACE_Argv_Type_Converter (int &argc, char** argv)
00037 : saved_argc_(argc),
00038 char_argv_(argv)
00039 #if defined (ACE_USES_WCHAR)
00040 , wchar_argv_(0),
00041 before_pass_argc_(argc),
00042 original_type_(0),
00043 wchar_passed_(0),
00044 char_passed_(0)
00045 {
00046 this->initialize();
00047
00048 for (int i = 0; i < argc; ++i)
00049 this->wchar_argv_[i] = ACE_OS::strdup (ACE_TEXT_ANTI_TO_TCHAR (argv[i]));
00050 }
00051 #else
00052 {
00053 }
00054 #endif // ACE_USES_WCHAR
00055
00056 ACE_Argv_Type_Converter::~ACE_Argv_Type_Converter (void)
00057 {
00058 #if defined (ACE_USES_WCHAR)
00059
00060 if (this->original_type_)
00061 {
00062
00063 if (this->char_passed_)
00064 this->align_wchar_with_char ();
00065
00066 for (int i = 0; i < this->before_pass_argc_; ++i)
00067 ACE_OS::free (this->char_argv_[i]);
00068
00069 delete [] this->char_argv_;
00070 }
00071 else
00072 {
00073
00074 if (this->wchar_passed_)
00075 this->align_char_with_wchar ();
00076
00077 for (int i = 0; i < this->before_pass_argc_; ++i)
00078 ACE_OS::free (this->wchar_argv_[i]);
00079
00080 delete [] this->wchar_argv_;
00081 }
00082 #endif // ACE_USES_WCHAR
00083 }
00084
00085 #if defined (ACE_USES_WCHAR)
00086 void
00087 ACE_Argv_Type_Converter::initialize (void)
00088 {
00089 if (this->original_type_)
00090 {
00091
00092
00093 ACE_NEW (char_argv_,
00094 char *[this->saved_argc_ + 1]);
00095 this->char_argv_[saved_argc_] = 0;
00096
00097 }
00098 else
00099 {
00100
00101 ACE_NEW (this->wchar_argv_,
00102 wchar_t*[this->saved_argc_ + 1]);
00103 this->wchar_argv_[saved_argc_] = 0;
00104 }
00105 }
00106
00107
00108 void
00109 ACE_Argv_Type_Converter::align_char_with_wchar (void)
00110 {
00111 int wchar_argv_index = 0;
00112 wchar_t* match_argv = this->wchar_argv_[0];
00113
00114 while (wchar_argv_index < this->saved_argc_)
00115 {
00116
00117 if (ACE_OS::strcmp (this->char_argv_[wchar_argv_index],
00118 ACE_TEXT_ALWAYS_CHAR (match_argv)) != 0)
00119 {
00120
00121
00122 for (int i = wchar_argv_index + 1; i < before_pass_argc_; ++i)
00123 {
00124 if (ACE_OS::strcmp (this->char_argv_[i],
00125 ACE_TEXT_ALWAYS_CHAR (match_argv)) == 0)
00126 {
00127
00128 char *temp = this->char_argv_[wchar_argv_index];
00129 this->char_argv_[wchar_argv_index] = this->char_argv_[i];
00130 this->char_argv_[i] = temp;
00131 break;
00132 }
00133 }
00134 }
00135
00136
00137 match_argv = this->wchar_argv_[++wchar_argv_index];
00138 }
00139
00140 this->cleanup ();
00141 }
00142
00143 void
00144 ACE_Argv_Type_Converter::align_wchar_with_char (void)
00145 {
00146 int char_argv_index = 0;
00147 char* match_argv = this->char_argv_[0];
00148
00149 while (char_argv_index < saved_argc_)
00150 {
00151
00152 if (ACE_OS::strcmp (
00153 ACE_TEXT_ALWAYS_CHAR (this->wchar_argv_[char_argv_index]),
00154 match_argv) != 0)
00155 {
00156
00157
00158 for (int i = char_argv_index + 1; i < this->before_pass_argc_; ++i)
00159 {
00160 if (ACE_OS::strcmp (
00161 ACE_TEXT_ALWAYS_CHAR(this->wchar_argv_[i]),
00162 match_argv) == 0) {
00163
00164 wchar_t* temp = this->wchar_argv_[char_argv_index];
00165 this->wchar_argv_[char_argv_index] = this->wchar_argv_[i];
00166 this->wchar_argv_[i] = temp;
00167 break;
00168 }
00169 }
00170 }
00171
00172
00173 match_argv = this->char_argv_[++char_argv_index];
00174 }
00175
00176 this->cleanup();
00177 }
00178
00179 void
00180 ACE_Argv_Type_Converter::cleanup (void)
00181 {
00182 for (int i = this->saved_argc_; i < this->before_pass_argc_; ++i)
00183 {
00184
00185 if (original_type_ == 1)
00186 {
00187 ACE_OS::free (this->char_argv_[i]);
00188 this->char_argv_[i] = 0;
00189 }
00190 else
00191 {
00192 ACE_OS::free (this->wchar_argv_[i]);
00193 this->wchar_argv_[i] = 0;
00194 }
00195 }
00196
00197 this->before_pass_argc_ = this->saved_argc_;
00198
00199 this->wchar_passed_ = 0;
00200 this->char_passed_ = 0;
00201 }
00202 #endif // ACE_USES_WCHAR
00203
00204 ACE_END_VERSIONED_NAMESPACE_DECL