aboutsummaryrefslogblamecommitdiffstats
path: root/doc/api-documentation/html/rawstr_8cpp-source.html
blob: 16d7bd3455a823f37f2cd87ea6cf6d984df8931c (plain) (tree)
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


































































































































































































































































































































































































































































































































































































                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
<!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>rawstr.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>rawstr.cpp</h1><div class="fragment"><pre>00001 <font class="comment">/******************************************************************************</font>
00002 <font class="comment"> *  rawstr.cpp   - code for class 'RawStr'- a module that reads raw text</font>
00003 <font class="comment"> *                              files:  ot and nt using indexs ??.bks ??.cps ??.vss</font>
00004 <font class="comment"> *                              and provides lookup and parsing functions based on</font>
00005 <font class="comment"> *                              class StrKey</font>
00006 <font class="comment"> */</font>
00007 
00008 
00009 <font class="preprocessor">#include &lt;stdio.h&gt;</font>
00010 <font class="preprocessor">#include &lt;fcntl.h&gt;</font>
00011 <font class="preprocessor">#include &lt;errno.h&gt;</font>
00012 
00013 <font class="preprocessor">#ifndef __GNUC__</font>
00014 <font class="preprocessor"></font><font class="preprocessor">#include &lt;io.h&gt;</font>
00015 <font class="preprocessor">#else</font>
00016 <font class="preprocessor"></font><font class="preprocessor">#include &lt;unistd.h&gt;</font>
00017 <font class="preprocessor">#endif</font>
00018 <font class="preprocessor"></font>
00019 <font class="preprocessor">#include &lt;string.h&gt;</font>
00020 <font class="preprocessor">#include &lt;stdlib.h&gt;</font>
00021 <font class="preprocessor">#include &lt;utilfuns.h&gt;</font>
00022 <font class="preprocessor">#include &lt;rawstr.h&gt;</font>
00023 <font class="preprocessor">#include &lt;sysdata.h&gt;</font>
00024 <font class="comment">/******************************************************************************</font>
00025 <font class="comment"> * RawStr Statics</font>
00026 <font class="comment"> */</font>
00027 
00028 <font class="keywordtype">int</font> RawStr::instance = 0;
00029 <font class="keywordtype">char</font> RawStr::nl = <font class="charliteral">'\n'</font>;
00030 
00031 
00032 <font class="comment">/******************************************************************************</font>
00033 <font class="comment"> * RawStr Constructor - Initializes data for instance of RawStr</font>
00034 <font class="comment"> *</font>
00035 <font class="comment"> * ENT: ipath - path of the directory where data and index files are located.</font>
00036 <font class="comment"> *              be sure to include the trailing separator (e.g. '/' or '\')</font>
00037 <font class="comment"> *              (e.g. 'modules/texts/rawtext/webster/')</font>
00038 <font class="comment"> */</font>
00039 
00040 RawStr::RawStr(<font class="keyword">const</font> <font class="keywordtype">char</font> *ipath, <font class="keywordtype">int</font> fileMode)
00041 {
00042         <font class="keywordtype">char</font> buf[127];
00043 
00044         lastoff = -1;
00045         path = 0;
00046         stdstr(&amp;path, ipath);
00047 
00048 <font class="preprocessor">#ifndef O_BINARY                // O_BINARY is needed in Borland C++ 4.53</font>
00049 <font class="preprocessor"></font><font class="preprocessor">#define O_BINARY 0              // If it hasn't been defined than we probably</font>
00050 <font class="preprocessor"></font><font class="preprocessor">#endif                          // don't need it.</font>
00051 <font class="preprocessor"></font>
00052         <font class="keywordflow">if</font> (fileMode == -1) { <font class="comment">// try read/write if possible</font>
00053                 fileMode = O_RDWR;
00054         }
00055                 
00056         sprintf(buf, <font class="stringliteral">"%s.idx"</font>, path);
00057         idxfd = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, <font class="keyword">true</font>);
00058 
00059         sprintf(buf, <font class="stringliteral">"%s.dat"</font>, path);
00060         datfd = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, <font class="keyword">true</font>);
00061 
00062         <font class="keywordflow">if</font> (datfd &lt; 0) {
00063                 sprintf(buf, <font class="stringliteral">"Error: %d"</font>, errno);
00064                 perror(buf);
00065         }
00066 
00067         instance++;
00068 }
00069 
00070 
00071 <font class="comment">/******************************************************************************</font>
00072 <font class="comment"> * RawStr Destructor - Cleans up instance of RawStr</font>
00073 <font class="comment"> */</font>
00074 
00075 RawStr::~RawStr()
00076 {
00077         <font class="keywordflow">if</font> (path)
00078                 <font class="keyword">delete</font> [] path;
00079 
00080         --instance;
00081 
00082         FileMgr::systemFileMgr.close(idxfd);
00083         FileMgr::systemFileMgr.close(datfd);
00084 }
00085 
00086 
00087 <font class="comment">/******************************************************************************</font>
00088 <font class="comment"> * RawStr::getidxbufdat - Gets the index string at the given idx offset</font>
00089 <font class="comment"> *                                              NOTE: buf is allocated and must be freed by</font>
00090 <font class="comment"> *                                                      calling function</font>
00091 <font class="comment"> *</font>
00092 <font class="comment"> * ENT: ioffset - offset in dat file to lookup</font>
00093 <font class="comment"> *              buf             - address of pointer to allocate for storage of string</font>
00094 <font class="comment"> */</font>
00095 
00096 <font class="keywordtype">void</font> RawStr::getidxbufdat(<font class="keywordtype">long</font> ioffset, <font class="keywordtype">char</font> **buf)
00097 {
00098         <font class="keywordtype">int</font> size;
00099         <font class="keywordtype">char</font> ch;
00100         <font class="keywordflow">if</font> (datfd &gt; 0) {
00101                 lseek(datfd-&gt;getFd(), ioffset, SEEK_SET);
00102                 <font class="keywordflow">for</font> (size = 0; read(datfd-&gt;getFd(), &amp;ch, 1) == 1; size++) {
00103                         <font class="keywordflow">if</font> ((ch == <font class="charliteral">'\\'</font>) || (ch == 10) || (ch == 13))
00104                                 <font class="keywordflow">break</font>;
00105                 }
00106                 *buf = (*buf) ? (<font class="keywordtype">char</font> *)realloc(*buf, size + 1) : (char *)malloc(size + 1);
00107                 <font class="keywordflow">if</font> (size) {
00108                         lseek(datfd-&gt;getFd(), ioffset, SEEK_SET);
00109                         read(datfd-&gt;getFd(), *buf, size);
00110                 }
00111                 (*buf)[size] = 0;
00112                 <font class="keywordflow">for</font> (size--; size &gt; 0; size--)
00113                         (*buf)[size] = SW_toupper((*buf)[size]);
00114         }
00115         <font class="keywordflow">else</font> {
00116                 *buf = (*buf) ? (<font class="keywordtype">char</font> *)realloc(*buf, 1) : (char *)malloc(1);
00117                 **buf = 0;
00118         }
00119 }
00120 
00121 
00122 <font class="comment">/******************************************************************************</font>
00123 <font class="comment"> * RawStr::getidxbuf    - Gets the index string at the given idx offset</font>
00124 <font class="comment"> *                                              NOTE: buf is allocated and must be freed by</font>
00125 <font class="comment"> *                                                      calling function</font>
00126 <font class="comment"> *</font>
00127 <font class="comment"> * ENT: ioffset - offset in idx file to lookup</font>
00128 <font class="comment"> *              buf             - address of pointer to allocate for storage of string</font>
00129 <font class="comment"> */</font>
00130 
00131 <font class="keywordtype">void</font> RawStr::getidxbuf(<font class="keywordtype">long</font> ioffset, <font class="keywordtype">char</font> **buf)
00132 {
00133         <font class="keywordtype">char</font> *trybuf, *targetbuf;
00134         <font class="keywordtype">long</font> offset;
00135         
00136         <font class="keywordflow">if</font> (idxfd &gt; 0) {
00137                 lseek(idxfd-&gt;getFd(), ioffset, SEEK_SET);
00138                 read(idxfd-&gt;getFd(), &amp;offset, 4);
00139 
00140                 offset = swordtoarch32(offset);
00141 
00142                 getidxbufdat(offset, buf);
00143                 <font class="keywordflow">for</font> (trybuf = targetbuf = *buf; *trybuf; trybuf++, targetbuf++) {
00144 <font class="comment">/*</font>
00145 <font class="comment">                        if (*trybuf == '-') {           // ignore '-' because alphabetized silly in file</font>
00146 <font class="comment">                                targetbuf--;</font>
00147 <font class="comment">                                continue;</font>
00148 <font class="comment">                        }</font>
00149 <font class="comment">*/</font>
00150                         *targetbuf = SW_toupper(*trybuf);
00151                 }
00152                 *targetbuf = 0;
00153                 trybuf = 0;
00154         }
00155 }
00156 
00157 
00158 <font class="comment">/******************************************************************************</font>
00159 <font class="comment"> * RawStr::findoffset   - Finds the offset of the key string from the indexes</font>
00160 <font class="comment"> *</font>
00161 <font class="comment"> * ENT: key             - key string to lookup</font>
00162 <font class="comment"> *              start   - address to store the starting offset</font>
00163 <font class="comment"> *              size            - address to store the size of the entry</font>
00164 <font class="comment"> *              away            - number of entries before of after to jump</font>
00165 <font class="comment"> *                                      (default = 0)</font>
00166 <font class="comment"> *</font>
00167 <font class="comment"> * RET: error status</font>
00168 <font class="comment"> */</font>
00169 
00170 <font class="keywordtype">signed</font> <font class="keywordtype">char</font> RawStr::findoffset(<font class="keyword">const</font> <font class="keywordtype">char</font> *ikey, <font class="keywordtype">long</font> *start, <font class="keywordtype">unsigned</font> <font class="keywordtype">short</font> *size, <font class="keywordtype">long</font> away, <font class="keywordtype">long</font> *idxoff)
00171 {
00172         <font class="keywordtype">char</font> *trybuf, *targetbuf, *key, quitflag = 0;
00173         <font class="keywordtype">signed</font> <font class="keywordtype">char</font> retval = 0;
00174         <font class="keywordtype">long</font> headoff, tailoff, tryoff = 0, maxoff = 0;
00175 
00176         <font class="keywordflow">if</font> (idxfd-&gt;getFd() &gt;=0) {
00177                 tailoff = maxoff = lseek(idxfd-&gt;getFd(), 0, SEEK_END) - 6;
00178                 <font class="keywordflow">if</font> (*ikey) {
00179                         headoff = 0;
00180 
00181                         key = <font class="keyword">new</font> <font class="keywordtype">char</font> [ strlen(ikey) + 1 ];
00182                         strcpy(key, ikey);
00183 
00184                         <font class="keywordflow">for</font> (trybuf = targetbuf = key; *trybuf; trybuf++, targetbuf++) {
00185         <font class="comment">/*</font>
00186 <font class="comment">                                if (*trybuf == '-') {           // ignore '-' because alphabetized silly in file</font>
00187 <font class="comment">                                        targetbuf--;</font>
00188 <font class="comment">                                        continue;</font>
00189 <font class="comment">                                }</font>
00190 <font class="comment">        */</font>
00191                                 *targetbuf = SW_toupper(*trybuf);
00192                         }
00193                         *targetbuf = 0;
00194                         trybuf = 0;
00195 
00196                         <font class="keywordflow">while</font> (headoff &lt; tailoff) {
00197                                 tryoff = (lastoff == -1) ? headoff + ((((tailoff / 6) - (headoff / 6))) / 2) * 6 : lastoff; 
00198                                 lastoff = -1;
00199                                 getidxbuf(tryoff, &amp;trybuf);
00200 
00201                                 <font class="keywordflow">if</font> (!*trybuf &amp;&amp; tryoff) {               <font class="comment">// In case of extra entry at end of idx (not first entry)</font>
00202                                         tryoff += (tryoff &gt; (maxoff / 2))?-6:6;
00203                                         retval = -1;
00204                                         <font class="keywordflow">break</font>;
00205                                 }
00206                                         
00207                                 <font class="keywordflow">if</font> (!strcmp(key, trybuf))
00208                                         <font class="keywordflow">break</font>;
00209 
00210                                 <font class="keywordtype">int</font> diff = strcmp(key, trybuf);
00211                                 <font class="keywordflow">if</font> (diff &lt; 0)
00212                                         tailoff = (tryoff == headoff) ? headoff : tryoff;
00213                                 <font class="keywordflow">else</font> headoff = tryoff;
00214                                 <font class="keywordflow">if</font> (tailoff == headoff + 6) {
00215                                         <font class="keywordflow">if</font> (quitflag++)
00216                                                 headoff = tailoff;
00217                                 }
00218                         }
00219                         <font class="keywordflow">if</font> (headoff &gt;= tailoff)
00220                                 tryoff = headoff;
00221                         <font class="keywordflow">if</font> (trybuf)
00222                                 free(trybuf);
00223                         <font class="keyword">delete</font> [] key;
00224                 }
00225                 <font class="keywordflow">else</font>    tryoff = 0;
00226 
00227                 lseek(idxfd-&gt;getFd(), tryoff, SEEK_SET);
00228 
00229                 *start = *size = 0;
00230                 read(idxfd-&gt;getFd(), start, 4);
00231                 read(idxfd-&gt;getFd(), size, 2);
00232                 <font class="keywordflow">if</font> (idxoff)
00233                         *idxoff = tryoff;
00234 
00235                 *start = swordtoarch32(*start);
00236                 *size  = swordtoarch16(*size);
00237 
00238                 <font class="keywordflow">while</font> (away) {
00239                         <font class="keywordtype">long</font> laststart = *start;
00240                         <font class="keywordtype">unsigned</font> <font class="keywordtype">short</font> lastsize = *size;
00241                         <font class="keywordtype">long</font> lasttry = tryoff;
00242                         tryoff += (away &gt; 0) ? 6 : -6;
00243                 
00244                         <font class="keywordtype">bool</font> bad = <font class="keyword">false</font>;
00245                         <font class="keywordflow">if</font> (((tryoff + (away*6)) &lt; -6) || (tryoff + (away*6) &gt; (maxoff+6)))
00246                                 bad = <font class="keyword">true</font>;
00247                         <font class="keywordflow">else</font> <font class="keywordflow">if</font> (lseek(idxfd-&gt;getFd(), tryoff, SEEK_SET) &lt; 0)
00248                                 bad = <font class="keyword">true</font>;
00249                         <font class="keywordflow">if</font> (bad) {
00250                                 retval = -1;
00251                                 *start = laststart;
00252                                 *size = lastsize;
00253                                 tryoff = lasttry;
00254                                 <font class="keywordflow">if</font> (idxoff)
00255                                         *idxoff = tryoff;
00256                                 <font class="keywordflow">break</font>;
00257                         }
00258                         read(idxfd-&gt;getFd(), start, 4);
00259                         read(idxfd-&gt;getFd(), size, 2);
00260                         <font class="keywordflow">if</font> (idxoff)
00261                                 *idxoff = tryoff;
00262 
00263                         *start = swordtoarch32(*start);
00264                         *size  = swordtoarch16(*size);
00265 
00266                         <font class="keywordflow">if</font> (((laststart != *start) || (lastsize != *size)) &amp;&amp; (*start &gt;= 0) &amp;&amp; (*size)) 
00267                                 away += (away &lt; 0) ? 1 : -1;
00268                 }
00269         
00270                 lastoff = tryoff;
00271         }
00272         <font class="keywordflow">else</font> {
00273                 *start = 0;
00274                 *size  = 0;
00275                 <font class="keywordflow">if</font> (idxoff)
00276                         *idxoff = 0;
00277                 retval = -1;
00278         }
00279         <font class="keywordflow">return</font> retval;
00280 }
00281 
00282 
00283 <font class="comment">/******************************************************************************</font>
00284 <font class="comment"> * RawStr::preptext     - Prepares the text before returning it to external</font>
00285 <font class="comment"> *                                      objects</font>
00286 <font class="comment"> *</font>
00287 <font class="comment"> * ENT: buf     - buffer where text is stored and where to store the prep'd</font>
00288 <font class="comment"> *                              text.</font>
00289 <font class="comment"> */</font>
00290 
00291 <font class="keywordtype">void</font> RawStr::preptext(<font class="keywordtype">char</font> *buf) {
00292         <font class="keywordtype">char</font> *to, *from, space = 0, cr = 0, realdata = 0, nlcnt = 0;
00293 
00294         <font class="keywordflow">for</font> (to = from = buf; *from; from++) {
00295                 <font class="keywordflow">switch</font> (*from) {
00296                 <font class="keywordflow">case</font> 10:
00297                         <font class="keywordflow">if</font> (!realdata)
00298                                 <font class="keywordflow">continue</font>;
00299                         space = (cr) ? 0 : 1;
00300                         cr = 0;
00301                         nlcnt++;
00302                         <font class="keywordflow">if</font> (nlcnt &gt; 1) {
00303 <font class="comment">//                              *to++ = nl;</font>
00304                                 *to++ = nl;
00305 <font class="comment">//                              nlcnt = 0;</font>
00306                         }
00307                         <font class="keywordflow">continue</font>;
00308                 <font class="keywordflow">case</font> 13:
00309                         <font class="keywordflow">if</font> (!realdata)
00310                                 <font class="keywordflow">continue</font>;
00311                         *to++ = nl;
00312                         space = 0;
00313                         cr = 1;
00314                         <font class="keywordflow">continue</font>;
00315                 }
00316                 realdata = 1;
00317                 nlcnt = 0;
00318                 <font class="keywordflow">if</font> (space) {
00319                         space = 0;
00320                         <font class="keywordflow">if</font> (*from != <font class="charliteral">' '</font>) {
00321                                 *to++ = <font class="charliteral">' '</font>;
00322                                 from--;
00323                                 <font class="keywordflow">continue</font>;
00324                         }
00325                 }
00326                 *to++ = *from;
00327         }
00328         *to = 0;
00329 
00330         <font class="keywordflow">while</font> (to &gt; (buf+1)) {                  <font class="comment">// remove trailing excess</font>
00331                 to--;
00332                 <font class="keywordflow">if</font> ((*to == 10) || (*to == <font class="charliteral">' '</font>))
00333                         *to = 0;
00334                 <font class="keywordflow">else</font> <font class="keywordflow">break</font>;
00335         }
00336 }
00337 
00338 
00339 <font class="comment">/******************************************************************************</font>
00340 <font class="comment"> * RawStr::gettext      - gets text at a given offset</font>
00341 <font class="comment"> *</font>
00342 <font class="comment"> * ENT:</font>
00343 <font class="comment"> *      start   - starting offset where the text is located in the file</font>
00344 <font class="comment"> *      size            - size of text entry</font>
00345 <font class="comment"> *      buf             - buffer to store text</font>
00346 <font class="comment"> *</font>
00347 <font class="comment"> */</font>
00348 
00349 <font class="keywordtype">void</font> RawStr::gettext(<font class="keywordtype">long</font> istart, <font class="keywordtype">unsigned</font> <font class="keywordtype">short</font> isize, <font class="keywordtype">char</font> *idxbuf, <font class="keywordtype">char</font> *buf)
00350 {
00351         <font class="keywordtype">char</font> *ch;
00352         <font class="keywordtype">char</font> *idxbuflocal = 0;
00353         getidxbufdat(istart, &amp;idxbuflocal);
00354         <font class="keywordtype">long</font> start = istart;
00355         <font class="keywordtype">unsigned</font> <font class="keywordtype">short</font> size = isize;
00356 
00357         <font class="keywordflow">do</font> {
00358                 memset(buf, 0, size);
00359                 lseek(datfd-&gt;getFd(), start, SEEK_SET);
00360                 read(datfd-&gt;getFd(), buf, (int)(size - 2));
00361 
00362                 <font class="keywordflow">for</font> (ch = buf; *ch; ch++) {             <font class="comment">// skip over index string</font>
00363                         <font class="keywordflow">if</font> (*ch == 10) {
00364                                 ch++;
00365                                 <font class="keywordflow">break</font>;
00366                         }
00367                 }
00368                 size -= (<font class="keywordtype">unsigned</font> <font class="keywordtype">short</font>)(ch-buf);
00369                 memmove(buf, ch, size);
00370                 buf[size] = 0;
00371                 buf[size+1] = 0;
00372 
00373                 <font class="comment">// resolve link</font>
00374                 <font class="keywordflow">if</font> (!strncmp(buf, <font class="stringliteral">"@LINK"</font>, 5)) {
00375                         <font class="keywordflow">for</font> (ch = buf; *ch; ch++) {             <font class="comment">// null before nl</font>
00376                                 <font class="keywordflow">if</font> (*ch == 10) {
00377                                         *ch = 0;
00378                                         <font class="keywordflow">break</font>;
00379                                 }
00380                         }
00381                         findoffset(buf + 6, &amp;start, &amp;size);
00382                         <font class="comment">// TODO: FIX!  THIS IS WRONG!!!  buf is not reallocated for the appropriate size!</font>
00383                 }
00384                 <font class="keywordflow">else</font> <font class="keywordflow">break</font>;
00385         }
00386         <font class="keywordflow">while</font> (true);   <font class="comment">// while we're resolving links</font>
00387 
00388         <font class="keywordflow">if</font> (idxbuflocal) {
00389                 <font class="keywordtype">int</font> localsize = strlen(idxbuflocal);
00390                 localsize = (localsize &lt; (size - 1)) ? localsize : (size - 1);
00391                 strncpy(idxbuf, idxbuflocal, localsize);
00392                 idxbuf[localsize] = 0;
00393                 free(idxbuflocal);
00394         }
00395 }
00396 
00397 
00398 <font class="comment">/******************************************************************************</font>
00399 <font class="comment"> * RawLD::settext       - Sets text for current offset</font>
00400 <font class="comment"> *</font>
00401 <font class="comment"> * ENT: key     - key for this entry</font>
00402 <font class="comment"> *      buf     - buffer to store</font>
00403 <font class="comment"> *      len     - length of buffer (0 - null terminated)</font>
00404 <font class="comment"> */</font>
00405 
00406 <font class="keywordtype">void</font> RawStr::settext(<font class="keyword">const</font> <font class="keywordtype">char</font> *ikey, <font class="keyword">const</font> <font class="keywordtype">char</font> *buf, <font class="keywordtype">long</font> len)
00407 {
00408 
00409         <font class="keywordtype">long</font> start, outstart;
00410         <font class="keywordtype">long</font> idxoff;
00411         <font class="keywordtype">long</font> endoff;
00412         <font class="keywordtype">long</font> shiftSize;
00413         <font class="keywordtype">unsigned</font> <font class="keywordtype">short</font> size;
00414         <font class="keywordtype">unsigned</font> <font class="keywordtype">short</font> outsize;
00415         <font class="keyword">static</font> <font class="keyword">const</font> <font class="keywordtype">char</font> nl[] = {13, 10};
00416         <font class="keywordtype">char</font> *tmpbuf = 0;
00417         <font class="keywordtype">char</font> *key = 0;
00418         <font class="keywordtype">char</font> *dbKey = 0;
00419         <font class="keywordtype">char</font> *idxBytes = 0;
00420         <font class="keywordtype">char</font> *outbuf = 0;
00421         <font class="keywordtype">char</font> *ch = 0;
00422 
00423         findoffset(ikey, &amp;start, &amp;size, 0, &amp;idxoff);
00424         stdstr(&amp;key, ikey);
00425         <font class="keywordflow">for</font> (ch = key; *ch; ch++)
00426                 *ch = SW_toupper(*ch);
00427         ch = 0;
00428 
00429         getidxbufdat(start, &amp;dbKey);
00430 
00431         <font class="keywordflow">if</font> (strcmp(key, dbKey) &lt; 0) {
00432         }
00433         <font class="keywordflow">else</font> <font class="keywordflow">if</font> (strcmp(key, dbKey) &gt; 0) {
00434                 idxoff += 6;
00435         } <font class="keywordflow">else</font> <font class="keywordflow">if</font> ((!strcmp(key, dbKey)) &amp;&amp; (len || strlen(buf) <font class="comment">/*we're not deleting*/</font>)) { <font class="comment">// got absolute entry</font>
00436                 <font class="keywordflow">do</font> {
00437                         tmpbuf = <font class="keyword">new</font> <font class="keywordtype">char</font> [ size + 2 ];
00438                         memset(tmpbuf, 0, size + 2);
00439                         lseek(datfd-&gt;getFd(), start, SEEK_SET);
00440                         read(datfd-&gt;getFd(), tmpbuf, (int)(size - 1));
00441 
00442                         <font class="keywordflow">for</font> (ch = tmpbuf; *ch; ch++) {          <font class="comment">// skip over index string</font>
00443                                 <font class="keywordflow">if</font> (*ch == 10) {
00444                                         ch++;
00445                                         <font class="keywordflow">break</font>;
00446                                 }
00447                         }
00448                         memmove(tmpbuf, ch, size - (<font class="keywordtype">unsigned</font> <font class="keywordtype">short</font>)(ch-tmpbuf));
00449 
00450                         <font class="comment">// resolve link</font>
00451                         <font class="keywordflow">if</font> (!strncmp(tmpbuf, <font class="stringliteral">"@LINK"</font>, 5) &amp;&amp; (len ? len : strlen(buf))) {
00452                                 <font class="keywordflow">for</font> (ch = tmpbuf; *ch; ch++) {          <font class="comment">// null before nl</font>
00453                                         <font class="keywordflow">if</font> (*ch == 10) {
00454                                                 *ch = 0;
00455                                                 <font class="keywordflow">break</font>;
00456                                         }
00457                                 }
00458                                 findoffset(tmpbuf + 6, &amp;start, &amp;size, 0, &amp;idxoff);
00459                         }
00460                         <font class="keywordflow">else</font> <font class="keywordflow">break</font>;
00461                 }
00462                 <font class="keywordflow">while</font> (true);   <font class="comment">// while we're resolving links</font>
00463         }
00464 
00465         endoff = lseek(idxfd-&gt;getFd(), 0, SEEK_END);
00466 
00467         shiftSize = endoff - idxoff;
00468 
00469         <font class="keywordflow">if</font> (shiftSize &gt; 0) {
00470                 idxBytes = <font class="keyword">new</font> <font class="keywordtype">char</font> [ shiftSize ];
00471                 lseek(idxfd-&gt;getFd(), idxoff, SEEK_SET);
00472                 read(idxfd-&gt;getFd(), idxBytes, shiftSize);
00473         }
00474 
00475         outbuf = <font class="keyword">new</font> <font class="keywordtype">char</font> [ (len ? len : strlen(buf)) + strlen(key) + 5 ];
00476         sprintf(outbuf, <font class="stringliteral">"%s%c%c"</font>, key, 13, 10);
00477         size = strlen(outbuf);
00478         memcpy (outbuf + size, buf, len ? len : strlen(buf));
00479         size = outsize = size + (len ? len : strlen(buf));
00480 
00481         start = outstart = lseek(datfd-&gt;getFd(), 0, SEEK_END);
00482 
00483         outstart = archtosword32(start);
00484         outsize  = archtosword16(size);
00485 
00486         lseek(idxfd-&gt;getFd(), idxoff, SEEK_SET);
00487         <font class="keywordflow">if</font> (len ? len : strlen(buf)) {
00488                 lseek(datfd-&gt;getFd(), start, SEEK_SET);
00489                 write(datfd-&gt;getFd(), outbuf, (int)size);
00490 
00491                 <font class="comment">// add a new line to make data file easier to read in an editor</font>
00492                 write(datfd-&gt;getFd(), &amp;nl, 2);
00493                 
00494                 write(idxfd-&gt;getFd(), &amp;outstart, 4);
00495                 write(idxfd-&gt;getFd(), &amp;outsize, 2);
00496                 <font class="keywordflow">if</font> (idxBytes) {
00497                         write(idxfd-&gt;getFd(), idxBytes, shiftSize);
00498                         <font class="keyword">delete</font> [] idxBytes;
00499                 }
00500         }
00501         <font class="keywordflow">else</font> {  <font class="comment">// delete entry</font>
00502                 <font class="keywordflow">if</font> (idxBytes) {
00503                         write(idxfd-&gt;getFd(), idxBytes+6, shiftSize-6);
00504                         lseek(idxfd-&gt;getFd(), -1, SEEK_CUR);    <font class="comment">// last valid byte</font>
00505                         FileMgr::systemFileMgr.trunc(idxfd);    <font class="comment">// truncate index</font>
00506                         <font class="keyword">delete</font> [] idxBytes;
00507                 }
00508         }
00509 
00510         <font class="keyword">delete</font> [] key;
00511         <font class="keyword">delete</font> [] outbuf;
00512         free(dbKey);
00513 }
00514 
00515 
00516 <font class="comment">/******************************************************************************</font>
00517 <font class="comment"> * RawLD::linkentry     - links one entry to another</font>
00518 <font class="comment"> *</font>
00519 <font class="comment"> * ENT: testmt  - testament to find (0 - Bible/module introduction)</font>
00520 <font class="comment"> *      destidxoff      - dest offset into .vss</font>
00521 <font class="comment"> *      srcidxoff               - source offset into .vss</font>
00522 <font class="comment"> */</font>
00523 
00524 <font class="keywordtype">void</font> RawStr::linkentry(<font class="keyword">const</font> <font class="keywordtype">char</font> *destkey, <font class="keyword">const</font> <font class="keywordtype">char</font> *srckey) {
00525         <font class="keywordtype">char</font> *text = <font class="keyword">new</font> <font class="keywordtype">char</font> [ strlen(destkey) + 7 ];
00526         sprintf(text, <font class="stringliteral">"@LINK %s"</font>, destkey);
00527         settext(srckey, text);
00528         <font class="keyword">delete</font> [] text;
00529 }
00530 
00531 
00532 <font class="comment">/******************************************************************************</font>
00533 <font class="comment"> * RawLD::CreateModule  - Creates new module files</font>
00534 <font class="comment"> *</font>
00535 <font class="comment"> * ENT: path    - directory to store module files</font>
00536 <font class="comment"> * RET: error status</font>
00537 <font class="comment"> */</font>
00538 
00539 <font class="keywordtype">signed</font> <font class="keywordtype">char</font> RawStr::createModule(<font class="keyword">const</font> <font class="keywordtype">char</font> *ipath)
00540 {
00541         <font class="keywordtype">char</font> *path = 0;
00542         <font class="keywordtype">char</font> *buf = <font class="keyword">new</font> <font class="keywordtype">char</font> [ strlen (ipath) + 20 ];
00543         FileDesc *fd, *fd2;
00544 
00545         stdstr(&amp;path, ipath);
00546 
00547         <font class="keywordflow">if</font> ((path[strlen(path)-1] == <font class="charliteral">'/'</font>) || (path[strlen(path)-1] == <font class="charliteral">'\\'</font>))
00548                 path[strlen(path)-1] = 0;
00549 
00550         sprintf(buf, <font class="stringliteral">"%s.dat"</font>, path);
00551         unlink(buf);
00552         fd = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
00553         fd-&gt;getFd();
00554         FileMgr::systemFileMgr.close(fd);
00555 
00556         sprintf(buf, <font class="stringliteral">"%s.idx"</font>, path);
00557         unlink(buf);
00558         fd2 = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
00559         fd2-&gt;getFd();
00560         FileMgr::systemFileMgr.close(fd2);
00561 
00562         <font class="keyword">delete</font> [] path;
00563         
00564         <font class="keywordflow">return</font> 0;
00565 }
</pre></div><hr><address align="right"><small>Generated on Thu Jun 20 22:13:00 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>