fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / FileIO / ScanParseSkel / OSGScanParseSkel.cpp
blobee667869c3768f9662dcd7d1a2600af5cecb3e95
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <cstdlib>
44 #include <cstdio>
46 #include "OSGConfig.h"
48 #include <iostream>
49 #include <sstream>
51 #ifdef OSG_FLEX_USE_IOSTREAM_INPUT
52 #include <fstream>
53 #endif
55 #include "OSGScanParseLexer.h"
56 #include "OSGScanParseSkel.h"
57 #include "OSGBaseFunctions.h"
58 #include "OSGLog.h"
61 OSG_USING_NAMESPACE
63 /*-------------------------------------------------------------------------*/
64 /* Constructors */
66 ScanParseSkel::ScanParseSkel(void) :
67 _bMapTypeIds (false ),
68 _szReferenceHeader(NULL ),
69 _pLexer (new OSGScanParseLexer ),
71 _tmpString1 ( ),
72 _tmpString2 ( ),
73 _tmpString3 ( ),
74 _tmpFloat1 (0.f ),
75 _tmpFloat2 (0.f ),
76 _tmpFloat3 (0.f ),
77 _tmpFloat4 (0.f ),
78 _tmpBitVector1 (TypeTraits<BitVector>::BitsClear),
80 _image (NULL ),
81 _imageDataPtr (0 ),
82 _options (0 )
86 /*-------------------------------------------------------------------------*/
87 /* Destructor */
89 ScanParseSkel::~ScanParseSkel(void)
91 _image = NULL;
93 delete _pLexer;
95 delete [] _szReferenceHeader;
98 /*-------------------------------------------------------------------------*/
99 /* Scan */
101 extern int OSGScanParseSkel_parse(void *);
103 #ifdef OSG_FLEX_USE_IOSTREAM_INPUT
104 void ScanParseSkel::scanStream(std::istream &iStream)
106 if(iStream.good())
108 _pLexer->switch_streams(&iStream, 0);
109 OSGScanParseSkel_parse(this);
112 #endif
114 void ScanParseSkel::scanFile(const Char8 *szFilename)
116 if(szFilename == NULL)
117 return;
119 #ifdef OSG_FLEX_USE_IOSTREAM_INPUT
120 std::ifstream iStream(szFilename);
122 if(iStream.good())
124 PNOTICE << "Loading Stream: " << szFilename << std::endl;
126 scanStream(iStream);
128 iStream.close();
130 #else
131 FILE *pInFile = fopen(szFilename, "r");
133 if(pInFile != NULL)
135 PNOTICE << "Loading File : " << szFilename << std::endl;
137 OSGScanParseSkel_in = pInFile;
139 OSGScanParseSkel_parse(this);
141 fclose(pInFile);
143 #endif
146 Int32 ScanParseSkel::lex(YYSTYPE *lvalp)
148 return _pLexer->yylex(lvalp);
151 const Char8 *ScanParseSkel::getText(void)
153 return _pLexer->YYText();
156 void ScanParseSkel::handleError(void *pSelf, const Char8 *szErrorText)
158 FWARNING(("-----> %s in Line %d, read '%s'\n",
159 szErrorText,
160 _pLexer->lineno(),
161 _pLexer->YYText()));
164 /*-------------------------------------------------------------------------*/
165 /* Prototypes */
167 bool ScanParseSkel::verifyHeader(const Char8 *szHeader)
169 bool returnValue = true;
171 if(szHeader == NULL)
173 SLOG << "Error Null Header String Read" << std::endl;
174 return false;
177 if(_szReferenceHeader != NULL)
179 if(strncmp(szHeader,
180 _szReferenceHeader,
181 osgStringLen(_szReferenceHeader)) != 0)
183 SLOG << "Error : Header [" << szHeader << "] does not "
184 << "match reference [" << _szReferenceHeader << "]"
185 << std::endl;
187 returnValue = false;
190 else
192 SLOG << "Error Null Reference Header, Check disabled " << std::endl;
193 return true;
196 return returnValue;
199 bool ScanParseSkel::checkHeader(void)
201 return false;
204 void ScanParseSkel::beginProto(const Char8 *)
208 void ScanParseSkel::endProto(void)
212 void ScanParseSkel::endProtoInterface(void)
216 void ScanParseSkel::beginExternProto(const Char8 *)
220 void ScanParseSkel::endExternProtoInterface(void)
224 void ScanParseSkel::endExternProto(void)
228 void ScanParseSkel::beginScript(const Char8 *)
232 void ScanParseSkel::endScript(void)
236 void ScanParseSkel::beginEventInDecl (const Char8 *,
237 const UInt32 ,
238 const Char8 *)
242 void ScanParseSkel::beginEventOutDecl(const Char8 *,
243 const UInt32 ,
244 const Char8 *)
248 void ScanParseSkel::endEventDecl(void)
252 void ScanParseSkel::beginFieldDecl(const Char8 *,
253 const UInt32 ,
254 const Char8 *)
258 void ScanParseSkel::endFieldDecl(void)
262 void ScanParseSkel::beginExposedFieldDecl(const Char8 *,
263 const UInt32 ,
264 const Char8 *)
268 void ScanParseSkel::endExposedFieldDecl(void)
273 void ScanParseSkel::addExternEventInDecl (const Char8 *,
274 const UInt32 ,
275 const Char8 *)
279 void ScanParseSkel::addExternEventOutDecl(const Char8 *,
280 const UInt32 ,
281 const Char8 *)
285 void ScanParseSkel::addExternFieldDecl(const Char8 *,
286 const UInt32 ,
287 const Char8 *)
291 void ScanParseSkel::addExternExposedFieldDecl(const Char8 *,
292 const UInt32 ,
293 const Char8 *)
297 /*-------------------------------------------------------------------------*/
298 /* Fields */
300 void ScanParseSkel::beginField(const Char8 *,
301 const UInt32)
305 void ScanParseSkel::endField(void)
309 /*-------------------------------------------------------------------------*/
310 /* Nodes */
312 void ScanParseSkel::beginNode(const Char8 *szNodeTypename,
313 const Char8 *szNodename,
314 const BitVector bvLocalFlags)
316 fprintf(stderr, "Calling default begin node %s %s\n",
317 szNodeTypename,
318 szNodename);
321 void ScanParseSkel::endNode(void)
325 void ScanParseSkel::nullNode(void)
329 void ScanParseSkel::use(const Char8 *szName)
331 PINFO << "Use : " << szName << std::endl;
334 void ScanParseSkel::is(const Char8 *)
338 void ScanParseSkel::profileElement(const Char8 *szProfileName)
340 fprintf(stderr, "PROFILE %s\n", szProfileName);
343 void ScanParseSkel::componentElement (const Char8 *szComponentName)
345 fprintf(stderr, "COMPONENT %s\n", szComponentName);
348 void ScanParseSkel::metaElement (const Char8 *szMetaKey,
349 const Char8 *szMetaValue)
351 fprintf(stderr, "META %s %s\n", szMetaKey, szMetaValue);
354 void ScanParseSkel::importElement(const Char8 *szInlineName,
355 const Char8 *szNodeName,
356 const Char8 *szImportAs )
358 fprintf(stderr, "IMPORT %s.%s AS %s\n",
359 szInlineName, szNodeName, szImportAs);
362 void ScanParseSkel::exportElement(const Char8 *szNodeName,
363 const Char8 *szExportAs )
365 fprintf(stderr, "EXPORT %s AS %s\n", szNodeName, szExportAs);
368 /*-------------------------------------------------------------------------*/
369 /* Helper */
371 void ScanParseSkel::addBufferAsStringValue(void)
373 addStringValue(_pLexer->YYText());
376 void ScanParseSkel::addFieldValue(const Char8 *)
380 void ScanParseSkel::addBoolValue(bool b)
382 addFieldValue(_pLexer->YYText());
385 void ScanParseSkel::addColorValue(const Color3f &c)
387 addFieldValue(_tmpString1.c_str());
390 void ScanParseSkel::addColorRGBAValue(const Color4f &c)
392 addFieldValue(_tmpString1.c_str());
395 void ScanParseSkel::addDoubleValue(Real64 d)
397 addFieldValue(_pLexer->YYText());
400 void ScanParseSkel::addFloatValue(Real32 f)
402 addFieldValue(_pLexer->YYText());
405 void ScanParseSkel::addImageValue(Image *img)
407 UInt32 numComponents = img->getComponents();
408 std::ostringstream os;
409 os << img->getWidth() << ' ' << img->getHeight() << ' ' << numComponents;
410 addFieldValue(os.str().c_str());
411 UInt32 numPixels = img->getWidth() * img->getHeight();
412 UInt32 pixel;
413 UInt8 *data = img->editData();
414 for (UInt32 i = 0; i < numPixels; ++i)
416 pixel = 0;
417 for (UInt32 j = 0; j < numComponents; ++j)
418 pixel = (pixel << 8) | *data++;
419 os.str(std::string());
420 os << pixel;
421 addFieldValue(os.str().c_str());
425 void ScanParseSkel::addInt32Value(Int32 i)
427 addFieldValue(_pLexer->YYText());
430 void ScanParseSkel::addMatrix3dValue(Real64 m00, Real64 m10, Real64 m20,
431 Real64 m01, Real64 m11, Real64 m21,
432 Real64 m02, Real64 m12, Real64 m22)
434 addFieldValue(_tmpString1.c_str());
437 void ScanParseSkel::addMatrix3fValue(Real32 m00, Real32 m10, Real32 m20,
438 Real32 m01, Real32 m11, Real32 m21,
439 Real32 m02, Real32 m12, Real32 m22)
441 addFieldValue(_tmpString1.c_str());
444 void ScanParseSkel::addMatrix4dValue(const Matrix4d &m)
446 addFieldValue(_tmpString1.c_str());
449 void ScanParseSkel::addMatrix4fValue(const Matrix4f &m)
451 addFieldValue(_tmpString1.c_str());
454 void ScanParseSkel::addRotationValue(const Vec3f &axis, Real32 angle)
456 addFieldValue(_tmpString1.c_str());
459 void ScanParseSkel::addStringValue(const std::string &s)
461 addFieldValue(s.c_str());
464 void ScanParseSkel::addTimeValue(Time t)
466 addFieldValue(_pLexer->YYText());
469 void ScanParseSkel::addVec2dValue(const Vec2d &v)
471 addFieldValue(_tmpString1.c_str());
474 void ScanParseSkel::addVec3iValue(const Vec3i &v)
476 addFieldValue(_tmpString1.c_str());
479 void ScanParseSkel::addVec2fValue(const Vec2f &v)
481 addFieldValue(_tmpString1.c_str());
484 void ScanParseSkel::addVec3dValue(const Vec3d &v)
486 addFieldValue(_tmpString1.c_str());
489 void ScanParseSkel::addVec3fValue(const Vec3f &v)
491 addFieldValue(_tmpString1.c_str());
494 void ScanParseSkel::addVec4dValue(const Vec4d &v)
496 addFieldValue(_tmpString1.c_str());
499 void ScanParseSkel::addVec4fValue(const Vec4f &v)
501 addFieldValue(_tmpString1.c_str());
504 void ScanParseSkel::addColor4iValue(const Color4ub &c)
506 addFieldValue(_tmpString1.c_str());
509 void ScanParseSkel::addPnt2fValue(const Pnt2f &p)
511 addFieldValue(_tmpString1.c_str());
514 void ScanParseSkel::addPnt3fValue(const Pnt3f &p)
516 addFieldValue(_tmpString1.c_str());
519 void ScanParseSkel::addPnt4fValue(const Pnt4f &p)
521 addFieldValue(_tmpString1.c_str());
524 void ScanParseSkel::addVec2iValue(const Vec2i &)
526 addFieldValue(_tmpString1.c_str());
529 void ScanParseSkel::addPlaneValue(const Plane &p)
531 addFieldValue(_tmpString1.c_str());
534 void ScanParseSkel::addVolumeValue(const BoxVolume &v)
536 addFieldValue(_tmpString1.c_str());
539 void ScanParseSkel::addRoute(const Char8 *,
540 const Char8 *,
541 const Char8 *,
542 const Char8 *)
546 /*-------------------------------------------------------------------------*/
547 /* Type Information */
549 bool ScanParseSkel::getMapFieldTypes(void)
551 return _bMapTypeIds;
554 Int32 ScanParseSkel::mapExtIntFieldType(const Char8*, const Int32 iFieldType)
556 return iFieldType;
559 UInt32 ScanParseSkel::getFieldType(const Char8 *)
561 return 0;
564 OSGScanParseLexer *ScanParseSkel::getLexer(void)
566 return _pLexer;
570 /*-------------------------------------------------------------------------*/
571 /* Options */
573 void ScanParseSkel::addOptions(UInt32 options)
575 _options |= options;
578 void ScanParseSkel::subOptions(UInt32 options)
580 _options &= ~options;
583 UInt32 ScanParseSkel::getOptions(void) const
585 return _options;
588 /*-------------------------------------------------------------------------*/
589 /* Set */
591 void ScanParseSkel::reset(void)
595 void ScanParseSkel::setMapTypeIds(bool bVal)
597 _bMapTypeIds = bVal;
600 void ScanParseSkel::setReferenceHeader(const Char8 *szReferenceHeader)
602 osgStringDup(szReferenceHeader, _szReferenceHeader);
605 /*-------------------------------------------------------------------------*/
607 void ScanParseSkel::beginValue()
609 _tmpString1.assign(_pLexer->YYText(), _pLexer->YYLeng());
612 void ScanParseSkel::appendValue()
614 _tmpString1.push_back(' ');
615 _tmpString1.append(_pLexer->YYText(), _pLexer->YYLeng());
618 void ScanParseSkel::beginImage(Int32 width, Int32 height, Int32 components)
620 if (_image == NULL)
622 _image = Image::create();
624 UInt32 pixelFormat;
625 switch (components)
627 case 1: pixelFormat = Image::OSG_L_PF; break;
628 case 2: pixelFormat = Image::OSG_LA_PF; break;
629 case 3: pixelFormat = Image::OSG_RGB_PF; break;
630 case 4: pixelFormat = Image::OSG_RGBA_PF; break;
631 default: pixelFormat = Image::OSG_INVALID_PF; break;
633 _image->set(pixelFormat, width, height);
634 _imageDataPtr = _image->editData();
635 _pLexer->expectImageInts(width * height);
638 void ScanParseSkel::addImagePixel(Int32 pixel)
640 switch (_image->getPixelFormat())
642 case Image::OSG_RGBA_PF:
643 *_imageDataPtr++ = pixel >> 24;
644 // Fall through
645 case Image::OSG_RGB_PF:
646 *_imageDataPtr++ = pixel >> 16;
647 // Fall through
648 case Image::OSG_LA_PF:
649 *_imageDataPtr++ = pixel >> 8;
650 // Fall through
651 case Image::OSG_L_PF:
652 *_imageDataPtr++ = pixel;
653 // Fall through
654 default:
655 break;
659 void ScanParseSkel::endImage()
661 _imageDataPtr = 0;
663 addImageValue(_image);
665 _image = NULL;