]> icculus.org git repositories - taylor/freespace2.git/blob - src/inetfile/inetgetfile.cpp
clean up Windows #include's and use winsock2
[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
58 #ifndef PLAT_UNIX
59 #include <winsock2.h>
60 #endif
61
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65
66 #include "pstypes.h"
67 #include "cfile.h"
68 #include "cfilesystem.h"
69 #include "cftp.h"
70 #include "chttpget.h"
71
72 #include "inetgetfile.h"
73
74
75 #define INET_STATE_CONNECTING           1
76 #define INET_STATE_ERROR                        2
77 #define INET_STATE_RECEIVING            3
78 #define INET_STATE_GOT_FILE             4
79
80 void InetGetFile::AbortGet()
81 {
82         if(m_bUseHTTP)
83         {
84                 http->AbortGet();
85         }
86         else
87         {
88                 ftp->AbortGet();
89         }
90 }
91
92 InetGetFile::InetGetFile(char *URL, char *filename, int cf_type)
93 {
94         m_HardError = 0;
95         http=NULL;
96         ftp=NULL;
97         if ( (URL == NULL) || (filename == NULL) || !CF_TYPE_SPECIFIED(cf_type) ) {
98                 m_HardError = INET_ERROR_BADPARMS;
99                 return;
100         }
101
102         // create directory if not already there.
103         cf_create_directory(cf_type);
104
105         // create full path for file
106         char localfile[MAX_PATH_LEN] = "";
107         cf_create_default_path_string(localfile, cf_type, filename);
108
109         if (strstr(URL,"http:")) {
110                 m_bUseHTTP = true;
111
112                 // using http proxy?
113                 extern char Multi_options_proxy[512];
114                 extern ushort Multi_options_proxy_port;
115                 if(strlen(Multi_options_proxy) > 0){
116                         http = new ChttpGet(URL, localfile, Multi_options_proxy, Multi_options_proxy_port);
117                 } else {
118                         http = new ChttpGet(URL, localfile);
119                 }
120
121                 if (http==NULL) {
122                         m_HardError = INET_ERROR_NO_MEMORY;
123                 }
124         } else if (strstr(URL,"ftp:")) {
125                 m_bUseHTTP = false;
126                 ftp = new CFtpGet(URL,localfile);
127                 if (ftp==NULL) {
128                         m_HardError = INET_ERROR_NO_MEMORY;
129                 }
130         } else {
131                 m_HardError = INET_ERROR_CANT_PARSE_URL;
132         }
133         SDL_Delay(1000);
134 }
135
136 InetGetFile::~InetGetFile()
137 {
138         if(http!=NULL) delete http;
139         if(ftp!=NULL) delete ftp;
140 }
141
142 bool InetGetFile::IsConnecting()
143 {
144         int state;
145         if(m_bUseHTTP)
146         {
147                 state = http->GetStatus();
148         }
149         else
150         {
151                 state = ftp->GetStatus();
152         }
153         if(state == FTP_STATE_CONNECTING)
154         {
155                 return true;
156         }
157         else
158         {
159                 return false;
160         }
161
162 }
163
164 bool InetGetFile::IsReceiving()
165 {
166         int state;
167         if(m_bUseHTTP)
168         {
169                 state = http->GetStatus();
170         }
171         else
172         {
173                 state = ftp->GetStatus();
174         }
175         if(state == FTP_STATE_RECEIVING)
176         {
177                 return true;
178         }
179         else
180         {
181                 return false;
182         }
183 }
184
185 bool InetGetFile::IsFileReceived()
186 {
187         int state;
188         if(m_bUseHTTP)
189         {
190                 state = http->GetStatus();
191         }
192         else
193         {
194                 state = ftp->GetStatus();
195         }
196         if(state == FTP_STATE_FILE_RECEIVED)
197         {
198                 return true;
199         }
200         else
201         {
202                 return false;
203         }
204 }
205
206 bool InetGetFile::IsFileError()
207 {
208         int state;
209         if(m_HardError) return true;
210         if(m_bUseHTTP)
211         {
212                 state = http->GetStatus();
213         }
214         else
215         {
216                 state = ftp->GetStatus();
217         }
218         switch(state)
219         {
220
221         case FTP_STATE_URL_PARSING_ERROR:
222         case FTP_STATE_HOST_NOT_FOUND:
223         case FTP_STATE_DIRECTORY_INVALID:
224         case FTP_STATE_FILE_NOT_FOUND:
225         case FTP_STATE_CANT_CONNECT:
226         case FTP_STATE_LOGIN_ERROR:
227         case FTP_STATE_INTERNAL_ERROR:
228         case FTP_STATE_SOCKET_ERROR:
229         case FTP_STATE_UNKNOWN_ERROR:
230         case FTP_STATE_RECV_FAILED:
231         case FTP_STATE_CANT_WRITE_FILE:
232                 return true;
233         case FTP_STATE_CONNECTING:
234                 return false;
235         default:
236                 return false;
237         }
238 }
239
240 int InetGetFile::GetErrorCode()
241 {
242         int state;
243         if(m_HardError) return m_HardError;
244         if(m_bUseHTTP)
245         {
246                 state = http->GetStatus();
247         }
248         else
249         {
250                 state = ftp->GetStatus();
251         }
252         switch(state)
253         {
254
255         case FTP_STATE_URL_PARSING_ERROR:
256                 return INET_ERROR_CANT_PARSE_URL;
257
258         case FTP_STATE_HOST_NOT_FOUND:
259                 return INET_ERROR_HOST_NOT_FOUND;
260
261
262         case FTP_STATE_DIRECTORY_INVALID:
263         case FTP_STATE_FILE_NOT_FOUND:
264                 return INET_ERROR_BAD_FILE_OR_DIR;
265
266         case FTP_STATE_CANT_CONNECT:
267         case FTP_STATE_LOGIN_ERROR:
268         case FTP_STATE_INTERNAL_ERROR:
269         case FTP_STATE_SOCKET_ERROR:
270         case FTP_STATE_UNKNOWN_ERROR:
271         case FTP_STATE_RECV_FAILED:
272
273                 return INET_ERROR_UNKNOWN_ERROR;
274
275         case FTP_STATE_CANT_WRITE_FILE:
276                 return INET_ERROR_CANT_WRITE_FILE;
277         default:
278                 return INET_ERROR_NO_ERROR;
279         }
280 }
281
282 int InetGetFile::GetTotalBytes()
283 {
284         if(m_bUseHTTP)
285         {
286                 return http->GetTotalBytes();
287         }
288         else
289         {
290                 return ftp->GetTotalBytes();
291         }
292 }
293
294 int InetGetFile::GetBytesIn()
295 {
296         if(m_bUseHTTP)
297         {
298                 return http->GetBytesIn();
299         }
300         else
301         {
302                 return ftp->GetBytesIn();
303         }
304 }