Skip to content

Instantly share code, notes, and snippets.

@codedot
Created November 14, 2012 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codedot/4071205 to your computer and use it in GitHub Desktop.
Save codedot/4071205 to your computer and use it in GitHub Desktop.
Convert text into C strings
#ifndef _DEF_H
#define _DEF_H
typedef const struct {
int type, index;
const void *aux[];
} inagent;
void inpush(inagent *left, inagent *right);
void interact(void);
inagent *inaux(void *aux);
#endif
all: verbatim.h
verbatim.h: txt2cs.sed def.h sim.c
printf '#define %s \\\n%s\n\n' >$*.tmp \
INDEF "$$(sed -f txt2cs.sed def.h)"
printf '#define %s \\\n%s\n' >>$*.tmp \
INSIM "$$(sed -f txt2cs.sed sim.c)"
mv $*.tmp $@
clean:
-rm -fr verbatim.h *.tmp
#include "def.h"
static struct inpair {
inagent *left, *right;
struct inpair *next;
} instack;
static void oomtest(const void *ptr, const char *str)
{
if (!ptr) {
perror(str);
exit(EXIT_FAILURE);
}
}
static inagent *incpy(inagent *agent)
{
inagent *copy = malloc(sizeof *copy);
oomtest(copy, "malloc");
return memcpy(copy, agent, sizeof *copy);
}
void inpush(inagent *left, inagent *right)
{
struct inpair *pair = malloc(sizeof *pair);
oomtest(pair, "malloc");
pair->left = incpy(left);
pair->right = incpy(right);
pair->next = instack;
instack = pair;
}
s/\\/\\\\/g
s/"/\\"/g
s/ /\\t/g
$!s/^\(.*\)$/ "\1\\n" \\/
$s/^\(.*\)$/ "\1\\n"/
#define INDEF \
"#ifndef _DEF_H\n" \
"#define _DEF_H\n" \
"\n" \
"typedef const struct {\n" \
"\tint type, index;\n" \
"\tconst void *aux[];\n" \
"} inagent;\n" \
"\n" \
"void inpush(inagent *left, inagent *right);\n" \
"void interact(void);\n" \
"inagent *inaux(void *aux);\n" \
"\n" \
"#endif\n"
#define INSIM \
"#include \"def.h\"\n" \
"\n" \
"static struct inpair {\n" \
"\tinagent *left, *right;\n" \
"\tstruct inpair *next;\n" \
"} instack;\n" \
"\n" \
"static void oomtest(const void *ptr, const char *str)\n" \
"{\n" \
"\tif (!ptr) {\n" \
"\t\tperror(str);\n" \
"\t\texit(EXIT_FAILURE);\n" \
"\t}\n" \
"}\n" \
"\n" \
"static inagent *incpy(inagent *agent)\n" \
"{\n" \
"\tinagent *copy = malloc(sizeof *copy);\n" \
"\n" \
"\toomtest(copy, \"malloc\");\n" \
"\n" \
"\treturn memcpy(copy, agent, sizeof *copy);\n" \
"}\n" \
"\n" \
"void inpush(inagent *left, inagent *right)\n" \
"{\n" \
"\tstruct inpair *pair = malloc(sizeof *pair);\n" \
"\n" \
"\toomtest(pair, \"malloc\");\n" \
"\n" \
"\tpair->left = incpy(left);\n" \
"\tpair->right = incpy(right);\n" \
"\tpair->next = instack;\n" \
"\n" \
"\tinstack = pair;\n" \
"}\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment