| 1 | /* |
| 2 | Copyright (C) 2001-2006, William Joseph. |
| 3 | All Rights Reserved. |
| 4 | |
| 5 | This file is part of GtkRadiant. |
| 6 | |
| 7 | GtkRadiant is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | GtkRadiant is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with GtkRadiant; if not, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include "filetypes.h" |
| 23 | |
| 24 | #include "debugging/debugging.h" |
| 25 | |
| 26 | #include "ifiletypes.h" |
| 27 | |
| 28 | #include "string/string.h" |
| 29 | #include "os/path.h" |
| 30 | #include <vector> |
| 31 | #include <map> |
| 32 | |
| 33 | class RadiantFileTypeRegistry : public IFileTypeRegistry |
| 34 | { |
| 35 | struct filetype_copy_t |
| 36 | { |
| 37 | filetype_copy_t( const char* moduleName, const filetype_t other ) |
| 38 | : m_can_load( other.can_load ), m_can_import( other.can_import ), m_can_save( other.can_save ), m_moduleName( moduleName ), m_name( other.name ), m_pattern( other.pattern ) { |
| 39 | } |
| 40 | const char* getModuleName() const { |
| 41 | return m_moduleName.c_str(); |
| 42 | } |
| 43 | filetype_t getType() const { |
| 44 | return filetype_t( m_name.c_str(), m_pattern.c_str(), m_can_load, m_can_save, m_can_import ); |
| 45 | } |
| 46 | bool m_can_load; |
| 47 | bool m_can_import; |
| 48 | bool m_can_save; |
| 49 | private: |
| 50 | CopiedString m_moduleName; |
| 51 | CopiedString m_name; |
| 52 | CopiedString m_pattern; |
| 53 | }; |
| 54 | typedef std::vector<filetype_copy_t> filetype_list_t; |
| 55 | std::map<CopiedString, filetype_list_t> m_typelists; |
| 56 | public: |
| 57 | RadiantFileTypeRegistry(){ |
| 58 | addType( "*", "*", filetype_t( "All Files", "*.*" ) ); |
| 59 | } |
| 60 | void addType( const char* moduleType, const char* moduleName, filetype_t type ){ |
| 61 | m_typelists[moduleType].push_back( filetype_copy_t( moduleName, type ) ); |
| 62 | } |
| 63 | void getTypeList( const char* moduleType, IFileTypeList* typelist, bool want_load, bool want_import, bool want_save ){ |
| 64 | filetype_list_t& list_ref = m_typelists[moduleType]; |
| 65 | for ( filetype_list_t::iterator i = list_ref.begin(); i != list_ref.end(); ++i ) |
| 66 | { |
| 67 | if ( want_load && !( *i ).m_can_load ) { |
| 68 | return; |
| 69 | } |
| 70 | if ( want_import && !( *i ).m_can_import ) { |
| 71 | return; |
| 72 | } |
| 73 | if ( want_save && !( *i ).m_can_save ) { |
| 74 | return; |
| 75 | } |
| 76 | typelist->addType( ( *i ).getModuleName(), ( *i ).getType() ); |
| 77 | } |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | static RadiantFileTypeRegistry g_patterns; |
| 82 | |
| 83 | IFileTypeRegistry* GetFileTypeRegistry(){ |
| 84 | return &g_patterns; |
| 85 | } |
| 86 | |
| 87 | const char* findModuleName( IFileTypeRegistry* registry, const char* moduleType, const char* extension ){ |
| 88 | class SearchFileTypeList : public IFileTypeList |
| 89 | { |
| 90 | char m_pattern[128]; |
| 91 | const char* m_moduleName; |
| 92 | public: |
| 93 | SearchFileTypeList( const char* ext ) |
| 94 | : m_moduleName( "" ){ |
| 95 | m_pattern[0] = '*'; |
| 96 | m_pattern[1] = '.'; |
| 97 | strncpy( m_pattern + 2, ext, 125 ); |
| 98 | m_pattern[127] = '\0'; |
| 99 | } |
| 100 | void addType( const char* moduleName, filetype_t type ){ |
| 101 | if ( extension_equal( m_pattern, type.pattern ) ) { |
| 102 | m_moduleName = moduleName; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | const char* getModuleName(){ |
| 107 | return m_moduleName; |
| 108 | } |
| 109 | } search( extension ); |
| 110 | registry->getTypeList( moduleType, &search ); |
| 111 | return search.getModuleName(); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | #include "modulesystem/singletonmodule.h" |
| 116 | #include "modulesystem/moduleregistry.h" |
| 117 | |
| 118 | class FiletypesAPI |
| 119 | { |
| 120 | IFileTypeRegistry* m_filetypes; |
| 121 | public: |
| 122 | typedef IFileTypeRegistry Type; |
| 123 | STRING_CONSTANT( Name, "*" ); |
| 124 | |
| 125 | FiletypesAPI(){ |
| 126 | m_filetypes = GetFileTypeRegistry(); |
| 127 | } |
| 128 | IFileTypeRegistry* getTable(){ |
| 129 | return m_filetypes; |
| 130 | } |
| 131 | }; |
| 132 | |
| 133 | typedef SingletonModule<FiletypesAPI> FiletypesModule; |
| 134 | typedef Static<FiletypesModule> StaticFiletypesModule; |
| 135 | StaticRegisterModule staticRegisterFiletypes( StaticFiletypesModule::instance() ); |