00001 // Filename: loaderFileTypeRegistry.cxx 00002 // Created by: drose (20Jun00) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 #include "loaderFileTypeRegistry.h" 00020 #include "loaderFileType.h" 00021 #include "config_pgraph.h" 00022 00023 #include <string_utils.h> 00024 #include <indent.h> 00025 00026 #include <algorithm> 00027 00028 LoaderFileTypeRegistry *LoaderFileTypeRegistry::_global_ptr; 00029 00030 //////////////////////////////////////////////////////////////////// 00031 // Function: LoaderFileTypeRegistry::Constructor 00032 // Access: Public 00033 // Description: 00034 //////////////////////////////////////////////////////////////////// 00035 LoaderFileTypeRegistry:: 00036 LoaderFileTypeRegistry() { 00037 } 00038 00039 //////////////////////////////////////////////////////////////////// 00040 // Function: LoaderFileTypeRegistry::Destructor 00041 // Access: Public 00042 // Description: 00043 //////////////////////////////////////////////////////////////////// 00044 LoaderFileTypeRegistry:: 00045 ~LoaderFileTypeRegistry() { 00046 } 00047 00048 //////////////////////////////////////////////////////////////////// 00049 // Function: LoaderFileTypeRegistry::get_ptr 00050 // Access: Public, Static 00051 // Description: Returns a pointer to the global LoaderFileTypeRegistry 00052 // object. 00053 //////////////////////////////////////////////////////////////////// 00054 LoaderFileTypeRegistry *LoaderFileTypeRegistry:: 00055 get_ptr() { 00056 if (_global_ptr == (LoaderFileTypeRegistry *)NULL) { 00057 _global_ptr = new LoaderFileTypeRegistry; 00058 } 00059 return _global_ptr; 00060 } 00061 00062 //////////////////////////////////////////////////////////////////// 00063 // Function: LoaderFileTypeRegistry::get_num_types 00064 // Access: Public 00065 // Description: Returns the total number of types registered. 00066 //////////////////////////////////////////////////////////////////// 00067 int LoaderFileTypeRegistry:: 00068 get_num_types() const { 00069 return _types.size(); 00070 } 00071 00072 //////////////////////////////////////////////////////////////////// 00073 // Function: LoaderFileTypeRegistry::get_type 00074 // Access: Public 00075 // Description: Returns the nth type registered. 00076 //////////////////////////////////////////////////////////////////// 00077 LoaderFileType *LoaderFileTypeRegistry:: 00078 get_type(int n) const { 00079 nassertr(n >= 0 && n < (int)_types.size(), NULL); 00080 return _types[n]; 00081 } 00082 00083 //////////////////////////////////////////////////////////////////// 00084 // Function: LoaderFileTypeRegistry::get_type_from_extension 00085 // Access: Public 00086 // Description: Determines the type of the file based on the indicated 00087 // extension (without a leading dot). Returns NULL if 00088 // the extension matches no known file types. 00089 //////////////////////////////////////////////////////////////////// 00090 LoaderFileType *LoaderFileTypeRegistry:: 00091 get_type_from_extension(const string &extension) const { 00092 Extensions::const_iterator ei; 00093 ei = _extensions.find(downcase(extension)); 00094 if (ei == _extensions.end()) { 00095 // Nothing matches that extension. 00096 return NULL; 00097 } 00098 00099 return (*ei).second; 00100 } 00101 00102 //////////////////////////////////////////////////////////////////// 00103 // Function: LoaderFileTypeRegistry::write_types 00104 // Access: Public 00105 // Description: Writes a list of supported file types to the 00106 // indicated output stream, one per line. 00107 //////////////////////////////////////////////////////////////////// 00108 void LoaderFileTypeRegistry:: 00109 write_types(ostream &out, int indent_level) const { 00110 if (_types.empty()) { 00111 indent(out, indent_level) << "(No file types are known).\n"; 00112 } else { 00113 Types::const_iterator ti; 00114 for (ti = _types.begin(); ti != _types.end(); ++ti) { 00115 LoaderFileType *type = (*ti); 00116 string name = type->get_name(); 00117 indent(out, indent_level) << name; 00118 indent(out, max(30 - (int)name.length(), 0)) 00119 << " ." << type->get_extension() << "\n"; 00120 } 00121 } 00122 } 00123 00124 //////////////////////////////////////////////////////////////////// 00125 // Function: LoaderFileTypeRegistry::register_type 00126 // Access: Public 00127 // Description: Defines a new LoaderFileType in the universe. 00128 //////////////////////////////////////////////////////////////////// 00129 void LoaderFileTypeRegistry:: 00130 register_type(LoaderFileType *type) { 00131 // Make sure we haven't already registered this type. 00132 if (find(_types.begin(), _types.end(), type) != _types.end()) { 00133 loader_cat.warning() 00134 << "Attempt to register LoaderFileType " << type->get_name() 00135 << " (" << type->get_type() << ") more than once.\n"; 00136 return; 00137 } 00138 00139 _types.push_back(type); 00140 00141 string extension = downcase(type->get_extension()); 00142 Extensions::const_iterator ei; 00143 ei = _extensions.find(extension); 00144 if (ei != _extensions.end()) { 00145 loader_cat.warning() 00146 << "Multiple LoaderFileTypes registered that use the extension " 00147 << extension << "\n"; 00148 } else { 00149 _extensions.insert(Extensions::value_type(extension, type)); 00150 } 00151 }