2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Inetfile/inetgetfile.cpp $
15 * InternetGetFile Class
18 * Revision 1.6 2005/08/12 08:50:09 taylor
19 * recursively create directories (hurt more on OSX) and update all _mkdir() calls accordingly
21 * Revision 1.5 2002/06/09 04:41:21 relnev
22 * added copyright header
24 * Revision 1.4 2002/05/26 20:32:24 theoddone33
25 * Fix some minor stuff
27 * Revision 1.3 2002/05/26 20:20:54 relnev
32 * Revision 1.2 2002/05/07 03:16:45 theoddone33
33 * The Great Newline Fix
35 * Revision 1.1.1.1 2002/05/03 03:28:09 root
39 * 4 8/22/99 1:19p Dave
40 * Fixed up http proxy code. Cleaned up scoring code. Reverse the order in
41 * which d3d cards are detected.
43 * 3 6/16/99 10:27a Andsager
44 * Make directory if it does not already exist.
46 * 2 4/20/99 6:39p Dave
47 * Almost done with artillery targeting. Added support for downloading
48 * images on the PXO screen.
50 * 1 4/20/99 4:37p Dave
61 #include <sys/stat.h> // mkdir
62 #include <sys/types.h> // mkdir
74 #include "inetgetfile.h"
76 #define INET_STATE_CONNECTING 1
77 #define INET_STATE_ERROR 2
78 #define INET_STATE_RECEIVING 3
79 #define INET_STATE_GOT_FILE 4
81 void InetGetFile::AbortGet()
93 InetGetFile::InetGetFile(char *URL,char *localfile)
98 if ((URL==NULL)||(localfile==NULL)) {
99 m_HardError = INET_ERROR_BADPARMS;
102 // create directory if not already there.
103 char dir_name[256], *end;
105 // make sure localfile has \ in it or we'll be here a long time.
106 if (strstr(localfile, "\\")) {
107 strcpy(dir_name, localfile);
108 int len = strlen(localfile);
109 end = dir_name + len;
111 // start from end of localfile and go to first \ to get dirname
112 while ( *end != '\\' ) {
117 if ( _mkdir(dir_name)==0 ) {
118 mprintf(( "CFILE: Created new directory '%s'\n", dir_name ));
122 if (strstr(URL,"http:")) {
126 extern char Multi_options_proxy[512];
127 extern ushort Multi_options_proxy_port;
128 if(strlen(Multi_options_proxy) > 0){
129 http = new ChttpGet(URL, localfile, Multi_options_proxy, Multi_options_proxy_port);
131 http = new ChttpGet(URL, localfile);
135 m_HardError = INET_ERROR_NO_MEMORY;
137 } else if (strstr(URL,"ftp:")) {
139 ftp = new CFtpGet(URL,localfile);
141 m_HardError = INET_ERROR_NO_MEMORY;
144 m_HardError = INET_ERROR_CANT_PARSE_URL;
149 InetGetFile::~InetGetFile()
151 if(http!=NULL) delete http;
152 if(ftp!=NULL) delete ftp;
155 BOOL InetGetFile::IsConnecting()
160 state = http->GetStatus();
164 state = ftp->GetStatus();
166 if(state == FTP_STATE_CONNECTING)
177 BOOL InetGetFile::IsReceiving()
182 state = http->GetStatus();
186 state = ftp->GetStatus();
188 if(state == FTP_STATE_RECEIVING)
198 BOOL InetGetFile::IsFileReceived()
203 state = http->GetStatus();
207 state = ftp->GetStatus();
209 if(state == FTP_STATE_FILE_RECEIVED)
219 BOOL InetGetFile::IsFileError()
222 if(m_HardError) return TRUE;
225 state = http->GetStatus();
229 state = ftp->GetStatus();
234 case FTP_STATE_URL_PARSING_ERROR:
235 case FTP_STATE_HOST_NOT_FOUND:
236 case FTP_STATE_DIRECTORY_INVALID:
237 case FTP_STATE_FILE_NOT_FOUND:
238 case FTP_STATE_CANT_CONNECT:
239 case FTP_STATE_LOGIN_ERROR:
240 case FTP_STATE_INTERNAL_ERROR:
241 case FTP_STATE_SOCKET_ERROR:
242 case FTP_STATE_UNKNOWN_ERROR:
243 case FTP_STATE_RECV_FAILED:
244 case FTP_STATE_CANT_WRITE_FILE:
246 case FTP_STATE_CONNECTING:
253 int InetGetFile::GetErrorCode()
256 if(m_HardError) return m_HardError;
259 state = http->GetStatus();
263 state = ftp->GetStatus();
268 case FTP_STATE_URL_PARSING_ERROR:
269 return INET_ERROR_CANT_PARSE_URL;
271 case FTP_STATE_HOST_NOT_FOUND:
272 return INET_ERROR_HOST_NOT_FOUND;
275 case FTP_STATE_DIRECTORY_INVALID:
276 case FTP_STATE_FILE_NOT_FOUND:
277 return INET_ERROR_BAD_FILE_OR_DIR;
279 case FTP_STATE_CANT_CONNECT:
280 case FTP_STATE_LOGIN_ERROR:
281 case FTP_STATE_INTERNAL_ERROR:
282 case FTP_STATE_SOCKET_ERROR:
283 case FTP_STATE_UNKNOWN_ERROR:
284 case FTP_STATE_RECV_FAILED:
286 return INET_ERROR_UNKNOWN_ERROR;
288 case FTP_STATE_CANT_WRITE_FILE:
289 return INET_ERROR_CANT_WRITE_FILE;
291 return INET_ERROR_NO_ERROR;
295 int InetGetFile::GetTotalBytes()
299 return http->GetTotalBytes();
303 return ftp->GetTotalBytes();
307 int InetGetFile::GetBytesIn()
311 return http->GetBytesIn();
315 return ftp->GetBytesIn();