aboutsummaryrefslogtreecommitdiffstats
path: root/doc/api-documentation/html/rawstr4_8cpp-source.html
blob: 2626f05a54685cd29f529accf3c70ecd67ea0d6c (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
<!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>rawstr4.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>rawstr4.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 <font class="preprocessor">#include &lt;stdio.h&gt;</font>
00009 <font class="preprocessor">#include &lt;fcntl.h&gt;</font>
00010 <font class="preprocessor">#include &lt;errno.h&gt;</font>
00011 
00012 <font class="preprocessor">#ifndef __GNUC__</font>
00013 <font class="preprocessor"></font><font class="preprocessor">#include &lt;io.h&gt;</font>
00014 <font class="preprocessor">#else</font>
00015 <font class="preprocessor"></font><font class="preprocessor">#include &lt;unistd.h&gt;</font>
00016 <font class="preprocessor">#endif</font>
00017 <font class="preprocessor"></font>
00018 <font class="preprocessor">#include &lt;string.h&gt;</font>
00019 <font class="preprocessor">#include &lt;stdlib.h&gt;</font>
00020 <font class="preprocessor">#include &lt;utilfuns.h&gt;</font>
00021 <font class="preprocessor">#include &lt;rawstr4.h&gt;</font>
00022 <font class="preprocessor">#include &lt;sysdata.h&gt;</font>
00023 
00024 <font class="comment">/******************************************************************************</font>
00025 <font class="comment"> * RawStr Statics</font>
00026 <font class="comment"> */</font>
00027 
00028 <font class="keywordtype">int</font> RawStr4::instance = 0;
00029 
00030 
00031 <font class="comment">/******************************************************************************</font>
00032 <font class="comment"> * RawStr Constructor - Initializes data for instance of RawStr</font>
00033 <font class="comment"> *</font>
00034 <font class="comment"> * ENT: ipath - path of the directory where data and index files are located.</font>
00035 <font class="comment"> *              be sure to include the trailing separator (e.g. '/' or '\')</font>
00036 <font class="comment"> *              (e.g. 'modules/texts/rawtext/webster/')</font>
00037 <font class="comment"> */</font>
00038 
00039 RawStr4::RawStr4(<font class="keyword">const</font> <font class="keywordtype">char</font> *ipath, <font class="keywordtype">int</font> fileMode)
00040 {
00041         <font class="keywordtype">char</font> buf[127];
00042 
00043         nl = <font class="charliteral">'\n'</font>;
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 RawStr4::~RawStr4()
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"> * RawStr4::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> RawStr4::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"> * RawStr4::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> RawStr4::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"> * RawStr4::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> RawStr4::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">long</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                 <font class="keywordflow">if</font> (*ikey) {
00178                         headoff = 0;
00179                         tailoff = maxoff = lseek(idxfd-&gt;getFd(), 0, SEEK_END) - 8;
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 / 8) - (headoff / 8))) / 2) * 8 : lastoff; 
00198                                 lastoff = -1;
00199                                 getidxbuf(tryoff, &amp;trybuf);
00200 
00201                                 <font class="keywordflow">if</font> (!*trybuf) {         <font class="comment">// In case of extra entry at end of idx</font>
00202                                         tryoff += (tryoff &gt; (maxoff / 2))?-8:8;
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 + 8) {
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, 4);
00232                 <font class="keywordflow">if</font> (idxoff)
00233                         *idxoff = tryoff;
00234 
00235                 *start = swordtoarch32(*start);
00236                 *size  = swordtoarch32(*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">long</font> lastsize = *size;
00241                         <font class="keywordtype">long</font> lasttry = tryoff;
00242                         tryoff += (away &gt; 0) ? 8 : -8;
00243 
00244                         <font class="keywordtype">bool</font> bad = <font class="keyword">false</font>;
00245                         <font class="keywordflow">if</font> (((tryoff + (away*8)) &lt; -8) || (tryoff + (away*8) &gt; (maxoff+8)))
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, 4);
00260                         <font class="keywordflow">if</font> (idxoff)
00261                                 *idxoff = tryoff;
00262 
00263                         *start = swordtoarch32(*start);
00264                         *size  = swordtoarch32(*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"> * RawStr4::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> RawStr4::preptext(<font class="keywordtype">char</font> *buf)
00292 {
00293         <font class="keywordtype">char</font> *to, *from, space = 0, cr = 0, realdata = 0, nlcnt = 0;
00294 
00295         <font class="keywordflow">for</font> (to = from = buf; *from; from++) {
00296                 <font class="keywordflow">switch</font> (*from) {
00297                 <font class="keywordflow">case</font> 10:
00298                         <font class="keywordflow">if</font> (!realdata)
00299                                 <font class="keywordflow">continue</font>;
00300                         space = (cr) ? 0 : 1;
00301                         cr = 0;
00302                         nlcnt++;
00303                         <font class="keywordflow">if</font> (nlcnt &gt; 1) {
00304 <font class="comment">//                              *to++ = nl;</font>
00305                                 *to++ = nl;
00306 <font class="comment">//                              nlcnt = 0;</font>
00307                         }
00308                         <font class="keywordflow">continue</font>;
00309                 <font class="keywordflow">case</font> 13:
00310                         <font class="keywordflow">if</font> (!realdata)
00311                                 <font class="keywordflow">continue</font>;
00312                         *to++ = nl;
00313                         space = 0;
00314                         cr = 1;
00315                         <font class="keywordflow">continue</font>;
00316                 }
00317                 realdata = 1;
00318                 nlcnt = 0;
00319                 <font class="keywordflow">if</font> (space) {
00320                         space = 0;
00321                         <font class="keywordflow">if</font> (*from != <font class="charliteral">' '</font>) {
00322                                 *to++ = <font class="charliteral">' '</font>;
00323                                 from--;
00324                                 <font class="keywordflow">continue</font>;
00325                         }
00326                 }
00327                 *to++ = *from;
00328         }
00329         *to = 0;
00330 
00331         <font class="keywordflow">while</font> (to &gt; (buf+1)) {                  <font class="comment">// remove trailing excess</font>
00332                 to--;
00333                 <font class="keywordflow">if</font> ((*to == 10) || (*to == <font class="charliteral">' '</font>))
00334                         *to = 0;
00335                 <font class="keywordflow">else</font> <font class="keywordflow">break</font>;
00336         }
00337 }
00338 
00339 
00340 <font class="comment">/******************************************************************************</font>
00341 <font class="comment"> * RawStr4::gettext     - gets text at a given offset</font>
00342 <font class="comment"> *</font>
00343 <font class="comment"> * ENT:</font>
00344 <font class="comment"> *      start   - starting offset where the text is located in the file</font>
00345 <font class="comment"> *      size            - size of text entry</font>
00346 <font class="comment"> *      buf             - buffer to store text</font>
00347 <font class="comment"> *</font>
00348 <font class="comment"> */</font>
00349 
00350 <font class="keywordtype">void</font> RawStr4::gettext(<font class="keywordtype">long</font> istart, <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> isize, <font class="keywordtype">char</font> *idxbuf, <font class="keywordtype">char</font> *buf)
00351 {
00352         <font class="keywordtype">char</font> *ch;
00353         <font class="keywordtype">char</font> *idxbuflocal = 0;
00354         getidxbufdat(istart, &amp;idxbuflocal);
00355         <font class="keywordtype">long</font> start = istart;
00356         <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> size = isize;
00357 
00358         <font class="keywordflow">do</font> {
00359                 memset(buf, 0, size);
00360                 lseek(datfd-&gt;getFd(), start, SEEK_SET);
00361                 read(datfd-&gt;getFd(), buf, (int)(size - 1));
00362 
00363                 <font class="keywordflow">for</font> (ch = buf; *ch; ch++) {             <font class="comment">// skip over index string</font>
00364                         <font class="keywordflow">if</font> (*ch == 10) {
00365                                 ch++;
00366                                 <font class="keywordflow">break</font>;
00367                         }
00368                 }
00369                 memmove(buf, ch, size - (<font class="keywordtype">unsigned</font> <font class="keywordtype">long</font>)(ch-buf));
00370 
00371                 <font class="comment">// resolve link</font>
00372                 <font class="keywordflow">if</font> (!strncmp(buf, <font class="stringliteral">"@LINK"</font>, 5)) {
00373                         <font class="keywordflow">for</font> (ch = buf; *ch; ch++) {             <font class="comment">// null before nl</font>
00374                                 <font class="keywordflow">if</font> (*ch == 10) {
00375                                         *ch = 0;
00376                                         <font class="keywordflow">break</font>;
00377                                 }
00378                         }
00379                         findoffset(buf + 8, &amp;start, &amp;size);
00380                 }
00381                 <font class="keywordflow">else</font> <font class="keywordflow">break</font>;
00382         }
00383         <font class="keywordflow">while</font> (true);   <font class="comment">// while we're resolving links</font>
00384 
00385         <font class="keywordflow">if</font> (idxbuflocal) {
00386                 <font class="keywordtype">int</font> localsize = strlen(idxbuflocal);
00387                 localsize = (localsize &lt; (size - 1)) ? localsize : (size - 1);
00388                 strncpy(idxbuf, idxbuflocal, localsize);
00389                 idxbuf[localsize] = 0;
00390                 free(idxbuflocal);
00391         }
00392 }
00393 
00394 
00395 <font class="comment">/******************************************************************************</font>
00396 <font class="comment"> * RawLD::settext       - Sets text for current offset</font>
00397 <font class="comment"> *</font>
00398 <font class="comment"> * ENT: key     - key for this entry</font>
00399 <font class="comment"> *      buf     - buffer to store</font>
00400 <font class="comment"> *      len     - length of buffer (0 - null terminated)</font>
00401 <font class="comment"> */</font>
00402 
00403 <font class="keywordtype">void</font> RawStr4::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)
00404 {
00405 
00406         <font class="keywordtype">long</font> start, outstart;
00407         <font class="keywordtype">long</font> idxoff;
00408         <font class="keywordtype">long</font> endoff;
00409         <font class="keywordtype">long</font> shiftSize;
00410         <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> size;
00411         <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> outsize;
00412         <font class="keyword">static</font> <font class="keyword">const</font> <font class="keywordtype">char</font> nl[] = {13, 10};
00413         <font class="keywordtype">char</font> *tmpbuf = 0;
00414         <font class="keywordtype">char</font> *key = 0;
00415         <font class="keywordtype">char</font> *dbKey = 0;
00416         <font class="keywordtype">char</font> *idxBytes = 0;
00417         <font class="keywordtype">char</font> *outbuf = 0;
00418         <font class="keywordtype">char</font> *ch = 0;
00419 
00420         findoffset(ikey, &amp;start, &amp;size, 0, &amp;idxoff);
00421         stdstr(&amp;key, ikey);
00422         <font class="keywordflow">for</font> (ch = key; *ch; ch++)
00423                 *ch = SW_toupper(*ch);
00424         ch = 0;
00425 
00426         getidxbufdat(start, &amp;dbKey);
00427 
00428         <font class="keywordflow">if</font> (strcmp(key, dbKey) &lt; 0) {
00429         }
00430         <font class="keywordflow">else</font> <font class="keywordflow">if</font> (strcmp(key, dbKey) &gt; 0) {
00431                 idxoff += 8;
00432         } <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>
00433                 <font class="keywordflow">do</font> {
00434                         tmpbuf = <font class="keyword">new</font> <font class="keywordtype">char</font> [ size + 2 ];
00435                         memset(tmpbuf, 0, size + 2);
00436                         lseek(datfd-&gt;getFd(), start, SEEK_SET);
00437                         read(datfd-&gt;getFd(), tmpbuf, (int)(size - 1));
00438 
00439                         <font class="keywordflow">for</font> (ch = tmpbuf; *ch; ch++) {          <font class="comment">// skip over index string</font>
00440                                 <font class="keywordflow">if</font> (*ch == 10) {
00441                                         ch++;
00442                                         <font class="keywordflow">break</font>;
00443                                 }
00444                         }
00445                         memmove(tmpbuf, ch, size - (<font class="keywordtype">unsigned</font> <font class="keywordtype">long</font>)(ch-tmpbuf));
00446 
00447                         <font class="comment">// resolve link</font>
00448                         <font class="keywordflow">if</font> (!strncmp(tmpbuf, <font class="stringliteral">"@LINK"</font>, 5) &amp;&amp; (len ? len : strlen(buf))) {
00449                                 <font class="keywordflow">for</font> (ch = tmpbuf; *ch; ch++) {          <font class="comment">// null before nl</font>
00450                                         <font class="keywordflow">if</font> (*ch == 10) {
00451                                                 *ch = 0;
00452                                                 <font class="keywordflow">break</font>;
00453                                         }
00454                                 }
00455                                 findoffset(tmpbuf + 8, &amp;start, &amp;size, 0, &amp;idxoff);
00456                         }
00457                         <font class="keywordflow">else</font> <font class="keywordflow">break</font>;
00458                 }
00459                 <font class="keywordflow">while</font> (true);   <font class="comment">// while we're resolving links</font>
00460         }
00461 
00462         endoff = lseek(idxfd-&gt;getFd(), 0, SEEK_END);
00463 
00464         shiftSize = endoff - idxoff;
00465 
00466         <font class="keywordflow">if</font> (shiftSize &gt; 0) {
00467                 idxBytes = <font class="keyword">new</font> <font class="keywordtype">char</font> [ shiftSize ];
00468                 lseek(idxfd-&gt;getFd(), idxoff, SEEK_SET);
00469                 read(idxfd-&gt;getFd(), idxBytes, shiftSize);
00470         }
00471 
00472         outbuf = <font class="keyword">new</font> <font class="keywordtype">char</font> [ (len ? len : strlen(buf)) + strlen(key) + 5 ];
00473         sprintf(outbuf, <font class="stringliteral">"%s%c%c"</font>, key, 13, 10);
00474         size = strlen(outbuf);
00475         memcpy (outbuf + size, buf, len ? len : strlen(buf));
00476         size = outsize = size + (len ? len : strlen(buf));
00477 
00478         start = outstart = lseek(datfd-&gt;getFd(), 0, SEEK_END);
00479 
00480         outstart = archtosword32(start);
00481         outsize  = archtosword32(size);
00482 
00483         lseek(idxfd-&gt;getFd(), idxoff, SEEK_SET);
00484         <font class="keywordflow">if</font> (len ? len : strlen(buf)) {
00485                 lseek(datfd-&gt;getFd(), start, SEEK_SET);
00486                 write(datfd-&gt;getFd(), outbuf, (long)size);
00487 
00488                 <font class="comment">// add a new line to make data file easier to read in an editor</font>
00489                 write(datfd-&gt;getFd(), &amp;nl, 2);
00490                 
00491                 write(idxfd-&gt;getFd(), &amp;outstart, 4);
00492                 write(idxfd-&gt;getFd(), &amp;outsize, 4);
00493                 <font class="keywordflow">if</font> (idxBytes) {
00494                         write(idxfd-&gt;getFd(), idxBytes, shiftSize);
00495                         <font class="keyword">delete</font> [] idxBytes;
00496                 }
00497         }
00498         <font class="keywordflow">else</font> {  <font class="comment">// delete entry</font>
00499                 <font class="keywordflow">if</font> (idxBytes) {
00500                         write(idxfd-&gt;getFd(), idxBytes+8, shiftSize-8);
00501                         lseek(idxfd-&gt;getFd(), -1, SEEK_CUR);    <font class="comment">// last valid byte</font>
00502                         FileMgr::systemFileMgr.trunc(idxfd);    <font class="comment">// truncate index</font>
00503                         <font class="keyword">delete</font> [] idxBytes;
00504                 }
00505         }
00506 
00507         <font class="keyword">delete</font> [] key;
00508         <font class="keyword">delete</font> [] outbuf;
00509         free(dbKey);
00510 }
00511 
00512 
00513 <font class="comment">/******************************************************************************</font>
00514 <font class="comment"> * RawLD::linkentry     - links one entry to another</font>
00515 <font class="comment"> *</font>
00516 <font class="comment"> * ENT: testmt  - testament to find (0 - Bible/module introduction)</font>
00517 <font class="comment"> *      destidxoff      - dest offset into .vss</font>
00518 <font class="comment"> *      srcidxoff               - source offset into .vss</font>
00519 <font class="comment"> */</font>
00520 
00521 <font class="keywordtype">void</font> RawStr4::linkentry(<font class="keyword">const</font> <font class="keywordtype">char</font> *destkey, <font class="keyword">const</font> <font class="keywordtype">char</font> *srckey) {
00522         <font class="keywordtype">char</font> *text = <font class="keyword">new</font> <font class="keywordtype">char</font> [ strlen(destkey) + 7 ];
00523         sprintf(text, <font class="stringliteral">"@LINK %s"</font>, destkey);
00524         settext(srckey, text);
00525         <font class="keyword">delete</font> [] text;
00526 }
00527 
00528 
00529 <font class="comment">/******************************************************************************</font>
00530 <font class="comment"> * RawLD::CreateModule  - Creates new module files</font>
00531 <font class="comment"> *</font>
00532 <font class="comment"> * ENT: path    - directory to store module files</font>
00533 <font class="comment"> * RET: error status</font>
00534 <font class="comment"> */</font>
00535 
00536 <font class="keywordtype">signed</font> <font class="keywordtype">char</font> RawStr4::createModule(<font class="keyword">const</font> <font class="keywordtype">char</font> *ipath)
00537 {
00538         <font class="keywordtype">char</font> *path = 0;
00539         <font class="keywordtype">char</font> *buf = <font class="keyword">new</font> <font class="keywordtype">char</font> [ strlen (ipath) + 20 ];
00540         FileDesc *fd, *fd2;
00541 
00542         stdstr(&amp;path, ipath);
00543 
00544         <font class="keywordflow">if</font> ((path[strlen(path)-1] == <font class="charliteral">'/'</font>) || (path[strlen(path)-1] == <font class="charliteral">'\\'</font>))
00545                 path[strlen(path)-1] = 0;
00546 
00547         sprintf(buf, <font class="stringliteral">"%s.dat"</font>, path);
00548         unlink(buf);
00549         fd = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
00550         fd-&gt;getFd();
00551         FileMgr::systemFileMgr.close(fd);
00552 
00553         sprintf(buf, <font class="stringliteral">"%s.idx"</font>, path);
00554         unlink(buf);
00555         fd2 = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
00556         fd2-&gt;getFd();
00557         FileMgr::systemFileMgr.close(fd2);
00558 
00559         <font class="keyword">delete</font> [] path;
00560         
00561         <font class="keywordflow">return</font> 0;
00562 }
</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>