git ssb

1+

cel / pngspark



Commit 7d1b2f8dab921f01fb353d94728ca41743b71b63

Initial commit

Charles Lehner committed on 2/9/2015, 10:29:39 PM

Files changed

.gitignoreadded
Makefileadded
main.cadded
package.jsonadded
pngspark.cadded
pngspark.hadded
.gitignoreView
@@ -1,0 +1,2 @@
1 +*.o
2 +pngspark
MakefileView
@@ -1,0 +1,21 @@
1 +SRC = $(wildcard *.c)
2 +OBJ = $(SRC:c=o)
3 +BIN = pngspark
4 +PREFIX ?= /usr/local
5 +
6 +CFLAGS = -Wall -Wextra -Werror -O2 -std=c99 -pedantic
7 +
8 +all: $(BIN)
9 +
10 +pngspark: pngspark.o main.o
11 +
12 +install: $(BIN)
13 + cp -f $(BIN) $(PREFIX)/bin/$(BIN)
14 +
15 +uninstall:
16 + rm $(PREFIX)/bin/$(BIN)
17 +
18 +clean:
19 + rm -f test $(BIN) $(OBJ)
20 +
21 +.PHONY: clean install uninstall
main.cView
@@ -1,0 +1,75 @@
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 +
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 +int 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 +}
package.jsonView
@@ -1,0 +1,9 @@
1 +{
2 + "name": "pngspark",
3 + "version": "0.0.1",
4 + "repo": "clehner/pngspark",
5 + "description": "sparkline PNG images",
6 + "keywords": ["spark", "chart", "stats", "sparkline", "image", "png"],
7 + "license": "MIT",
8 + "install": "make install"
9 +}
pngspark.cView
@@ -1,0 +1,23 @@
1 +#include "pngspark.h"
2 +
3 +int pngspark_init(struct pngspark *ps, int fd, int height, const char *color)
4 +{
5 + ps->fd = fd;
6 + ps->height = height;
7 + (void)color;
8 + return 0;
9 +}
10 +
11 +int pngspark_append(struct pngspark *ps, double value)
12 +{
13 + (void)ps;
14 + (void)value;
15 + return 0;
16 +}
17 +
18 +int pngspark_end(struct pngspark *ps)
19 +{
20 + (void)ps;
21 + return 0;
22 +}
23 +
pngspark.hView
@@ -1,0 +1,13 @@
1 +#ifndef __PNGSPARK_H
2 +#define __PNGSPARK_H
3 +
4 +struct pngspark {
5 + int fd;
6 + int height;
7 +};
8 +
9 +int pngspark_init(struct pngspark *, int, int, const char *);
10 +int pngspark_append(struct pngspark *, double);
11 +int pngspark_end(struct pngspark *);
12 +
13 +#endif /* __PNGSPARK_H */

Built with git-ssb-web