git ssb

1+

cel / pngspark



Tree: 1954e349b19978c7e219c6610f4b143d6658af82

Files: 1954e349b19978c7e219c6610f4b143d6658af82 / main.c

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

Built with git-ssb-web