[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / dep / ACE_wrappers / ace / ARGV.cpp
blobedfd4efa790278bff944bb5556ab49152b56cb84
1 // $Id: ARGV.cpp 81374 2008-04-16 13:07:47Z iliyan $
3 #ifndef ACE_ARGV_CPP
4 #define ACE_ARGV_CPP
6 #include "ace/Log_Msg.h"
7 #include "ace/OS_NS_unistd.h"
8 #include "ace/OS_NS_string.h"
9 #include "ace/OS_Memory.h"
11 #if !defined (__ACE_INLINE__)
12 #include "ace/ARGV.inl"
13 #endif /* __ACE_INLINE__ */
15 ACE_RCSID(ace, ARGV, "$Id: ARGV.cpp 81374 2008-04-16 13:07:47Z iliyan $")
17 // Open versioned namespace, if enabled by the user.
18 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
20 ACE_ALLOC_HOOK_DEFINE (ACE_ARGV_Queue_Entry)
21 ACE_ALLOC_HOOK_DEFINE (ACE_ARGV)
23 template <typename CHAR_TYPE>
24 void
25 ACE_ARGV_Queue_Entry_T<CHAR_TYPE>::dump (void) const
27 #if defined (ACE_HAS_DUMP)
28 ACE_TRACE ("ACE_ARGV_Queue_Entry_T::dump");
30 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
31 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("arg_ = %s"), this->arg_));
32 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("quote_arg_ = %d"), (int)this->quote_arg_));
33 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
34 #endif /* ACE_HAS_DUMP */
37 template <typename CHAR_TYPE>
38 void
39 ACE_ARGV_T<CHAR_TYPE>::dump (void) const
41 #if defined (ACE_HAS_DUMP)
42 ACE_TRACE ("ACE_ARGV_T::dump");
44 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
45 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("argc_ = %d"), this->argc_));
47 ACE_ARGV *this_obj = const_cast<ACE_ARGV *> (this);
49 for (int i = 0; i < this->argc_; i++)
50 ACE_DEBUG ((LM_DEBUG,
51 ACE_TEXT ("\nargv_[%i] = %s"),
53 this_obj->argv ()[i]));
55 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nbuf = %s\n"), this->buf_));
56 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
57 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
58 #endif /* ACE_HAS_DUMP */
61 // Creates this->argv_ out of this->buf_. New memory is allocated for
62 // each element of the array. This is used by the array-to-string
63 // style constructor and for creating this->argv_ when in iterative
64 // mode.
66 template <typename CHAR_TYPE>
67 int
68 ACE_ARGV_T<CHAR_TYPE>::string_to_argv (void)
70 ACE_TRACE ("ACE_ARGV_T::string_to_argv");
72 return ACE_OS::string_to_argv (this->buf_,
73 this->argc_,
74 this->argv_,
75 this->substitute_env_args_);
78 template <typename CHAR_TYPE>
79 ACE_ARGV_T<CHAR_TYPE>::ACE_ARGV_T (const CHAR_TYPE buf[],
80 bool substitute_env_args)
81 : substitute_env_args_ (substitute_env_args),
82 iterative_ (false),
83 argc_ (0),
84 argv_ (0),
85 buf_ (0),
86 length_ (0),
87 queue_ ()
89 ACE_TRACE ("ACE_ARGV_T::ACE_ARGV_T CHAR_TYPE[] to CHAR_TYPE *[]");
91 if (buf == 0 || buf[0] == 0)
92 return;
94 // Make an internal copy of the string.
95 ACE_NEW (this->buf_,
96 CHAR_TYPE[ACE_OS::strlen (buf) + 1]);
97 ACE_OS::strcpy (this->buf_, buf);
99 // Create this->argv_.
100 if (this->string_to_argv () == -1)
101 ACE_ERROR ((LM_ERROR,
102 ACE_TEXT ("%p\n"),
103 ACE_TEXT ("string_to_argv")));
106 template <typename CHAR_TYPE>
107 ACE_ARGV_T<CHAR_TYPE>::ACE_ARGV_T (CHAR_TYPE *argv[],
108 bool substitute_env_args,
109 bool quote_arg)
110 : substitute_env_args_ (substitute_env_args),
111 iterative_ (false),
112 argc_ (0),
113 argv_ (0),
114 buf_ (0),
115 length_ (0),
116 queue_ ()
118 ACE_TRACE ("ACE_ARGV_T::ACE_ARGV_T CHAR_TYPE*[] to CHAR_TYPE[]");
120 if (argv == 0 || argv[0] == 0)
121 return;
123 this->argc_ = ACE_OS::argv_to_string (argv,
124 this->buf_,
125 substitute_env_args,
126 quote_arg);
129 template <typename CHAR_TYPE>
130 ACE_ARGV_T<CHAR_TYPE>::ACE_ARGV_T (int argc,
131 CHAR_TYPE *argv[],
132 bool substitute_env_args,
133 bool quote_arg)
134 : substitute_env_args_ (substitute_env_args),
135 iterative_ (false),
136 argc_ (0),
137 argv_ (0),
138 buf_ (0),
139 length_ (0),
140 queue_ ()
142 ACE_TRACE ("ACE_ARGV_T::ACE_ARGV_T int,CHAR_TYPE*[] to CHAR_TYPE[]");
144 this->argc_ = ACE_OS::argv_to_string (argc,
145 argv,
146 this->buf_,
147 substitute_env_args,
148 quote_arg);
152 template <typename CHAR_TYPE>
153 ACE_ARGV_T<CHAR_TYPE>::ACE_ARGV_T (CHAR_TYPE *first_argv[],
154 CHAR_TYPE *second_argv[],
155 bool substitute_env_args,
156 bool quote_args)
157 : substitute_env_args_ (substitute_env_args),
158 iterative_ (false),
159 argc_ (0),
160 argv_ (0),
161 buf_ (0),
162 length_ (0),
163 queue_ ()
165 ACE_TRACE ("ACE_ARGV_T::ACE_ARGV_T CHAR_TYPE*[] + CHAR_TYPE *[] to CHAR_TYPE[]");
167 int first_argc = 0;
168 int second_argc = 0;
170 CHAR_TYPE *first_buf = 0;
171 CHAR_TYPE *second_buf = 0;
173 // convert the first argv to a string
174 if (first_argv != 0 && first_argv[0] != 0)
176 first_argc = ACE_OS::argv_to_string (first_argv,
177 first_buf,
178 substitute_env_args,
179 quote_args);
182 // convert the second argv to a string
183 if (second_argv != 0 && second_argv[0] != 0)
185 second_argc = ACE_OS::argv_to_string (second_argv,
186 second_buf,
187 substitute_env_args,
188 quote_args);
191 // Add the number of arguments in both the argvs.
192 this->argc_ = first_argc + second_argc;
194 size_t buf_len =
195 ACE_OS::strlen (first_buf) + ACE_OS::strlen (second_buf) + 1;
197 // Allocate memory to the lenght of the combined argv string.
198 ACE_NEW (this->buf_,
199 CHAR_TYPE[buf_len + 1]);
201 // copy the first argv string to the buffer
202 ACE_OS::strcpy (this->buf_, first_buf);
204 // concatenate the second argv string to the buffer
205 ACE_OS::strcat (this->buf_, second_buf);
207 // Delete the first and second buffers
208 delete [] first_buf;
209 delete [] second_buf;
212 template <typename CHAR_TYPE>
213 ACE_ARGV_T<CHAR_TYPE>::ACE_ARGV_T (bool substitute_env_args)
214 : substitute_env_args_ (substitute_env_args),
215 iterative_ (true),
216 argc_ (0),
217 argv_ (0),
218 buf_ (0),
219 length_ (0),
220 queue_ ()
222 ACE_TRACE ("ACE_ARGV_T::ACE_ARGV_T Iterative");
224 // Nothing to do yet -- the user puts in arguments via add ()
227 template <typename CHAR_TYPE>
229 ACE_ARGV_T<CHAR_TYPE>::add (const CHAR_TYPE *next_arg, bool quote_arg)
231 // Only allow this to work in the "iterative" verion -- the
232 // ACE_ARGVs created with the one argument constructor.
233 if (!this->iterative_)
235 errno = EINVAL;
236 return -1;
239 this->length_ += ACE_OS::strlen (next_arg);
240 if (quote_arg && ACE_OS::strchr (next_arg, ' ') != 0)
242 this->length_ += 2;
243 if (ACE_OS::strchr (next_arg, '"') != 0)
244 for (const CHAR_TYPE * p = next_arg; *p != '\0'; ++p)
245 if (*p == '"') ++this->length_;
247 else
249 quote_arg = false;
252 // Put the new argument at the end of the queue.
253 if (this->queue_.enqueue_tail (ACE_ARGV_Queue_Entry_T<CHAR_TYPE> (next_arg, quote_arg)) == -1)
254 ACE_ERROR_RETURN ((LM_ERROR,
255 ACE_TEXT ("Can't add more to ARGV queue")),
256 -1);
258 ++this->argc_;
260 // Wipe argv_ and buf_ away so that they will be recreated if the
261 // user calls argv () or buf ().
262 if (this->argv_ != 0)
264 for (int i = 0; this->argv_[i] != 0; i++)
265 ACE_OS::free ((void *) this->argv_[i]);
267 delete [] this->argv_;
268 this->argv_ = 0;
271 delete [] this->buf_;
272 this->buf_ = 0;
274 return 0;
277 template <typename CHAR_TYPE>
279 ACE_ARGV_T<CHAR_TYPE>::add (CHAR_TYPE *argv[], bool quote_args)
281 for (int i = 0; argv[i] != 0; i++)
282 if (this->add (argv[i], quote_args) == -1)
283 return -1;
285 return 0;
288 // Free up argv_ and buf_
290 template <typename CHAR_TYPE>
291 ACE_ARGV_T<CHAR_TYPE>::~ACE_ARGV_T (void)
293 ACE_TRACE ("ACE_ARGV_T::~ACE_ARGV_T");
295 if (this->argv_ != 0)
296 for (int i = 0; this->argv_[i] != 0; i++)
297 ACE_OS::free ((void *) this->argv_[i]);
299 delete [] this->argv_;
300 delete [] this->buf_;
303 // Create buf_ out of the queue_. This is only used in the
304 // "iterative" mode.
306 template <typename CHAR_TYPE>
308 ACE_ARGV_T<CHAR_TYPE>::create_buf_from_queue (void)
310 ACE_TRACE ("ACE_ARGV_T::create_buf_from_queue");
312 // If the are no arguments, don't do anything
313 if (this->argc_ <= 0)
314 return -1;
316 delete [] this->buf_;
318 ACE_NEW_RETURN (this->buf_,
319 CHAR_TYPE[this->length_ + this->argc_],
320 -1);
322 // Get an iterator over the queue
323 ACE_Unbounded_Queue_Iterator<ACE_ARGV_Queue_Entry_T<CHAR_TYPE> > iter (this->queue_);
325 ACE_ARGV_Queue_Entry_T<CHAR_TYPE> *arg = 0;
326 CHAR_TYPE *ptr = this->buf_;
327 size_t len;
329 while (!iter.done ())
331 // Get next argument from the queue.
332 iter.next (arg);
333 iter.advance ();
335 if (arg->quote_arg_)
337 *ptr++ = '"';
338 if (ACE_OS::strchr (arg->arg_, '"') != 0)
340 CHAR_TYPE prev = 0;
341 for (const CHAR_TYPE * p = arg->arg_; *p != '\0'; ++p)
343 if (*p == '"' && prev != '\\') *ptr++ = '\\';
344 prev = *ptr++ = *p;
347 else
349 len = ACE_OS::strlen (arg->arg_);
350 // Copy the argument into buf_
351 ACE_OS::memcpy ((void *) ptr,
352 (const void *) (arg->arg_),
353 len * sizeof (CHAR_TYPE));
354 // Move the pointer down.
355 ptr += len;
357 *ptr++ = '"';
359 else
361 len = ACE_OS::strlen (arg->arg_);
362 // Copy the argument into buf_
363 ACE_OS::memcpy ((void *) ptr,
364 (const void *) (arg->arg_),
365 len * sizeof (CHAR_TYPE));
366 // Move the pointer down.
367 ptr += len;
370 // Put in an argument separating space.
371 *ptr++ = ' ';
374 // Put in the NUL terminator
375 ptr[-1] = '\0';
377 return 0;
380 // Close versioned namespace, if enabled by the user.
381 ACE_END_VERSIONED_NAMESPACE_DECL
383 #endif /* ACE_ARGV_CPP */