1 // ============================================================================
7 // This simple test illustrates how to use advanced features of
11 // Suresh Kannan <kannan@uav.ae.gatech.edu> and
12 // Duane Binder <duane.binder@veritas.com>
14 // ============================================================================
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"
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);
37 test_simple_argv (ACE_TCHAR
*argv
[])
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 (),
53 ACE_TEXT ("argc = %d\n"),
56 // Print the contents of the combined <ACE_ARGV>.
57 for (int i
= 0; i
< a
.argc (); i
++)
59 ACE_TEXT (" (%d) %s\n"),
67 test_argv_type_converter (void)
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");
77 ACE_OS::memcpy (save_argv
, argv
, sizeof (argv
));
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
++)
99 ACE_TEXT (" (%d) %C\n"),
104 for (size_t i
= 0; save_argv
[i
]; ++i
)
105 ACE_OS::free (save_argv
[i
]);
111 test_argv_type_converter2 (void)
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"));
120 ACE_TCHAR
*save_argv
[20];
121 ACE_OS::memcpy (save_argv
, argv
, sizeof (argv
));
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"),
141 for (size_t i
= 0; save_argv
[i
]; ++i
)
142 ACE_OS::free (save_argv
[i
]);
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
168 l_argv
[0] = const_cast <ACE_TCHAR
*> (t
);
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 ()));
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
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
]));
205 for (int i
= 0; i
< argc
; ++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 ()));
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 ()));
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 ()));
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 ();