Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / tests / ARGV_Test.cpp
blob64f8e106264ae3f2915eac62648589c6fd3104b3
1 // ============================================================================
2 //
3 // = LIBRARY
4 // tests
5 //
6 // = DESCRIPTION
7 // This simple test illustrates how to use advanced features of
8 // <ACE_ARGV>.
9 //
10 // = AUTHOR
11 // Suresh Kannan <kannan@uav.ae.gatech.edu> and
12 // Duane Binder <duane.binder@veritas.com>
14 // ============================================================================
16 #include "ace/ARGV.h"
17 #include "ace/Arg_Shifter.h"
18 #include "ace/OS_NS_string.h"
19 #include "ace/Argv_Type_Converter.h"
20 #include "test_config.h"
23 static void
24 consume_arg (int argc, ACE_TCHAR *argv[])
26 ACE_Arg_Shifter arg_shifter (argc, argv);
28 if (arg_shifter.is_anything_left ())
29 arg_shifter.consume_arg (1);
30 // Once we initialize an arg_shifter, we must iterate through it all!
31 while ((arg_shifter.is_anything_left ()))
32 arg_shifter.ignore_arg (1);
35 static int
36 test_simple_argv (ACE_TCHAR *argv[])
38 // From command line.
39 ACE_ARGV cl (argv);
41 // My own stuff.
42 ACE_ARGV my;
44 // Add to my stuff.
45 my.add (ACE_TEXT ("-ORBEndpoint iiop://localhost:12345"));
47 // Combine the two (see the ace/ARGV.h constructors documentation).
48 ACE_ARGV a (cl.argv (),
49 my.argv ());
51 ACE_DEBUG ((LM_DEBUG,
52 ACE_TEXT ("argc = %d\n"),
53 a.argc ()));
55 // Print the contents of the combined <ACE_ARGV>.
56 for (int i = 0; i < a.argc (); i++)
57 ACE_DEBUG ((LM_DEBUG,
58 ACE_TEXT (" (%d) %s\n"),
60 a.argv ()[i]));
62 return 0;
65 static int
66 test_argv_type_converter ()
68 char *argv[20];
69 argv[0] = ACE_OS::strdup ("one");
70 argv[1] = ACE_OS::strdup ("two");
71 argv[2] = ACE_OS::strdup ("three");
72 argv[3] = ACE_OS::strdup ("four");
73 argv[4] = 0;
75 char *save_argv[20];
76 ACE_OS::memcpy (save_argv, argv, sizeof (argv));
78 int argc = 4;
81 ACE_Argv_Type_Converter ct2 (argc, argv);
85 ACE_Argv_Type_Converter ct (argc, argv);
86 ct.get_argc (); ct.get_TCHAR_argv ();
87 consume_arg ( ct.get_argc (), ct.get_TCHAR_argv ());
90 ACE_Argv_Type_Converter ct3 (argc, argv);
91 ct3.get_argc (); ct3.get_ASCII_argv ();
92 consume_arg ( ct3.get_argc (), ct3.get_TCHAR_argv ());
96 for (size_t i = 0; i < 4; i++)
97 ACE_DEBUG ((LM_DEBUG,
98 ACE_TEXT (" (%d) %C\n"),
100 argv[i]));
103 for (size_t i = 0; save_argv[i]; ++i)
104 ACE_OS::free (save_argv[i]);
106 return 0;
109 static int
110 test_argv_type_converter2 ()
112 ACE_TCHAR *argv[20];
113 argv[0] = ACE_OS::strdup (ACE_TEXT ("one"));
114 argv[1] = ACE_OS::strdup (ACE_TEXT ("two"));
115 argv[2] = ACE_OS::strdup (ACE_TEXT ("three"));
116 argv[3] = ACE_OS::strdup (ACE_TEXT ("four"));
117 argv[4] = 0;
119 ACE_TCHAR *save_argv[20];
120 ACE_OS::memcpy (save_argv, argv, sizeof (argv));
122 int argc = 4;
125 ACE_Argv_Type_Converter ct (argc, argv);
126 ct.get_argc (); ct.get_TCHAR_argv ();
127 consume_arg ( ct.get_argc (), ct.get_TCHAR_argv ());
130 consume_arg (argc, argv);
133 for (size_t i = 0; i < 4; i++)
134 ACE_DEBUG ((LM_DEBUG,
135 ACE_TEXT (" (%d) %s\n"),
137 argv[i]));
140 for (size_t i = 0; save_argv[i]; ++i)
141 ACE_OS::free (save_argv[i]);
143 return 0;
146 static int
147 test_argv_buf ()
149 pid_t parent_pid = ACE_OS::getpid ();
151 ACE_TCHAR *l_argv[3];
152 ACE_TCHAR pid_str[100];
153 // Store the parent's process id so we can pass it to the child
154 // portably. Also, pass the test number, as well.
155 ACE_OS::snprintf (pid_str, 100,
156 ACE_TEXT ("-p %ld -t %d"),
157 static_cast <long> (parent_pid),
160 // We're going to create a new process that runs this program again,
161 // so we need to indicate that it's the child.
162 const ACE_TCHAR *t = ACE_TEXT (".")
163 ACE_DIRECTORY_SEPARATOR_STR
164 ACE_TEXT ("Signal_Test")
165 ACE_PLATFORM_EXE_SUFFIX
166 ACE_TEXT (" -c");
167 l_argv[0] = const_cast <ACE_TCHAR *> (t);
168 l_argv[1] = pid_str;
169 l_argv[2] = 0;
171 ACE_ARGV my_argv (l_argv);
173 // This shouldn't have any quotes in it.
174 ACE_DEBUG ((LM_DEBUG, "%s\n", my_argv.buf ()));
175 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("argc: %d\n"), my_argv.argc ()));
176 return 0;
179 static int
180 test_argv_quotes ()
182 const ACE_TCHAR *argv_const[] = { ACE_TEXT ("first without quotes"),
183 ACE_TEXT ("'second in single quotes'"),
184 ACE_TEXT ("\"third in double quotes\""),
187 ACE_TCHAR **argv = const_cast<ACE_TCHAR **> (argv_const);
188 // The convoluted argv definition is to silence warnings from gcc 4.3, which
189 // warns if the string-literals are not assigned to const char* and also
190 // warns if the const char*[] is passed to the ACE_ARGV constructor as in
191 // "args3" below.
193 int argc = 3;
195 // (argc, argv)
196 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n*** argv ***\n")));
197 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("argc: %d\n"), argc));
199 for (int i = 0; i < argc; ++i)
200 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("argv[%02d] = %s\n"), i, argv[i]));
202 // args
203 ACE_ARGV args;
204 for (int i = 0; i < argc; ++i)
205 args.add (argv[i]);
206 args.add (ACE_TEXT ("'fourth in single quotes'"));
207 args.add (ACE_TEXT ("\"fifth in double quotes\""));
208 args.add (ACE_TEXT ("sixth without any quotes"));
210 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n*** args-1 ***\n")));
211 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argc (): %d\n"), args.argc ()));
213 for (int i = 0; i < args.argc (); ++i)
214 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args[%02d]: %s\n"), i, args[i]));
216 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argc (): %d\n"), args.argc ()));
218 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n*** args-2 ***\n")));
219 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argc (): %d\n"), args.argc ()));
221 for (int i = 0; i < args.argc (); ++i)
222 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argv ()[%02d]: %s\n"),
223 i, args.argv ()[i]));
225 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argc (): %d\n"), args.argc ()));
227 // args2
228 ACE_ARGV args2;
229 for (int i = 0; i < argc; ++i)
230 args2.add (argv[i], true);
231 args2.add (ACE_TEXT ("'fourth in single quotes'"), true);
232 args2.add (ACE_TEXT ("\"fifth in double quotes\""), true);
233 args2.add (ACE_TEXT ("sixth without any quotes"), true);
235 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n*** args-3 ***\n")));
236 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argc (): %d\n"), args2.argc ()));
238 for (int i = 0; i < args2.argc (); ++i)
239 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args[%02d]: %s\n"), i, args2[i]));
241 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argc (): %d\n"), args2.argc ()));
243 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n*** args-4 ***\n")));
244 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argc (): %d\n"), args2.argc ()));
246 for (int i = 0; i < args2.argc (); ++i)
247 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argv ()[%02d]: %s\n"),
248 i, args2.argv ()[i]));
250 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argc (): %d\n"), args2.argc ()));
252 // args3
253 ACE_ARGV args3 (argv);
255 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n*** args-5 ***\n")));
256 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argc (): %d\n"), args3.argc ()));
258 for (int i = 0; i < args3.argc (); ++i)
259 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args[%02d]: %s\n"), i, args3[i]));
261 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argc (): %d\n"), args3.argc ()));
263 return 0;
267 run_main (int, ACE_TCHAR *argv[])
269 ACE_START_TEST (ACE_TEXT ("ARGV_Test"));
271 test_simple_argv (argv);
272 test_argv_type_converter2 ();
273 test_argv_type_converter ();
274 test_argv_quotes ();
275 test_argv_buf ();
277 ACE_END_TEST;
278 return 0;