ansi-to-src.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #define ZF_LOGD(...)
  5. #include "utils.h"
  6. int read_file(const char *filename) {
  7. FILE *fp;
  8. char buffer[10240];
  9. int read;
  10. fp = fopen(filename, "rb");
  11. if (fp == NULL) {
  12. ZF_LOGD("Failed to open %s", filename);
  13. return 0;
  14. }
  15. {
  16. const char *ccp = filename;
  17. while (strchr(ccp, '/') != NULL)
  18. ccp = strchr(ccp, '/') + 1;
  19. strcpy(buffer, ccp);
  20. }
  21. char *cp;
  22. if ((cp = strchr(buffer, '.')) != NULL) {
  23. *cp = 0;
  24. }
  25. cp = buffer;
  26. while (*cp != 0) {
  27. if (!isalpha(*cp))
  28. memmove(cp, cp + 1, strlen(cp));
  29. cp++;
  30. }
  31. printf("const char * %s[] = {\n", buffer);
  32. if ((read = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
  33. char *nl;
  34. buffer[read] = 0;
  35. cp = repr(buffer);
  36. int first = 1;
  37. while ((nl = strstr(cp, "\\r\\n")) != NULL) {
  38. strcpy(nl, "\"");
  39. if (!first) {
  40. printf(",\n");
  41. };
  42. first = 0;
  43. printf(" \"%s", cp);
  44. cp = nl + 4;
  45. }
  46. if (strlen(cp) > 0)
  47. printf(",\n \"%s\"\n", cp);
  48. else
  49. printf("\n");
  50. printf("};\n");
  51. };
  52. fclose(fp);
  53. return 1;
  54. }
  55. int main(int argc, char *argv[]) {
  56. for (int pos = 1; pos < argc; ++pos) {
  57. read_file(argv[pos]);
  58. }
  59. return 0;
  60. }