Class to track data flow and detect a specific pattern
Diff: PatternFinder.h
- Revision:
- 0:b8cca837d413
- Child:
- 1:038d0018a542
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PatternFinder.h Mon Jan 11 04:48:08 2016 +0000 @@ -0,0 +1,49 @@ +#ifndef PATTERN_FINDER_H +#define PATTERN_FINDER_H +#include <stdint.h> +#include <string.h> + + +template <typename T> +class PatternFinder +{ + private: + T *m_PatternReference; + T *m_CurrentPattern; + uint32_t m_PatternSize; + void ShiftDataInCurrentPattern(T data); + bool CheckForPattern(void); + public: + PatternFinder(T* Pattern,uint32_t PatternSize = 1); + ~PatternFinder(); + bool CheckPattern(T data); //returns true if pattern was found in the stream +}; + +template <class T> +inline void PatternFinder<T>::ShiftDataInCurrentPattern(T data) +{ + for(int x = (m_PatternSize-1);x > 0;x--) + m_CurrentPattern[x] = m_CurrentPattern[x-1]; + m_CurrentPattern[0] = data; +} +template <class T> +inline bool PatternFinder<T>::CheckForPattern(void) +{ + uint32_t NumberOfIdenticalElementsFoundInPattern = 0; + for(int x = 0;x < m_PatternSize;x++) + { + if(m_PatternReference[x] == m_CurrentPattern[x]) + { + NumberOfIdenticalElementsFoundInPattern++; + } + } + return NumberOfIdenticalElementsFoundInPattern == m_PatternSize; +} + +template <class T> +inline bool PatternFinder<T>::CheckPattern(T data) +{ + ShiftDataInCurrentPattern(data); + return CheckForPattern(); +} +#endif \ No newline at end of file