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
71 #include "inetgetfile.h"
74 #define INET_STATE_CONNECTING 1
75 #define INET_STATE_ERROR 2
76 #define INET_STATE_RECEIVING 3
77 #define INET_STATE_GOT_FILE 4
79 void InetGetFile::AbortGet()
91 InetGetFile::InetGetFile(char *URL,char *localfile)
96 if ((URL==NULL)||(localfile==NULL)) {
97 m_HardError = INET_ERROR_BADPARMS;
100 // create directory if not already there.
101 char dir_name[256], *end;
103 // make sure localfile has \ in it or we'll be here a long time.
104 if (strstr(localfile, DIR_SEPARATOR_STR)) {
105 strcpy(dir_name, localfile);
106 int len = strlen(localfile);
107 end = dir_name + len;
109 // start from end of localfile and go to first \ to get dirname
110 while ( *end != DIR_SEPARATOR_CHAR ) {
115 if ( _mkdir(dir_name)==0 ) {
116 mprintf(( "CFILE: Created new directory '%s'\n", dir_name ));
120 if (strstr(URL,"http:")) {
124 extern char Multi_options_proxy[512];
125 extern ushort Multi_options_proxy_port;
126 if(strlen(Multi_options_proxy) > 0){
127 http = new ChttpGet(URL, localfile, Multi_options_proxy, Multi_options_proxy_port);
129 http = new ChttpGet(URL, localfile);
133 m_HardError = INET_ERROR_NO_MEMORY;
135 } else if (strstr(URL,"ftp:")) {
137 ftp = new CFtpGet(URL,localfile);
139 m_HardError = INET_ERROR_NO_MEMORY;
142 m_HardError = INET_ERROR_CANT_PARSE_URL;
147 InetGetFile::~InetGetFile()
149 if(http!=NULL) delete http;
150 if(ftp!=NULL) delete ftp;
153 BOOL InetGetFile::IsConnecting()
158 state = http->GetStatus();
162 state = ftp->GetStatus();
164 if(state == FTP_STATE_CONNECTING)
175 BOOL InetGetFile::IsReceiving()
180 state = http->GetStatus();
184 state = ftp->GetStatus();
186 if(state == FTP_STATE_RECEIVING)
196 BOOL InetGetFile::IsFileReceived()
201 state = http->GetStatus();
205 state = ftp->GetStatus();
207 if(state == FTP_STATE_FILE_RECEIVED)
217 BOOL InetGetFile::IsFileError()
220 if(m_HardError) return TRUE;
223 state = http->GetStatus();
227 state = ftp->GetStatus();
232 case FTP_STATE_URL_PARSING_ERROR:
233 case FTP_STATE_HOST_NOT_FOUND:
234 case FTP_STATE_DIRECTORY_INVALID:
235 case FTP_STATE_FILE_NOT_FOUND:
236 case FTP_STATE_CANT_CONNECT:
237 case FTP_STATE_LOGIN_ERROR:
238 case FTP_STATE_INTERNAL_ERROR:
239 case FTP_STATE_SOCKET_ERROR:
240 case FTP_STATE_UNKNOWN_ERROR:
241 case FTP_STATE_RECV_FAILED:
242 case FTP_STATE_CANT_WRITE_FILE:
244 case FTP_STATE_CONNECTING:
251 int InetGetFile::GetErrorCode()
254 if(m_HardError) return m_HardError;
257 state = http->GetStatus();
261 state = ftp->GetStatus();
266 case FTP_STATE_URL_PARSING_ERROR:
267 return INET_ERROR_CANT_PARSE_URL;
269 case FTP_STATE_HOST_NOT_FOUND:
270 return INET_ERROR_HOST_NOT_FOUND;
273 case FTP_STATE_DIRECTORY_INVALID:
274 case FTP_STATE_FILE_NOT_FOUND:
275 return INET_ERROR_BAD_FILE_OR_DIR;
277 case FTP_STATE_CANT_CONNECT:
278 case FTP_STATE_LOGIN_ERROR:
279 case FTP_STATE_INTERNAL_ERROR:
280 case FTP_STATE_SOCKET_ERROR:
281 case FTP_STATE_UNKNOWN_ERROR:
282 case FTP_STATE_RECV_FAILED:
284 return INET_ERROR_UNKNOWN_ERROR;
286 case FTP_STATE_CANT_WRITE_FILE:
287 return INET_ERROR_CANT_WRITE_FILE;
289 return INET_ERROR_NO_ERROR;
293 int InetGetFile::GetTotalBytes()
297 return http->GetTotalBytes();
301 return ftp->GetTotalBytes();
305 int InetGetFile::GetBytesIn()
309 return http->GetBytesIn();
313 return ftp->GetBytesIn();