tpircher-zz / pycrc

Free, easy to use Cyclic Redundancy Check (CRC) calculator and source code generator
https://pycrc.org
MIT License
169 stars 36 forks source link

example main program, read from stdin #16

Closed setop closed 7 years ago

setop commented 7 years ago

Nice tool ! I needed to implement Zip CRC and it worked. Thanks

However, I found the generated main function not so useful. I propose :

#include <stdio.h>
#include <string.h>
#include "crc.h"

#define BLOCK_SIZE 16384

int main(void) {
    char* buffer[BLOCK_SIZE];
    crc_t crc = crc_init();
    for(;;) {
        size_t bytes = fread(buffer,  sizeof(char),BLOCK_SIZE,stdin);
        crc = crc_update(crc, buffer, bytes);
        if (bytes < BLOCK_SIZE)
            if (feof(stdin))
                break;
    }
    crc = crc_finalize(crc);
    printf("%lx", (unsigned long)crc);
    return 0;
}

which prints crc of stdin to stdout.

tpircher-zz commented 7 years ago

Hi setop, thanks for the feed-back, I'm glad pycrc was useful. I'm not sure how to procede with this bug though. The example main() is supposed to be just that, an example and to show the very basic usage of pycrc and not to do anything useful. Also as a new user I wouldn't know whether the program is busy calculating or just waiting for an input.

Unless I'm missing the main point of the bug -- let me know --, I'm tempted to close wontfix this bug.

PS: there is a small error in the code. The buffer should be declared as char buffer[BLOCK_SIZE]; i.e. without the asterisk.

setop commented 7 years ago

As a new user, I was surprised "generate main" only produce a hard coded test and not something that can be used for real.

What I propose is very basic but is does the job : produce the CRC of the stdin to stdout. It could be stated as this in the documentation or the help.

What pycrc currently produce is more like a test and it could be called like this.

Fill free to close wontfix. I won't mind. If a user are looking for a practical main function, she will be able to find it here.

PS : thanks for the fix. I'm definitely not a C expert and this asterisk is a vestige of an attempt to put code snippets together.

tpircher-zz commented 7 years ago

Hi setop, I have added the example program as https://pycrc.org/example-stdin.html Thanks again for your feed-back!

setop commented 7 years ago

That's good news.

May I add a small remark :

./crc < file

or

<produce some bytes> | ./crc

would be a better illustration of the usage. It avoids to use CTRL+D and allows any binary content.

tpircher-zz commented 7 years ago

Should be all set now. Thanks for your help!