Diffstat (limited to 'noncore/apps/opie-reader/Navigation.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/apps/opie-reader/Navigation.cpp | 112 |
1 files changed, 67 insertions, 45 deletions
diff --git a/noncore/apps/opie-reader/Navigation.cpp b/noncore/apps/opie-reader/Navigation.cpp index 4f11887..36e33b4 100644 --- a/noncore/apps/opie-reader/Navigation.cpp +++ b/noncore/apps/opie-reader/Navigation.cpp | |||
@@ -1,100 +1,122 @@ | |||
1 | #include "Navigation.h" | 1 | #ifdef _WINDOWS |
2 | |||
3 | #include <string.h> | 2 | #include <string.h> |
3 | #endif | ||
4 | #include "Navigation.h" | ||
5 | //#include <stdio.h> | ||
4 | 6 | ||
7 | /* | ||
8 | void saveposn(size_t posn) | ||
9 | save/push position to history buffer for future use of back() function | ||
10 | */ | ||
5 | void CNavigation::saveposn(size_t posn) | 11 | void CNavigation::saveposn(size_t posn) |
6 | { | 12 | { |
7 | // qDebug("Saved:%u [%u,%u,%u]", posn, historystart, historycurrent, historyend); | 13 | //printf("saving position %u, depth %u\n",posn,historycurrent); |
8 | historycurrent = historyend = (historycurrent+1)%NAVIGATION_HISTORY_SIZE; | ||
9 | history[historycurrent] = posn; | 14 | history[historycurrent] = posn; |
10 | if (historystart == historyend) historystart = (historystart+1)%NAVIGATION_HISTORY_SIZE; | 15 | historycurrent=(historycurrent+1)%NAVIGATION_HISTORY_SIZE; |
11 | // qDebug("Saved:%u [%u,%u,%u]", posn, historystart, historycurrent, historyend); | 16 | if (historycurrent==historystart) |
17 | // circular buffer full, forget oldest record | ||
18 | historystart=(historystart+1)%NAVIGATION_HISTORY_SIZE; | ||
19 | // no forward possible after saveposn | ||
20 | historyend = historycurrent; | ||
12 | } | 21 | } |
13 | 22 | ||
14 | bool CNavigation::forward(size_t& loc) | 23 | /* |
24 | void writeposn(size_t posn) | ||
25 | overwrite current (unused) position | ||
26 | useful for saving current position before using back button | ||
27 | */ | ||
28 | void CNavigation::writeposn(size_t posn) | ||
15 | { | 29 | { |
16 | if (historycurrent != historyend) | 30 | //printf("witing position %u, depth %u\n",posn,historycurrent); |
17 | { | 31 | history[historycurrent] = posn; |
18 | historycurrent = (historycurrent + 1)%NAVIGATION_HISTORY_SIZE; | ||
19 | loc = history[historycurrent]; | ||
20 | //qDebug("Forward:%u [%u,%u,%u]", loc, historystart, historycurrent, historyend); | ||
21 | return true; | ||
22 | } | ||
23 | else | ||
24 | { | ||
25 | return false; | ||
26 | } | ||
27 | } | 32 | } |
28 | 33 | ||
29 | bool CNavigation::back(size_t& loc) | 34 | /* |
35 | bool back(size_t& posn) | ||
36 | go back in history | ||
37 | restore last position saved with saveposn() and return true | ||
38 | return false if there is nothing saved in history | ||
39 | */ | ||
40 | bool CNavigation::back(size_t& posn) | ||
30 | { | 41 | { |
31 | if (historyend != historystart) | 42 | if (historycurrent!=historystart) { |
32 | { | 43 | // buffer is not empty |
33 | //qDebug("Back:%u [%u,%u,%u]", loc, historystart, historycurrent, historyend); | 44 | if (historycurrent==0) |
34 | if (historycurrent == historyend && history[historycurrent] != loc) | 45 | historycurrent=NAVIGATION_HISTORY_SIZE-1; |
35 | { | 46 | else |
36 | historyend = (historyend+1) % NAVIGATION_HISTORY_SIZE; | 47 | historycurrent--; |
37 | history[historyend] = loc; | 48 | posn=history[historycurrent]; |
38 | } | 49 | //printf("back(): going back to %u depth %u\n",posn,historycurrent); |
39 | else | 50 | return true; |
40 | { | 51 | |
41 | size_t sv = historycurrent; | 52 | } else { |
42 | historycurrent = (historycurrent + NAVIGATION_HISTORY_SIZE - 1) % NAVIGATION_HISTORY_SIZE; | 53 | // circular buffer empty |
43 | if (historycurrent == historystart) | 54 | //printf("back(): empty history\n"); |
44 | { | 55 | return false; |
45 | historycurrent = sv; | ||
46 | return false; | ||
47 | } | ||
48 | } | ||
49 | loc = history[historycurrent]; | ||
50 | //qDebug("Back:%u [%u,%u,%u]", loc, historystart, historycurrent, historyend); | ||
51 | return true; | ||
52 | } | ||
53 | else | ||
54 | { | ||
55 | return false; | ||
56 | } | 56 | } |
57 | } | 57 | } |
58 | 58 | ||
59 | #include <stdio.h> | 59 | /* |
60 | bool forward(size_t& posn) | ||
61 | go forward in history, if possible | ||
62 | undo calling of back() | ||
63 | */ | ||
64 | bool CNavigation::forward(size_t& posn) | ||
65 | { | ||
66 | if (historycurrent!=historyend) { | ||
67 | // [historycurrent] = current position | ||
68 | // [historycurrent+1] = position we need | ||
69 | historycurrent=(historycurrent+1)%NAVIGATION_HISTORY_SIZE; | ||
70 | posn = history[historycurrent]; | ||
71 | //printf("forward(): going to position %d\n",posn); | ||
72 | return true; | ||
73 | } else { | ||
74 | //printf("forward(): there is no future :)\n"); | ||
75 | return false; | ||
76 | } | ||
77 | } | ||
60 | 78 | ||
61 | void CNavigation::setSaveData(unsigned char*& data, unsigned short& len, unsigned char* src, unsigned short srclen) | 79 | void CNavigation::setSaveData(unsigned char*& data, unsigned short& len, unsigned char* src, unsigned short srclen) |
62 | { | 80 | { |
63 | len = srclen+sizeof(size_t)*(3+NAVIGATION_HISTORY_SIZE); | 81 | len = srclen+sizeof(size_t)*(3+NAVIGATION_HISTORY_SIZE); |
64 | data = new unsigned char[len]; | 82 | data = new unsigned char[len]; |
65 | unsigned char* p = data; | 83 | unsigned char* p = data; |
66 | memcpy(p, src, srclen); | 84 | memcpy(p, src, srclen); |
67 | p += srclen; | 85 | p += srclen; |
68 | memcpy(p, &historystart, sizeof(size_t)); | 86 | memcpy(p, &historystart, sizeof(size_t)); |
69 | p += sizeof(size_t); | 87 | p += sizeof(size_t); |
70 | memcpy(p, &historyend, sizeof(size_t)); | 88 | memcpy(p, &historyend, sizeof(size_t)); |
71 | p += sizeof(size_t); | 89 | p += sizeof(size_t); |
72 | memcpy(p, &historycurrent, sizeof(size_t)); | 90 | memcpy(p, &historycurrent, sizeof(size_t)); |
73 | p += sizeof(size_t); | 91 | p += sizeof(size_t); |
74 | memcpy(p, history, sizeof(size_t)*NAVIGATION_HISTORY_SIZE); | 92 | memcpy(p, history, sizeof(size_t)*NAVIGATION_HISTORY_SIZE); |
93 | /* | ||
75 | printf("<%u,%u,%u>\n", historystart, historyend, historycurrent); | 94 | printf("<%u,%u,%u>\n", historystart, historyend, historycurrent); |
76 | for (int i = historystart; i <= historyend; i++) | 95 | for (int i = historystart; i <= historyend; i++) |
77 | printf("<%u> ", history[i]); | 96 | printf("<%u> ", history[i]); |
78 | printf("\n"); | 97 | printf("\n"); |
98 | */ | ||
79 | } | 99 | } |
80 | 100 | ||
81 | void CNavigation::putSaveData(unsigned char*& src, unsigned short& srclen) | 101 | void CNavigation::putSaveData(unsigned char*& src, unsigned short& srclen) |
82 | { | 102 | { |
83 | if (srclen >= sizeof(size_t)*(3+NAVIGATION_HISTORY_SIZE)) | 103 | if (srclen >= sizeof(size_t)*(3+NAVIGATION_HISTORY_SIZE)) |
84 | { | 104 | { |
85 | unsigned char* p = src; | 105 | unsigned char* p = src; |
86 | memcpy(&historystart, p, sizeof(size_t)); | 106 | memcpy(&historystart, p, sizeof(size_t)); |
87 | p += sizeof(size_t); | 107 | p += sizeof(size_t); |
88 | memcpy(&historyend, p, sizeof(size_t)); | 108 | memcpy(&historyend, p, sizeof(size_t)); |
89 | p += sizeof(size_t); | 109 | p += sizeof(size_t); |
90 | memcpy(&historycurrent, p, sizeof(size_t)); | 110 | memcpy(&historycurrent, p, sizeof(size_t)); |
91 | p += sizeof(size_t); | 111 | p += sizeof(size_t); |
92 | memcpy(history, p, sizeof(size_t)*NAVIGATION_HISTORY_SIZE); | 112 | memcpy(history, p, sizeof(size_t)*NAVIGATION_HISTORY_SIZE); |
93 | src = p + sizeof(size_t)*NAVIGATION_HISTORY_SIZE; | 113 | src = p + sizeof(size_t)*NAVIGATION_HISTORY_SIZE; |
94 | srclen -= sizeof(size_t)*(3+NAVIGATION_HISTORY_SIZE); | 114 | srclen -= sizeof(size_t)*(3+NAVIGATION_HISTORY_SIZE); |
95 | } | 115 | } |
116 | /* | ||
96 | printf("<%u,%u,%u>\n", historystart, historyend, historycurrent); | 117 | printf("<%u,%u,%u>\n", historystart, historyend, historycurrent); |
97 | for (int i = historystart; i <= historyend; i++) | 118 | for (int i = historystart; i <= historyend; i++) |
98 | printf("<%u> ", history[i]); | 119 | printf("<%u> ", history[i]); |
99 | printf("\n"); | 120 | printf("\n"); |
121 | */ | ||
100 | } | 122 | } |