-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookMark.c
177 lines (167 loc) · 4.7 KB
/
bookMark.c
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright (C) 2002-2010 XimpleWare, [email protected]
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "bookMark.h"
BookMark *createBookMark(){
BookMark *bm = (BookMark*) malloc(sizeof(BookMark));
if (bm==NULL) {
throwException2(out_of_mem,
"BookMark allocation failed ");
}
bm->ba = NULL;
bm->ba_len = -1;
bm->vn1 = NULL;
return bm;
}
BookMark *createBookMark2(VTDNav *vn){
BookMark *bm = (BookMark*) malloc(sizeof(BookMark));
if (bm==NULL) {
throwException2(out_of_mem,
"BookMark allocation failed ");
}
bm->ba = NULL;
bm->ba_len = -1;
bm->vn1 = NULL;
bind4BookMark(bm,vn);
recordCursorPosition2(bm);
return bm;
}
void freeBookMark(BookMark *bm){
free(bm->ba);
free(bm);
}
void unbind4BookMark(BookMark *bm){
bm->vn1 = NULL;
}
void bind4BookMark(BookMark *bm, VTDNav *vn){
if (vn==NULL)
throwException2(invalid_argument,"vn can't be null");
bm->vn1 = vn;
if (bm->ba == NULL || vn->nestingLevel+8 != bm->ba_len){
if (vn->nestingLevel+8 != bm->ba_len){
free(bm->ba);
}
bm->ba = malloc(sizeof(int)*(vn->nestingLevel + 8));
if (bm->ba == NULL){
throwException2(out_of_mem,
"BookMark.ba allocation failed ");
}
}
bm->ba[0]= -2 ; // this would never happen in a VTDNav obj's context
}
VTDNav* getNav4BookMark(BookMark *bm){
return bm->vn1;
}
Boolean setCursorPosition(BookMark *bm, VTDNav *vn){
int i=0;
if (bm->vn1 != vn || bm->ba == NULL || bm->ba[0] == -2)
return FALSE;
for (i = 0; i < vn->nestingLevel; i++) {
vn->context[i] = bm->ba[i];
}
vn->l1index = bm->ba[vn->nestingLevel];
vn->l2index = bm->ba[vn->nestingLevel + 1];
vn->l3index = bm->ba[vn->nestingLevel + 2];
vn->l2lower = bm->ba[vn->nestingLevel + 3];
vn->l2upper = bm->ba[vn->nestingLevel + 4];
vn->l3lower = bm->ba[vn->nestingLevel + 5];
vn->l3upper = bm->ba[vn->nestingLevel + 6];
if (bm->ba[vn->nestingLevel+7] < 0){
vn->atTerminal = TRUE;
} else
vn->atTerminal = FALSE;
vn->LN = bm->ba[vn->nestingLevel+7] & 0x7fffffff;
return TRUE;
}
Boolean setCursorPosition2(BookMark *bm){
return setCursorPosition(bm,bm->vn1);
}
/**
* Record the cursor position
* This method is implemented to be lenient on loading in
* that it can load nodes from any VTDNav object
* if vn is null, return false
*/
Boolean recordCursorPosition(BookMark *bm, VTDNav *vn){
int i;
if (vn == NULL)
return FALSE;
if (vn== bm->vn1){
}else {
bind4BookMark(bm,vn);
}
for (i = 0; i < vn->nestingLevel; i++) {
bm->ba[i] = bm->vn1->context[i];
}
bm->ba[vn->nestingLevel]= vn->l1index ;
bm->ba[vn->nestingLevel + 1]= vn->l2index ;
bm->ba[vn->nestingLevel + 2]= vn->l3index ;
bm->ba[vn->nestingLevel + 3]= vn->l2lower ;
bm->ba[vn->nestingLevel + 4]= vn->l2upper ;
bm->ba[vn->nestingLevel + 5]= vn->l3lower ;
bm->ba[vn->nestingLevel + 6]= vn->l3upper ;
//ba[vn.nestingLevel + 7]=(vn.atTerminal == true)?1:0;
bm->ba[vn->nestingLevel + 7]=
(vn->atTerminal == TRUE)?
(vn->LN | 0x80000000) : vn->LN ;
return TRUE;
}
/**
* Record cursor position of the VTDNav object as embedded in the
* bookmark
*/
Boolean recordCursorPosition2(BookMark *bm){
return recordCursorPosition(bm,bm->vn1);
}
/**
* Compare the bookmarks to ensure they represent the same
* node in the same VTDnav instance
*/
Boolean equal4BookMark(BookMark *bm1, BookMark *bm2){
if (bm1 == bm2)
return TRUE;
return deepEqual4BookMark(bm1,bm2);
}
/**
* Returns the hash code which is a unique integer for every node
*/
int hashCode4BookMark(BookMark *bm){
if (bm->ba == NULL || bm->vn1==NULL || bm->ba[0]==-2)
return -2;
if (bm->vn1->atTerminal)
return bm->vn1->LN;
if (bm->ba[0]==1)
return bm->vn1->rootIndex;
return bm->ba[bm->ba[0]];
}
/**
* Compare the bookmarks to ensure they represent the same
* node in the same VTDnav instance
*/
Boolean deepEqual4BookMark(BookMark *bm1, BookMark *bm2){
if (bm2->vn1 == bm1->vn1){
if (bm2->ba[bm2->ba[0]]==bm1->ba[bm1->ba[0]]){
if (bm1->ba[bm1->vn1->nestingLevel+7] < 0){
if (bm1->ba[bm1->vn1->nestingLevel+7]
!= bm2->ba[bm1->vn1->nestingLevel+7])
return FALSE;
}
return TRUE;
}
}
return FALSE;
}