Advertisement
Guest User

c-quin

a guest
Nov 22nd, 2014
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.38 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. /* Here we include the source code up to this point. */
  4. const char* pre[] = {
  5.     "#include <stdio.h>",
  6.     "",
  7.     "/* Here we include the source code up to this point. */",
  8.     NULL
  9. };
  10.  
  11. const char* post[] = {
  12.     "/* Take as input an array of strings",
  13.     " * and output each one on its own line,",
  14.     " * ending when we get to a NULL pointer.",
  15.     " */",
  16.     "void putCode(const char** code)",
  17.     "{",
  18.     "    for(; *code != NULL; code++) {",
  19.     "        puts(*code);",
  20.     "    }",
  21.     "}",
  22.     "",
  23.     "/* Output the C escape sequence for a character",
  24.     " * inside of a C-string that we will print as code.",
  25.     " *",
  26.     " * If you use more special characters in the source,",
  27.     " * you need to expand this function.",
  28.     " *",
  29.     " * The encoding of this function in the string",
  30.     " * version of the code is particularily entertaining",
  31.     " * because all the characters are double-escaped.",
  32.     " */",
  33.     "const char* encodeChar(char c)",
  34.     "{",
  35.     "    if(c == '\"')",
  36.     "        return \"\\\\\\\"\";",
  37.     "    else if(c == '\\\\')",
  38.     "        return \"\\\\\\\\\"; /* lol */",
  39.     "    return NULL;",
  40.     "}",
  41.     "",
  42.     "/* Output the code for a const char* array",
  43.     " * given the source code for that array.",
  44.     " */",
  45.     "void putStr(const char* varName, const char** code)",
  46.     "{",
  47.     "    printf(\"const char* %s[] = {\\n\", varName);",
  48.     "    for(; *code != NULL; code++)",
  49.     "    {",
  50.     "        const char* line;",
  51.     "",
  52.     "        /* each line starts with an indent and a quote */",
  53.     "        printf(\"    \\\"\");",
  54.     "        for(line = *code; *line != '\\0'; ++line)",
  55.     "        {",
  56.     "            const char* encoded = encodeChar(*line);",
  57.     "",
  58.     "            if(encoded == NULL)",
  59.     "            {",
  60.     "                /* Most characters encode to themselves */",
  61.     "                printf(\"%c\", *line);",
  62.     "            }",
  63.     "            else",
  64.     "            {",
  65.     "                /* But some need extra magic */",
  66.     "                printf(\"%s\", encoded);",
  67.     "            }",
  68.     "        }",
  69.     "",
  70.     "        /* and ends with a quote, comma, and newline */",
  71.     "        printf(\"\\\",\\n\");",
  72.     "    }",
  73.     "",
  74.     "    /* terminate the array with a NULL sentinel */",
  75.     "    printf(\"    NULL\\n};\\n\\n\");",
  76.     "}",
  77.     "",
  78.     "int main()",
  79.     "{",
  80.     "    putCode(pre);",
  81.     "    putStr(\"pre\", pre);",
  82.     "    putStr(\"post\", post);",
  83.     "    putCode(post);",
  84.     "    return 0;",
  85.     "}",
  86.     NULL
  87. };
  88.  
  89. /* Take as input an array of strings
  90.  * and output each one on its own line,
  91.  * ending when we get to a NULL pointer.
  92.  */
  93. void putCode(const char** code)
  94. {
  95.     for(; *code != NULL; code++) {
  96.         puts(*code);
  97.     }
  98. }
  99.  
  100. /* Output the C escape sequence for a character
  101.  * inside of a C-string that we will print as code.
  102.  *
  103.  * If you use more special characters in the source,
  104.  * you need to expand this function.
  105.  *
  106.  * The encoding of this function in the string
  107.  * version of the code is particularily entertaining
  108.  * because all the characters are double-escaped.
  109.  */
  110. const char* encodeChar(char c)
  111. {
  112.     if(c == '"')
  113.         return "\\\"";
  114.     else if(c == '\\')
  115.         return "\\\\"; /* lol */
  116.     return NULL;
  117. }
  118.  
  119. /* Output the code for a const char* array
  120.  * given the source code for that array.
  121.  */
  122. void putStr(const char* varName, const char** code)
  123. {
  124.     printf("const char* %s[] = {\n", varName);
  125.     for(; *code != NULL; code++)
  126.     {
  127.         const char* line;
  128.  
  129.         /* each line starts with an indent and a quote */
  130.         printf("    \"");
  131.         for(line = *code; *line != '\0'; ++line)
  132.         {
  133.             const char* encoded = encodeChar(*line);
  134.  
  135.             if(encoded == NULL)
  136.             {
  137.                 /* Most characters encode to themselves */
  138.                 printf("%c", *line);
  139.             }
  140.             else
  141.             {
  142.                 /* But some need extra magic */
  143.                 printf("%s", encoded);
  144.             }
  145.         }
  146.  
  147.         /* and ends with a quote, comma, and newline */
  148.         printf("\",\n");
  149.     }
  150.  
  151.     /* terminate the array with a NULL sentinel */
  152.     printf("    NULL\n};\n\n");
  153. }
  154.  
  155. int main()
  156. {
  157.     putCode(pre);
  158.     putStr("pre", pre);
  159.     putStr("post", post);
  160.     putCode(post);
  161.     return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement