Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / protocols / examples / INet / FTP_Simple_exec.cpp
bloba43308cc6f1aabcedf06083808deb4130580eac1
1 #include "ace/Get_Opt.h"
2 #include <memory>
3 #include "ace/OS_NS_errno.h"
4 #include "ace/INet/FTP_URL.h"
5 #include "ace/INet/FTP_ClientRequestHandler.h"
6 #include <iostream>
7 #include <fstream>
9 ACE_CString url;
10 ACE_CString outfile;
11 bool do_login = false;
12 ACE_CString password;
13 bool do_active = false;
15 void
16 usage ()
18 std::cout << "usage: ftp_simple_wget [options] <url>\n";
19 std::cout << "Executes an FTP download request and sends the result to STDOUT or file\n";
20 std::cout << "\t-a\t\tuse active mode for data connections\n";
21 std::cout << "\t-o <filename>\t\tfile to write output to\n";
22 std::cout << "\t-l\t\task for password at login\n";
23 std::cout << "\t-p <password>\t\tuse <password> for login\n";
26 bool
27 parse_args (int argc, ACE_TCHAR *argv [])
29 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("ao:lp:h"), 0, 0, ACE_Get_Opt::RETURN_IN_ORDER);
31 int c;
32 ACE_CString s;
33 while ((c = get_opt ()) != EOF)
35 switch (c)
37 case 1:
38 url = ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg ());
39 break;
41 case 'a':
42 do_active = true;
43 break;
45 case 'o':
46 outfile = ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg ());
47 break;
49 case 'l':
50 do_login = true;
51 break;
53 case 'p':
54 password = ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg ());
55 break;
57 case 'h':
58 default:
59 usage ();
60 return false;
64 return true;
67 class My_Authenticator
68 : public ACE::INet::AuthenticatorBase
70 public:
71 My_Authenticator () {}
72 ~My_Authenticator () {}
74 virtual bool authenticate(ACE::INet::AuthenticationBase& auth) const
76 if (!password.empty ()) auth.password (password);
77 if (do_login)
79 std::cout << "Authentication (" << auth.scheme() << ") required." << std::endl;
80 std::cout << "Realm : " << auth.realm () << std::endl
81 << "User: " << auth.user() << std::endl
82 << "Password: \t\t\t (Enter password or press enter for default)\rPassword: ";
83 char buf[80] = {0};
84 u_int n = 0;
85 int ch = 0;
86 while (n < (sizeof(buf)-1) && (ch = std::cin.get ()) != '\n' && ch != std::char_traits<char>::eof ())
88 buf[n++] = char(ch);
90 if (n>0)
91 auth.password (buf);
93 return true;
97 class My_FTP_RequestHandler
98 : public ACE::FTP::ClientRequestHandler
100 public:
101 My_FTP_RequestHandler () : read_length_ (0) {}
102 virtual ~My_FTP_RequestHandler () {}
104 protected:
105 virtual void handle_request_error (const ACE::FTP::URL& url)
107 std::cout << "ERROR" << std::endl;
108 std::cerr << "Failed to handle download for " << url.to_string ().c_str () << std::endl;
111 virtual void handle_connection_error (const ACE::FTP::URL& url)
113 std::cout << "ERROR" << std::endl;
114 std::cerr << "Failed to set up connection for " << url.to_string ().c_str () << std::endl;
117 virtual void after_read (const char_type* /*buffer*/, int length_read)
119 this->read_length_ += length_read;
120 std::cout << "\r [" << this->read_length_ << "/???]";
123 virtual void on_eof ()
125 ACE::FTP::ClientRequestHandler::on_eof ();
126 std::cout << std::endl;
129 private:
130 int read_length_;
134 ACE_TMAIN (int argc, ACE_TCHAR *argv [])
136 std::unique_ptr<std::ofstream> fout;
137 std::ostream* sout = &std::cout;
139 if (!parse_args (argc, argv))
141 return 1;
144 ACE::INet::URL_INetAuthBase::add_authenticator ("my_auth",
145 new My_Authenticator);
147 std::cout << "Starting..." << std::endl;
149 if (!url.empty ())
151 if (!outfile.empty ())
153 fout.reset (new std::ofstream (outfile.c_str (), std::ios_base::binary|std::ios_base::out));
155 if (!*fout)
157 std::cerr << "Failed to open output file : " << outfile.c_str () << std::endl;
158 return 1;
161 sout = fout.get ();
164 ACE::FTP::URL ftp_url;
166 std::cout << "Parsing url [" << url.c_str () << "]" << std::endl;
168 if (!ftp_url.parse (url))
170 std::cerr << "Failed parsing url [" << url << "]" << std::endl;
171 std::cerr << "\tresult = " << ftp_url.to_string ().c_str ();
172 return 1;
175 std::cout << "Opening url..." << std::endl;
176 My_FTP_RequestHandler my_rh;
177 if (do_active) my_rh.use_active_mode ();
178 ACE::INet::URLStream urlin = ftp_url.open (my_rh);
179 if (urlin)
181 std::cout << "Saving to: ";
182 if (!outfile.empty ())
183 std::cout << '\'' << outfile.c_str () << '\'' << std::endl;
184 else
185 std::cout << "(stdout)" << std::endl;
187 (*sout) << urlin->rdbuf ();
188 sout->flush ();
191 else
193 std::cerr << "ERROR: No URL specified!" << std::endl;
194 usage ();
195 return 1;
198 std::cout << "Done" << std::endl;
200 return 0;