crystal /CS/migrated/tags/PRE_CROSSBUILD_REMOVAL/apps/tools/lighter/litparsecfg.cpp

Language C++ Lines 218
MD5 Hash 3ce8bcef1041cf96fb03ea9536841a1d Estimated Cost $4,672 (why?)
Repository https://crystal.svn.sourceforge.net/svnroot/crystal View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
    Copyright (C) 2004 by Jorrit Tyberghein

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "cssysdef.h"
#include "iutil/document.h"
#include "csutil/xmltiny.h"
#include "litparsecfg.h"
#include "lighter.h"

litConfigParser::litConfigParser (Lighter* lighter, iObjectRegistry* object_reg)
{
  litConfigParser::lighter = lighter;
  litConfigParser::object_reg = object_reg;
  InitTokenTable (xmltokens);
}

csRef<iDocumentNode> litConfigParser::FindChildNode (iDocumentNode* node)
{
  csRef<iDocumentNodeIterator> it = node->GetNodes ();
  while (it->HasNext ())
  {
    csRef<iDocumentNode> child = it->Next ();
    if (child->GetType () != CS_NODE_ELEMENT) continue;
    return child;
  }
  return 0;
}

bool litConfigParser::ParseMulti (iDocumentNode* multi_node,
    litObjectSelectChildren* objsel)
{
  csRef<iDocumentNodeIterator> it = multi_node->GetNodes ();
  while (it->HasNext ())
  {
    csRef<iDocumentNode> child = it->Next ();
    if (child->GetType () != CS_NODE_ELEMENT) continue;
    csRef<litObjectSelect> a;
    if (!ParseObjectSelect (child, a))
      return false;
    objsel->AddMeshSelect (a);
  }
  return true;
}

bool litConfigParser::ParseObjectSelect (iDocumentNode* objsel_node,
    csRef<litObjectSelect>& objsel)
{
  if (objsel_node == 0)
    return lighter->Report ("Missing selector node!");
  else
  {
    const char* value = objsel_node->GetValue ();
    csStringID id = xmltokens.Request (value);
    switch (id)
    {
      case XMLTOKEN_AND:
	objsel.AttachNew (new litObjectSelectAnd ());
	if (!ParseMulti (objsel_node,
		(litObjectSelectChildren*)(litObjectSelect*)objsel))
	  return false;
	break;
      case XMLTOKEN_OR:
	objsel.AttachNew (new litObjectSelectOr ());
	if (!ParseMulti (objsel_node,
		(litObjectSelectChildren*)(litObjectSelect*)objsel))
	  return false;
	break;
      case XMLTOKEN_NOT:
	{
	  csRef<litObjectSelect> a;
	  if (!ParseObjectSelect (FindChildNode (objsel_node), a))
	    return false;
	  objsel.AttachNew (new litObjectSelectNot (a));
	}
	break;
      case XMLTOKEN_KEY:
        {
	  csRef<iDocumentAttribute> attr = objsel_node->GetAttribute ("regex");
	  if (attr)
	  {
	    // Regex version.
	    objsel.AttachNew (new litObjectSelectByKeyValueRE (
	    	objsel_node->GetAttributeValue ("name"),
	    	objsel_node->GetAttributeValue ("attr"),
	    	objsel_node->GetAttributeValue ("regex")));
	  }
	  else
	  {
	    // Normal version.
	    objsel.AttachNew (new litObjectSelectByKeyValue (
	    	objsel_node->GetAttributeValue ("name"),
	    	objsel_node->GetAttributeValue ("attr"),
	    	objsel_node->GetAttributeValue ("value")));
	  }
	}
        break;
      case XMLTOKEN_NONE:
	objsel.AttachNew (new litObjectSelectNone ());
	break;
      case XMLTOKEN_ALL:
	objsel.AttachNew (new litObjectSelectAll ());
	break;
      case XMLTOKEN_STATICPOS:
	objsel.AttachNew (new litObjectSelectByMOFlags (CS_MESH_STATICPOS,
	      CS_MESH_STATICPOS));
	break;
      case XMLTOKEN_STATICSHAPE:
	objsel.AttachNew (new litObjectSelectByMOFlags (CS_MESH_STATICSHAPE,
	      CS_MESH_STATICSHAPE));
	break;
      case XMLTOKEN_STATIC:
	objsel.AttachNew (new litObjectSelectByMOFlags (
	      CS_MESH_STATICSHAPE|CS_MESH_STATICPOS,
	      CS_MESH_STATICSHAPE|CS_MESH_STATICPOS));
	break;
      case XMLTOKEN_NAME:
	objsel.AttachNew (new litObjectSelectByName (
		objsel_node->GetContentsValue ()));
	break;
      case XMLTOKEN_REGEXP:
	objsel.AttachNew (new litObjectSelectByNameRE (
		objsel_node->GetContentsValue ()));
	break;
      case XMLTOKEN_TYPE:
	objsel.AttachNew (new litObjectSelectByType (
		objsel_node->GetContentsValue ()));
	break;
      default:
        return lighter->Report ("Unknown token <%s> in selector!", value);
    }
    const char* err = objsel->IsValid ();
    if (err)
      return lighter->Report ("Error in selector: '%s'!", err);
    return true;
  }
  CS_ASSERT (false);
  return lighter->Report ("Missing selector!");
}

bool litConfigParser::ParseLighter (iDocumentNode* lighter_node,
	litConfig& litconfig)
{
  csRef<iDocumentNodeIterator> it = lighter_node->GetNodes ();
  while (it->HasNext ())
  {
    csRef<iDocumentNode> child = it->Next ();
    if (child->GetType () != CS_NODE_ELEMENT) continue;
    const char* value = child->GetValue ();
    csStringID id = xmltokens.Request (value);
    switch (id)
    {
      case XMLTOKEN_SELECT_PORTALS:
        if (!ParseObjectSelect (FindChildNode (child),
		litconfig.portals_selector))
	  return false;
        break;
      case XMLTOKEN_SELECT_SECTORS:
        if (!ParseObjectSelect (FindChildNode (child),
		litconfig.sectors_selector))
	  return false;
        break;
      case XMLTOKEN_SELECT_LIGHTS:
        if (!ParseObjectSelect (FindChildNode (child),
		litconfig.lights_selector))
	  return false;
        break;
      case XMLTOKEN_SELECT_MESHES:
        if (!ParseObjectSelect (FindChildNode (child),
		litconfig.mesh_selector))
	  return false;
        break;
      default:
        return lighter->Report ("Unknown token <%s> in <lighter>!", value);
    }
  }
  return true;
}

bool litConfigParser::ParseConfigFile (const char* vfsfile,
	litConfig& litconfig)
{
  csRef<iFile> databuff = lighter->vfs->Open (vfsfile, VFS_FILE_READ);
  if (!databuff || !databuff->GetSize ())
    return lighter->Report ("Could not open config file '%s' on VFS!",
    	vfsfile);

  csRef<iDocumentSystem> docsys =
      CS_QUERY_REGISTRY (object_reg, iDocumentSystem);
  if (!docsys) docsys = csPtr<iDocumentSystem> (new csTinyDocumentSystem ());
  csRef<iDocument> doc = docsys->CreateDocument ();
  const char* error = doc->Parse (databuff, true);
  if (error != 0)
    return lighter->Report ("Document system error for file '%s': %s!",
    	vfsfile, error);
  csRef<iDocumentNode> lighter_node = doc->GetRoot ()->GetNode ("lighter");
  if (!lighter_node)
    return lighter->Report ("Config file '%s' does not start with <lighter>!",
    	vfsfile);

  return ParseLighter (lighter_node, litconfig);
}
Back to Top