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"
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);
36 test_simple_argv (ACE_TCHAR
*argv
[])
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 (),
52 ACE_TEXT ("argc = %d\n"),
55 // Print the contents of the combined <ACE_ARGV>.
56 for (int i
= 0; i
< a
.argc (); i
++)
58 ACE_TEXT (" (%d) %s\n"),
66 test_argv_type_converter ()
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");
76 ACE_OS::memcpy (save_argv
, argv
, sizeof (argv
));
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
++)
98 ACE_TEXT (" (%d) %C\n"),
103 for (size_t i
= 0; save_argv
[i
]; ++i
)
104 ACE_OS::free (save_argv
[i
]);
110 test_argv_type_converter2 ()
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"));
119 ACE_TCHAR
*save_argv
[20];
120 ACE_OS::memcpy (save_argv
, argv
, sizeof (argv
));
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"),
140 for (size_t i
= 0; save_argv
[i
]; ++i
)
141 ACE_OS::free (save_argv
[i
]);
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
167 l_argv
[0] = const_cast <ACE_TCHAR
*> (t
);
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 ()));
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
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
]));
204 for (int i
= 0; i
< argc
; ++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 ()));
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 ()));
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 ()));
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 ();