32 PNMFileTypeRegistry() {
33 _requires_sort =
false;
40 ~PNMFileTypeRegistry() {
48 if (pnmimage_cat->is_debug()) {
50 <<
"Registering image type " << type->get_name() <<
"\n";
55 if (handle != PNMFileType::get_class_type()) {
56 Handles::iterator hi = _handles.find(handle);
57 if (hi != _handles.end()) {
58 pnmimage_cat->warning()
59 <<
"Attempt to register PNMFileType " << type->get_name()
60 <<
" (" << type->get_type() <<
") more than once.\n";
63 _handles.insert(Handles::value_type(handle, type));
66 _types.push_back(type);
71 for (
int i = 0; i < num_extensions; i++) {
74 if (!unique_extensions.insert(extension).second) {
75 pnmimage_cat->warning()
76 <<
"PNMFileType " << type->get_name()
77 <<
" (" << type->get_type() <<
") defined extension " 78 << extension <<
" more than once.\n";
83 for (ui = unique_extensions.begin(); ui != unique_extensions.end(); ++ui) {
84 _extensions[*ui].push_back(type);
87 _requires_sort =
true;
95 if (pnmimage_cat->is_debug()) {
97 <<
"Unregistering image type " << type->get_name() <<
"\n";
101 if (handle != PNMFileType::get_class_type()) {
102 Handles::iterator hi = _handles.find(handle);
103 if (hi != _handles.end()) {
108 _types.erase(std::remove(_types.begin(), _types.end(), type),
111 Extensions::iterator ei;
112 for (ei = _extensions.begin(); ei != _extensions.end(); ++ei) {
113 Types &types = ei->second;
114 types.erase(std::remove(types.begin(), types.end(), type),
118 _requires_sort =
true;
124 int PNMFileTypeRegistry::
125 get_num_types()
const {
126 if (_requires_sort) {
129 return _types.size();
137 nassertr(n >= 0 && n < (
int)_types.size(),
nullptr);
148 if (_requires_sort) {
157 size_t dot = filename.rfind(
'.');
159 if (dot == string::npos) {
160 extension = filename;
162 extension = filename.substr(dot + 1);
166 if (extension ==
"pz" || extension ==
"gz") {
169 size_t prev_dot = filename.rfind(
'.', dot - 1);
170 if (prev_dot == string::npos) {
171 extension = filename.substr(0, dot);
173 extension = filename.substr(prev_dot + 1, dot - prev_dot - 1);
178 if (extension.find(
'/') != string::npos) {
185 Extensions::const_iterator ei;
186 ei = _extensions.find(extension);
187 if (ei == _extensions.end() || (*ei).second.empty()) {
191 ei = _extensions.find(
downcase(extension));
193 if (ei == _extensions.end() || (*ei).second.empty()) {
200 return (*ei).second.front();
211 if (_requires_sort) {
215 Types::const_iterator ti;
216 for (ti = _types.begin(); ti != _types.end(); ++ti) {
234 Handles::const_iterator hi;
235 hi = _handles.find(handle);
236 if (hi != _handles.end()) {
248 write(std::ostream &out,
int indent_level)
const {
249 if (_types.empty()) {
250 indent(out, indent_level) <<
"(No image types are known).\n";
252 Types::const_iterator ti;
253 for (ti = _types.begin(); ti != _types.end(); ++ti) {
255 string name = type->get_name();
256 indent(out, indent_level) << name;
257 indent(out, std::max(30 - (
int)name.length(), 0)) <<
" ";
260 if (num_extensions == 1) {
262 }
else if (num_extensions > 1) {
264 for (
int i = 1; i < num_extensions; i++) {
278 if (_global_ptr ==
nullptr) {
290 void PNMFileTypeRegistry::
294 _requires_sort =
false;
PNMFileType * get_type_from_magic_number(const std::string &magic_number) const
Tries to determine what the PNMFileType is likely to be for a particular image file based on its magi...
void write(std::ostream &out, int indent_level=0) const
Writes a list of supported image file types to the indicated output stream, one per line.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the base class of a family of classes that represent particular image file types that PNMImag...
string downcase(const string &s)
Returns the input string with all uppercase letters converted to lowercase.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static PNMFileTypeRegistry * get_global_ptr()
Returns a pointer to the global PNMFileTypeRegistry object.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
PNMFileType * get_type_by_handle(TypeHandle handle) const
Returns the PNMFileType instance stored in the registry for the given TypeHandle, e....
get_extension
Returns the nth possible filename extension associated with this particular file type,...
PNMFileType * get_type_from_extension(const std::string &filename) const
Tries to determine what the PNMFileType is likely to be for a particular image file based on its exte...
virtual bool matches_magic_number(const std::string &magic_number) const
Returns true if the indicated "magic number" byte stream (the initial few bytes read from the file) m...
This class maintains the set of all known PNMFileTypes in the universe.
void unregister_type(PNMFileType *type)
Removes a PNMFileType previously passed to register_type.
virtual bool has_magic_number() const
Returns true if this particular file type uses a magic number to identify it, false otherwise.
This is our own Panda specialization on the default STL set.
TypeHandle is the identifier used to differentiate C++ class types.
get_num_extensions
Returns the number of different possible filename extensions associated with this particular file typ...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void register_type(PNMFileType *type)
Defines a new PNMFileType in the universe.
get_type
Returns the nth type registered.