aboutsummaryrefslogtreecommitdiffstats
path: root/doc/api-documentation/html/treekeyidx_8cpp-source.html
blob: bb88f5bd4ca6581280fddb37c31c500b01608f26 (plain) (blame)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>treekeyidx.cpp Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.2.15 -->
<center>
<a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="namespaces.html">Namespace List</a> &nbsp; <a class="qindex" href="hierarchy.html">Class Hierarchy</a> &nbsp; <a class="qindex" href="classes.html">Alphabetical List</a> &nbsp; <a class="qindex" href="annotated.html">Compound List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; </center>
<hr><h1>treekeyidx.cpp</h1><div class="fragment"><pre>00001 <font class="comment">/******************************************************************************</font>
00002 <font class="comment"> *  versekey.h - code for class 'versekey'- a standard Biblical verse key</font>
00003 <font class="comment"> *</font>
00004 <font class="comment"> * $Id: treekeyidx_8cpp-source.html,v 1.3 2002/06/20 20:23:10 mgruner Exp $</font>
00005 <font class="comment"> *</font>
00006 <font class="comment"> * Copyright 1998 CrossWire Bible Society (http://www.crosswire.org)</font>
00007 <font class="comment"> *      CrossWire Bible Society</font>
00008 <font class="comment"> *      P. O. Box 2528</font>
00009 <font class="comment"> *      Tempe, AZ  85280-2528</font>
00010 <font class="comment"> *</font>
00011 <font class="comment"> * This program is free software; you can redistribute it and/or modify it</font>
00012 <font class="comment"> * under the terms of the GNU General Public License as published by the</font>
00013 <font class="comment"> * Free Software Foundation version 2.</font>
00014 <font class="comment"> *</font>
00015 <font class="comment"> * This program is distributed in the hope that it will be useful, but</font>
00016 <font class="comment"> * WITHOUT ANY WARRANTY; without even the implied warranty of</font>
00017 <font class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU</font>
00018 <font class="comment"> * General Public License for more details.</font>
00019 <font class="comment"> *</font>
00020 <font class="comment"> */</font>
00021 
00022 
00023 <font class="preprocessor">#include &lt;treekeyidx.h&gt;</font>
00024 <font class="preprocessor">#include &lt;fcntl.h&gt;</font>
00025 <font class="preprocessor">#include &lt;stdio.h&gt;</font>
00026 <font class="preprocessor">#include &lt;errno.h&gt;</font>
00027 <font class="preprocessor">#include &lt;string&gt;</font>
00028 
00029 <font class="preprocessor">#ifndef __GNUC__</font>
00030 <font class="preprocessor"></font><font class="preprocessor">#include &lt;io.h&gt;</font>
00031 <font class="preprocessor">#else</font>
00032 <font class="preprocessor"></font><font class="preprocessor">#include &lt;unistd.h&gt;</font>
00033 <font class="preprocessor">#endif</font>
00034 <font class="preprocessor"></font>
00035 <font class="keyword">using</font> <font class="keyword">namespace </font>std;
00036 <font class="keyword">static</font> <font class="keyword">const</font> <font class="keywordtype">char</font> nl = <font class="charliteral">'\n'</font>;
00037 <font class="keyword">static</font> <font class="keyword">const</font> <font class="keywordtype">char</font> *classes[] = {<font class="stringliteral">"TreeKeyIdx"</font>, <font class="stringliteral">"TreeKey"</font>, <font class="stringliteral">"SWKey"</font>, <font class="stringliteral">"SWObject"</font>, 0};
00038 <a class="code" href="class_s_w_class.html">SWClass</a> TreeKeyIdx::classdef(classes);
00039 
00040 
00041 TreeKeyIdx::TreeKeyIdx(<font class="keyword">const</font> <a class="code" href="class_tree_key_idx.html">TreeKeyIdx</a> &amp;ikey) : currentNode() {
00042         init();
00043         path = 0;
00044         idxfd = 0;
00045         datfd = 0;
00046         <a class="code" href="class_s_w_key.html#a8">copyFrom</a>(ikey);
00047 }
00048 
00049 TreeKeyIdx::TreeKeyIdx(<font class="keyword">const</font> <font class="keywordtype">char</font> *idxPath, <font class="keywordtype">int</font> fileMode) : currentNode() {
00050         <font class="keywordtype">char</font> buf[127];
00051 
00052         init();
00053         path = 0;
00054         stdstr(&amp;path, idxPath);
00055 
00056 <font class="preprocessor">#ifndef O_BINARY                // O_BINARY is needed in Borland C++ 4.53</font>
00057 <font class="preprocessor"></font><font class="preprocessor">#define O_BINARY 0              // If it hasn't been defined than we probably</font>
00058 <font class="preprocessor"></font><font class="preprocessor">#endif                          // don't need it.</font>
00059 <font class="preprocessor"></font>
00060         <font class="keywordflow">if</font> (fileMode == -1) { <font class="comment">// try read/write if possible</font>
00061                 fileMode = O_RDWR;
00062         }
00063                 
00064         sprintf(buf, <font class="stringliteral">"%s.idx"</font>, path);
00065         idxfd = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, <font class="keyword">true</font>);
00066         sprintf(buf, <font class="stringliteral">"%s.dat"</font>, path);
00067         datfd = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, <font class="keyword">true</font>);
00068 
00069         <font class="keywordflow">if</font> (datfd &lt;= 0) {
00070                 sprintf(buf, <font class="stringliteral">"Error: %d"</font>, errno);
00071                 perror(buf);
00072                 error = errno;
00073         }
00074         <font class="keywordflow">else</font> {
00075                 root();
00076         }
00077 }
00078 
00079 
00080 <font class="keywordtype">void</font> TreeKeyIdx::init() {
00081         myclass = &amp;classdef;
00082 }
00083 
00084 
00085 TreeKeyIdx::~TreeKeyIdx () {
00086         <font class="keywordflow">if</font> (path)
00087                 <font class="keyword">delete</font> [] path;
00088 
00089         FileMgr::systemFileMgr.close(idxfd);
00090         FileMgr::systemFileMgr.close(datfd);
00091 }
00092 
00093 
00094 <font class="keyword">const</font> <font class="keywordtype">char</font> *TreeKeyIdx::getLocalName() {
00095         <font class="keywordflow">return</font> currentNode.name;
00096 }
00097 
00098 
00099 <font class="keyword">const</font> <font class="keywordtype">char</font> *TreeKeyIdx::getUserData(<font class="keywordtype">int</font> *size) {
00100         <font class="keywordflow">if</font> (size)
00101                 *size = (int)currentNode.dsize;
00102         <font class="keywordflow">return</font> currentNode.userData;
00103 }
00104 
00105 
00106 <font class="keywordtype">void</font> TreeKeyIdx::setUserData(<font class="keyword">const</font> <font class="keywordtype">char</font> *userData, <font class="keywordtype">int</font> size) {
00107         <font class="keywordflow">if</font> (currentNode.userData)
00108                 <font class="keyword">delete</font> currentNode.userData;
00109 
00110         <font class="keywordflow">if</font> (!size)
00111                 size = strlen(userData) + 1;
00112 
00113         currentNode.userData = <font class="keyword">new</font> <font class="keywordtype">char</font> [ size ];
00114         memcpy(currentNode.userData, userData, size);
00115         currentNode.dsize = size;
00116 }
00117 
00118 <font class="keyword">const</font> <font class="keywordtype">char</font> *TreeKeyIdx::setLocalName(<font class="keyword">const</font> <font class="keywordtype">char</font> *newName) {
00119         stdstr(&amp;(currentNode.name), newName);
00120         <font class="keywordflow">return</font> currentNode.name;
00121 }
00122 
00123 
00124 <font class="keywordtype">void</font> TreeKeyIdx::save() {
00125         saveTreeNode(&amp;currentNode);
00126 }
00127 
00128 
00129 <font class="keyword">const</font> <font class="keywordtype">char</font> *TreeKeyIdx::getFullName()<font class="keyword"> const </font>{
00130         TreeNode parent;
00131         <font class="keyword">static</font> string fullPath;
00132         fullPath = currentNode.name;
00133         parent.parent = currentNode.parent;
00134         <font class="keywordflow">while</font> (parent.parent &gt; -1) {
00135                 getTreeNodeFromIdxOffset(parent.parent, &amp;parent);
00136                 fullPath = ((string)parent.name) + (string) <font class="stringliteral">"/"</font> + fullPath;
00137         }
00138         <font class="keywordflow">return</font> fullPath.c_str();
00139 }
00140 
00141 
00142 <font class="keywordtype">void</font> TreeKeyIdx::root() {
00143         error = getTreeNodeFromIdxOffset(0, &amp;currentNode);
00144 }
00145 
00146 
00147 <font class="keywordtype">bool</font> TreeKeyIdx::parent() {
00148         <font class="keywordflow">if</font> (currentNode.parent &gt; -1) {
00149                 error = getTreeNodeFromIdxOffset(currentNode.parent, &amp;currentNode);
00150                 <font class="keywordflow">return</font> <font class="keyword">true</font>;
00151         }
00152         <font class="keywordflow">return</font> <font class="keyword">false</font>;
00153 }
00154 
00155 
00156 <font class="keywordtype">bool</font> TreeKeyIdx::firstChild() {
00157         <font class="keywordflow">if</font> (currentNode.firstChild &gt; -1) {
00158                 error = getTreeNodeFromIdxOffset(currentNode.firstChild, &amp;currentNode);
00159                 <font class="keywordflow">return</font> <font class="keyword">true</font>;
00160         }
00161         <font class="keywordflow">return</font> <font class="keyword">false</font>;
00162 }
00163 
00164 
00165 <font class="keywordtype">bool</font> TreeKeyIdx::nextSibling() {
00166         <font class="keywordflow">if</font> (currentNode.next &gt; -1) {
00167                 error = getTreeNodeFromIdxOffset(currentNode.next, &amp;currentNode);
00168                 <font class="keywordflow">return</font> <font class="keyword">true</font>;
00169         }
00170         <font class="keywordflow">return</font> <font class="keyword">false</font>;
00171 }
00172 
00173 
00174 <font class="keywordtype">bool</font> TreeKeyIdx::previousSibling() {
00175         TreeNode iterator;
00176         __u32 target = currentNode.offset;
00177         <font class="keywordflow">if</font> (currentNode.parent &gt; -1) {
00178                 getTreeNodeFromIdxOffset(currentNode.parent, &amp;iterator);
00179                 getTreeNodeFromIdxOffset(iterator.firstChild, &amp;iterator);
00180                 <font class="keywordflow">if</font> (iterator.offset != target) {
00181                         <font class="keywordflow">while</font> ((iterator.next != target) &amp;&amp; (iterator.next &gt; -1))
00182                                 getTreeNodeFromIdxOffset(iterator.next, &amp;iterator);
00183                         <font class="keywordflow">if</font> (iterator.next &gt; -1) {
00184                                 error = getTreeNodeFromIdxOffset(iterator.offset, &amp;currentNode);
00185                                 <font class="keywordflow">return</font> <font class="keyword">true</font>;
00186                         }
00187                 }
00188         }
00189         <font class="keywordflow">return</font> <font class="keyword">false</font>;
00190 }
00191 
00192 
00193 <font class="keywordtype">bool</font> TreeKeyIdx::hasChildren() {
00194         <font class="keywordflow">return</font> (currentNode.firstChild &gt; -1);
00195 }
00196 
00197 
00198 <font class="keywordtype">void</font> TreeKeyIdx::append() {
00199         TreeNode lastSib;
00200         <font class="keywordflow">if</font> (currentNode.offset) {
00201                 getTreeNodeFromIdxOffset(currentNode.offset, &amp;lastSib);
00202                 <font class="keywordflow">while</font> (lastSib.next &gt; -1) {
00203                         getTreeNodeFromIdxOffset(lastSib.next, &amp;lastSib);
00204                 }
00205                 __u32 idxOffset = lseek(idxfd-&gt;getFd(), 0, SEEK_END);
00206                 lastSib.next = idxOffset;
00207                 saveTreeNodeOffsets(&amp;lastSib);
00208                 __u32 parent = currentNode.parent;
00209                 currentNode.clear();
00210                 currentNode.offset = idxOffset;
00211                 currentNode.parent = parent;
00212         }
00213 }
00214 
00215 
00216 <font class="keywordtype">void</font> TreeKeyIdx::appendChild() {
00217         <font class="keywordflow">if</font> (firstChild()) {
00218                 append();
00219         }
00220         <font class="keywordflow">else</font> {
00221                 __u32 idxOffset = lseek(idxfd-&gt;getFd(), 0, SEEK_END);
00222                 currentNode.firstChild = idxOffset;
00223                 saveTreeNodeOffsets(&amp;currentNode);
00224                 __u32 parent = currentNode.offset;
00225                 currentNode.clear();
00226                 currentNode.offset = idxOffset;
00227                 currentNode.parent = parent;
00228         }
00229 }
00230 
00231 
00232 <font class="keywordtype">void</font> TreeKeyIdx::insertBefore() {
00233 }
00234 
00235 
00236 <font class="keywordtype">void</font> TreeKeyIdx::remove() {
00237 }
00238 
00239 
00240 <font class="comment">/******************************************************************************</font>
00241 <font class="comment"> * TreeKeyIdx::Create   - Creates new key idx/dat files</font>
00242 <font class="comment"> *</font>
00243 <font class="comment"> * ENT: path    - directory to store module files</font>
00244 <font class="comment"> * RET: error status</font>
00245 <font class="comment"> */</font>
00246 
00247 <font class="keywordtype">signed</font> <font class="keywordtype">char</font> TreeKeyIdx::create(<font class="keyword">const</font> <font class="keywordtype">char</font> *ipath) {
00248         <font class="keywordtype">char</font> *path = 0;
00249         <font class="keywordtype">char</font> *buf = <font class="keyword">new</font> <font class="keywordtype">char</font> [ strlen (ipath) + 20 ];
00250         FileDesc *fd, *fd2;
00251 
00252         stdstr(&amp;path, ipath);
00253 
00254         <font class="keywordflow">if</font> ((path[strlen(path)-1] == <font class="charliteral">'/'</font>) || (path[strlen(path)-1] == <font class="charliteral">'\\'</font>))
00255                 path[strlen(path)-1] = 0;
00256 
00257         sprintf(buf, <font class="stringliteral">"%s.dat"</font>, path);
00258         unlink(buf);
00259         fd = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
00260         fd-&gt;getFd();
00261         FileMgr::systemFileMgr.close(fd);
00262 
00263         sprintf(buf, <font class="stringliteral">"%s.idx"</font>, path);
00264         unlink(buf);
00265         fd2 = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
00266         fd2-&gt;getFd();
00267         FileMgr::systemFileMgr.close(fd2);
00268 
00269         <a class="code" href="class_tree_key_idx.html">TreeKeyIdx</a> newTree(path);
00270         TreeKeyIdx::TreeNode root;
00271         stdstr(&amp;(root.name), <font class="stringliteral">""</font>);
00272         newTree.<a class="code" href="class_tree_key_idx.html#c2">saveTreeNode</a>(&amp;root);
00273 
00274         <font class="keyword">delete</font> [] path;
00275         
00276         <font class="keywordflow">return</font> 0;
00277 }
00278 
00279 
00280 <font class="comment">/******************************************************************************</font>
00281 <font class="comment"> * zStr::getidxbufdat   - Gets the index string at the given dat offset</font>
00282 <font class="comment"> *                              NOTE: buf is calloc'd, or if not null, realloc'd and must</font>
00283 <font class="comment"> *                                      be free'd by calling function</font>
00284 <font class="comment"> *</font>
00285 <font class="comment"> * ENT: ioffset - offset in dat file to lookup</font>
00286 <font class="comment"> *              node            - address of pointer to allocate for storage of string</font>
00287 <font class="comment"> */</font>
00288 
00289 <font class="keywordtype">void</font> TreeKeyIdx::getTreeNodeFromDatOffset(<font class="keywordtype">long</font> ioffset, TreeNode *node)<font class="keyword"> const </font>{
00290         <font class="keywordtype">char</font> ch;
00291         __s32  tmp;
00292         __u16  tmp2;
00293 
00294         <font class="keywordflow">if</font> (datfd &gt; 0) {
00295 
00296                 lseek(datfd-&gt;getFd(), ioffset, SEEK_SET);
00297 
00298                 read(datfd-&gt;getFd(), &amp;tmp, 4);
00299                 node-&gt;parent = swordtoarch32(tmp);
00300 
00301                 read(datfd-&gt;getFd(), &amp;tmp, 4);
00302                 node-&gt;next = swordtoarch32(tmp);
00303 
00304                 read(datfd-&gt;getFd(), &amp;tmp, 4);
00305                 node-&gt;firstChild = swordtoarch32(tmp);
00306 
00307                 string name;
00308                 <font class="keywordflow">do</font> {
00309                         read(datfd-&gt;getFd(), &amp;ch, 1);
00310                         name += ch;
00311                 } <font class="keywordflow">while</font> (ch);
00312 
00313                 stdstr(&amp;(node-&gt;name), name.c_str());
00314 
00315                 read(datfd-&gt;getFd(), &amp;tmp2, 2);
00316                 node-&gt;dsize = swordtoarch16(tmp2);
00317 
00318                 <font class="keywordflow">if</font> (node-&gt;dsize) {
00319                         <font class="keywordflow">if</font> (node-&gt;userData)
00320                                 <font class="keyword">delete</font> [] node-&gt;userData;
00321                         node-&gt;userData = <font class="keyword">new</font> <font class="keywordtype">char</font> [node-&gt;dsize];
00322                         read(datfd-&gt;getFd(), node-&gt;userData, node-&gt;dsize);
00323                 }
00324         }
00325 }
00326 
00327 
00328 <font class="comment">/******************************************************************************</font>
00329 <font class="comment"> * zStr::getidxbuf      - Gets the index string at the given idx offset</font>
00330 <font class="comment"> *                                              NOTE: buf is calloc'd, or if not null, realloc'd</font>
00331 <font class="comment"> *                                                      and must be freed by calling function</font>
00332 <font class="comment"> *</font>
00333 <font class="comment"> * ENT: ioffset - offset in idx file to lookup</font>
00334 <font class="comment"> *              buf             - address of pointer to allocate for storage of string</font>
00335 <font class="comment"> */</font>
00336 
00337 <font class="keywordtype">char</font> TreeKeyIdx::getTreeNodeFromIdxOffset(<font class="keywordtype">long</font> ioffset, TreeNode *node)<font class="keyword"> const </font>{
00338         __u32 offset;
00339         <font class="keywordtype">char</font> error = 0;
00340         
00341         <font class="keywordflow">if</font> (ioffset &lt; 0) {
00342                 ioffset = 0;
00343                 error = KEYERR_OUTOFBOUNDS;
00344         }
00345 
00346         node-&gt;offset = ioffset;
00347         <font class="keywordflow">if</font> (idxfd &gt; 0) {
00348                 lseek(idxfd-&gt;getFd(), ioffset, SEEK_SET);
00349                 <font class="keywordflow">if</font> (read(idxfd-&gt;getFd(), &amp;offset, 4) == 4) {
00350                         offset = swordtoarch32(offset);
00351                         getTreeNodeFromDatOffset(offset, node);
00352                 }
00353                 <font class="keywordflow">else</font> {
00354                         lseek(idxfd-&gt;getFd(), -4, SEEK_END);
00355                         <font class="keywordflow">if</font> (read(idxfd-&gt;getFd(), &amp;offset, 4) == 4) {
00356                                 offset = swordtoarch32(offset);
00357                                 getTreeNodeFromDatOffset(offset, node);
00358                         }
00359                         error = KEYERR_OUTOFBOUNDS;
00360                 }
00361         }
00362         <font class="keywordflow">return</font> error;
00363 }
00364 
00365 
00366 <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> TreeKeyIdx::getOffset()<font class="keyword"> const </font>{
00367         <font class="keywordflow">return</font> currentNode.offset;
00368 }
00369 
00370 <font class="keywordtype">void</font> TreeKeyIdx::setOffset(<font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> offset) {
00371         error = getTreeNodeFromIdxOffset(offset, &amp;currentNode);
00372 }
00373 
00374 
00375 <font class="keywordtype">void</font> TreeKeyIdx::saveTreeNodeOffsets(TreeNode *node) {
00376         <font class="keywordtype">long</font> datOffset = 0;
00377         __s32 tmp;
00378 
00379         <font class="keywordflow">if</font> (idxfd &gt; 0) {
00380                 lseek(idxfd-&gt;getFd(), node-&gt;offset, SEEK_SET);
00381                 <font class="keywordflow">if</font> (read(idxfd-&gt;getFd(), &amp;tmp, 4) != 4) {
00382                         datOffset = lseek(datfd-&gt;getFd(), 0, SEEK_END);
00383                         tmp = archtosword32(datOffset);
00384                         write(idxfd-&gt;getFd(), &amp;tmp, 4);
00385                 }
00386                 <font class="keywordflow">else</font> {
00387                         datOffset = swordtoarch32(tmp);
00388                         lseek(datfd-&gt;getFd(), datOffset, SEEK_SET);
00389                 }
00390 
00391                 tmp = archtosword32(node-&gt;parent);
00392                 write(datfd-&gt;getFd(), &amp;tmp, 4);
00393 
00394                 tmp = archtosword32(node-&gt;next);
00395                 write(datfd-&gt;getFd(), &amp;tmp, 4);
00396 
00397                 tmp = archtosword32(node-&gt;firstChild);
00398                 write(datfd-&gt;getFd(), &amp;tmp, 4);
00399         }
00400 }
00401 
00402 
00403 <font class="keywordtype">void</font> <a class="code" href="class_tree_key_idx.html#a20">TreeKeyIdx::copyFrom</a>(<font class="keyword">const</font> <a class="code" href="class_tree_key_idx.html">TreeKeyIdx</a> &amp;ikey) {
00404 
00405         <a class="code" href="class_s_w_key.html#a8">SWKey::copyFrom</a>(ikey);
00406 
00407         currentNode.offset = ikey.<a class="code" href="class_tree_key_idx.html#o0">currentNode</a>.offset;
00408         currentNode.parent = ikey.<a class="code" href="class_tree_key_idx.html#o0">currentNode</a>.parent;
00409         currentNode.next = ikey.<a class="code" href="class_tree_key_idx.html#o0">currentNode</a>.next;
00410         currentNode.firstChild = ikey.<a class="code" href="class_tree_key_idx.html#o0">currentNode</a>.firstChild;
00411         stdstr(&amp;(currentNode.name), ikey.<a class="code" href="class_tree_key_idx.html#o0">currentNode</a>.name);
00412         currentNode.dsize = ikey.<a class="code" href="class_tree_key_idx.html#o0">currentNode</a>.dsize;
00413 
00414         <font class="keywordflow">if</font> (currentNode.userData)
00415                 <font class="keyword">delete</font> [] currentNode.userData;
00416         <font class="keywordflow">if</font> (currentNode.dsize) {
00417                 currentNode.userData = <font class="keyword">new</font> <font class="keywordtype">char</font> [ currentNode.dsize ];
00418                 memcpy(currentNode.userData, ikey.<a class="code" href="class_tree_key_idx.html#o0">currentNode</a>.userData, currentNode.dsize);
00419         }
00420         <font class="keywordflow">else</font> currentNode.userData = 0;
00421 
00422         <font class="keywordtype">bool</font> newFiles = <font class="keyword">true</font>;
00423 
00424         <font class="keywordflow">if</font> (path &amp;&amp; ikey.<a class="code" href="class_tree_key_idx.html#o1">path</a>)
00425                 newFiles = strcmp(path, ikey.<a class="code" href="class_tree_key_idx.html#o1">path</a>);
00426 
00427         <font class="keywordflow">if</font> (newFiles) {
00428                 stdstr(&amp;path, ikey.<a class="code" href="class_tree_key_idx.html#o1">path</a>);
00429 
00430                 <font class="keywordflow">if</font> (idxfd) {
00431                         FileMgr::systemFileMgr.close(idxfd);
00432                         FileMgr::systemFileMgr.close(datfd);
00433                 }
00434                 idxfd = FileMgr::systemFileMgr.open(ikey.<a class="code" href="class_tree_key_idx.html#o2">idxfd</a>-&gt;path, ikey.<a class="code" href="class_tree_key_idx.html#o2">idxfd</a>-&gt;mode, ikey.<a class="code" href="class_tree_key_idx.html#o2">idxfd</a>-&gt;perms);
00435                 datfd = FileMgr::systemFileMgr.open(ikey.<a class="code" href="class_tree_key_idx.html#o3">datfd</a>-&gt;path, ikey.<a class="code" href="class_tree_key_idx.html#o3">datfd</a>-&gt;mode, ikey.<a class="code" href="class_tree_key_idx.html#o3">datfd</a>-&gt;perms);
00436         }
00437 }
00438 
00439 
00440 <font class="keywordtype">void</font> TreeKeyIdx::saveTreeNode(TreeNode *node) {
00441         <font class="keywordtype">long</font> datOffset = 0;
00442         __s32 tmp;
00443         <font class="keywordflow">if</font> (idxfd &gt; 0) {
00444 
00445                 lseek(idxfd-&gt;getFd(), node-&gt;offset, SEEK_SET);
00446                 datOffset = lseek(datfd-&gt;getFd(), 0, SEEK_END);
00447                 tmp = archtosword32(datOffset);
00448                 write(idxfd-&gt;getFd(), &amp;tmp, 4);
00449 
00450                 saveTreeNodeOffsets(node);
00451 
00452                 write(datfd-&gt;getFd(), node-&gt;name, strlen(node-&gt;name));
00453                 <font class="keywordtype">char</font> null = 0;
00454                 write(datfd-&gt;getFd(), &amp;null, 1);
00455 
00456                 __u16 tmp2 = archtosword16(node-&gt;dsize);
00457                 write(datfd-&gt;getFd(), &amp;tmp2, 2);
00458 
00459                 <font class="keywordflow">if</font> (node-&gt;dsize) {
00460                         write(datfd-&gt;getFd(), node-&gt;userData, node-&gt;dsize);
00461                 }
00462         }
00463 }
00464 
00465 
<a name="l00466"></a><a class="code" href="class_tree_key_idx.html#a24">00466</a> <font class="keywordtype">void</font> <a class="code" href="class_tree_key_idx.html#a24">TreeKeyIdx::setText</a>(<font class="keyword">const</font> <font class="keywordtype">char</font> *ikey) {
00467         <font class="keywordtype">char</font> *buf = 0;
00468         stdstr(&amp;buf, ikey);
00469         <font class="keywordtype">char</font> *leaf = strtok(buf, <font class="stringliteral">"/"</font>);
00470         root();
00471         <font class="keywordflow">while</font> ((leaf) &amp;&amp; (!<a class="code" href="class_s_w_key.html#a6">Error</a>())) {
00472                 <font class="keywordtype">bool</font> ok, inChild = <font class="keyword">false</font>;
00473                 <font class="keywordflow">for</font> (ok = firstChild(); ok; ok = nextSibling()) {
00474                         inChild = <font class="keyword">true</font>;
00475                         <font class="keywordflow">if</font> (!stricmp(leaf, getLocalName()))
00476                                 <font class="keywordflow">break</font>;
00477                 }
00478                 leaf = strtok(0, <font class="stringliteral">"/"</font>);
00479                 <font class="keywordflow">if</font> (!ok) {
00480                         <font class="keywordflow">if</font> (inChild) {  <font class="comment">// if we didn't find a matching child node, default to first child</font>
00481                                 parent();
00482                                 firstChild();
00483                         }
00484                         <font class="keywordflow">if</font> (leaf)
00485                                 error = KEYERR_OUTOFBOUNDS;
00486                         <font class="keywordflow">break</font>;
00487                 }
00488         }
00489         <font class="keyword">delete</font> [] buf;
00490 }
00491 
00492 
00493 
<a name="l00494"></a><a class="code" href="class_tree_key_idx.html#a20">00494</a> <font class="keywordtype">void</font> <a class="code" href="class_tree_key_idx.html#a20">TreeKeyIdx::copyFrom</a>(<font class="keyword">const</font> <a class="code" href="class_s_w_key.html">SWKey</a> &amp;ikey) {
00495         <a class="code" href="class_s_w_key.html#a8">SWKey::copyFrom</a>(ikey);
00496 }
00497 
00498 <font class="keywordtype">void</font> TreeKeyIdx::setPosition(SW_POSITION p) {
00499         <font class="keywordflow">switch</font> (p) {
00500         <font class="keywordflow">case</font> POS_TOP:
00501                 root();
00502                 <font class="keywordflow">break</font>;
00503         <font class="keywordflow">case</font> POS_BOTTOM:
00504                 error = getTreeNodeFromIdxOffset(lseek(idxfd-&gt;getFd(), -4, SEEK_END), &amp;currentNode);
00505                 <font class="keywordflow">break</font>;
00506         } 
00507         <a class="code" href="class_s_w_key.html#a6">Error</a>();        <font class="comment">// clear error from normalize</font>
00508 }
00509 
<a name="l00510"></a><a class="code" href="class_tree_key_idx.html#a26">00510</a> <font class="keyword">const</font> <font class="keywordtype">char</font> *<a class="code" href="class_tree_key_idx.html#a26">TreeKeyIdx::getText</a>()<font class="keyword"> const </font>{
00511         <font class="keywordflow">return</font> getFullName();
00512 }
00513 
00514 
00515 <font class="keywordtype">int</font> TreeKeyIdx::_compare (<font class="keyword">const</font> <a class="code" href="class_tree_key_idx.html">TreeKeyIdx</a> &amp; ikey) {
00516                 <font class="keywordflow">return</font> (getOffset() - ikey.<a class="code" href="class_tree_key_idx.html#a23">getOffset</a>());
00517 }
00518 
00519 
<a name="l00520"></a><a class="code" href="class_tree_key_idx.html#a28">00520</a> <font class="keywordtype">int</font> <a class="code" href="class_tree_key_idx.html#a28">TreeKeyIdx::compare</a>(<font class="keyword">const</font> <a class="code" href="class_s_w_key.html">SWKey</a> &amp;ikey) {
00521         <a class="code" href="class_tree_key_idx.html">TreeKeyIdx</a> *treeKey = SWDYNAMIC_CAST(<a class="code" href="class_tree_key_idx.html">TreeKeyIdx</a>, (&amp;ikey));
00522         <font class="keywordflow">if</font> (treeKey)
00523                 <font class="keywordflow">return</font> _compare(*treeKey);
00524         <font class="keywordflow">return</font> <a class="code" href="class_s_w_key.html#a11">SWKey::compare</a>(ikey);
00525 }
00526 
00527 
<a name="l00528"></a><a class="code" href="class_tree_key_idx.html#a29">00528</a> <font class="keywordtype">void</font> <a class="code" href="class_tree_key_idx.html#a29">TreeKeyIdx::decrement</a>(<font class="keywordtype">int</font> steps) {
00529         error = getTreeNodeFromIdxOffset(currentNode.offset - (4*steps), &amp;currentNode);
00530 }
00531 
<a name="l00532"></a><a class="code" href="class_tree_key_idx.html#a30">00532</a> <font class="keywordtype">void</font> <a class="code" href="class_tree_key_idx.html#a30">TreeKeyIdx::increment</a>(<font class="keywordtype">int</font> steps) {
00533         error = getTreeNodeFromIdxOffset(currentNode.offset + (4*steps), &amp;currentNode);
00534 
00535 <font class="comment">/*</font>
00536 <font class="comment">        // assert positive</font>
00537 <font class="comment">        if (steps &lt; 0) {</font>
00538 <font class="comment">                decrement(steps * -1);</font>
00539 <font class="comment">                return;</font>
00540 <font class="comment">        }</font>
00541 <font class="comment"></font>
00542 <font class="comment">        while (steps &gt; 0) {</font>
00543 <font class="comment">                if (!firstChild()) {</font>
00544 <font class="comment">                        if (!nextSibbling() {</font>
00545 <font class="comment">                                error = KEYERR_OUTOFBOUNDS;</font>
00546 <font class="comment">                                return;</font>
00547 <font class="comment">                        }</font>
00548 <font class="comment">                }</font>
00549 <font class="comment">                steps--;</font>
00550 <font class="comment">        }</font>
00551 <font class="comment">*/</font>
00552 }
00553 
00554 
00555 
00556 TreeKeyIdx::TreeNode::TreeNode() {
00557 
00558         name       = 0;
00559         stdstr(&amp;name, <font class="stringliteral">""</font>);
00560         userData   = 0;
00561 
00562         clear();
00563 }
00564 
00565 
00566 <font class="keywordtype">void</font> TreeKeyIdx::TreeNode::clear() {
00567         offset     = 0;
00568         parent     = -1;
00569         next       = -1;
00570         firstChild = -1;
00571         dsize      = 0;
00572 
00573         <font class="keywordflow">if</font> (name)
00574                 <font class="keyword">delete</font> [] name;
00575         name = 0;
00576         stdstr(&amp;name, <font class="stringliteral">""</font>);
00577 
00578         <font class="keywordflow">if</font> (userData)
00579                 <font class="keyword">delete</font> [] userData;
00580         userData   = 0;
00581 }
00582 
00583 
00584 TreeKeyIdx::TreeNode::~TreeNode() {
00585         <font class="keywordflow">if</font> (name)
00586                 <font class="keyword">delete</font> [] name;
00587         
00588         <font class="keywordflow">if</font> (userData)
00589                 <font class="keyword">delete</font> [] userData;
00590 }
</pre></div><hr><address align="right"><small>Generated on Thu Jun 20 22:13:01 2002 for The Sword Project by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 
width=110 height=53></a>1.2.15 </small></address>
</body>
</html>