Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / tests / ARGV_Test.cpp
blob59d72dab714c103bc94a7829fdfb178b7709f8fd
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"
24 static void
25 consume_arg (int argc, ACE_TCHAR *argv[])
27 ACE_Arg_Shifter arg_shifter (argc, argv);
29 if (arg_shifter.is_anything_left ())
30 arg_shifter.consume_arg (1);
31 // Once we initialize an arg_shifter, we must iterate through it all!
32 while ((arg_shifter.is_anything_left ()))
33 arg_shifter.ignore_arg (1);
36 static int
37 test_simple_argv (ACE_TCHAR *argv[])
39 // From command line.
40 ACE_ARGV cl (argv);
42 // My own stuff.
43 ACE_ARGV my;
45 // Add to my stuff.
46 my.add (ACE_TEXT ("-ORBEndpoint iiop://localhost:12345"));
48 // Combine the two (see the ace/ARGV.h constructors documentation).
49 ACE_ARGV a (cl.argv (),
50 my.argv ());
52 ACE_DEBUG ((LM_DEBUG,
53 ACE_TEXT ("argc = %d\n"),
54 a.argc ()));
56 // Print the contents of the combined <ACE_ARGV>.
57 for (int i = 0; i < a.argc (); i++)
58 ACE_DEBUG ((LM_DEBUG,
59 ACE_TEXT (" (%d) %s\n"),
61 a.argv ()[i]));
63 return 0;
66 static int
67 test_argv_type_converter (void)
69 char *argv[20];
70 argv[0] = ACE_OS::strdup ("one");
71 argv[1] = ACE_OS::strdup ("two");
72 argv[2] = ACE_OS::strdup ("three");
73 argv[3] = ACE_OS::strdup ("four");
74 argv[4] = 0;
76 char *save_argv[20];
77 ACE_OS::memcpy (save_argv, argv, sizeof (argv));
79 int argc = 4;
82 ACE_Argv_Type_Converter ct2 (argc, argv);
86 ACE_Argv_Type_Converter ct (argc, argv);
87 ct.get_argc (); ct.get_TCHAR_argv ();
88 consume_arg ( ct.get_argc (), ct.get_TCHAR_argv ());
91 ACE_Argv_Type_Converter ct3 (argc, argv);
92 ct3.get_argc (); ct3.get_ASCII_argv ();
93 consume_arg ( ct3.get_argc (), ct3.get_TCHAR_argv ());
97 for (size_t i = 0; i < 4; i++)
98 ACE_DEBUG ((LM_DEBUG,
99 ACE_TEXT (" (%d) %C\n"),
101 argv[i]));
104 for (size_t i = 0; save_argv[i]; ++i)
105 ACE_OS::free (save_argv[i]);
107 return 0;
110 static int
111 test_argv_type_converter2 (void)
113 ACE_TCHAR *argv[20];
114 argv[0] = ACE_OS::strdup (ACE_TEXT ("one"));
115 argv[1] = ACE_OS::strdup (ACE_TEXT ("two"));
116 argv[2] = ACE_OS::strdup (ACE_TEXT ("three"));
117 argv[3] = ACE_OS::strdup (ACE_TEXT ("four"));
118 argv[4] = 0;
120 ACE_TCHAR *save_argv[20];
121 ACE_OS::memcpy (save_argv, argv, sizeof (argv));
123 int argc = 4;
126 ACE_Argv_Type_Converter ct (argc, argv);
127 ct.get_argc (); ct.get_TCHAR_argv ();
128 consume_arg ( ct.get_argc (), ct.get_TCHAR_argv ());
131 consume_arg (argc, argv);
134 for (size_t i = 0; i < 4; i++)
135 ACE_DEBUG ((LM_DEBUG,
136 ACE_TEXT (" (%d) %s\n"),
138 argv[i]));
141 for (size_t i = 0; save_argv[i]; ++i)
142 ACE_OS::free (save_argv[i]);
144 return 0;
147 static int
148 test_argv_buf (void)
150 pid_t parent_pid = ACE_OS::getpid ();
152 ACE_TCHAR *l_argv[3];
153 ACE_TCHAR pid_str[100];
154 // Store the parent's process id so we can pass it to the child
155 // portably. Also, pass the test number, as well.
156 ACE_OS::snprintf (pid_str, 100,
157 ACE_TEXT ("-p %ld -t %d"),
158 static_cast <long> (parent_pid),
161 // We're going to create a new process that runs this program again,
162 // so we need to indicate that it's the child.
163 const ACE_TCHAR *t = ACE_TEXT (".")
164 ACE_DIRECTORY_SEPARATOR_STR
165 ACE_TEXT ("Signal_Test")
166 ACE_PLATFORM_EXE_SUFFIX
167 ACE_TEXT (" -c");
168 l_argv[0] = const_cast <ACE_TCHAR *> (t);
169 l_argv[1] = pid_str;
170 l_argv[2] = 0;
172 ACE_ARGV my_argv (l_argv);
174 // This shouldn't have any quotes in it.
175 ACE_DEBUG ((LM_DEBUG, "%s\n", my_argv.buf ()));
176 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("argc: %d\n"), my_argv.argc ()));
177 return 0;
180 static int
181 test_argv_quotes (void)
183 const ACE_TCHAR *argv_const[] = { ACE_TEXT ("first without quotes"),
184 ACE_TEXT ("'second in single quotes'"),
185 ACE_TEXT ("\"third in double quotes\""),
188 ACE_TCHAR **argv = const_cast<ACE_TCHAR **> (argv_const);
189 // The convoluted argv definition is to silence warnings from gcc 4.3, which
190 // warns if the string-literals are not assigned to const char* and also
191 // warns if the const char*[] is passed to the ACE_ARGV constructor as in
192 // "args3" below.
194 int argc = 3;
196 // (argc, argv)
197 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n*** argv ***\n")));
198 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("argc: %d\n"), argc));
200 for (int i = 0; i < argc; ++i)
201 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("argv[%02d] = %s\n"), i, argv[i]));
203 // args
204 ACE_ARGV args;
205 for (int i = 0; i < argc; ++i)
206 args.add (argv[i]);
207 args.add (ACE_TEXT ("'fourth in single quotes'"));
208 args.add (ACE_TEXT ("\"fifth in double quotes\""));
209 args.add (ACE_TEXT ("sixth without any quotes"));
211 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n*** args-1 ***\n")));
212 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argc (): %d\n"), args.argc ()));
214 for (int i = 0; i < args.argc (); ++i)
215 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args[%02d]: %s\n"), i, args[i]));
217 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argc (): %d\n"), args.argc ()));
219 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n*** args-2 ***\n")));
220 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argc (): %d\n"), args.argc ()));
222 for (int i = 0; i < args.argc (); ++i)
223 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argv ()[%02d]: %s\n"),
224 i, args.argv ()[i]));
226 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argc (): %d\n"), args.argc ()));
228 // args2
229 ACE_ARGV args2;
230 for (int i = 0; i < argc; ++i)
231 args2.add (argv[i], true);
232 args2.add (ACE_TEXT ("'fourth in single quotes'"), true);
233 args2.add (ACE_TEXT ("\"fifth in double quotes\""), true);
234 args2.add (ACE_TEXT ("sixth without any quotes"), true);
236 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n*** args-3 ***\n")));
237 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argc (): %d\n"), args2.argc ()));
239 for (int i = 0; i < args2.argc (); ++i)
240 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args[%02d]: %s\n"), i, args2[i]));
242 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argc (): %d\n"), args2.argc ()));
244 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n*** args-4 ***\n")));
245 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argc (): %d\n"), args2.argc ()));
247 for (int i = 0; i < args2.argc (); ++i)
248 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argv ()[%02d]: %s\n"),
249 i, args2.argv ()[i]));
251 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argc (): %d\n"), args2.argc ()));
253 // args3
254 ACE_ARGV args3 (argv);
256 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n*** args-5 ***\n")));
257 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argc (): %d\n"), args3.argc ()));
259 for (int i = 0; i < args3.argc (); ++i)
260 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args[%02d]: %s\n"), i, args3[i]));
262 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("args.argc (): %d\n"), args3.argc ()));
264 return 0;
268 run_main (int, ACE_TCHAR *argv[])
270 ACE_START_TEST (ACE_TEXT ("ARGV_Test"));
272 test_simple_argv (argv);
273 test_argv_type_converter2 ();
274 test_argv_type_converter ();
275 test_argv_quotes ();
276 test_argv_buf ();
278 ACE_END_TEST;
279 return 0;