git ssb

1+

cel / pngspark



Tree: 0b29f5c16c806103ae9326149404b382ab3b7cfd

Files: 0b29f5c16c806103ae9326149404b382ab3b7cfd / main.c

1704 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
17int main(int argc, char *argv[])
18{
19 struct pngspark ps;
20 const char *color = "#000000";
21 const char *filename = "pngspark.png";
22 int height = 10;
23
24 for (int i = 1; i < argc; i++) {
25 if (argv[i][0] != '-') continue;
26 if (argv[i][1] == '-') {
27 if (strcmp("help", argv[i]+2)) {
28 errx(1, "Usage: %s [--help] [-h height] "
29 "[-o output.png] [-c color]", argv[0]);
30 }
31 } else if (!argv[i][2]) switch (argv[i][1]) {
32 case 'c':
33 if (++i < argc) color = argv[i];
34 break;
35 case 'o':
36 if (++i < argc) filename = argv[i];
37 break;
38 case 'h':
39 if (++i < argc) height = atoi(argv[i]);
40 break;
41 }
42 }
43
44 FILE *file = fopen(filename, "w");
45 if (!file) err(1, "unable to open file %s", filename);
46
47 if (pngspark_init(&ps, height, color) < 0)
48 return 1;
49
50 char c;
51 char buffer[32];
52 do {
53 unsigned int i = 0;
54 for (c = getchar(); float_chars[(unsigned char)c]; c = getchar())
55 if (i >= sizeof buffer) {
56 warnx("number too big: %.*s\n", (int)(sizeof buffer), buffer);
57 break;
58 } else
59 buffer[i++] = c;
60 if (!i) continue;
61 buffer[i] = '\0';
62 double value = atof(buffer);
63 if (pngspark_append(&ps, value) < 0)
64 return 1;
65 } while (c != EOF);
66
67 if (pngspark_write(&ps, file) < 0)
68 return 1;
69
70 if (pngspark_end(&ps) < 0)
71 return 1;
72
73 if (fclose(file) < 0)
74 err(1, "close %s", filename);
75
76 return 0;
77}
78

Built with git-ssb-web