consoleParser.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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/console.h"
  24. #include "console/consoleParser.h"
  25. namespace Compiler
  26. {
  27. static ConsoleParser *gParserList = NULL;
  28. static ConsoleParser *gDefaultParser = NULL;
  29. void freeConsoleParserList(void)
  30. {
  31. ConsoleParser *pParser;
  32. while((pParser = gParserList))
  33. {
  34. gParserList = pParser->next;
  35. delete pParser;
  36. }
  37. gDefaultParser = NULL;
  38. }
  39. bool addConsoleParser( const char *ext, fnGetCurrentFile gcf, fnGetCurrentLine gcl, fnParse p, fnRestart r, fnSetScanBuffer ssb, bool def /* = false */)
  40. {
  41. AssertFatal(ext && gcf && gcl && p && r, "AddConsoleParser called with one or more NULL arguments");
  42. ConsoleParser *pParser;
  43. if((pParser = new ConsoleParser))
  44. {
  45. pParser->ext = ext;
  46. pParser->getCurrentFile = gcf;
  47. pParser->getCurrentLine = gcl;
  48. pParser->parse = p;
  49. pParser->restart = r;
  50. pParser->setScanBuffer = ssb;
  51. if(def)
  52. gDefaultParser = pParser;
  53. pParser->next = gParserList;
  54. gParserList = pParser;
  55. return true;
  56. }
  57. return false;
  58. }
  59. ConsoleParser * getParserForFile(const char *filename)
  60. {
  61. char *ptr;
  62. if(filename == NULL)
  63. return gDefaultParser;
  64. if((ptr = dStrrchr((char *)filename, '.')))
  65. {
  66. ptr++;
  67. ConsoleParser *p;
  68. for(p = gParserList;p;p = p->next)
  69. {
  70. if(dStricmp(ptr, p->ext) == 0)
  71. return p;
  72. }
  73. }
  74. return gDefaultParser;
  75. }
  76. } // end namespace Con