consoleParser.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "console/consoleParser.h"
  24. #include "core/strings/stringFunctions.h"
  25. #include "console/console.h"
  26. namespace Compiler
  27. {
  28. static ConsoleParser *gParserList = NULL;
  29. static ConsoleParser *gDefaultParser = NULL;
  30. void freeConsoleParserList(void)
  31. {
  32. while(gParserList)
  33. {
  34. ConsoleParser * pParser = gParserList;
  35. gParserList = pParser->next;
  36. delete pParser;
  37. }
  38. gDefaultParser = NULL;
  39. }
  40. bool addConsoleParser(char *ext, fnGetCurrentFile gcf, fnGetCurrentLine gcl, fnParse p, fnRestart r, fnSetScanBuffer ssb, bool def)
  41. {
  42. AssertFatal(ext && gcf && gcl && p && r, "AddConsoleParser called with one or more NULL arguments");
  43. ConsoleParser * pParser = new ConsoleParser;
  44. if(pParser != NULL)
  45. {
  46. pParser->ext = ext;
  47. pParser->getCurrentFile = gcf;
  48. pParser->getCurrentLine = gcl;
  49. pParser->parse = p;
  50. pParser->restart = r;
  51. pParser->setScanBuffer = ssb;
  52. if(def)
  53. gDefaultParser = pParser;
  54. pParser->next = gParserList;
  55. gParserList = pParser;
  56. return true;
  57. }
  58. return false;
  59. }
  60. ConsoleParser * getParserForFile(const char *filename)
  61. {
  62. if(filename == NULL)
  63. return gDefaultParser;
  64. char *ptr = dStrrchr((char *)filename, '.');
  65. if(ptr != NULL)
  66. {
  67. ptr++;
  68. ConsoleParser *p;
  69. for(p = gParserList; p; p = p->next)
  70. {
  71. if(dStricmp(ptr, p->ext) == 0)
  72. return p;
  73. }
  74. }
  75. return gDefaultParser;
  76. }
  77. } // end namespace Con