OpenCores
URL https://opencores.org/ocsvn/igor/igor/trunk

Subversion Repositories igor

[/] [igor/] [trunk/] [simulator/] [bindump.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 atypic
#include <stdio.h>
2
#include <err.h>
3
#include <stdlib.h>
4
 
5
void
6
bindump(char *infile, char *outfile, int line_length, int size)
7
{
8
        FILE *in, *out;
9
        int c;
10
        int i, j;
11
        char patterns[256][9];
12
 
13
        for (i = 0; i < 256; i++) {
14
                for (j = 0; j < 8; j++) {
15
                        patterns[i][j] = ((i>>(7-j))&1)+'0';
16
                }
17
                patterns[i][8] = '\0';
18
        }
19
 
20
        if (line_length % 8 != 0)
21
                errx(1, "Line length %d is not a multiple of 8\n", line_length);
22
        line_length /= 8;
23
 
24
        if ((in = fopen(infile, "r")) == NULL)
25
                err(1, "could not open %s for reading", infile);
26
        if ((out = fopen(outfile, "w")) == NULL)
27
                err(1, "could not open %s for writing", outfile);
28
        i = 0;
29
        while (1) {
30
                c = fgetc(in);
31
                if (feof(in)) break;
32
                if (ferror(in))
33
                        err(1, "error reading from %s", infile);
34
 
35
                if (fputs(patterns[c], out) == EOF)
36
                        err(1, "error writing to %s", outfile);
37
 
38
                if ((++i) % line_length == 0) {
39
                        if (fputc('\n', out) == EOF)
40
                                err(1, "error writing to %s", outfile);
41
                }
42
        }
43
 
44
        if (i % line_length != 0) {
45
                if (fputc('\n', out) == EOF)
46
                        err(1, "error writing to %s", outfile);
47
                warn("file length not multiple of line length (%d bits)", line_length*8);
48
        }
49
 
50
        if (size != 0) {
51
                int remaining = size-i/line_length;
52
                if (remaining < 0)
53
                        errx(1, "too long file, length is %d\n", i/line_length);
54
                if (remaining > 0) {
55
                        for (i = 0; i < remaining; i++) {
56
                                for (j = 0; j < line_length; j++)
57
                                        fputs("00000000", out);
58
                                fputc('\n', out);
59
                        }
60
                }
61
        }
62
 
63
        fclose(in);
64
        fclose(out);
65
}
66
 
67
int
68
main(int argc, char **argv)
69
{
70
        int size;
71
 
72
        if (argc < 4) {
73
                fprintf(stderr,
74
                        "usage: %s INFILE OUTFILE LINE-LENGTH [SIZE]\n"
75
                        "Writes each bit from INFILE as ASCII '1' or '0' in OUTFILE,\n"
76
                        "with a newline after every LINE-LENGTH bits.\n",
77
                        argv[0]);
78
                return 1;
79
        }
80
 
81
        size = (argc == 4) ? 0 : atoi(argv[4]);
82
        bindump(argv[1], argv[2], atoi(argv[3]), size);
83
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.