git ssb

1+

cel / pngspark



Tree: a18e2f94c5c1c202b703482e5aabdaa261c3d95a

Files: a18e2f94c5c1c202b703482e5aabdaa261c3d95a / main.c

1860 bytesRaw
1#define _POSIX_SOURCE
2#include <err.h>
3#include <fcntl.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <unistd.h>
8
9#include "pngspark.h"
10
11char 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
17void usage(const char *progname)
18{
19 errx(1, "Usage: %s output.png [-h height] "
20 "[-c color] [-s scaling]", progname);
21}
22
23int main(int argc, char *argv[])
24{
25 struct pngspark ps;
26 const char *color = "#000000";
27 const char *filename = NULL;
28 double scaling = 0.8;
29 int height = 10;
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 if (pngspark_end(&ps) < 0)
84 return 1;
85
86 if (fclose(file) < 0)
87 err(1, "close %s", filename);
88
89 return 0;
90}
91

Built with git-ssb-web