00001 /* 00002 * Copyright 2006 Sony Computer Entertainment Inc. 00003 * 00004 * Licensed under the MIT Open Source License, for details please see license.txt or the website 00005 * http://www.opensource.org/licenses/mit-license.php 00006 * 00007 */ 00008 00009 #ifndef __DAE__ 00010 #define __DAE__ 00011 00012 // We use the boost filesystem library for cross-platform file system support. You'll need 00013 // to have boost on your machine for this to work. For the Windows build boost is provided 00014 // in the external-libs folder, but for Linux it's expected that you'll install a boost 00015 // obtained via your distro's package manager. For example on Debian/Ubuntu, you can run 00016 // apt-get install libboost-filesystem-dev 00017 // to install the boost filesystem library on your machine. 00018 // 00019 // Disable the warnings we get from Boost 00020 // warning C4180: qualifier applied to function type has no meaning; ignored 00021 // warning C4245: 'argument' : conversion from 'int' to 'boost::filesystem::system_error_type', 00022 // signed/unsigned mismatch 00023 #ifdef _MSC_VER 00024 #pragma warning(push) 00025 #pragma warning(disable: 4180 4245) 00026 #endif 00027 #ifndef NO_BOOST 00028 #include <boost/filesystem/convenience.hpp> 00029 #endif 00030 #ifdef _MSC_VER 00031 #pragma warning(pop) 00032 #endif 00033 00034 #include <dae/daeTypes.h> 00035 #include <dae/daeError.h> 00036 #include <dae/daeDatabase.h> 00037 #include <dae/daeIOPlugin.h> 00038 #include <dae/daeAtomicType.h> 00039 #include <dae/daeMetaElement.h> 00040 #include <dae/daeIDRef.h> 00041 #include <dae/daeURI.h> 00042 #include <dae/daeUtils.h> 00043 #include <dae/daeRawResolver.h> 00044 #include <dae/daeSIDResolver.h> 00045 00046 class domCOLLADA; 00047 typedef daeSmartRef<domCOLLADA> domCOLLADARef; 00048 class daeDatabase; 00049 00050 // The DAE class is the core interface via which you interact with the DOM. It 00051 // has methods to load/save documents, get the root element of each document, 00052 // etc. Although internally the DOM works exclusively with URIs, the methods of 00053 // the DAE class that take document paths can take URIs or OS-specific file 00054 // paths. 00055 class DLLSPEC DAE 00056 { 00057 public: 00058 // Constructor. If no database or IO plugin are provided, a default database and 00059 // IO plugin will be used. 00060 DAE(daeDatabase* database = NULL, daeIOPlugin* ioPlugin = NULL) 00061 : atomicTypes(*this), 00062 baseUri(*this, cdom::getCurrentDirAsUri().c_str()) 00063 { 00064 // See the end of the thread linked below for an explanation of why we have the DAE 00065 // constructor set up this way. Basically, I'm going to be changing the build output 00066 // location, and when this happens people sometimes continue to link against the old 00067 // libraries by accident (e.g. if they just do an svn update). By introducing a new 00068 // function that gets called from a function in a header file, I'm ensuring that someone 00069 // who tries linking against old libraries will get a link error. This may not sound 00070 // very nice, but it's certainly better than getting bizarre runtime crashes. 00071 // https://collada.org/public_forum/viewtopic.php?t=771&sid=f13c34f2d17ca720c5021bccbe5128b7 00072 init(database, ioPlugin); 00073 dummyFunction1(); 00074 } 00075 00076 virtual ~DAE(); 00077 00078 // Release all memory used by the DOM. You never need to call this explicitly. It's 00079 // called automatically when all DAE objects go out of scope. 00080 // Deletes directory returned by cdom::getSafeTmpDir(). 00081 static void cleanup(); 00082 00083 public: 00084 // Database setup 00085 virtual daeDatabase* getDatabase(); 00086 virtual daeInt setDatabase(daeDatabase* database); 00087 00088 // IO Plugin setup 00089 virtual daeIOPlugin* getIOPlugin(); 00090 virtual daeInt setIOPlugin(daeIOPlugin* plugin); 00091 00092 // Creates a new document, returning null on failure. 00093 virtual domCOLLADA* add(const std::string& path); 00094 // Opens an existing document, returning null on failure. 00095 virtual domCOLLADA* open(const std::string& path); 00096 // Opens a document from memory, returning null on failure. 00097 virtual domCOLLADA* openFromMemory(const std::string& path, daeString buffer); 00098 // Write a document to the path specified by the document's URI, returning false on failure. 00099 virtual bool write(const std::string& path); 00100 // Write a document to the path specified in the second parameter, returning false on failure. 00101 virtual bool writeTo(const std::string& docPath, const std::string& pathToWriteTo); 00102 // Writes all documents, returning false if any document failed to write. 00103 virtual bool writeAll(); 00104 // Close a specific document, unloading all memory used by the document. Returns false on failure. 00105 virtual void close(const std::string& path); 00106 // Remove all loaded documents. Always returns DAE_OK. 00107 virtual daeInt clear(); 00108 00109 // Returns the total number of documents. 00110 virtual int getDocCount(); 00111 // Returns the i'th document . 00112 virtual daeDocument* getDoc(int i); 00113 // Returns a document matching the path. 00114 virtual daeDocument* getDoc(const std::string& path); 00115 00116 // Get the root domCOLLADA object corresponding to a particular document. 00117 virtual domCOLLADA* getRoot(const std::string& path); 00118 // Set the root domCOLLADA object corresponding to a particular document, returning false on failure. 00119 virtual bool setRoot(const std::string& path, domCOLLADA* root); 00120 00121 // Returns the Collada version, i.e. 1.4, 1.5, etc. Note that this _isn't_ the 00122 // same as the DOM version (1.3, 2.0, ...). 00123 virtual daeString getDomVersion(); 00124 00125 // Returns the (modifiable) list of atomic type objects. 00126 daeAtomicTypeList& getAtomicTypes(); 00127 00128 // Get/set a daeMetaElement object given the meta object's type ID. 00129 daeMetaElement* getMeta(daeInt typeID); 00130 void setMeta(daeInt typeID, daeMetaElement& meta); 00131 00132 // Get all daeMetaElement objects. 00133 daeMetaElementRefArray& getAllMetas(); 00134 00135 // Returns the list of URI resolvers. You can modify the list to add new resolvers. 00136 daeURIResolverList& getURIResolvers(); 00137 00138 // The base URI used for resolving relative URI references. 00139 daeURI& getBaseURI(); 00140 void setBaseURI(const daeURI& uri); 00141 void setBaseURI(const std::string& uri); 00142 00143 // Returns the list of ID reference resolvers. You can modify the list to add new 00144 // resolvers. 00145 daeIDRefResolverList& getIDRefResolvers(); 00146 00147 // Meant for internal DOM use only. 00148 daeRawRefCache& getRawRefCache(); 00149 daeSidRefCache& getSidRefCache(); 00150 00151 // These functions specify the client's character encoding for the DOM. The 00152 // default is Utf8, but if you specify Latin1 then the DOM will use libxml's 00153 // character conversion functions to convert to Utf8 when writing data and 00154 // convert to Latin1 when reading data. This can help with the handling of 00155 // non-ASCII characters on Windows. Only when using libxml for xml I/O does 00156 // any character conversion occur. 00157 // 00158 // Most people can probably just ignore this completely. If you have trouble 00159 // with non-ASCII characters on Windows, try setting the char encoding to 00160 // Latin1 to see if that helps. 00161 // 00162 // Frankly this certainly isn't the best way of handling non-ASCII character 00163 // support on Windows, so this interface is a likely target for significant 00164 // changes in the future. 00165 // 00166 // See this Sourceforge thread for more info: 00167 // http://sourceforge.net/tracker/index.php?func=detail&aid=1818473&group_id=157838&atid=805426 00168 // 00169 enum charEncoding { 00170 Utf8, 00171 Latin1 00172 }; 00173 00174 // Global encoding setting. Defaults to Utf8. Set this if you want to make a 00175 // char encoding change and apply it to all DAE objects. 00176 static charEncoding getGlobalCharEncoding(); 00177 static void setGlobalCharEncoding(charEncoding encoding); 00178 00179 // Local encoding setting. If set, overrides the global setting. Useful for setting 00180 // a specific char encoding for a single DAE object but not for all DAE objects. 00181 charEncoding getCharEncoding(); 00182 void setCharEncoding(charEncoding encoding); 00183 00184 // Deprecated. Alternative methods are given. 00185 virtual daeInt load(daeString uri, daeString docBuffer = NULL); // Use open 00186 virtual daeInt save(daeString uri, daeBool replace=true); // Use write 00187 virtual daeInt save(daeUInt documentIndex, daeBool replace=true); // Use write 00188 virtual daeInt saveAs(daeString uriToSaveTo, daeString docUri, daeBool replace=true); // Use writeTo 00189 virtual daeInt saveAs(daeString uriToSaveTo, daeUInt documentIndex=0, daeBool replace=true); // Use writeTo 00190 virtual daeInt unload(daeString uri); // Use close 00191 virtual domCOLLADA* getDom(daeString uri); // use getRoot 00192 virtual daeInt setDom(daeString uri, domCOLLADA* dom); // use setRoot 00193 00194 private: 00195 void init(daeDatabase* database, daeIOPlugin* ioPlugin); 00196 void dummyFunction1(); 00197 std::string makeFullUri(const std::string& path); 00198 domCOLLADA* openCommon(const std::string& path, daeString buffer); 00199 bool writeCommon(const std::string& docPath, const std::string& pathToWriteTo, bool replace); 00200 00201 daeDatabase *database; 00202 daeIOPlugin *plugin; 00203 bool defaultDatabase; 00204 bool defaultPlugin; 00205 daeAtomicTypeList atomicTypes; 00206 daeMetaElementRefArray metas; 00207 daeURI baseUri; 00208 daeURIResolverList uriResolvers; 00209 daeIDRefResolverList idRefResolvers; 00210 daeRawRefCache rawRefCache; 00211 daeSidRefCache sidRefCache; 00212 00213 std::auto_ptr<charEncoding> localCharEncoding; 00214 static charEncoding globalCharEncoding; 00215 }; 00216 00217 00218 template <typename T> 00219 inline T *daeSafeCast(daeElement *element) 00220 { 00221 if (element && element->typeID() == T::ID()) 00222 return (T*)element; 00223 return NULL; 00224 } 00225 00226 00227 #endif // __DAE_INTERFACE__