makeres.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <bdlib/src/String.h>
  2. #include <bdlib/src/Stream.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. #include <stdlib.h>
  7. #include <algorithm>
  8. int skipline (const char *line, int *skip) {
  9. static int multi = 0;
  10. if ((!strncmp(line, "//", 2))) {
  11. (*skip)++;
  12. } else if ( (strstr(line, "/*")) && (strstr(line, "*/")) ) {
  13. multi = 0;
  14. (*skip)++;
  15. } else if ( (strstr(line, "/*")) ) {
  16. (*skip)++;
  17. multi = 1;
  18. } else if ( (strstr(line, "*/")) ) {
  19. multi = 0;
  20. } else {
  21. if (!multi) (*skip) = 0;
  22. }
  23. return (*skip);
  24. }
  25. int parse_res(const bd::String& inFile, const bd::String& outFile, const bd::String& outsFile) {
  26. bd::Stream in, out, outs;
  27. bd::String buffer, cmd, buf, lower_resps;
  28. int skip = 0, total_responses = 0;
  29. in.loadFile(inFile);
  30. printf("Parsing res file '%s'", inFile.c_str());
  31. out << buf.printf( "/* DO NOT EDIT THIS FILE. */\n\
  32. #ifndef _RESPONSE_H\n\
  33. #define _RESPONSE_H\n\
  34. \n\
  35. typedef unsigned int response_t;\n\n\
  36. enum {\n");
  37. outs << buf.printf("/* DO NOT EDIT THIS FILE. */\n\
  38. #ifndef _RESPONSES_H\n\
  39. #define _RESPONSES_H\n\
  40. \n\
  41. typedef const char * res_t;\n\n");
  42. while (in.tell() < in.length()) {
  43. buffer = in.getline().chomp();
  44. if ((skipline(buffer.c_str(), &skip))) continue;
  45. if (buffer[0] == ':') { /* New cmd */
  46. if (cmd.length()) { /* CLOSE LAST RES */
  47. out << ",\n"; /* for enum */
  48. outs << "\tNULL\n};\n\n";
  49. cmd.clear();
  50. }
  51. cmd = buffer(1);
  52. if (cmd != "end") { /* NEXT RES */
  53. ++total_responses;
  54. printf(".");
  55. bd::String cmdUpper(cmd);
  56. std::transform(cmdUpper.begin(), cmdUpper.end(), cmdUpper.mdata(), (int(*)(int)) toupper);
  57. out << buf.printf("\tRES_%s", cmdUpper.c_str());
  58. if (total_responses == 1)
  59. out << " = 1";
  60. outs << buf.printf("static res_t res_%s[] = {\n", cmd.c_str());
  61. lower_resps += buf.printf(",\n\tres_%s", cmd.c_str());
  62. } else { /* END */
  63. out << buf.printf("\tRES_END\n};\n\n#define RES_TYPES %d\n", total_responses);
  64. out << buf.printf("const char *response(response_t);\nvoid init_responses();\nconst char *r_banned(struct chanset_t* chan);\n\n#endif /* !_RESPONSE_H */\n");
  65. outs << buf.printf("static res_t *res[] = {\n\tNULL%s\n};\n#endif /* !_RESPONSES_H */\n", lower_resps.c_str());
  66. }
  67. } else { /* NEXT RES TEXT */
  68. ++buffer;
  69. outs << buf.printf("\t\"%s\",\n", buffer.c_str());
  70. }
  71. }
  72. printf(" Success\n");
  73. out.writeFile(outFile);
  74. outs.writeFile(outsFile);
  75. return 0;
  76. }
  77. int main(int argc, char **argv) {
  78. if (argc < 3) return 1;
  79. bd::String in(argv[1]), out, outs;
  80. out.printf("%s/response.h%s", argv[2], argc == 4 ? "~" : "");
  81. outs.printf("%s/responses.h%s", argv[2], argc == 4 ? "~" : "");
  82. return parse_res(in, out, outs);
  83. }