You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
2.6 KiB

  1. /* Copyright (C) 2004 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. /*
  13. Written by Magnus Svensson
  14. */
  15. /*
  16. Converts a SQL file into a C file that can be compiled and linked
  17. into other programs
  18. */
  19. #include <stdarg.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. FILE *in, *out;
  23. static void die(const char *fmt, ...)
  24. {
  25. va_list args;
  26. /* Print the error message */
  27. fprintf(stderr, "FATAL ERROR: ");
  28. if (fmt)
  29. {
  30. va_start(args, fmt);
  31. vfprintf(stderr, fmt, args);
  32. va_end(args);
  33. }
  34. else
  35. fprintf(stderr, "unknown error");
  36. fprintf(stderr, "\n");
  37. fflush(stderr);
  38. /* Close any open files */
  39. if (in)
  40. fclose(in);
  41. if (out)
  42. fclose(out);
  43. exit(1);
  44. }
  45. int main(int argc, char *argv[])
  46. {
  47. char buff[512];
  48. char* struct_name= argv[1];
  49. char* infile_name= argv[2];
  50. char* outfile_name= argv[3];
  51. if (argc != 4)
  52. die("Usage: comp_sql <struct_name> <sql_filename> <c_filename>");
  53. /* Open input and output file */
  54. if (!(in= fopen(infile_name, "r")))
  55. die("Failed to open SQL file '%s'", infile_name);
  56. if (!(out= fopen(outfile_name, "w")))
  57. die("Failed to open output file '%s'", outfile_name);
  58. fprintf(out, "const char* %s={\n\"", struct_name);
  59. while (fgets(buff, sizeof(buff), in))
  60. {
  61. char *curr= buff;
  62. while (*curr)
  63. {
  64. if (*curr == '\n')
  65. {
  66. /*
  67. Reached end of line, add escaped newline, escaped
  68. backslash and a newline to outfile
  69. */
  70. fprintf(out, "\\n \"\n\"");
  71. curr++;
  72. }
  73. else if (*curr == '\r')
  74. {
  75. curr++; /* Skip */
  76. }
  77. else
  78. {
  79. if (*curr == '"')
  80. {
  81. /* Needs escape */
  82. fputc('\\', out);
  83. }
  84. fputc(*curr, out);
  85. curr++;
  86. }
  87. }
  88. if (*(curr-1) != '\n')
  89. {
  90. /*
  91. Some compilers have a max string length,
  92. insert a newline at every 512th char in long
  93. strings
  94. */
  95. fprintf(out, "\"\n\"");
  96. }
  97. }
  98. fprintf(out, "\\\n\"};\n");
  99. fclose(in);
  100. fclose(out);
  101. exit(0);
  102. }