summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-reader/Navigation.h
blob: d88443bdc5beb737a7a1707eb806555493625517 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef __NAVIGATION_H
#define __NAVIGATION_H

#include <string.h>
#include <stdlib.h>

const size_t NAVIGATION_HISTORY_SIZE = 32;

template<class T>
class CNavigation_base
{
protected:
    T history[NAVIGATION_HISTORY_SIZE];
    size_t historystart, historyend, historycurrent;
 public:
    CNavigation_base() : historystart(0),historyend(0),historycurrent(0) {}
    void saveposn(T posn);
    void writeposn(T posn);
    bool forward(T& loc);
    bool back(T& loc);
};

class CNavigation : public CNavigation_base<size_t>
{
 public:
    void setSaveData(unsigned char*& data, unsigned short& len, unsigned char* src, unsigned short srclen);
    void putSaveData(unsigned char*& src, unsigned short& srclen);
};

template<class T>
inline void CNavigation_base<T>::saveposn(T posn)
{
    history[historycurrent] = posn;
    historycurrent=(historycurrent+1)%NAVIGATION_HISTORY_SIZE;
    if (historycurrent==historystart)
      {
        historystart=(historystart+1)%NAVIGATION_HISTORY_SIZE;
      }
    historyend = historycurrent;
}

template<class T>
inline void CNavigation_base<T>::writeposn(T posn)
{
    history[historycurrent] = posn;
}

template<class T>
inline bool CNavigation_base<T>::back(T& posn)
{
  if (historycurrent!=historystart)
    {
      // buffer is not empty
      if (historycurrent==0)
	{
	  historycurrent=NAVIGATION_HISTORY_SIZE-1;
	}
      else
	{
	  historycurrent--;
	}
      posn=history[historycurrent];
      return true;   
    }
  else
    {
      // circular buffer empty
      return false;
    }
}

template<class T>
inline bool CNavigation_base<T>::forward(T& posn)
{
  if (historycurrent!=historyend)
    {
      historycurrent=(historycurrent+1)%NAVIGATION_HISTORY_SIZE;
      posn = history[historycurrent];
      return true;
    }
  else
    {
      return false;
    }
}

#endif