]> icculus.org git repositories - taylor/freespace2.git/blob - src/inetfile/inetgetfile.cpp
recursively create directories (hurt more on OSX) and update all _mkdir() calls accor...
[taylor/freespace2.git] / src / inetfile / inetgetfile.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
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
6  * the source.
7  */
8
9  /*
10  * $Logfile: /Freespace2/code/Inetfile/inetgetfile.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * InternetGetFile Class
16  *
17  * $Log$
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
20  *
21  * Revision 1.5  2002/06/09 04:41:21  relnev
22  * added copyright header
23  *
24  * Revision 1.4  2002/05/26 20:32:24  theoddone33
25  * Fix some minor stuff
26  *
27  * Revision 1.3  2002/05/26 20:20:54  relnev
28  * unix.h: updated
29  *
30  * inetfile: complete
31  *
32  * Revision 1.2  2002/05/07 03:16:45  theoddone33
33  * The Great Newline Fix
34  *
35  * Revision 1.1.1.1  2002/05/03 03:28:09  root
36  * Initial import.
37  *
38  * 
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.
42  * 
43  * 3     6/16/99 10:27a Andsager
44  * Make directory if it does not already exist.
45  * 
46  * 2     4/20/99 6:39p Dave
47  * Almost done with artillery targeting. Added support for downloading
48  * images on the PXO screen.
49  * 
50  * 1     4/20/99 4:37p Dave
51  *
52  * Initial version
53  *
54  * $NoKeywords: $
55  */
56
57 #ifndef PLAT_UNIX
58 #include <windows.h>
59 #include <direct.h>
60 #else
61 #include <sys/stat.h>   // mkdir
62 #include <sys/types.h>  // mkdir
63               
64 #include "pstypes.h"
65 #endif
66
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70
71 #include "cftp.h"
72 #include "chttpget.h"
73
74 #include "inetgetfile.h"
75
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
80
81 void InetGetFile::AbortGet()
82 {
83         if(m_bUseHTTP)
84         {
85                 http->AbortGet();
86         }
87         else
88         {
89                 ftp->AbortGet();
90         }
91 }
92
93 InetGetFile::InetGetFile(char *URL,char *localfile)
94 {
95         m_HardError = 0;
96         http=NULL;
97         ftp=NULL;
98         if ((URL==NULL)||(localfile==NULL)) {
99                 m_HardError = INET_ERROR_BADPARMS;
100         }
101
102         // create directory if not already there.
103         char dir_name[256], *end;
104
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;
110
111                 // start from end of localfile and go to first \ to get dirname
112                 while ( *end != '\\' ) {
113                         end--;
114                 }
115                 *end = '\0';
116
117                 if ( _mkdir(dir_name)==0 )      {       
118                         mprintf(( "CFILE: Created new directory '%s'\n", dir_name ));
119                 }
120         }
121
122         if (strstr(URL,"http:")) {
123                 m_bUseHTTP = TRUE;
124
125                 // using http proxy?
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);
130                 } else {
131                         http = new ChttpGet(URL, localfile);
132                 }
133
134                 if (http==NULL) {
135                         m_HardError = INET_ERROR_NO_MEMORY;
136                 }
137         } else if (strstr(URL,"ftp:")) {
138                 m_bUseHTTP = FALSE;
139                 ftp = new CFtpGet(URL,localfile);
140                 if (ftp==NULL) {
141                         m_HardError = INET_ERROR_NO_MEMORY;
142                 }
143         } else {
144                 m_HardError = INET_ERROR_CANT_PARSE_URL;
145         }
146         Sleep(1000);
147 }
148
149 InetGetFile::~InetGetFile()
150 {
151         if(http!=NULL) delete http;
152         if(ftp!=NULL) delete ftp;
153 }
154
155 BOOL InetGetFile::IsConnecting()
156 {
157         int state;
158         if(m_bUseHTTP)
159         {
160                 state = http->GetStatus();
161         }
162         else
163         {
164                 state = ftp->GetStatus();
165         }
166         if(state == FTP_STATE_CONNECTING)
167         {
168                 return TRUE;
169         }
170         else
171         {
172                 return FALSE;
173         }
174
175 }
176
177 BOOL InetGetFile::IsReceiving()
178 {
179         int state;
180         if(m_bUseHTTP)
181         {
182                 state = http->GetStatus();
183         }
184         else
185         {
186                 state = ftp->GetStatus();
187         }
188         if(state == FTP_STATE_RECEIVING)
189         {
190                 return TRUE;
191         }
192         else
193         {
194                 return FALSE;
195         }
196 }
197
198 BOOL InetGetFile::IsFileReceived()
199 {
200         int state;
201         if(m_bUseHTTP)
202         {
203                 state = http->GetStatus();
204         }
205         else
206         {
207                 state = ftp->GetStatus();
208         }
209         if(state == FTP_STATE_FILE_RECEIVED)
210         {
211                 return TRUE;
212         }
213         else
214         {
215                 return FALSE;
216         }
217 }
218
219 BOOL InetGetFile::IsFileError()
220 {
221         int state;
222         if(m_HardError) return TRUE;
223         if(m_bUseHTTP)
224         {
225                 state = http->GetStatus();
226         }
227         else
228         {
229                 state = ftp->GetStatus();
230         }
231         switch(state)
232         {
233
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:
245                 return TRUE;
246         case FTP_STATE_CONNECTING:
247                 return FALSE;
248         default:
249                 return FALSE;
250         }
251 }
252
253 int InetGetFile::GetErrorCode()
254 {
255         int state;
256         if(m_HardError) return m_HardError;
257         if(m_bUseHTTP)
258         {
259                 state = http->GetStatus();
260         }
261         else
262         {
263                 state = ftp->GetStatus();
264         }
265         switch(state)
266         {
267
268         case FTP_STATE_URL_PARSING_ERROR:
269                 return INET_ERROR_CANT_PARSE_URL;
270
271         case FTP_STATE_HOST_NOT_FOUND:
272                 return INET_ERROR_HOST_NOT_FOUND;
273
274
275         case FTP_STATE_DIRECTORY_INVALID:
276         case FTP_STATE_FILE_NOT_FOUND:
277                 return INET_ERROR_BAD_FILE_OR_DIR;
278
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:
285
286                 return INET_ERROR_UNKNOWN_ERROR;
287
288         case FTP_STATE_CANT_WRITE_FILE:
289                 return INET_ERROR_CANT_WRITE_FILE;
290         default:
291                 return INET_ERROR_NO_ERROR;
292         }
293 }
294
295 int InetGetFile::GetTotalBytes()
296 {
297         if(m_bUseHTTP)
298         {
299                 return http->GetTotalBytes();
300         }
301         else
302         {
303                 return ftp->GetTotalBytes();
304         }
305 }
306
307 int InetGetFile::GetBytesIn()
308 {
309         if(m_bUseHTTP)
310         {
311                 return http->GetBytesIn();
312         }
313         else
314         {
315                 return ftp->GetBytesIn();
316         }
317 }