root/OpenSceneGraph/trunk/src/osgDB/Output.cpp @ 13041

Revision 13041, 5.9 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13#include <osgDB/Output>
14#include <osgDB/Registry>
15#include <osgDB/FileNameUtils>
16
17#include <osg/Notify>
18
19#include <sstream>
20#include <stdio.h>
21#include <string.h>
22
23using namespace std;
24using namespace osgDB;
25
26static osg::ApplicationUsageProxy Output_e0(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_WRITE_OUT_DEFAULT_VALUES", "ON | OFF");
27
28Output::Output()
29{
30    init();
31}
32
33Output::Output(const char* name) : osgDB::ofstream(name)
34{
35    init();
36    _filename = name;
37}
38
39Output::~Output()
40{
41}
42
43
44void Output::init()
45{
46    _indent = 0;
47    _indentStep = 2;
48    _numIndicesPerLine = 10;
49    _pathNameHint = AS_IS;
50
51    _outputTextureFiles = false;
52    _textureFileNameNumber = 0;
53
54    _outputShaderFiles = false;
55    _shaderFileNameNumber = 0;
56
57    _writeOutDefaultValues = false;
58
59    const char* env = getenv("OSG_WRITE_OUT_DEFAULT_VALUES");
60    if (env)
61    {
62        _writeOutDefaultValues = strcmp(env,"ON")==0;
63    }
64}
65
66void Output::setOptions(const Options* options)
67{
68    _options = options;
69}
70
71void Output::open(const char *name)
72{
73    init();
74    osgDB::ofstream::open(name);
75    _filename = name;
76}
77
78// Comment out to avoid compile errors under new compilers, the int mode
79// is now a replaced by a class to wrap the mode.
80// This method is not used right now to hopefully nobody will miss it...
81// Jan 2002.
82// void Output::open(const char *name,int mode)
83// {
84//     init();
85//     ofstream::open(name,mode);
86//     _filename = name;
87// }
88
89Output& Output::indent()
90{
91    for(int i=0;i<_indent;++i) *this<<' ';
92    return *this;
93}
94
95
96
97void Output::moveIn()
98{
99    _indent += _indentStep;
100}
101
102
103void Output::moveOut()
104{
105    _indent -= _indentStep;
106    if (_indent<0) _indent=0;
107}
108
109std::string Output::wrapString(const char* str)
110{
111    if (!str) return std::string("\"\"");
112    return wrapString(std::string(str));
113}
114
115std::string Output::wrapString(const std::string& str)
116{
117    std::string newstring;
118    newstring += '"';
119    for(unsigned int i=0;i<str.size();++i)
120    {
121        if (str[i]=='\\')
122        {
123            newstring += '\\';
124            newstring += '\\';
125        }
126        else if (str[i]=='"')
127        {
128            newstring += '\\';
129            newstring += '"';
130        }
131        else newstring += (str[i]);
132    }
133    newstring += '"';
134    return newstring;
135}
136
137bool Output::writeObject(const osg::Object& obj)
138{
139    return Registry::instance()->getDeprecatedDotOsgObjectWrapperManager()->writeObject(obj,*this);
140}
141
142
143void Output::writeBeginObject(const std::string& name)
144{
145    indent() << name << " {" << std::endl;
146}
147
148void Output::writeEndObject()
149{
150    indent() << "}" << std::endl;
151}
152
153void Output::writeUseID(const std::string& id)
154{
155    indent() << "Use " << id << std::endl;
156}
157
158void Output::writeUniqueID(const std::string& id)
159{
160    indent() << "UniqueID " << id << std::endl;
161}
162
163bool Output::getUniqueIDForObject(const osg::Object* obj,std::string& uniqueID)
164{
165    UniqueIDToLabelMapping::iterator fitr = _objectToUniqueIDMap.find(obj);
166    if (fitr != _objectToUniqueIDMap.end())
167    {
168        uniqueID = (*fitr).second;
169        return true;
170    }
171    else return false;
172}
173
174
175bool Output::createUniqueIDForObject(const osg::Object* obj,std::string& uniqueID)
176{
177    char str[256];
178    sprintf(str,"%s_%i",obj->className(),(unsigned int)_objectToUniqueIDMap.size());
179    uniqueID = str;
180    return true;
181}
182
183
184bool Output::registerUniqueIDForObject(const osg::Object* obj,std::string& uniqueID)
185{
186    _objectToUniqueIDMap[obj] = uniqueID;
187    return true;
188}
189
190std::string Output::getFileNameForOutput(const std::string& filename) const
191{
192    switch(_pathNameHint)
193    {
194    case(FULL_PATH):
195        {
196            // need to think about how best to implement this first...
197            OSG_WARN<<"Warning: Output::getFileNameForOutput() does not support FULL_PATH yet."<< std::endl;
198            return filename;
199        }
200    case(RELATIVE_PATH):
201        {
202            // need to think about how best to implement this as well...
203            OSG_WARN<<"Warning: Output::getFileNameForOutput() does not support RELATIVE_PATH yet."<< std::endl;
204            return filename;
205        }
206    case(FILENAME_ONLY):
207        // this one is straight forward.
208        return getSimpleFileName(filename);
209    case(AS_IS):
210    default:
211        // and this one is even more trivial.
212        return filename;
213    }
214}
215
216std::string Output::getTextureFileNameForOutput()
217{
218    std::string fileName = osgDB::getNameLessExtension(_filename);
219    if (_textureFileNameNumber>0)
220    {
221        std::ostringstream o;
222        o << '_' << _textureFileNameNumber;
223        fileName += o.str();
224    }
225
226    fileName += ".dds";
227    ++_textureFileNameNumber;
228
229    return fileName;
230}
231
232std::string Output::getShaderFileNameForOutput()
233{
234    std::string fileName = osgDB::getNameLessExtension(_filename);
235    if (_shaderFileNameNumber>0)
236    {
237        std::ostringstream o;
238        o << '_' << _shaderFileNameNumber;
239        fileName += o.str();
240    }
241
242    fileName += ".glsl";
243    ++_shaderFileNameNumber;
244
245    return fileName;
246}
247
248void Output::setExternalFileWritten(const std::string& filename, bool hasBeenWritten)
249{
250    _externalFileWritten[filename] = hasBeenWritten;
251}
252
253bool Output::getExternalFileWritten(const std::string& filename) const
254{
255    ExternalFileWrittenMap::const_iterator itr = _externalFileWritten.find(filename);
256    if (itr != _externalFileWritten.end()) return itr->second;
257    return false;
258}
Note: See TracBrowser for help on using the browser.