|
1
2
3
4
5
6
7
8
9
10
|
/*
* File: _simple_fsa_impl.hpp
* Author: mlenart
*
* Created on October 20, 2013, 12:25 PM
*/
#ifndef _SIMPLE_FSA_IMPL_HPP
#define _SIMPLE_FSA_IMPL_HPP
|
|
11
|
#include <cstring>
|
|
12
13
14
|
#include <algorithm>
#include <utility>
#include <iostream>
|
|
15
|
#include <vector>
|
|
16
|
#include <string>
|
|
17
|
#include <sstream>
|
|
18
|
#include "const.hpp"
|
|
19
20
|
#include "../utils.hpp"
#include "../endianness.hpp"
|
|
21
|
|
|
22
|
//using namespace std;
|
|
23
|
//static const unsigned int FSA_OFFSET = 6;
|
|
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
template <class T>
bool FSA<T>::tryToRecognize(const char* input, T& value) const {
State<T> currState = this->getInitialState();
int i = 0;
while (!currState.isSink() && input[i] != '\0') {
#ifdef DEBUG_BUILD
cerr << "proceed to next " << input[i] << endl;
#endif
currState.proceedToNext(input[i]);
i++;
}
// input[i] == '\0'
// currState.proceedToNext(0);
if (currState.isAccepting()) {
value = currState.getValue();
|
|
41
|
// DEBUG(string("recognized: ")+input);
|
|
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
return true;
} else {
return false;
}
}
template <class T>
FSA<T>::FSA(const unsigned char* initialStatePtr, const Deserializer<T>& deserializer)
: initialStatePtr(initialStatePtr), deserializer(deserializer) {
}
template <class T>
State<T> FSA<T>::getInitialState() const {
return State<T>(*this);
}
template <class T>
|
|
60
|
FSA<T>* FSA<T>::getFSA(const std::string& filename, const Deserializer<T>& deserializer) {
|
|
61
|
return getFSA(readFile<unsigned char>(filename.c_str()), deserializer);
|
|
62
63
64
|
}
template <class T>
|
|
65
66
|
FSA<T>* FSA<T>::getFSA(const unsigned char* ptr, const Deserializer<T>& deserializer) {
|
|
67
|
uint32_t magicNumber = ntohl(*((const uint32_t*) ptr));
|
|
68
|
if (magicNumber != MAGIC_NUMBER) {
|
|
69
|
throw FSAException("Invalid file format");
|
|
70
71
72
73
|
}
uint8_t versionNum = *(ptr + VERSION_NUM_OFFSET);
if (versionNum != VERSION_NUM) {
|
|
74
|
std::ostringstream oss;
|
|
75
|
oss << "Invalid file format version number: " << (int) versionNum << ", should be: " << (int) VERSION_NUM;
|
|
76
|
throw FSAException(oss.str());
|
|
77
78
79
|
}
uint8_t implementationNum = *(ptr + IMPLEMENTATION_NUM_OFFSET);
|
|
80
|
|
|
81
|
const unsigned char* startPtr = ptr + FSA_DATA_OFFSET;
|
|
82
83
84
85
86
87
88
89
|
switch (implementationNum) {
case 0:
return new SimpleFSA<T>(startPtr, deserializer);
case 1:
return new CompressedFSA1<T>(startPtr, deserializer);
case 2:
return new CompressedFSA2<T>(startPtr, deserializer);
default:
|
|
90
91
92
|
std::ostringstream oss;
oss << "Invalid implementation number: " << versionNum << ", should be: " << VERSION_NUM;
throw FSAException(oss.str());
|
|
93
94
95
96
|
}
}
#endif /* _SIMPLE_FSA_IMPL_HPP */
|