git ssb

1+

cel / pngspark



Tree: 7d1b2f8dab921f01fb353d94728ca41743b71b63

Files: 7d1b2f8dab921f01fb353d94728ca41743b71b63 / main.c

1657 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 if (isatty(fileno(stdin)) && (argc < 2 ||
20 !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
21 return 1;
22 }
23
24 struct pngspark ps;
25 const char *color = "#000000";
26 const char *filename = "pngspark.png";
27 int height = 10;
28
29 for (int i = 1; i < argc; i++) {
30 char c = argv[i][1];
31 if(argv[i][0] != '-' || argv[i][2])
32 c = -1;
33 switch(c) {
34 case 'c':
35 if (++i < argc) color = argv[i];
36 break;
37 case 'f':
38 if (++i < argc) filename = argv[i];
39 break;
40 case 'h':
41 if (++i < argc) height = atoi(argv[i]);
42 break;
43 default:
44 fprintf(stderr, "Usage: %s [-h|--help] VALUE,...\n", argv[0]);
45 }
46 }
47
48 int fd = open(filename, O_CREAT | O_RDWR, 0644);
49 if (!fd) err(1, "unable to open file %s", filename);
50
51 if (pngspark_init(&ps, fd, height, color) < 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_end(&ps) < 0)
72 return 1;
73
74 return 0;
75}
76

Built with git-ssb-web