make_carthw_c.c 998 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. int main(int argc, char *argv[])
  5. {
  6. FILE *fi, *fo;
  7. char buf[256];
  8. if (argc != 3) {
  9. printf("usage:\n%s <carthw.cfg> <carthw.c>\n", argv[0]);
  10. return 1;
  11. }
  12. fi = fopen(argv[1], "r");
  13. fo = fopen(argv[2], "w");
  14. if (fi == NULL || fo == NULL) {
  15. printf("fopen failed\n");
  16. return 1;
  17. }
  18. fprintf(fo, "/* generated by %s, do not modify */\n", argv[0]);
  19. fprintf(fo, "static const char builtin_carthw_cfg[] =\n");
  20. while ((fgets(buf, sizeof(buf), fi)))
  21. {
  22. char bufd[256];
  23. char *d = bufd, *p = buf;
  24. int quote = 0;
  25. while (*p && isspace(*p))
  26. p++;
  27. if (*p == 0 || *p == '#')
  28. continue;
  29. /* section names not needed */
  30. if (*p == '[')
  31. strcpy(p, "[]");
  32. for (; *p != 0; p++) {
  33. if (!quote && isspace(*p))
  34. continue;
  35. if (*p == '"') {
  36. quote = !quote;
  37. *d++ = '\\';
  38. }
  39. *d++ = *p;
  40. }
  41. *d = 0;
  42. fprintf(fo, " \"%s\\n\"\n", bufd);
  43. }
  44. fprintf(fo, ";\n");
  45. fclose(fi);
  46. fclose(fo);
  47. return 0;
  48. }