www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D1: std.md5: corrections for the given example

reply notna <notna.remove.this ist-einmalig.de> writes:
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
Sep 16 2009
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
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.
Sep 16 2009
parent reply notna <notna.remove.this ist-einmalig.de> writes:
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
Sep 16 2009
parent reply downs <default_357-line yahoo.de> writes:
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
Sep 17 2009
next sibling parent notna <notna.remove.this ist-einmalig.de> writes:
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"...
Sep 17 2009
prev sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
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.
Sep 17 2009
next sibling parent notna <notna.remove.this ist-einmalig.de> writes:
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); }
Sep 17 2009
prev sibling parent reply downs <default_357-line yahoo.de> writes:
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.
Sep 17 2009
parent reply grauzone <none example.net> writes:
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
Sep 18 2009
parent reply notna <notna.remove.this ist-einmalig.de> writes:
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?
Sep 18 2009
next sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
notna wrote:
 grauzone schrieb:
<snip>
 http://d.puremagic.com/issues/enter_bug.cgi
??? What do you wanna tell us?
Where to report bugs.
 That
   while (auto len = file.readBlock(buffer.ptr, buffer.sizeof))
 has to work and does not because it's a bug?
If you want to know whether a piece of code is legal or not, try reading the D documentation.
 And that's why you think a bug report should be opened?
No, that this code example in the Phobos documentation is wrong and therefore a bug report should be opened. Stewart.
Sep 18 2009
prev sibling parent reply grauzone <none example.net> writes:
notna wrote:
 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?
Enhancement request to allow auto in while, to make it consistent with if. (You can write "if(auto x = foo()) {...}")
Sep 19 2009
parent Stewart Gordon <smjg_1998 yahoo.com> writes:
grauzone wrote:
 notna wrote:
<snip>
 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?
Enhancement request to allow auto in while, to make it consistent with if. (You can write "if(auto x = foo()) {...}")
The chance of an enhancement request being landed in the D1 line has always been small, and now it's even smaller. Stewart.
Sep 19 2009