Skip to content

Instantly share code, notes, and snippets.

@sachahjkl
Created February 24, 2024 07:55
Show Gist options
  • Save sachahjkl/7056b8ea8cf9fda7f979a4c39b7637fc to your computer and use it in GitHub Desktop.
Save sachahjkl/7056b8ea8cf9fda7f979a4c39b7637fc to your computer and use it in GitHub Desktop.
Supermaven generating recursive code
// function that generates c code to a file called "resources.c"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *fp;
char *filename;
char *c_code;
int i;
if (argc != 2)
{
printf("Usage: c_embed <filename>\n");
return 1;
}
filename = argv[1];
fp = fopen(filename, "w");
if (fp == NULL)
{
printf("Error opening file %s\n", filename);
return 1;
}
c_code = malloc(1024);
if (c_code == NULL)
{
printf("Error allocating memory for c code\n");
return 1;
}
strcpy(c_code, "#include <stdio.h>\n");
strcat(c_code, "#include <stdlib.h>\n");
strcat(c_code, "#include <string.h>\n\n");
strcat(c_code, "int main(int argc, char *argv[])\n");
strcat(c_code, "{\n");
strcat(c_code, " FILE *fp;\n");
strcat(c_code, " char *filename;\n");
strcat(c_code, " char *c_code;\n");
strcat(c_code, " int i;\n\n");
strcat(c_code, " if (argc != 2)\n");
strcat(c_code, " {\n");
strcat(c_code, " printf(\"Usage: c_embed <filename>\\n\");\n");
strcat(c_code, " return 1;\n");
strcat(c_code, " }\n\n");
strcat(c_code, " filename = argv[1];\n\n");
strcat(c_code, " fp = fopen(filename, \"w\");\n");
strcat(c_code, " if (fp == NULL)\n");
strcat(c_code, " {\n");
strcat(c_code, " printf(\"Error opening file %s\\n\", filename);\n");
strcat(c_code, " return 1;\n");
strcat(c_code, " }\n\n");
strcat(c_code, " c_code = malloc(1024);\n");
strcat(c_code, " if (c_code == NULL)\n");
strcat(c_code, " {\n");
strcat(c_code, " printf(\"Error allocating memory for c code\\n\");\n");
strcat(c_code, " return 1;\n");
strcat(c_code, " }\n\n");
strcat(c_code, " strcpy(c_code, \"#include <stdio.h>\\n\");\n");
strcat(c_code, " strcat(c_code, \"#include <stdlib.h>\\n\");\n");
strcat(c_code, " strcat(c_code, \"#include <string.h>\\n\\n\");\n\n");
strcat(c_code, " strcat(c_code, \"int main(int argc, char *argv[])\\n\");\n");
strcat(c_code, " strcat(c_code, \"{\\n\");\n");
strcat(c_code, " strcat(c_code, \" FILE *fp;\\n\");\n");
strcat(c_code, " strcat(c_code, \" char *filename;\\n\");\n");
strcat(c_code, " strcat(c_code, \" char *c_code;\\n\");\n");
strcat(c_code, " strcat(c_code, \" int i;\\n\\n\");\n\n");
strcat(c_code, " strcat(c_code, \" if (argc != 2)\\n\");\n");
strcat(c_code, " strcat(c_code, \" {\\n\");\n");
strcat(c_code, " strcat(c_code, \" printf(\\\"Usage: c_embed <filename>\\\\n\\\");\\n\");\n");
strcat(c_code, " strcat(c_code, \" return 1;\\n\");\n");
strcat(c_code, " strcat(c_code, \" }\\n\\n\");\n\n");
strcat(c_code, " strcat(c_code, \" filename = argv[1];\\n\\n\");\n\n");
strcat(c_code, " strcat(c_code, \" fp = fopen(filename, \\\"w\\\");\\n\");\n");
strcat(c_code, " strcat(c_code, \" if (fp == NULL)\\n\");\n");
strcat(c_code, " strcat(c_code, \" {\\n\");\n");
strcat(c_code, " strcat(c_code, \" printf(\\\"Error opening file %s\\\\n\\\", filename);\\n\");\n");
strcat(c_code, " strcat(c_code, \" return 1;\\n\");\n");
strcat(c_code, " strcat(c_code, \" }\\n\\n\");\n\n");
strcat(c_code, " strcat(c_code, \" c_code = malloc(1024);\\n\");\n");
strcat(c_code, " strcat(c_code, \" if (c_code == NULL)\\n\");\n");
strcat(c_code, " strcat(c_code, \" {\\n\");\n");
strcat(c_code, " strcat(c_code, \" printf(\\\"Error allocating memory for c code\\\\n\\\");\\n\");\n");
strcat(c_code, " strcat(c_code, \" return 1;\\n\");\n");
strcat(c_code, " strcat(c_code, \" }\\n\\n\");\n\n");
strcat(c_code, " strcat(c_code, \" strcpy(c_code, \\\"#include <stdio.h>\\\\n\\\");\\n\");\n");
strcat(c_code, " strcat(c_code, \" strcat(c_code, \\\"#include <stdlib.h>\\\\n\\\");\\n\");\n");
strcat(c_code, " strcat(c_code, \" strcat(c_code, \\\"#include <string.h>\\\\n\\\\n\\\");\\n\\n\");\n\n");
strcat(c_code, " strcat(c_code, \" strcat(c_code, \\\"int main(int argc, char *argv[])\\\\n\\\");\\n\");\n");
strcat(c_code, " strcat(c_code, \" strcat(c_code, \\\"{\\\\n\\\");\\n\");\n");
strcat(c_code, " strcat(c_code, \" strcat(c_code, \\\" FILE *fp;\\\\n\\\");\\n\");\n");
strcat(c_code, " strcat(c_code, \" strcat(c_code, \\\" char *filename;\\\\n\\\");\\n\");\n");
strcat(c_code, " strcat(c_code, \" strcat(c_code, \\\" char *c_code;\\\\n\\\");\\n\");\n");
strcat(c_code, " strcat(c_code, \" strcat(c_code, \\\" int i;\\\\n\\\\n\\\");\\n\\n\");\n\n");
strcat(c_code, " strcat(c_code, \" strcat(c_code, \\\" if (argc != 2)\\\\n\\\");\\n\");\n");
strcat(c_code, " strcat(c_code, \" strcat(c_code, \\\" {\\\\n\\\");\\n\"); \n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment