FreeWRL/FreeX3D  3.0.0
CParse.c
1 /*
2 
3 
4 ???
5 
6 */
7 
8 /****************************************************************************
9  This file is part of the FreeWRL/FreeX3D Distribution.
10 
11  Copyright 2009 CRC Canada. (http://www.crc.gc.ca)
12 
13  FreeWRL/FreeX3D is free software: you can redistribute it and/or modify
14  it under the terms of the GNU Lesser Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  FreeWRL/FreeX3D is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  GNU General Public License for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with FreeWRL/FreeX3D. If not, see <http://www.gnu.org/licenses/>.
25 ****************************************************************************/
26 
27 
28 
29 #include <config.h>
30 #include <system.h>
31 #include <system_threads.h>
32 #include <display.h>
33 #include <internal.h>
34 
35 #include <libFreeWRL.h>
36 
37 #include "../vrml_parser/Structs.h"
38 #include "../main/headers.h"
39 #include "CParseGeneral.h"
40 #include "../scenegraph/Vector.h"
41 #include "../vrml_parser/CFieldDecls.h"
42 #include "CParseParser.h"
43 #include "CParseLexer.h"
44 
45 
46 /* Keep a pointer to the parser for the main URL */
47 //struct VRMLParser* globalParser = NULL;
48 //int inWhichParser = 0; //not used
49 
50 typedef struct pCParse{
51  //struct VRMLParser* globalParser;// = NULL;
52  int ijunk;
53 }* ppCParse;
54 void *CParse_constructor(){
55  void *v = MALLOCV(sizeof(struct pCParse));
56  memset(v,0,sizeof(struct pCParse));
57  return v;
58 }
59 void CParse_init(struct tCParse *t){
60  //public
61  t->globalParser = NULL;
62  //private
63  t->prv = CParse_constructor();
64  {
65  // JAS ppCParse p = (ppCParse)t->prv;
66  //p->globalParser = NULL;
67  }
68 }
69 void CParse_clear(struct tCParse *t){
70  //public
71  struct VRMLParser* globalParser = (struct VRMLParser* )t->globalParser;
72  // some of this should already be done in kill_oldworldB > uload_globalParser
73  if(globalParser)
74  FREE_IF_NZ(globalParser->lexer);
75  FREE_IF_NZ(t->globalParser);
76  //private
77  return;
78 }
79 //ppCParse p = (ppCParse)gglobal()->CParse.prv;
80 
81 #undef TIMING
82 BOOL cParse(void *ectx, void* ptr, unsigned ofs, const char* data) {
83  struct VRMLParser* parser;
84  ttglobal tg = gglobal();
85 
86  #ifdef TIMING
87  double startt, endt;
88  startt = Time1970sec();
89  #endif
90  if (!tg->CParse.globalParser) {
91  /* printf ("cParse, new parser\n"); */
92  /* the FALSE in the newParser call signifies that we are using "VRML" formatted strings */
93  parser=newParser(ectx, ptr, ofs, FALSE);
94  tg->CParse.globalParser = (void *)parser;
95  } else {
96  /* printf ("cParse, using old parser\n"); */
97  parser=reuseParser(ectx, ptr,ofs);
98  }
99 
100  resetParseSuccessfullyFlag();
101 
102  parser_fromString(parser, (char *) data);
103  ASSERT(parser->lexer);
104 
105  if(!parser_vrmlScene(parser)) {
106  #define CHARS_TO_RETURN_ERROR_END 200
107  char finalChars[CHARS_TO_RETURN_ERROR_END];
108 
109  if (strlen(parser->lexer->nextIn) >= (CHARS_TO_RETURN_ERROR_END-10)) {
110  strncpy(finalChars,parser->lexer->nextIn, CHARS_TO_RETURN_ERROR_END-10);
111  finalChars[CHARS_TO_RETURN_ERROR_END-10] = '.';
112  finalChars[CHARS_TO_RETURN_ERROR_END-9] = '.';
113  finalChars[CHARS_TO_RETURN_ERROR_END-8] = '.';
114  finalChars[CHARS_TO_RETURN_ERROR_END-7] = '.';
115  } else {
116  strcpy (finalChars,parser->lexer->nextIn);
117  }
118 
119  ConsoleMessage ("Parsing complete, but have unrecognized data at end of input:\"%s\"",finalChars);
120  }
121 
122  /* printf ("after parsing in cParse, VRMLParser->DEFinedNodes %u\n",parser->DEFedNodes); */
123  /* deleteParser(parser); */
124 
125  /* force any remaining strings to be removed */
126  lexer_forceStringCleanup(parser->lexer);
127 
128  #ifdef TIMING
129  endt = Time1970sec();
130  printf ("time taken %lf\n",endt-startt);
131  #endif
132 
133  return parsedSuccessfully();
134 }
135 
136 /* ************************************************************************** */
137 /* Accessor methods */
138 
139 /* Return DEFed node from its name */
140 struct X3D_Node* parser_getNodeFromName(const char* name)
141 {
142  int ind;
143  ttglobal tg = gglobal();
144  struct VRMLParser *globalParser = (struct VRMLParser *)tg->CParse.globalParser;
145  /* is globalParser actually not NULL? */
146  if (globalParser == NULL) return NULL;
147 
148  ind=lexer_nodeName2id(globalParser->lexer, name);
149  if(ind==ID_UNDEFINED)
150  return NULL;
151 
152  ASSERT(!stack_empty(globalParser->DEFedNodes));
153  ASSERT(ind<vectorSize(stack_top(struct Vector*, globalParser->DEFedNodes)));
154  return vector_get(struct X3D_Node*,
155  stack_top(struct Vector*, globalParser->DEFedNodes), ind);
156 }
157 
158 struct ProtoDefinition *getVRMLbrotoDefinition (struct X3D_Proto *me);
159 /* Return PROTO name from its node, or NULL if not found */
160 char* parser_getPROTONameFromNode(struct X3D_Node *node)
161 {
162  struct ProtoDefinition* cpd;
163  if(node->_nodeType == NODE_Proto){
164  cpd = getVRMLbrotoDefinition(X3D_PROTO(node));
165  if (cpd != NULL) return cpd->protoName;
166  }
167  return NULL;
168 }
169 
170 
171 /* Return DEFed name from its node, or NULL if not found */
172 char *broto_getNameFromNode(struct X3D_Node* node);
173 
174 char* parser_getNameFromNode(struct X3D_Node *node)
175 {
176  return broto_getNameFromNode(node);
177 
178 }
179 
180 /* this is a real assert; hopefully we will never get one of these, as it kills FreeWRL, which is a bad thing. */
181 void fw_assert (char *file, int line) {
182  int looper;
183  printf ("FreeWRL Internal Error at line %d in %s\n",line,file);
184  ConsoleMessage ("FreeWRL Internal Error at line %d in %s",line,file);
185 
186  for (looper=1; looper<60; looper++) {
187  usleep(1000);
188  // sched_yield();
189  }
190  printf ("FreeWRL exiting...\n");
191  exit(1);
192 }
193 
Definition: CParse.c:50
Definition: Vector.h:36