]> git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/filetypes.cpp
115b1e2601808cd23c561a1330f40c9d2c76df45
[xonotic/netradiant.git] / radiant / filetypes.cpp
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 #include <util/buffer.h>
33
34 class RadiantFileTypeRegistry : public IFileTypeRegistry
35 {
36 struct filetype_copy_t
37 {
38         filetype_copy_t( const char* moduleName, const filetype_t other )
39                 : 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 ) {
40         }
41         const char* getModuleName() const {
42                 return m_moduleName.c_str();
43         }
44         filetype_t getType() const {
45                 return filetype_t( m_name.c_str(), m_pattern.c_str(), m_can_load, m_can_save, m_can_import );
46         }
47         bool m_can_load;
48         bool m_can_import;
49         bool m_can_save;
50 private:
51         CopiedString m_moduleName;
52         CopiedString m_name;
53         CopiedString m_pattern;
54 };
55 typedef std::vector<filetype_copy_t> filetype_list_t;
56 std::map<CopiedString, filetype_list_t> m_typelists;
57 public:
58 RadiantFileTypeRegistry(){
59         addType( "*", "*", filetype_t( "All Files", "*.*" ) );
60 }
61 void addType( const char* moduleType, const char* moduleName, filetype_t type ){
62         m_typelists[moduleType].push_back( filetype_copy_t( moduleName, type ) );
63 }
64 void getTypeList( const char* moduleType, IFileTypeList* typelist, bool want_load, bool want_import, bool want_save ){
65         filetype_list_t& list_ref = m_typelists[moduleType];
66         for ( filetype_list_t::iterator i = list_ref.begin(); i != list_ref.end(); ++i )
67         {
68                 if ( want_load && !( *i ).m_can_load ) {
69                         return;
70                 }
71                 if ( want_import && !( *i ).m_can_import ) {
72                         return;
73                 }
74                 if ( want_save && !( *i ).m_can_save ) {
75                         return;
76                 }
77                 typelist->addType( ( *i ).getModuleName(), ( *i ).getType() );
78         }
79 }
80 };
81
82 static RadiantFileTypeRegistry g_patterns;
83
84 IFileTypeRegistry* GetFileTypeRegistry(){
85         return &g_patterns;
86 }
87
88 const char* findModuleName( IFileTypeRegistry* registry, const char* moduleType, const char* extension ){
89         class SearchFileTypeList : public IFileTypeList
90         {
91         u::BufferVal<128> m_pattern;
92         const char* m_moduleName;
93 public:
94         SearchFileTypeList( const char* ext )
95                 : m_moduleName( "" ){
96                 m_pattern[0] = '*';
97                 m_pattern[1] = '.';
98                 strncpy( m_pattern + 2, ext, 125 );
99                 m_pattern[127] = '\0';
100         }
101         void addType( const char* moduleName, filetype_t type ){
102                 if ( extension_equal( m_pattern, type.pattern ) ) {
103                         m_moduleName = moduleName;
104                 }
105         }
106
107         const char* getModuleName(){
108                 return m_moduleName;
109         }
110         } search( extension );
111         registry->getTypeList( moduleType, &search );
112         return search.getModuleName();
113 }
114
115
116 #include "modulesystem/singletonmodule.h"
117 #include "modulesystem/moduleregistry.h"
118
119 class FiletypesAPI
120 {
121 IFileTypeRegistry* m_filetypes;
122 public:
123 typedef IFileTypeRegistry Type;
124 STRING_CONSTANT( Name, "*" );
125
126 FiletypesAPI(){
127         m_filetypes = GetFileTypeRegistry();
128 }
129 IFileTypeRegistry* getTable(){
130         return m_filetypes;
131 }
132 };
133
134 typedef SingletonModule<FiletypesAPI> FiletypesModule;
135 typedef Static<FiletypesModule> StaticFiletypesModule;
136 StaticRegisterModule staticRegisterFiletypes( StaticFiletypesModule::instance() );