Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 16, 2009 D1: std.md5: corrections for the given example | ||||
---|---|---|---|---|
| ||||
Hi. In the given example of the D1 std.md5, this line can be found: while ((len = fread(buffer, 1, buffer.sizeof, file)) != 0) This isn't working here (DMD v1.042, Windows XP Pro). I had to replace it with: while ((len = fread(buffer.ptr, cast(uint)1, buffer.sizeof, file)) != 0) ^^^ ^^^^^^^^^^ Someone may want to check & adopt this in the std.md5 module... Thanks & regards notna |
September 16, 2009 Re: D1: std.md5: corrections for the given example | ||||
---|---|---|---|---|
| ||||
Posted in reply to notna | notna wrote:
<snip>
> This isn't working here (DMD v1.042, Windows XP Pro). I had to replace it with:
> while ((len = fread(buffer.ptr, cast(uint)1, buffer.sizeof, file)) != 0)
> ^^^ ^^^^^^^^^^
<snip>
The .ptr is necessary, but the cast(uint) isn't. Even if a change of type were necessary, just 1U would do. (U is a suffix meaning unsigned. There's also L meaning long.)
Stewart.
|
September 17, 2009 Re: D1: std.md5: corrections for the given example | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | Stewart Gordon schrieb:
> The .ptr is necessary, but the cast(uint) isn't. Even if a change of type were necessary, just 1U would do. (U is a suffix meaning unsigned. There's also L meaning long.)
>
> Stewart.
Thank Stewart.
As the std.md5-example is not working with "unicode" file names (fopen...) I rewrote the "MDFile" function and use "file.readBlock" instead now. Maybe the std.md5 example should be changed to use a "file.read" or "file.readBlock".
Here is my code (I've checkedthe file exists and is readable before):
string MD5File(string fileName) {
File file = new File;
MD5_CTX context;
int len;
ubyte[4 * 1024] buffer;
ubyte digest[16];
file.open(fileName, FileMode.In);
context.start();
while ((len = file.readBlock(buffer.ptr, buffer.sizeof)) != 0)
context.update(buffer[0 .. len]);
context.finish(digest);
file.close();
string MD5Sum = (digestToString(digest));
return MD5Sum;
}
Regards
notna
|
September 17, 2009 Re: D1: std.md5: corrections for the given example | ||||
---|---|---|---|---|
| ||||
Posted in reply to notna | notna wrote: > Stewart Gordon schrieb: >> The .ptr is necessary, but the cast(uint) isn't. Even if a change of >> type were necessary, just 1U would do. (U is a suffix meaning >> unsigned. There's also L meaning long.) >> >> Stewart. > > Thank Stewart. > > As the std.md5-example is not working with "unicode" file names (fopen...) I rewrote the "MDFile" function and use "file.readBlock" instead now. Maybe the std.md5 example should be changed to use a "file.read" or "file.readBlock". > > Here is my code (I've checkedthe file exists and is readable before): > > string MD5File(string fileName) { > > File file = new File; scope file = new File(fileName, FileMode.In); > MD5_CTX context; > int len; // int len; > ubyte[4 * 1024] buffer; > ubyte digest[16]; > > file.open(fileName, FileMode.In); // file.open(fileName, FileMode.In); > context.start(); > while ((len = file.readBlock(buffer.ptr, buffer.sizeof)) != 0) while (auto len = file.readBlock(buffer.ptr, buffer.sizeof)) > context.update(buffer[0 .. len]); > context.finish(digest); > file.close(); > // What's up with this? > string MD5Sum = (digestToString(digest)); > return MD5Sum; // return digestToString(digest); > > } > > Regards > notna |
September 17, 2009 Re: D1: std.md5: corrections for the given example | ||||
---|---|---|---|---|
| ||||
Posted in reply to downs | downs wrote:
> scope file = new File(fileName, FileMode.In);
> // int len;
> while (auto len = file.readBlock(buffer.ptr, buffer.sizeof))
> // return digestToString(digest);
Thanks, that looks "cleaner"...
|
September 17, 2009 Re: D1: std.md5: corrections for the given example | ||||
---|---|---|---|---|
| ||||
Posted in reply to downs | downs wrote:
<snip>
> while (auto len = file.readBlock(buffer.ptr, buffer.sizeof))
<snip>
md5_example_2.d(7): expression expected, not 'auto'
md5_example_2.d(7): found 'len' when expecting ')'
md5_example_2.d(7): found '=' instead of statement
(this is line 7 after I removed comments and blank lines from your edit)
Stewart.
|
September 17, 2009 Re: D1: std.md5: corrections for the given example | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | Stewart Gordon schrieb:
> downs wrote:
> <snip>
>> while (auto len = file.readBlock(buffer.ptr, buffer.sizeof))
> <snip>
>
> md5_example_2.d(7): expression expected, not 'auto'
> md5_example_2.d(7): found 'len' when expecting ')'
> md5_example_2.d(7): found '=' instead of statement
>
> (this is line 7 after I removed comments and blank lines from your edit)
>
> Stewart.
yeah, you're right again.
sorr, I had'n the time to test as I posted the message before. that's why I wrote "it looks cleaner" :-(
anyhow, also the " != 0 " is needed. otherwise I get this:
Error: '=' does not give a boolean result
so, the working code is:
string MD5File(string fileName) {
File file = new File(fileName, FileMode.In);
MD5_CTX context;
int len;
ubyte[4 * 1024] buffer;
ubyte digest[16];
context.start();
while ((len = file.readBlock(buffer.ptr, buffer.sizeof)) != 0)
context.update(buffer[0 .. len]);
context.finish(digest);
file.close();
return digestToString(digest);
}
|
September 17, 2009 Re: D1: std.md5: corrections for the given example | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | Stewart Gordon wrote:
> downs wrote:
> <snip>
>> while (auto len = file.readBlock(buffer.ptr, buffer.sizeof))
> <snip>
>
> md5_example_2.d(7): expression expected, not 'auto'
> md5_example_2.d(7): found 'len' when expecting ')'
> md5_example_2.d(7): found '=' instead of statement
>
> (this is line 7 after I removed comments and blank lines from your edit)
>
> Stewart.
God I'm stupid.
Sorry.
I guess I wish that worked.
|
September 18, 2009 Re: D1: std.md5: corrections for the given example | ||||
---|---|---|---|---|
| ||||
Posted in reply to downs | downs wrote: > Stewart Gordon wrote: >> downs wrote: >> <snip> >>> while (auto len = file.readBlock(buffer.ptr, buffer.sizeof)) >> <snip> >> >> md5_example_2.d(7): expression expected, not 'auto' >> md5_example_2.d(7): found 'len' when expecting ')' >> md5_example_2.d(7): found '=' instead of statement >> >> (this is line 7 after I removed comments and blank lines from your edit) >> >> Stewart. > > God I'm stupid. > > Sorry. > > I guess I wish that worked. http://d.puremagic.com/issues/enter_bug.cgi |
September 18, 2009 Re: D1: std.md5: corrections for the given example | ||||
---|---|---|---|---|
| ||||
Posted in reply to grauzone | grauzone schrieb:
>>> md5_example_2.d(7): expression expected, not 'auto'
>>> md5_example_2.d(7): found 'len' when expecting ')'
>>> md5_example_2.d(7): found '=' instead of statement
>>>
>>> (this is line 7 after I removed comments and blank lines from your edit)
>>>
>>> Stewart.
>>
>> God I'm stupid.
>>
>> Sorry.
>>
>> I guess I wish that worked.
>
> http://d.puremagic.com/issues/enter_bug.cgi
???
What do you wanna tell us? That
while (auto len = file.readBlock(buffer.ptr, buffer.sizeof))
has to work and does not because it's a bug? And that's why you think a bug report should be opened?
|
Copyright © 1999-2021 by the D Language Foundation