forked from SimonKagstrom/kcov
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser-manager.cc
65 lines (47 loc) · 1.06 KB
/
parser-manager.cc
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
#include <file-parser.hh>
#include <utils.hh>
#include <vector>
using namespace kcov;
class ParserManager : public IParserManager
{
public:
ParserManager()
{
}
void registerParser(IFileParser &parser)
{
m_parsers.push_back(&parser);
}
IFileParser *matchParser(const std::string &fileName)
{
IFileParser *best = NULL;
size_t sz;
uint8_t *data = (uint8_t *)peek_file(&sz, "%s", fileName.c_str());
if (!data)
return NULL;
for (ParserList_t::const_iterator it = m_parsers.begin();
it != m_parsers.end();
++it) {
unsigned int myVal = (*it)->matchParser(fileName, data, sz);
if (myVal == match_none)
continue;
if (!best)
best = *it;
unsigned int bestVal = best->matchParser(fileName, data, sz);
if (myVal > bestVal)
best = *it;
}
free((void *)data);
return best;
}
private:
typedef std::vector<IFileParser *> ParserList_t;
ParserList_t m_parsers;
};
static ParserManager *g_instance;
IParserManager &IParserManager::getInstance()
{
if (!g_instance)
g_instance = new ParserManager();
return *g_instance;
}