fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / FileIO / OSG / OSGOSGSceneFileType.cpp
blob7d00768954e775770f717672918326485f6bbaa1
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 "OSGOSGSceneFileType.h"
50 #include "OSGOSGWriter.h"
52 #include "OSGOSGLoader.h"
54 OSG_USING_NAMESPACE
57 /*! \class OSG::OSGSceneFileType
60 /***************************************************************************\
61 * Types *
62 \***************************************************************************/
64 /***************************************************************************\
65 * Class variables *
66 \***************************************************************************/
68 const Char8 *OSGSceneFileType::_suffixA[] = { "osg" };
70 OSGSceneFileType OSGSceneFileType::_the(_suffixA,
71 sizeof(_suffixA),
72 false,
73 10,
74 OSG_READ_SUPPORTED |
75 OSG_WRITE_SUPPORTED);
77 /***************************************************************************\
78 * Class methods *
79 \***************************************************************************/
81 /*-------------------------------------------------------------------------*\
82 - public -
83 \*-------------------------------------------------------------------------*/
85 OSGSceneFileType &OSGSceneFileType::the(void)
87 return _the;
90 /*-------------------------------------------------------------------------*\
91 - protected -
92 \*-------------------------------------------------------------------------*/
94 /*-------------------------------------------------------------------------*\
95 - private -
96 \*-------------------------------------------------------------------------*/
98 /***************************************************************************\
99 * Instance methods *
100 \***************************************************************************/
102 /*-------------------------------------------------------------------------*\
103 - public -
104 \*-------------------------------------------------------------------------*/
106 /*------------------------------ access -----------------------------------*/
108 const Char8 *OSGSceneFileType::getName(void) const
110 return "OSG Ascii Geometry";
114 NodeTransitPtr OSGSceneFileType::read( std::istream &is,
115 const Char8 *fileNameOrExtension,
116 Resolver resolver ) const
118 FileContextAttachmentUnrecPtr
119 pFileContext = FileContextAttachment::create();
121 pFileContext->setResolvedName(fileNameOrExtension);
123 OSGLoader *_pFile = new OSGLoader(_endNodeFunctors, pFileContext);
125 NodeTransitPtr returnValue = _pFile->scanStream(is, resolver);
127 if(returnValue->getNChildren() == 1)
129 returnValue = returnValue->getChild(0);
132 returnValue->addAttachment(pFileContext);
134 delete _pFile;
136 commitChanges();
138 return returnValue;
141 FieldContainerTransitPtr OSGSceneFileType::readContainer(
142 const Char8 *fileName,
143 Resolver resolver) const
145 if(fileName == NULL)
147 SWARNING << "cannot read NULL file" << std::endl;
148 return FieldContainerTransitPtr(NULL);
151 FileContextAttachmentUnrecPtr
152 pFileContext = FileContextAttachment::create();
154 pFileContext->setResolvedName(fileName);
156 OSGLoader *_pFile = new OSGLoader(_endNodeFunctors, pFileContext);
158 std::ifstream is(fileName, std::ios::binary);
160 FieldContainerTransitPtr returnValue =
161 _pFile->scanStreamContainer(is, resolver);
163 AttachmentContainer *pAttCnt =
164 dynamic_cast<AttachmentContainer *>(returnValue.get());
166 if(pAttCnt != NULL)
168 pAttCnt->addAttachment(pFileContext);
171 delete _pFile;
173 commitChanges();
175 return returnValue;
178 bool OSGSceneFileType::write(Node * const root,
179 std::ostream &os,
180 Char8 const * ) const
182 if(!os)
184 FFATAL(("Can not open output stream!\n"));
185 return false;
188 IndentOutStreamMixin<OutStream> iOStream(os);
190 OSGWriter writer(iOStream, 4);
191 writer.write(root);
193 return true;
196 bool OSGSceneFileType::writeContainer(FieldContainer * const pContainer,
197 Char8 const *fileName) const
199 if(fileName == NULL)
201 SWARNING << "cannot write NULL file" << std::endl;
202 return false;
205 std::ofstream os(fileName, std::ios::binary);
207 IndentOutStreamMixin<OutStream> iOStream(os);
209 OSGWriter writer(iOStream, 4);
211 writer.write(pContainer);
213 return true;
216 /*---------------------------- properties ---------------------------------*/
218 void OSGSceneFileType::registerEndNodeCallback(const FieldContainerType &type,
219 const Functor &func)
221 if(_endNodeFunctors.size() <= type.getId())
222 _endNodeFunctors.resize(type.getId() + 1, NULL);
224 _endNodeFunctors[type.getId()] = func;
227 /*-------------------------- your_category---------------------------------*/
229 /*-------------------------- assignment -----------------------------------*/
231 /*-------------------------- comparison -----------------------------------*/
234 /*-------------------------------------------------------------------------*\
235 - protected -
236 \*-------------------------------------------------------------------------*/
239 /** \brief Constructor
242 OSGSceneFileType::OSGSceneFileType(const char *suffixArray[],
243 UInt16 suffixByteCount,
244 bool override,
245 UInt32 overridePriority,
246 UInt32 flags) :
247 Inherited(suffixArray,
248 suffixByteCount,
249 override,
250 overridePriority,
251 flags),
252 _endNodeFunctors()
254 return;
258 /** \brief Destructor
261 OSGSceneFileType::~OSGSceneFileType(void)
265 void OSGSceneFileType::terminate(void)
267 _endNodeFunctors.clear();