FS2_Open
Open source remastering of the Freespace 2 engine
inetgetfile.cpp
Go to the documentation of this file.
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 the
6  * source.
7  *
8 */
9 
10 
11 
12 #include "globalincs/pstypes.h"
13 
14 #ifdef _WIN32
15 #include <windows.h>
16 #include <direct.h>
17 #endif
18 #include <stdio.h>
19 #include <string.h>
20 
21 #include "inetfile/cftp.h"
22 #include "inetfile/chttpget.h"
23 
24 #include "inetfile/inetgetfile.h"
25 
26 
27 
28 #define INET_STATE_CONNECTING 1
29 #define INET_STATE_ERROR 2
30 #define INET_STATE_RECEIVING 3
31 #define INET_STATE_GOT_FILE 4
32 
33 
35 {
36 #ifdef USE_INETFILE
37  if (m_bUseHTTP) {
38  http->AbortGet();
39  } else {
40  ftp->AbortGet();
41  }
42 #endif
43 }
44 
45 InetGetFile::InetGetFile(char *URL, char *localfile)
46 {
47 #ifdef USE_INETFILE
48  m_HardError = 0;
49  http = NULL;
50  ftp = NULL;
51 
52  if ( (URL == NULL) || (localfile == NULL) )
54 
55  // create directory if not already there.
56  char dir_name[256], *end;
57 
58  // make sure localfile has \ in it or we'll be here a long time.
59  if ( strstr(localfile, DIR_SEPARATOR_STR) ) {
60  strcpy_s(dir_name, localfile);
61  int len = strlen(localfile);
62  end = dir_name + len;
63 
64  // start from end of localfile and go to first \ to get dirname
65  while ( *end != DIR_SEPARATOR_CHAR ) {
66  end--;
67  }
68 
69  *end = '\0';
70 
71  if ( _mkdir(dir_name) == 0 ) {
72  mprintf(( "CFILE: Created new directory '%s'\n", dir_name ));
73  }
74  }
75 printf("URL: %s\n", URL);
76  if ( strstr(URL, "http:") ) {
77  m_bUseHTTP = true;
78 printf("using http!\n");
79  // using http proxy?
80  extern char Multi_options_proxy[512];
82 
83  if ( strlen(Multi_options_proxy) > 0 ) {
84  http = new ChttpGet(URL, localfile, Multi_options_proxy, Multi_options_proxy_port);
85  } else {
86  http = new ChttpGet(URL, localfile);
87  }
88 
89  if (http == NULL) {
91  }
92  } else if ( strstr(URL, "ftp:") ) {
93  m_bUseHTTP = FALSE;
94 printf("using ftp! (%s)\n", URL);
95  ftp = new CFtpGet(URL,localfile);
96 
97  if (ftp == NULL) {
99  }
100  } else {
102  }
103 
104  Sleep(1000);
105 #endif
106 }
107 
109 {
110  if (http != NULL)
111  delete http;
112 
113  if (ftp != NULL)
114  delete ftp;
115 }
116 
118 {
119 #ifdef USE_INETFILE
120  int state;
121 
122  if (m_bUseHTTP) {
123  state = http->GetStatus();
124  } else {
125  state = ftp->GetStatus();
126  }
127 
128  if (state == FTP_STATE_CONNECTING) {
129  return true;
130  } else {
131  return false;
132  }
133 #else
134  return false;
135 #endif
136 }
137 
139 {
140 #ifdef USE_INETFILE
141  int state;
142 
143  if (m_bUseHTTP) {
144  state = http->GetStatus();
145  } else {
146  state = ftp->GetStatus();
147  }
148 
149  if (state == FTP_STATE_RECEIVING) {
150  return true;
151  } else {
152  return false;
153  }
154 #else
155  return false;
156 #endif
157 }
158 
160 {
161 #ifdef USE_INETFILE
162  int state;
163 
164  if (m_bUseHTTP) {
165  state = http->GetStatus();
166  } else {
167  state = ftp->GetStatus();
168  }
169 
170  if (state == FTP_STATE_FILE_RECEIVED) {
171  return true;
172  } else {
173  return false;
174  }
175 #else
176  return false;
177 #endif
178 }
179 
181 {
182 #ifdef USE_INETFILE
183  int state;
184 
185  if (m_HardError)
186  return true;
187 
188  if (m_bUseHTTP) {
189  state = http->GetStatus();
190  } else {
191  state = ftp->GetStatus();
192  }
193 printf("state: %i\n", state);
194  switch (state)
195  {
207  return true;
208 
210  return false;
211 
212  default:
213  return false;
214  }
215 #endif
216  return false;
217 }
218 
220 {
221 #ifdef USE_INETFILE
222  int state;
223 
224  if (m_HardError)
225  return m_HardError;
226 
227  if (m_bUseHTTP) {
228  state = http->GetStatus();
229  } else {
230  state = ftp->GetStatus();
231  }
232 
233  switch (state)
234  {
237 
240 
244 
252 
255 
256  default:
257  return INET_ERROR_NO_ERROR;
258  }
259 #endif
260  return INET_ERROR_NO_ERROR;
261 }
262 
264 {
265 #ifdef USE_INETFILE
266  if (m_bUseHTTP) {
267  return http->GetTotalBytes();
268  } else {
269  return ftp->GetTotalBytes();
270  }
271 #else
272  return 0;
273 #endif
274 }
275 
277 {
278 #ifdef USE_INETFILE
279  if (m_bUseHTTP) {
280  return http->GetBytesIn();
281  } else {
282  return ftp->GetBytesIn();
283  }
284 #else
285  return 0;
286 #endif
287 }
bool m_bUseHTTP
Definition: inetgetfile.h:46
#define FTP_STATE_DIRECTORY_INVALID
Definition: cftp.h:32
#define FTP_STATE_CANT_WRITE_FILE
Definition: cftp.h:38
#define FTP_STATE_URL_PARSING_ERROR
Definition: cftp.h:25
#define INET_ERROR_NO_MEMORY
Definition: inetgetfile.h:27
#define INET_ERROR_BADPARMS
Definition: inetgetfile.h:21
#define FTP_STATE_CONNECTING
Definition: cftp.h:26
#define mprintf(args)
Definition: pstypes.h:238
#define DIR_SEPARATOR_CHAR
Definition: pstypes.h:43
#define INET_ERROR_CANT_WRITE_FILE
Definition: inetgetfile.h:22
ushort Multi_options_proxy_port
#define INET_ERROR_NO_ERROR
Definition: inetgetfile.h:20
void AbortGet()
Definition: cftp.cpp:48
int GetTotalBytes()
uint GetBytesIn()
Definition: cftp.cpp:238
uint GetTotalBytes()
Definition: cftp.cpp:243
bool IsConnecting()
#define FTP_STATE_SOCKET_ERROR
Definition: cftp.h:24
int GetBytesIn()
#define INET_ERROR_UNKNOWN_ERROR
Definition: inetgetfile.h:26
bool IsFileReceived()
#define INET_ERROR_CANT_PARSE_URL
Definition: inetgetfile.h:23
bool IsReceiving()
int _mkdir(const char *path)
bool IsFileError()
#define DIR_SEPARATOR_STR
Definition: pstypes.h:44
#define FTP_STATE_FILE_RECEIVED
Definition: cftp.h:35
#define INET_ERROR_BAD_FILE_OR_DIR
Definition: inetgetfile.h:24
void AbortGet()
Definition: chttpget.cpp:70
int GetStatus()
Definition: chttpget.cpp:208
int GetStatus()
Definition: cftp.cpp:233
uint GetTotalBytes()
Definition: chttpget.cpp:218
Definition: cftp.h:48
#define INET_ERROR_HOST_NOT_FOUND
Definition: inetgetfile.h:25
#define FTP_STATE_FILE_NOT_FOUND
Definition: cftp.h:33
#define FTP_STATE_LOGIN_ERROR
Definition: cftp.h:30
int GetErrorCode()
#define FTP_STATE_UNKNOWN_ERROR
Definition: cftp.h:36
#define FTP_STATE_CANT_CONNECT
Definition: cftp.h:28
int m_HardError
Definition: inetgetfile.h:49
#define FTP_STATE_HOST_NOT_FOUND
Definition: cftp.h:27
unsigned short ushort
Definition: pstypes.h:63
void Sleep(int mili)
InetGetFile(char *URL, char *localfile)
Definition: inetgetfile.cpp:45
void AbortGet()
Definition: inetgetfile.cpp:34
ChttpGet * http
Definition: inetgetfile.h:45
#define FTP_STATE_INTERNAL_ERROR
Definition: cftp.h:23
GLenum GLsizei len
Definition: Glext.h:6283
CFtpGet * ftp
Definition: inetgetfile.h:44
char Multi_options_proxy[512]
#define FTP_STATE_RECV_FAILED
Definition: cftp.h:37
#define FALSE
Definition: pstypes.h:400
uint GetBytesIn()
Definition: chttpget.cpp:213
GLuint GLuint end
Definition: Gl.h:1502
#define FTP_STATE_RECEIVING
Definition: cftp.h:34
#define strcpy_s(...)
Definition: safe_strings.h:67