Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Delphi Translation

67 views
Skip to first unread message

CJ

unread,
Oct 31, 2007, 4:08:39 PM10/31/07
to
Can someone who knows Delphi and BCB help me out with this translation? I
use BCB6
I translated a few that seem to be easy, but I still have the rest and I am
not sure of those.


Thanks
CJ


private
bool FIsDownloading;
int FPos;
//function GetResourceSize(const AURL: string): Integer;
//function DownloadAtPos(const AURL, AFileName: string; APos: Integer):
Integer;
//procedure DoProgress(ACompleted: Integer);
//procedure ParseURL(const AURL: string; var AHost, AResource: string);

int GetResourceSize(String AURL);
int DownloadAtPos(String AURL, String AFileName, int APos);
void DoProgress(int ACompleted);
void ParseURL(String AURL, String AHost, String AResource);

/*
procedure TMainForm.btnDownloadClick(Sender: TObject);
var
Size: Integer;
begin
if FIsDownloading then Exit;
FIsDownloading := True;
try
Size := GetResourceSize(edtURL.Text);
if (Size = 0) or (not FileExists(edtFile.Text)) then
begin
FPos := 0;
end;
ProgressBar.Max := Size;
ProgressBar.Position := FPos;
FPos := DownloadAtPos(edtURL.Text, edtFile.Text, FPos);
if (FPos = Size) then
begin
FPos := 0;
end;
if FIsDownloading then
begin
ShowMessage('The file has been downloaded');
end;
finally
FIsDownloading := False;
end;
end;
*/

