parse.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. Copyright (C) 2001-2006, William Joseph.
  3. All Rights Reserved.
  4. This file is part of GtkRadiant.
  5. GtkRadiant is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. GtkRadiant is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GtkRadiant; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #include "parse.h"
  18. #include <list>
  19. #include "ientity.h"
  20. #include "ibrush.h"
  21. #include "ipatch.h"
  22. #include "ieclass.h"
  23. #include "iscriplib.h"
  24. #include "scenelib.h"
  25. #include "string/string.h"
  26. #include "stringio.h"
  27. #include "eclasslib.h"
  28. inline MapImporter* Node_getMapImporter(scene::Node& node)
  29. {
  30. return NodeTypeCast<MapImporter>::cast(node);
  31. }
  32. typedef std::list< std::pair<CopiedString, CopiedString> > KeyValues;
  33. NodeSmartReference g_nullNode(NewNullNode());
  34. NodeSmartReference Entity_create(EntityCreator& entityTable, EntityClass* entityClass, const KeyValues& keyValues)
  35. {
  36. scene::Node& entity(entityTable.createEntity(entityClass));
  37. for(KeyValues::const_iterator i = keyValues.begin(); i != keyValues.end(); ++i)
  38. {
  39. Node_getEntity(entity)->setKeyValue((*i).first.c_str(), (*i).second.c_str());
  40. }
  41. return NodeSmartReference(entity);
  42. }
  43. NodeSmartReference Entity_parseTokens(Tokeniser& tokeniser, EntityCreator& entityTable, const PrimitiveParser& parser, int index)
  44. {
  45. NodeSmartReference entity(g_nullNode);
  46. KeyValues keyValues;
  47. const char* classname = "";
  48. int count_primitives = 0;
  49. while(1)
  50. {
  51. tokeniser.nextLine();
  52. const char* token = tokeniser.getToken();
  53. if(token == 0)
  54. {
  55. Tokeniser_unexpectedError(tokeniser, token, "#entity-token");
  56. return g_nullNode;
  57. }
  58. if (!strcmp(token, "}")) // end entity
  59. {
  60. if(entity == g_nullNode)
  61. {
  62. // entity does not have brushes
  63. entity = Entity_create(entityTable, GlobalEntityClassManager().findOrInsert(classname, false), keyValues);
  64. }
  65. return entity;
  66. }
  67. else if(!strcmp(token, "{")) // begin primitive
  68. {
  69. if(entity == g_nullNode)
  70. {
  71. // entity has brushes
  72. entity = Entity_create(entityTable, GlobalEntityClassManager().findOrInsert(classname, true), keyValues);
  73. }
  74. tokeniser.nextLine();
  75. NodeSmartReference primitive(parser.parsePrimitive(tokeniser));
  76. if(primitive == g_nullNode || !Node_getMapImporter(primitive)->importTokens(tokeniser))
  77. {
  78. globalErrorStream() << "brush " << count_primitives << ": parse error\n";
  79. return g_nullNode;
  80. }
  81. scene::Traversable* traversable = Node_getTraversable(entity);
  82. if(Node_getEntity(entity)->isContainer() && traversable != 0)
  83. {
  84. traversable->insert(primitive);
  85. }
  86. else
  87. {
  88. globalErrorStream() << "entity " << index << ": type " << classname << ": discarding brush " << count_primitives << "\n";
  89. }
  90. ++count_primitives;
  91. }
  92. else // epair
  93. {
  94. CopiedString key(token);
  95. token = tokeniser.getToken();
  96. if(token == 0)
  97. {
  98. Tokeniser_unexpectedError(tokeniser, token, "#epair-value");
  99. return g_nullNode;
  100. }
  101. keyValues.push_back(KeyValues::value_type(key, token));
  102. if(string_equal(key.c_str(), "classname"))
  103. {
  104. classname = keyValues.back().second.c_str();
  105. }
  106. }
  107. }
  108. // unreachable code
  109. return g_nullNode;
  110. }
  111. void Map_Read(scene::Node& root, Tokeniser& tokeniser, EntityCreator& entityTable, const PrimitiveParser& parser)
  112. {
  113. int count_entities = 0;
  114. for(;;)
  115. {
  116. tokeniser.nextLine();
  117. if (!tokeniser.getToken()) // { or 0
  118. break;
  119. NodeSmartReference entity(Entity_parseTokens(tokeniser, entityTable, parser, count_entities));
  120. if(entity == g_nullNode)
  121. {
  122. globalErrorStream() << "entity " << count_entities << ": parse error\n";
  123. return;
  124. }
  125. Node_getTraversable(root)->insert(entity);
  126. ++count_entities;
  127. }
  128. }