Added small program to include HTML-like text files in C programs.

pull/4/head
Adrian Perez 2007-08-21 19:31:49 +02:00
parent bd1c9cd6af
commit 5931340a39
2 changed files with 86 additions and 0 deletions

24
templates/Makefile Normal file
View File

@ -0,0 +1,24 @@
#
# Makefile
# acastro, 2007-08-21 18:41
#
all_t := $(wildcard *.t)
all_inc := $(patsubst %.t,%.inc,$(all_t))
all: $(all_inc)
t2inc: t2inc.c
.INTERMEDIATE: t2inc
$(all_inc): t2inc
%.inc: %.t
./t2inc < $< > $@
# vim:ft=make
#

62
templates/t2inc.c Normal file
View File

@ -0,0 +1,62 @@
/*
* t2h.c
* Copyright (C) 2007 acastro <acastro@slump>
*
* Distributed under terms of the MIT license.
*/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <errno.h>
#define MAX_W 70
int
main(int argc, char **argv)
{
(void) argc; /* unused */
(void) argv; /* unused */
int c = 0;
int w = 0;
putchar('"');
while ((c = getchar()) != EOF) {
/* skip whitespace */
if ((c != ' ') && isspace(c)) continue;
/* take care of long lines */
if (w++ >= MAX_W) {
printf("\"\n\"");
w = 0;
}
/* escape non-printable characters */
if (!isprint(c)) {
printf("\\x%02x", c);
continue;
}
#define C(_m, _p) case _m : printf(_p); break
switch (c) {
C('\\', "\\\\"); /* backslashes */
C('"' , "\\\""); /* double quotes */
default:
putchar(c);
}
}
printf("\"\n");
/* check whether an error was produced */
if ((c == EOF) && ferror(stdin)) {
fprintf(stderr, "%s: error reading input, %s\n", argv[0], strerror(errno));
fflush(stderr);
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}