Files: b366059646d0ae1ea73ca6a0598323b4db1fd2cf / main.c
1838 bytesRaw
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | char float_chars[(unsigned char)-1] = { |
12 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
13 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
14 | ['.'] = 1, ['-'] = 1, ['e'] = 1, ['E'] = 1, 0 |
15 | }; |
16 | |
17 | void usage(const char *progname) |
18 | { |
19 | errx(1, "Usage: %s output.png [-h height] " |
20 | "[-c color] [-s scaling]", progname); |
21 | } |
22 | |
23 | int main(int argc, char *argv[]) |
24 | { |
25 | struct pngspark ps; |
26 | const char *color = "#000000"; |
27 | const char *filename = NULL; |
28 | double scaling = 1; |
29 | int height = 80; |
30 | |
31 | for (int i = 1; i < argc; i++) { |
32 | if (argv[i][0] != '-') { |
33 | if (filename) { |
34 | usage(argv[0]); |
35 | } else { |
36 | filename = argv[i]; |
37 | } |
38 | } else if (argv[i][2]) { |
39 | errx(1, "extra argument: %s", argv[i]); |
40 | } else switch (argv[i][1]) { |
41 | case 'c': |
42 | if (++i < argc) color = argv[i]; |
43 | break; |
44 | case 'h': |
45 | if (++i < argc) height = atoi(argv[i]); |
46 | break; |
47 | case 's': |
48 | if (++i < argc) scaling = atof(argv[i]); |
49 | break; |
50 | } |
51 | } |
52 | |
53 | if (!filename) { |
54 | usage(argv[0]); |
55 | } |
56 | |
57 | FILE *file = fopen(filename, "w"); |
58 | if (!file) err(1, "unable to open file %s", filename); |
59 | |
60 | if (pngspark_init(&ps, height, color, scaling) < 0) |
61 | return 1; |
62 | |
63 | char c; |
64 | char buffer[32]; |
65 | do { |
66 | unsigned int i = 0; |
67 | for (c = getchar(); float_chars[(unsigned char)c]; c = getchar()) |
68 | if (i >= sizeof buffer) { |
69 | warnx("number too big: %.*s\n", (int)(sizeof buffer), buffer); |
70 | break; |
71 | } else |
72 | buffer[i++] = c; |
73 | if (!i) continue; |
74 | buffer[i] = '\0'; |
75 | double value = atof(buffer); |
76 | if (pngspark_append(&ps, value) < 0) |
77 | return 1; |
78 | } while (c != EOF); |
79 | |
80 | if (pngspark_write(&ps, file) < 0) |
81 | return 1; |
82 | |
83 | pngspark_end(&ps); |
84 | |
85 | if (fclose(file) < 0) |
86 | err(1, "close %s", filename); |
87 | |
88 | return 0; |
89 | } |
90 |
Built with git-ssb-web