15 #include "pnmFileTypeRegistry.h"
16 #include "pnmFileType.h"
17 #include "config_pnmimage.h"
19 #include "string_utils.h"
33 PNMFileTypeRegistry() {
34 _requires_sort =
false;
43 ~PNMFileTypeRegistry() {
53 if (pnmimage_cat->is_debug()) {
55 <<
"Registering image type " << type->get_name() <<
"\n";
59 Handles::iterator hi = _handles.find(type->get_type());
60 if (hi != _handles.end()) {
61 pnmimage_cat->warning()
62 <<
"Attempt to register PNMFileType " << type->get_name()
63 <<
" (" << type->get_type() <<
") more than once.\n";
67 _types.push_back(type);
68 _handles.insert(Handles::value_type(type->get_type(), type));
73 for (
int i = 0; i < num_extensions; i++) {
76 if (!unique_extensions.insert(extension).second) {
77 pnmimage_cat->warning()
78 <<
"PNMFileType " << type->get_name()
79 <<
" (" << type->get_type() <<
") defined extension "
80 << extension <<
" more than once.\n";
85 for (ui = unique_extensions.begin(); ui != unique_extensions.end(); ++ui) {
86 _extensions[*ui].push_back(type);
89 _requires_sort =
true;
102 return _types.size();
112 nassertr(n >= 0 && n < (
int)_types.size(), NULL);
126 if (_requires_sort) {
135 size_t dot = filename.rfind(
'.');
137 if (dot == string::npos) {
138 extension = filename;
140 extension = filename.substr(dot + 1);
144 if (extension ==
"pz") {
148 size_t prev_dot = filename.rfind(
'.', dot - 1);
149 if (prev_dot == string::npos) {
150 extension = filename.substr(0, dot);
152 extension = filename.substr(prev_dot + 1, dot - prev_dot - 1);
157 if (extension.find(
'/') != string::npos) {
164 Extensions::const_iterator ei;
165 ei = _extensions.find(extension);
166 if (ei == _extensions.end() || (*ei).second.empty()) {
170 ei = _extensions.find(downcase(extension));
172 if (ei == _extensions.end() || (*ei).second.empty()) {
179 return (*ei).second.front();
193 if (_requires_sort) {
197 Types::const_iterator ti;
198 for (ti = _types.begin(); ti != _types.end(); ++ti) {
219 Handles::const_iterator hi;
220 hi = _handles.find(handle);
221 if (hi != _handles.end()) {
235 write(ostream &out,
int indent_level)
const {
236 if (_types.empty()) {
237 indent(out, indent_level) <<
"(No image types are known).\n";
239 Types::const_iterator ti;
240 for (ti = _types.begin(); ti != _types.end(); ++ti) {
242 string name = type->get_name();
243 indent(out, indent_level) << name;
244 indent(out, max(30 - (
int)name.length(), 0)) <<
" ";
247 if (num_extensions == 1) {
249 }
else if (num_extensions > 1) {
251 for (
int i = 1; i < num_extensions; i++) {
284 void PNMFileTypeRegistry::
288 _requires_sort =
false;
PNMFileType * get_type_by_handle(TypeHandle handle) const
Returns the PNMFileType instance stored in the registry for the given TypeHandle, e...
PNMFileType * get_type_from_extension(const string &filename) const
Tries to determine what the PNMFileType is likely to be for a particular image file based on its exte...
virtual string get_extension(int n) const
Returns the nth possible filename extension associated with this particular file type, without a leading dot.
This is the base class of a family of classes that represent particular image file types that PNMImag...
virtual int get_num_extensions() const
Returns the number of different possible filename extensions associated with this particular file typ...
static PNMFileTypeRegistry * get_global_ptr()
Returns a pointer to the global PNMFileTypeRegistry object.
PNMFileType * get_type_from_magic_number(const string &magic_number) const
Tries to determine what the PNMFileType is likely to be for a particular image file based on its magi...
virtual bool has_magic_number() const
Returns true if this particular file type uses a magic number to identify it, false otherwise...
int get_num_types() const
Returns the total number of types registered.
This class maintains the set of all known PNMFileTypes in the universe.
TypeHandle is the identifier used to differentiate C++ class types.
PNMFileType * get_type(int n) const
Returns the nth type registered.
virtual bool matches_magic_number(const string &magic_number) const
Returns true if the indicated "magic number" byte stream (the initial few bytes read from the file) m...
void register_type(PNMFileType *type)
Defines a new PNMFileType in the universe.
void write(ostream &out, int indent_level=0) const
Writes a list of supported image file types to the indicated output stream, one per line...