Use a variable on the stack to not have a temporary in the call
[ACE_TAO.git] / TAO / utils / catior / catior.cpp
blobbdb9a96ecae79049c76cec9edf9bd0587ddfc571
1 // ============================================================================
2 //
3 // = LIBRARY
4 // TAO/Utils/catior
5 //
6 // = FILENAME
7 // catior.cpp
8 //
9 // = DESCRIPTION
10 // Reads stringified IORs and outputs the encoded information.
12 // = AUTHORS
13 // Jeff Hopper <jrhopper@cts.com>
14 // SCIOP and Tagged component modifications by:
15 // Jason Cohen, Lockheed Martin ATL <jcohen@atl.lmco.com>
17 // ============================================================================
19 #include "Catior_i.h"
20 #include "ace/Get_Opt.h"
21 #include "ace/streams.h"
22 #include "tao/ORB.h"
23 #include "orbsvcs/CosNamingC.h"
25 int
26 ACE_TMAIN (int argcw, ACE_TCHAR *argvw[])
28 CORBA::ORB_var orb_var;
29 try
31 Catior_i catior_impl;
32 orb_var = CORBA::ORB_init (argcw, argvw);
33 CORBA::Boolean b = false;
34 CORBA::Boolean have_argument = false;
35 int opt;
37 ACE_Get_Opt get_opt (argcw, argvw, ACE_TEXT ("f:n:x"));
39 while ((opt = get_opt ()) != EOF)
41 // some arguments have been supplied
42 have_argument = 1;
43 switch (opt)
45 case 'n':
47 // Read the CosName from the NamingService convert the
48 // object_ptr to a CORBA::String_var via the call to
49 // object_to_string.
50 ACE_DEBUG ((LM_DEBUG,
51 "opening a connection to the NamingService\n"
52 "resolving the CosName %s\n",
53 get_opt.opt_arg ()));
55 CORBA::Object_var server_object;
57 try
59 // Find the Naming Service.
60 CORBA::Object_var naming_context_object =
61 orb_var->resolve_initial_references ("NameService");
62 CosNaming::NamingContextExt_var naming_context =
63 CosNaming::NamingContextExt::_narrow (naming_context_object.in ());
65 if (CORBA::is_nil (naming_context.in ()))
67 ACE_ERROR_RETURN ((LM_ERROR,
68 "NameService cannot be resolved\n"),
69 -1);
72 CosNaming::Name *name =
73 naming_context->to_name (ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg ()));
75 try
77 server_object = naming_context->resolve (*name);
78 if (CORBA::is_nil (server_object.in ()))
80 ACE_ERROR_RETURN ((LM_ERROR,
81 "name is not resolved to a valid object\n"),
82 -1);
85 catch (const CosNaming::NamingContext::NotFound& nf)
87 const ACE_TCHAR *reason;
89 switch (nf.why)
91 case CosNaming::NamingContext::missing_node:
92 reason = ACE_TEXT ("missing node");
93 break;
94 case CosNaming::NamingContext::not_context:
95 reason = ACE_TEXT ("not context");
96 break;
97 case CosNaming::NamingContext::not_object:
98 reason = ACE_TEXT ("not object");
99 break;
100 default:
101 reason = ACE_TEXT ("not known");
102 break;
104 ACE_ERROR_RETURN ((LM_ERROR,
105 "%s cannot be resolved, exception reason = %s\n",
106 get_opt.opt_arg (),
107 reason),
108 -1);
110 catch (const CosNaming::NamingContext::InvalidName&)
112 ACE_ERROR_RETURN ((LM_ERROR,
113 "%s cannot be resolved, exception reason = "
114 "Invalid Name"
115 "\n",
116 get_opt.opt_arg ()),
117 -1);
119 catch (const CosNaming::NamingContext::CannotProceed&)
121 ACE_ERROR_RETURN ((LM_ERROR,
122 "%s cannot be resolved, exception reason = "
123 "Cannot Proceed"
124 "\n",
125 get_opt.opt_arg ()),
126 -1);
128 catch (const CORBA::Exception&)
130 ACE_ERROR_RETURN ((LM_ERROR,
131 "%s cannot be resolved, exception reason = "
132 "Unexpected Exception"
133 "\n",
134 argvw[0]),
135 -1);
138 ACE_CString aString;
140 aString = orb_var->object_to_string (server_object.in ());
142 ACE_DEBUG ((LM_DEBUG,
143 "\nhere is the IOR\n%C\n\n",
144 aString.rep ()));
146 ACE_CString str;
147 b = catior_impl.decode(aString, str);
148 ACE_DEBUG ((LM_DEBUG, "%s", str.c_str()));
150 catch (const CORBA::Exception&)
152 ACE_ERROR_RETURN ((LM_ERROR,
153 "%s cannot be resolved, exception reason = "
154 "Unexpected Exception"
155 "\n",
156 argvw[0]),
157 -1);
160 if (b == 1)
161 ACE_DEBUG ((LM_DEBUG,
162 "catior returned true\n"));
163 else
164 ACE_DEBUG ((LM_DEBUG,
165 "catior returned false\n"));
166 break;
168 case 'f':
170 int have_some_input = 0;
171 int decode_pass_count = 0;
173 // Read the file into a CORBA::String_var.
174 ACE_DEBUG ((LM_DEBUG,
175 "reading the file %s\n",
176 get_opt.opt_arg ()));
178 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
179 ifstream ifstr (ACE_TEXT_ALWAYS_CHAR(get_opt.opt_arg ()));
181 if (!ifstr.good ())
183 ifstr.close ();
184 ACE_ERROR_RETURN ((LM_ERROR,
185 "%s "
186 "-f %s "
187 "\n"
188 "Invalid IOR file nominated"
189 "\n",
190 argvw[0],
191 get_opt.opt_arg ()),
192 -1);
195 while (!ifstr.eof())
197 ACE_CString aString;
199 have_some_input = 0;
201 while (!ifstr.eof ())
203 char ch = 0;
204 ifstr.get (ch);
205 if (ifstr.eof () || ch == '\n' || ch == '\r')
206 break;
207 aString += ch;
208 ++have_some_input;
210 #else
211 FILE* ifstr = ACE_OS::fopen (get_opt.opt_arg (), ACE_TEXT ("r"));
213 if (!ifstr || ferror (ifstr))
215 if (ifstr)
217 ACE_OS::fclose (ifstr);
219 ACE_ERROR_RETURN ((LM_ERROR,
220 "%s "
221 "-f %s "
222 "\n"
223 "Invalid IOR file nominated"
224 "\n",
225 argvw[0],
226 get_opt.opt_arg ()),
227 -1);
230 while (!feof (ifstr))
232 char ch;
233 ACE_CString aString;
235 have_some_input = 0;
237 while (!feof (ifstr))
239 ch = ACE_OS::fgetc (ifstr);
240 if (ch == EOF || ch == '\n' || ch == '\r')
241 break;
242 aString += ch;
243 ++have_some_input;
245 #endif /* !defined (ACE_LACKS_IOSTREAM_TOTALLY) */
246 if (have_some_input == 0 || !aString.length())
248 if (!decode_pass_count)
250 ACE_ERROR_RETURN ((LM_ERROR,
251 "%s "
252 "-f %s "
253 "\n"
254 "Empty IOR file nominated"
255 "\n",
256 argvw[0],
257 get_opt.opt_arg ()),
258 -1);
260 else
262 ACE_DEBUG ((LM_DEBUG,
263 "catior returned true\n"));
264 return 0; // All done now
268 ++decode_pass_count;
270 ACE_DEBUG ((LM_DEBUG,
271 "IOR %d: here is the IOR\n%C\n\n",
272 decode_pass_count, aString.rep ()));
274 ACE_CString str;
275 Catior_i fcatior_impl;
277 b = fcatior_impl.decode(aString, str);
278 ACE_DEBUG ((LM_DEBUG, "%s", str.c_str()));
280 if (b == 1)
281 ACE_DEBUG ((LM_DEBUG,
282 "catior returned true\n"));
283 else
284 ACE_DEBUG ((LM_DEBUG,
285 "catior returned false\n"));
286 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
287 ifstr.close ();
288 #else
289 ACE_OS::fclose (ifstr);
290 #endif /* !defined (ACE_LACKS_IOSTREAM_TOTALLY) */
292 break;
293 case 'x':
295 int have_some_input = 0;
296 int decode_pass_count = 0;
298 // Read the input into a CORBA::String_var.
299 ACE_DEBUG ((LM_DEBUG,
300 "reading from stdin\n"));
302 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
303 if (!cin.good ())
305 ACE_ERROR_RETURN ((LM_ERROR,
306 "%s "
307 "-x"
308 "\n"
309 "Invalid input stream"
310 "\n",
311 argvw[0]),
312 -1);
315 while (!cin.eof())
317 ACE_CString aString;
319 have_some_input = 0;
321 while (!cin.eof ())
323 char ch = 0;
324 cin.get (ch);
325 if (cin.eof () || ch == '\n' || ch == '\r')
326 break;
327 aString += ch;
328 ++have_some_input;
330 #else
331 FILE* ifstr = stdin;
333 if (!ifstr || ferror (ifstr))
335 if (ifstr)
337 ACE_OS::fclose (ifstr);
339 ACE_ERROR_RETURN ((LM_ERROR,
340 "%s "
341 "-x"
342 "\n"
343 "Invalid input stream"
344 "\n",
345 argvw[0]),
346 -1);
349 while (!feof (ifstr))
351 char ch;
352 ACE_CString aString;
354 have_some_input = 0;
356 while (!feof (ifstr))
358 ch = ACE_OS::fgetc (ifstr);
359 if (ch == EOF || ch == '\n' || ch == '\r')
360 break;
361 aString += ch;
362 ++have_some_input;
364 #endif /* !defined (ACE_LACKS_IOSTREAM_TOTALLY) */
366 if (have_some_input == 0)
368 if (!decode_pass_count)
370 ACE_ERROR_RETURN ((LM_ERROR,
371 "%s "
372 "-x"
373 "\n"
374 "Empty input stream"
375 "\n",
376 argvw[0]),
377 -1);
379 else
381 return 0; // All done now
385 ++decode_pass_count;
387 ACE_DEBUG ((LM_DEBUG,
388 "IOR: %d\nhere is the IOR\n%C\n\n",
389 decode_pass_count, aString.rep ()));
391 ACE_CString str;
392 b = catior_impl.decode(aString, str);
393 ACE_DEBUG ((LM_DEBUG, "%s", str.c_str()));
395 if (b == 1)
396 ACE_DEBUG ((LM_DEBUG,
397 "catior returned true\n"));
398 else
399 ACE_DEBUG ((LM_DEBUG,
400 "catior returned false\n"));
402 break;
403 case '?':
404 case 'h':
405 default:
406 ACE_ERROR_RETURN ((LM_ERROR,
407 "Usage: %s "
408 "-f filename "
409 "-n CosName "
410 "\n"
411 "Reads an IOR "
412 "and dumps the contents to stdout "
413 "\n",
414 argvw[0]),
419 // check that some relevant arguments have been supplied
420 if (have_argument == 0)
421 ACE_ERROR_RETURN ((LM_ERROR,
422 "Usage: %s "
423 "-f filename "
424 "-n CosName "
425 "\n"
426 "Reads an IOR "
427 "and dumps the contents to stdout "
428 "\n",
429 argvw[0]),
432 catch (const CORBA::Exception& ex)
434 ACE_DEBUG ((LM_DEBUG, "\nError:\n"));
435 ex._tao_print_exception ("Exception in nsadd");
436 orb_var->destroy ();
437 return 1;
439 return 0;