Click here to Skip to main content
15,922,166 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Insert Items in Hash Table Container. Pin
Ali Rafiee2-May-05 6:09
Ali Rafiee2-May-05 6:09 
Generalenum question Pin
joenching30-Apr-05 19:22
joenching30-Apr-05 19:22 
GeneralRe: enum question Pin
mark novak1-May-05 2:23
mark novak1-May-05 2:23 
GeneralRe: enum question Pin
joenching1-May-05 8:47
joenching1-May-05 8:47 
GeneralRe: enum question Pin
mark novak1-May-05 8:52
mark novak1-May-05 8:52 
GeneralRe: enum question Pin
joenching1-May-05 12:39
joenching1-May-05 12:39 
GeneralRe: enum question Pin
mark novak1-May-05 13:24
mark novak1-May-05 13:24 
Generalsource formatting problem in vs.net2003 Pin
followait30-Apr-05 17:58
followait30-Apr-05 17:58 
<pre>
//It just can't format this correctly.
//My version is 7.1.3088.
//Is this a bug?

// A file download subsystem.
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <wininet.h>
#include <fstream>
#include <cstdio>

using namespace std;

const int MAX_ERRMSG_SIZE = 80;
const int MAX_FILENAME_SIZE = 512;
const int BUF_SIZE = 1024;

// Exception class for download errors.
class DLExc {
char err[MAX_ERRMSG_SIZE];
public:
DLExc(char *exc) {
if(strlen(exc) < MAX_ERRMSG_SIZE)
strcpy(err, exc);
}

// Return a pointer to the error message.
const char * geterr() {
return err;
}
};

// A class for downloading files from the Internet.
class Download {
static bool ishttp(char *url);
static bool httpverOK(HINTERNET hIurl);
static bool getfname(char *url, char *fname);
static unsigned long openfile(char *url, bool reload,
ofstream &fout);
public:
static bool download(char *url, bool reload=false,
void (*update)(unsigned long, unsigned long)=NULL);
};


// Download a file.
//
// Pass the URL of the file to url.
//
// To reload a file, pass true to reload.
//
// To specify an update function that is called after
// each buffer is read, pass a pointer to that
// function as the third parameter. If no update
// function is desired, then let the third parameter
// default to null.
bool Download::download(char *url, bool reload,
void (*update)(unsigned long, unsigned long)) {

ofstream fout; // output stream
unsigned char buf[BUF_SIZE]; // input buffer
unsigned long numrcved; // number of bytes read
unsigned long filelen; // length of file on disk
HINTERNET hIurl=0, hInet=0; // Internet handles
unsigned long contentlen;// length of content
unsigned long len; // length of contentlen
unsigned long total = 0; // running total of bytes received
char header[80]; // holds Range header

try {
if(!ishttp(url))
throw DLExc("Must be HTTP url.");

// Open the file specified by url.
// The open stream will be returned
// in fout. If reload is true, then
// any preexisting file will be truncated.
// The length of any preexisting file (after
// possible truncation) is returned.
filelen = openfile(url, reload, fout);

// See if Internet connection available.
if(InternetAttemptConnect(0) != ERROR_SUCCESS)
throw DLExc("Can't connect.");

// Open Internet connection.
hInet = InternetOpen("downloader",
INTERNET_OPEN_TYPE_DIRECT,
NULL, NULL, 0);

if(hInet == NULL)
throw DLExc("Can't open connection.");

// Construct header requesting range of data.
sprintf(header, "Range:bytes=%d-", filelen);

// Open the URL and request range.
hIurl = InternetOpenUrl(hInet, url,
header, -1,
INTERNET_FLAG_NO_CACHE_WRITE, 0);

if(hIurl == NULL) throw DLExc("Can't open url.");

// Confirm that HTTP/1.1 or greater is supported.
if(!httpverOK(hIurl))
throw DLExc("HTTP/1.1 not supported.");

// Get content length.
len = sizeof contentlen;
if(!HttpQueryInfo(hIurl,
HTTP_QUERY_CONTENT_LENGTH |
HTTP_QUERY_FLAG_NUMBER,
&contentlen, &len, NULL))
throw DLExc("File or content length not found.");

// If existing file (if any) is not complete,
// then finish downloading.
if(filelen != contentlen && contentlen)
do {
// Read a buffer of info.
if(!InternetReadFile(hIurl, &buf,
BUF_SIZE, &numrcved))
throw DLExc("Error occurred during download.");

// Write buffer to disk.
fout.write((const char *) buf, numrcved);
if(!fout.good())
throw DLExc("Error writing file.");

total += numrcved; // update running total

// Call update function, if specified.
if(update && numrcved > 0)
update(contentlen, total+filelen);

} while(numrcved > 0);
else
if(update)
update(filelen, filelen);

} catch(DLExc) {
fout.close();
InternetCloseHandle(hIurl);
InternetCloseHandle(hInet);

throw; // rethrow the exception for use by caller
}

fout.close();
InternetCloseHandle(hIurl);
InternetCloseHandle(hInet);

return true;
}

// Return true if HTTP version of 1.1 or greater.
bool Download::httpverOK(HINTERNET hIurl) {
char str[80];
unsigned long len = 79;

// Get HTTP version.
if(!HttpQueryInfo(hIurl, HTTP_QUERY_VERSION, &str, &len, NULL))
return false;

// First, check major version number.
char *p = strchr(str, '/');
p++;
if(*p == '0') return false; // can't use HTTP 0.x

// Now, find start of minor HTTP version number.
p = strchr(str, '.');
p++;

// Convert to int.
int minorVerNum = atoi(p);

if(minorVerNum > 0) return true;
return false;
}

// Extract the filename from the URL. Return false if
// the filename cannot be found.
bool Download::getfname(char *url, char *fname) {
// Find last /.
char *p = strrchr(url, '/');

// Copy filename after the last /.
if(p && (strlen(p) < MAX_FILENAME_SIZE)) {
p++;
strcpy(fname, p);
return true;
}
else
return false;
}

// Open the output file, initialize the output
// stream, and return the file's length. If
// reload is true, first truncate any preexisting
// file.
unsigned long Download::openfile(char *url,
bool reload,
ofstream &fout) {
char fname[MAX_FILENAME_SIZE];

if(!getfname(url, fname))
throw DLExc("File name error.");

if(!reload)
fout.open(fname, ios::binary | ios::out |
ios::app | ios::ate);
else
fout.open(fname, ios::binary | ios::out |
ios::trunc);

if(!fout)
throw DLExc("Can't open output file.");

// Get current file length.
return fout.tellp();
}

// Confirm that the URL specifies HTTP.
bool Download::ishttp(char *url) {
char str[5] = "";

// Get first four characters from URL.
strncpy(str, url, 4);

// Convert to lowercase
for(char *p=str; *p; p++) *p = tolower(*p);

return !strcmp("http", str);
}
</pre>
GeneralRe: source formatting problem in vs.net2003 Pin
Gary R. Wheeler1-May-05 2:56
Gary R. Wheeler1-May-05 2:56 
GeneralCOleControl and &lt;Param&gt; or &lt;Embed&gt; Pin
ThomasABBE30-Apr-05 17:47
ThomasABBE30-Apr-05 17:47 
GeneralTCP or IP header configuration Pin
ahkt30-Apr-05 17:33
ahkt30-Apr-05 17:33 
GeneralRe: TCP or IP header configuration Pin
Alexander M.,1-May-05 2:37
Alexander M.,1-May-05 2:37 
GeneralRuunig external exe file Pin
Anonymous30-Apr-05 12:18
Anonymous30-Apr-05 12:18 
GeneralRe: Ruunig external exe file Pin
Jack Puppy30-Apr-05 12:37
Jack Puppy30-Apr-05 12:37 
GeneralRe: Ruunig external exe file Pin
Gary R. Wheeler1-May-05 3:00
Gary R. Wheeler1-May-05 3:00 
GeneralCreate a link from an MFC dialog box Pin
Member 192589330-Apr-05 11:03
Member 192589330-Apr-05 11:03 
GeneralRe: Create a link from an MFC dialog box Pin
PJ Arends30-Apr-05 11:43
professionalPJ Arends30-Apr-05 11:43 
GeneralXML Parser VC++ Pin
Reveur130-Apr-05 9:27
Reveur130-Apr-05 9:27 
GeneralCOM and structures Pin
Imtiaz Murtaza30-Apr-05 5:44
Imtiaz Murtaza30-Apr-05 5:44 
GeneralCustomize appwizard directx generated application Pin
Anonymous30-Apr-05 5:18
Anonymous30-Apr-05 5:18 
GeneralRe: Customize appwizard directx generated application Pin
_marsim_30-Apr-05 6:58
_marsim_30-Apr-05 6:58 
Generalchange background color of an edit control Pin
includeh1030-Apr-05 3:04
includeh1030-Apr-05 3:04 
GeneralRe: change background color of an edit control Pin
David Crow30-Apr-05 3:50
David Crow30-Apr-05 3:50 
GeneralRe: change background color of an edit control Pin
includeh1030-Apr-05 4:09
includeh1030-Apr-05 4:09 
GeneralRe: change background color of an edit control Pin
Jack Puppy30-Apr-05 4:53
Jack Puppy30-Apr-05 4:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.