forked from jps1973/Internet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInternet.cpp
More file actions
166 lines (117 loc) · 4.48 KB
/
Internet.cpp
File metadata and controls
166 lines (117 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Internet.cpp
#include "Internet.h"
// Global variables
HINTERNET g_hInternet;
BOOL InternetClose()
{
// Close internet
return InternetCloseHandle( g_hInternet );
} // End of function InternetClose
BOOL InternetConnect()
{
BOOL bResult = FALSE;
// Open internet
g_hInternet = InternetOpen( INTERNET_USER_AGENT, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
// Ensure that internet was opened
if( g_hInternet )
{
// Successfully opened internet
// Update return value
bResult = TRUE;
} // End of successfully opened internet
return bResult;
} // End of function InternetConnect
BOOL InternetDownloadFile( LPCTSTR lpszUrl, LPTSTR lpszLocalFilePath, BOOL( *lpStatusFunction )( LPCTSTR lpszStatusMessage ) )
{
BOOL bResult = FALSE;
// Get current folder into local file path
if( GetCurrentDirectory( STRING_LENGTH, lpszLocalFilePath ) )
{
// Successfully got current folder into local file path
HINTERNET hInternetFile;
LPTSTR lpszLastForwardSlash;
// Allocate string memory
LPTSTR lpszShortUrl = new char[ STRING_LENGTH ];
// Ensure that local file path ends with a back slash
if( lpszLocalFilePath[ lstrlen( lpszLocalFilePath ) - sizeof( ASCII_BACK_SLASH_CHARACTER ) ] != ASCII_BACK_SLASH_CHARACTER )
{
// Local file path does not end with a back slash
// Append a back slash onto local file path
lstrcat( lpszLocalFilePath, ASCII_BACK_SLASH_STRING );
} // End of local file path does not end with a back slash
// Copy url
lstrcpy( lpszShortUrl, lpszUrl );
// Remove forward slash characters from end of short url
while( lpszShortUrl[ lstrlen( lpszShortUrl ) - sizeof( char ) ] == ASCII_FORWARD_SLASH_CHARACTER )
{
// Remove forward slash character from end of short url
lpszShortUrl[ lstrlen( lpszShortUrl ) - sizeof( char ) ] = ( char )NULL;
}; // End of loop to remove forward slash characters from end of short url
// Find last forward slash in url
lpszLastForwardSlash = strrchr( lpszShortUrl, ASCII_FORWARD_SLASH_CHARACTER );
// Ensure that last forward was found slash in url
if( lpszLastForwardSlash )
{
// Successfully found last forward slash in url
// Append file name onto local file path
lstrcat( lpszLocalFilePath, ( lpszLastForwardSlash + sizeof( ASCII_FORWARD_SLASH_CHARACTER ) ) );
} // End of successfully found last forward slash in url
else
{
// Unable to find last forward slash in url
// Append entire url onto local file path
lstrcat( lpszLocalFilePath, lpszShortUrl );
} // End of unable to find last forward slash in url
// Open internet file
hInternetFile = InternetOpenUrl( g_hInternet, lpszShortUrl, NULL, 0, 0, 0 );
// Ensure that internet file was opened
if( hInternetFile )
{
// Successfully opened internet file
HANDLE hLocalFile;
// Open local file
hLocalFile = CreateFile( lpszLocalFilePath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
// Ensure that local file was opened
if( hLocalFile != INVALID_HANDLE_VALUE )
{
// Successfully opened local file
DWORD dwRead;
DWORD dwWritten;
DWORD dwTotalBytes = 0;
CHAR cBuffer[ INTERNET_DOWNLOAD_BUFFER_LENGTH ];
// Allocate string memory
LPTSTR lpszStatusMessage = new char[ STRING_LENGTH + sizeof( char ) ];
// Read internet file
while( InternetReadFile( hInternetFile, cBuffer, INTERNET_DOWNLOAD_BUFFER_LENGTH, &dwRead ) )
{
// Ensure that data was read into buffer
if( dwRead == 0 )
{
// No data was read into buffer
// Break out of loop
break;
} // End of no data was read into buffer
// Terminate buffer
cBuffer[ dwRead ] = ( char )NULL;
// Write buffer into local file
WriteFile( hLocalFile, cBuffer, dwRead, &dwWritten, NULL );
// Update total bytes
dwTotalBytes += dwWritten;
// Format status message
wsprintf( lpszStatusMessage, INTERNET_DOWNLOADING_STATUS_MESSAGE_FORMAT_STRING, lpszUrl, lpszLocalFilePath, dwTotalBytes );
// Show status message
( *lpStatusFunction )( lpszStatusMessage );
} // End of loop to read internet file
// Update return value
bResult = TRUE;
// Close file
CloseHandle( hLocalFile );
} // End of successfully opened local file
// Close internet file
InternetCloseHandle( hInternetFile );
} // End of successfully opened internet file
// Free string memory
delete [] lpszShortUrl;
} // End of successfully got current folder into local file path
return bResult;
} // End of function InternetDownloadFile