Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- /* Here we include the source code up to this point. */
- const char* pre[] = {
- "#include <stdio.h>",
- "",
- "/* Here we include the source code up to this point. */",
- NULL
- };
- const char* post[] = {
- "/* Take as input an array of strings",
- " * and output each one on its own line,",
- " * ending when we get to a NULL pointer.",
- " */",
- "void putCode(const char** code)",
- "{",
- " for(; *code != NULL; code++) {",
- " puts(*code);",
- " }",
- "}",
- "",
- "/* Output the C escape sequence for a character",
- " * inside of a C-string that we will print as code.",
- " *",
- " * If you use more special characters in the source,",
- " * you need to expand this function.",
- " *",
- " * The encoding of this function in the string",
- " * version of the code is particularily entertaining",
- " * because all the characters are double-escaped.",
- " */",
- "const char* encodeChar(char c)",
- "{",
- " if(c == '\"')",
- " return \"\\\\\\\"\";",
- " else if(c == '\\\\')",
- " return \"\\\\\\\\\"; /* lol */",
- " return NULL;",
- "}",
- "",
- "/* Output the code for a const char* array",
- " * given the source code for that array.",
- " */",
- "void putStr(const char* varName, const char** code)",
- "{",
- " printf(\"const char* %s[] = {\\n\", varName);",
- " for(; *code != NULL; code++)",
- " {",
- " const char* line;",
- "",
- " /* each line starts with an indent and a quote */",
- " printf(\" \\\"\");",
- " for(line = *code; *line != '\\0'; ++line)",
- " {",
- " const char* encoded = encodeChar(*line);",
- "",
- " if(encoded == NULL)",
- " {",
- " /* Most characters encode to themselves */",
- " printf(\"%c\", *line);",
- " }",
- " else",
- " {",
- " /* But some need extra magic */",
- " printf(\"%s\", encoded);",
- " }",
- " }",
- "",
- " /* and ends with a quote, comma, and newline */",
- " printf(\"\\\",\\n\");",
- " }",
- "",
- " /* terminate the array with a NULL sentinel */",
- " printf(\" NULL\\n};\\n\\n\");",
- "}",
- "",
- "int main()",
- "{",
- " putCode(pre);",
- " putStr(\"pre\", pre);",
- " putStr(\"post\", post);",
- " putCode(post);",
- " return 0;",
- "}",
- NULL
- };
- /* Take as input an array of strings
- * and output each one on its own line,
- * ending when we get to a NULL pointer.
- */
- void putCode(const char** code)
- {
- for(; *code != NULL; code++) {
- puts(*code);
- }
- }
- /* Output the C escape sequence for a character
- * inside of a C-string that we will print as code.
- *
- * If you use more special characters in the source,
- * you need to expand this function.
- *
- * The encoding of this function in the string
- * version of the code is particularily entertaining
- * because all the characters are double-escaped.
- */
- const char* encodeChar(char c)
- {
- if(c == '"')
- return "\\\"";
- else if(c == '\\')
- return "\\\\"; /* lol */
- return NULL;
- }
- /* Output the code for a const char* array
- * given the source code for that array.
- */
- void putStr(const char* varName, const char** code)
- {
- printf("const char* %s[] = {\n", varName);
- for(; *code != NULL; code++)
- {
- const char* line;
- /* each line starts with an indent and a quote */
- printf(" \"");
- for(line = *code; *line != '\0'; ++line)
- {
- const char* encoded = encodeChar(*line);
- if(encoded == NULL)
- {
- /* Most characters encode to themselves */
- printf("%c", *line);
- }
- else
- {
- /* But some need extra magic */
- printf("%s", encoded);
- }
- }
- /* and ends with a quote, comma, and newline */
- printf("\",\n");
- }
- /* terminate the array with a NULL sentinel */
- printf(" NULL\n};\n\n");
- }
- int main()
- {
- putCode(pre);
- putStr("pre", pre);
- putStr("post", post);
- putCode(post);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement