/
FileTypeRegistry.cpp
75 lines (59 loc) · 2 KB
/
FileTypeRegistry.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "FileTypeRegistry.h"
#include "i18n.h"
#include "itextstream.h"
#include "os/path.h"
#include "debugging/debugging.h"
#include "string/case_conv.h"
#include "module/StaticModule.h"
void FileTypeRegistry::registerPattern(const std::string& fileType, const FileTypePattern& pattern)
{
// Convert the file extension to lowercase
std::string fileTypeLower = string::to_lower_copy(fileType);
// Find or insert the fileType into the map
auto i = _fileTypes.find(fileTypeLower);
if (i == _fileTypes.end())
{
// Not found yet, insert an empty pattern list
i = _fileTypes.emplace(fileTypeLower, FileTypePatterns()).first;
}
// At this point we have a valid iterator
FileTypePatterns& patternList = i->second;
// Ensure the pattern contains a lowercase extension
FileTypePattern patternLocal = pattern;
string::to_lower(patternLocal.extension);
string::to_lower(patternLocal.pattern);
// Check if the pattern is already associated
for (const FileTypePattern& existing : patternList)
{
if (existing.extension == patternLocal.extension)
{
// Ignore this pattern
return;
}
}
// Insert the pattern at the end of the list
patternList.push_back(patternLocal);
}
FileTypePatterns FileTypeRegistry::getPatternsForType(const std::string& fileType)
{
// Convert the file extension to lowercase and try to find the matching list
auto i = _fileTypes.find(string::to_lower_copy(fileType));
return i != _fileTypes.end() ? i->second : FileTypePatterns();
}
const std::string& FileTypeRegistry::getName() const
{
static std::string _name(MODULE_FILETYPES);
return _name;
}
const StringSet& FileTypeRegistry::getDependencies() const
{
static StringSet _dependencies; // no dependencies
return _dependencies;
}
void FileTypeRegistry::initialiseModule(const IApplicationContext& ctx)
{
rMessage() << getName() << "::initialiseModule called." << std::endl;
registerPattern("*", FileTypePattern(_("All Files"), "*", "*.*"));
}
// Static module instance
module::StaticModule<FileTypeRegistry> fileTypesModule;