Created
November 14, 2012 09:33
-
-
Save codedot/4071205 to your computer and use it in GitHub Desktop.
Convert text into C strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s/\\/\\\\/g | |
s/"/\\"/g | |
s/ /\\t/g | |
$!s/^\(.*\)$/ "\1\\n" \\/ | |
$s/^\(.*\)$/ "\1\\n"/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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