void TForm1::btnDownloadClick(Sender: TObject);
{
int Size;

if( FIsDownloading) return;
FIsDownloading = true;
try
{
Size = GetResourceSize(edtURL->Text);
if ((Size = 0) || (! FileExists(edtFile->Text)))
FPos = 0;

ProgressBar->Max = Size;
ProgressBar->Position = FPos;
FPos = DownloadAtPos(edtURL->Text, edtFile->Text, FPos);

if (FPos = Size)
FPos := 0;

if(FIsDownloading)
ShowMessage('The file has been downloaded');
}
__finally
{
FIsDownloading := False;
}

/*
function TMainForm.GetResourceSize(const AURL: string): Integer;
var
hOpen, hConnect, hResource: HINTERNET;
host, resource: string;
buflen, tmp: DWORD;
begin
ParseURL(AURL, host, resource);

hOpen := InternetOpen('WinInet resuming sample',
INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
hConnect := InternetConnect(hOpen, PChar(host),
INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 0);
hResource := HttpOpenRequest(hConnect, 'HEAD', PChar(resource), nil, nil,
nil, 0, 0);
HttpSendRequest(hResource, nil, 0, nil, 0);

buflen := SizeOf(Result);
tmp := 0;
Result := 0;
HttpQueryInfo(hResource, HTTP_QUERY_CONTENT_LENGTH or
HTTP_QUERY_FLAG_NUMBER,
@Result, buflen, tmp);

InternetCloseHandle(hConnect);
InternetCloseHandle(hOpen);
end;
*/
int TForm1::GetResourceSize(String AURL: string)
{
HINTERNET hOpen, hConnect, hResource;
String host, resource;
DWORD buflen, tmp;

ParseURL(AURL, host, resource);

hOpen = InternetOpen("WinInet resuming sample",
INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
hConnect = InternetConnect(hOpen, host.c_str(),
INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
hResource = HttpOpenRequest(hConnect, "HEAD", resource.c_str(), NULL,
NULL, NULL, 0, 0);
HttpSendRequest(hResource, NULL, 0, NULL, 0);

buflen := sizeof(Result);
tmp = 0;
DWORD Result = 0;
HttpQueryInfo(hResource, HTTP_QUERY_CONTENT_LENGTH or
HTTP_QUERY_FLAG_NUMBER,
&Result, &buflen, &tmp);

InternetCloseHandle(hConnect);
InternetCloseHandle(hOpen);
return Result
}

/*
function TMainForm.DownloadAtPos(const AURL, AFileName: string; APos:
Integer): Integer;
const
FileOpenModes: array[Boolean] of DWORD = (fmCreate, fmOpenWrite);
var
FileStream: TFileStream;
hOpen, hConnect, hResource: HINTERNET;
host, resource: string;
DataProceed: array[0..8191] of Byte;
numread: DWORD;
begin
ParseURL(AURL, host, resource);

hOpen := InternetOpen('WinInet resuming sample',
INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
hConnect := InternetConnect(hOpen, PChar(host),
INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 0);
hResource := HttpOpenRequest(hConnect, 'GET', PChar(resource), nil, nil,
nil, 0, 0);
HttpSendRequest(hResource, nil, 0, nil, 0);

Result := APos;
if (Result > 0) then
begin
if not (Integer(InternetSetFilePointer(hResource, Result, nil,
FILE_BEGIN, 0)) > 0) then
begin
Result := 0;
end;
end;
FileStream := TFileStream.Create(AFileName,
FileOpenModes[FileExists(AFileName)]);
try
FileStream.Position := Result;
repeat
ZeroMemory(@DataProceed, SizeOf(DataProceed));
InternetReadFile(hResource, @DataProceed, SizeOf(DataProceed),
numread);
if (numread <= 0) then Break;
FileStream.Write(DataProceed, numread);
Result := Result + Integer(numread);
DoProgress(Result);
until (not FIsDownloading);
finally
FileStream.Free();
InternetCloseHandle(hConnect);
InternetCloseHandle(hOpen);
end;
end;
*/

procedure TMainForm.DoProgress(ACompleted: Integer);
begin
//ProgressBar.Position := ACompleted;
ProgressBar->Position = ACompleted;
Application->ProcessMessages();
end;

procedure TMainForm.ParseURL(const AURL: string; var AHost, AResource:
string);
procedure CleanArray(var Arr: array of Char);
begin
ZeroMemory(Arr + 0, High(Arr) - Low(Arr) + 1);
end;

var
UrlComponents: TURLComponents;
scheme: array[0..INTERNET_MAX_SCHEME_LENGTH - 1] of Char;
host: array[0..INTERNET_MAX_HOST_NAME_LENGTH - 1] of Char;
user: array[0..INTERNET_MAX_USER_NAME_LENGTH - 1] of Char;
password: array[0..INTERNET_MAX_PASSWORD_LENGTH - 1] of Char;
urlpath: array[0..INTERNET_MAX_PATH_LENGTH - 1] of Char;
fullurl: array[0..INTERNET_MAX_URL_LENGTH - 1] of Char;
extra: array[0..1024 - 1] of Char;
begin
CleanArray(scheme);
CleanArray(host);
CleanArray(user);
CleanArray(password);
CleanArray(urlpath);
CleanArray(fullurl);
CleanArray(extra);
ZeroMemory(@UrlComponents, SizeOf(TURLComponents));

UrlComponents.dwStructSize := SizeOf(TURLComponents);
UrlComponents.lpszScheme := scheme;
UrlComponents.dwSchemeLength := High(scheme) + 1;
UrlComponents.lpszHostName := host;
UrlComponents.dwHostNameLength := High(host) + 1;
UrlComponents.lpszUserName := user;
UrlComponents.dwUserNameLength := High(user) + 1;
UrlComponents.lpszPassword := password;
UrlComponents.dwPasswordLength := High(password) + 1;
UrlComponents.lpszUrlPath := urlpath;
UrlComponents.dwUrlPathLength := High(urlpath) + 1;
UrlComponents.lpszExtraInfo := extra;
UrlComponents.dwExtraInfoLength := High(extra) + 1;

InternetCrackUrl(PChar(AURL), Length(AURL), ICU_DECODE or ICU_ESCAPE,
UrlComponents);

AHost := host;
AResource := urlpath;
end;


Remy Lebeau (TeamB)

unread,
Oct 31, 2007, 5:41:52 PM10/31/07
to

"CJ" <cl...@yahoo.com> wrote in message
news:4728e0c4$1...@newsgroups.borland.com...

> Can someone who knows Delphi and BCB help me out with this translation?

Here you go:

private
bool FIsDownloading;
int FPos;

int __fastcall GetResourceSize(const String &AURL);
int __fastcall DownloadAtPos(const String &AURL, const String
&AFileName, int APos);
void __fastcall DoProgress(int ACompleted);
void __fastcall ParseURL(const String &AURL, String &AHost, String
&AResource);

void __fastcall TMainForm::btnDownloadClick(TObject *Sender)
{
if( FIsDownloading ) return;
FIsDownloading = true;
try
{
int Size = GetResourceSize(edtURL->Text);
if( (Size == 0) || !FileExists(edtFile->Text) )


FPos = 0;
ProgressBar->Max = Size;
ProgressBar->Position = FPos;
FPos = DownloadAtPos(edtURL->Text, edtFile->Text, FPos);

if( FPos == Size )
FPos = 0;
if( FIsDownloading )


ShowMessage("The file has been downloaded");
}
__finally
{

FIsDownloading = false;
}
}

int __fastcall TMainForm::GetResourceSize(const String &AURL)


{
HINTERNET hOpen, hConnect, hResource;
String host, resource;
DWORD buflen, tmp;

ParseURL(AURL, host, resource);

hOpen = InternetOpen("WinInet resuming sample",
INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
hConnect = InternetConnect(hOpen, host.c_str(),
INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
hResource = HttpOpenRequest(hConnect, "HEAD", resource.c_str(),
NULL, NULL, NULL, 0, 0);
HttpSendRequest(hResource, NULL, 0, NULL, 0);

buflen = sizeof(int);
tmp = 0;
int Result = 0;
HttpQueryInfo(hResource, HTTP_QUERY_CONTENT_LENGTH |
HTTP_QUERY_FLAG_NUMBER, &Result, &buflen, &tmp);

InternetCloseHandle(hConnect);
InternetCloseHandle(hOpen);

return Result;
}


int __fastcall TMainForm::DownloadAtPos(const String &AURL, const String
&AFileName, int APos)
{
const DWORD FileOpenModes[2] = {fmCreate, fmOpenReadWrite};

TFileStream *FileStream;


HINTERNET hOpen, hConnect, hResource;
String host, resource;

BYTE DataProceed[8192];
DWORD numread;

ParseURL(AURL, host, resource);

hOpen = InternetOpen("WinInet resuming sample",
INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
hConnect = InternetConnect(hOpen, host.c_str(),
INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);

hResource = HttpOpenRequest(hConnect, "GET", resource.c_str(), NULL,

NULL, NULL, 0, 0);
HttpSendRequest(hResource, NULL, 0, NULL, 0);

int Result = APos;
if( Result > 0 )
{
Result := InternetSetFilePointer(hResource, Result, NULL,
FILE_BEGIN, 0);
if( Result < 0 )
Result = 0;
}

FileStream = new TFileStream(AFileName,
FileOpenModes[FileExists(AFileName)]);
try
{
FileStream->Position = Result;
do
{
ZeroMemory(DataProceed, sizeof(DataProceed));
numread = 0;
InternetReadFile(hResource, DataProceed,
sizeof(DataProceed), &numread);
if( numread < 1 ) break;
FileStream->Write(DataProceed, numread);
Result += numread;
DoProgress(Result);
}
while( FIsDownloading );
}
__finally
{
delete FileStream;
InternetCloseHandle(hConnect);
InternetCloseHandle(hOpen);
}

return Result;
}

void __fastcall TMainForm::DoProgress(int ACompleted)
{


ProgressBar->Position = ACompleted;
Application->ProcessMessages();
}

void __fastcall TMainForm::ParseURL(const String AURL, String &AHost,
String &AResource)
{
URL_COMPONENTS UrlComponents = {0};
char scheme[INTERNET_MAX_SCHEME_LENGTH] = {0};
char host[INTERNET_MAX_HOST_NAME_LENGTH] = {0};
char user[INTERNET_MAX_USER_NAME_LENGTH] = {0};
char password[INTERNET_MAX_PASSWORD_LENGTH] = {0};
char urlpath[INTERNET_MAX_PATH_LENGTH] = {0};
char fullurl[INTERNET_MAX_URL_LENGTH] = {0};
char extra[1024] = {0};

UrlComponents.dwStructSize = sizeof(UrlComponents);
UrlComponents.lpszScheme = scheme;
UrlComponents.dwSchemeLength = INTERNET_MAX_SCHEME_LENGTH;
UrlComponents.lpszHostName = host;
UrlComponents.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH;
UrlComponents.lpszUserName = user;
UrlComponents.dwUserNameLength = INTERNET_MAX_USER_NAME_LENGTH;
UrlComponents.lpszPassword = password;
UrlComponents.dwPasswordLength = INTERNET_MAX_PASSWORD_LENGTH;
UrlComponents.lpszUrlPath = urlpath;
UrlComponents.dwUrlPathLength = INTERNET_MAX_PATH_LENGTH;
UrlComponents.lpszExtraInfo = extra;
UrlComponents.dwExtraInfoLength = 1024;

InternetCrackUrl(AURL.c_str(), AURL.Length(), ICU_DECODE |
ICU_ESCAPE, &UrlComponents);

AHost = host;
AResource = urlpath;
}


Gambit


CJ

unread,
Oct 31, 2007, 6:07:15 PM10/31/07
to
Thanks a lot, I will have to store this in case I come across similar code.

Thanks very much

CJ


"Remy Lebeau (TeamB)" <no....@no.spam.com> wrote in message
news:4728f6da$1...@newsgroups.borland.com...

CJ

unread,
Oct 31, 2007, 7:43:06 PM10/31/07
to
I missed this one, does the following equal what I have?

/*
s: string;

s := Format('Range: bytes=%d-', [Result]);
HttpAddRequestHeaders(hResource, PChar(s), Length(s),
HTTP_ADDREQ_FLAG_ADD_IF_NEW);
*/

String s;

s = Format(Result, 'Range: bytes=%d-', ????);
HttpAddRequestHeaders(hResource, s.c_str(), sizeof(s),
HTTP_ADDREQ_FLAG_ADD_IF_NEW);

Thanks again
CJ


FEEX

unread,
Nov 1, 2007, 9:51:24 AM11/1/07
to

I would translate that to

AnsiString s = Format("Range: bytes=%d-", ARRAYOFCONST(( Result )) );
HttpAddRequestHeaders(hResource, s.c_str(), s.Length(),
HTTP_ADDREQ_FLAG_ADD_IF_NEW);


--
Roger Aelbrecht
www.drehoeksw.net
www.bcbzip.net

CJ

unread,
Nov 1, 2007, 12:30:50 PM11/1/07
to
Thanks a lot.

"FEEX" <fe...@drehoek.net> wrote in message
news:4729...@newsgroups.borland.com...
> CJ wrote:


Remy Lebeau (TeamB)

unread,
Nov 1, 2007, 2:21:25 PM11/1/07
to

"CJ" <cl...@yahoo.com> wrote in message
news:47291306$1...@newsgroups.borland.com...

> I missed this one, does the following equal what I have?

No, it does not. You need to use the ARRAYOFCONST macro when calling
Format(), and you cannot use sizeof() to get the length of a String. Try
this instead:

String s;

// yes, you need double parenthesis when using ARRAYOFCONST()...


s = Format("Range: bytes=%d-", ARRAYOFCONST(( Result )) );

HttpAddRequestHeaders(hResource, s.c_str(), s.Length(),
HTTP_ADDREQ_FLAG_ADD_IF_NEW);


Gambit


0 new messages