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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
/*
Copyright 1990-2008 Light Infocon Tecnologia S/A
Este arquivo é parte do programa LightBase - Banco de Dados Textual Documental
O LightBase é um software livre; você pode redistribui-lo e/ou modifica-lo dentro
dos termos da Licença Pública Geral GNU como publicada pela Fundação do Software
Livre (FSF); na versão 2 da Licença.
Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou APLICAÇÃO
EM PARTICULAR. Veja a Licença Pública Geral GNU para maiores detalhes.
Você deve ter recebido uma cópia da Licença Pública Geral GNU versao 2, sob o
título "LICENCA.txt", junto com este programa, se não, escreva para a Fundação do
Software Livre(FSF) Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
//
// Sun lumps all this stuff into unistd.h. I suspect nobody else will,
// so this might be better off as #ifdef AL_SUN4. We will see when
// we tackle the next UNIX system.
//
//
// void * ALFile::operator new( size_t size )
//
// ARGUMENTS:
//
// size : The number of bytes needed to create a new ALFile object.
//
// RETURNS
//
// A pointer to the newly allocated storage area, or 0 if no storage
// was available.
//
// DESCRIPTION
//
// When using a DLL, it is easy to get into a dangerous situation when
// creating objects whose ctor and dtor are both in the DLL. The problem
// arises because when you create an object using new, the memory for
// the object will be allocated from the EXE. However, when you destroy
// the object using delete, the memory is freed inside the DLL. Since
// the DLL doesn't really own that memory, bad things can happen.
//
// But, you say, won't the space just go back to the Windows heap regardless
// of who tries to free it? Maybe, but maybe not. If the DLL is using
// a subsegment allocation scheme, it might do some sort of local free
// before returning the space to the windows heap. That is the point where
// you could conceivably cook your heap.
//
// By providing our own version of operator new inside this class, we
// ensure that all memory allocation for the class will be done from
// inside the DLL, not the EXE calling the DLL.
//
// REVISION HISTORY
//
// May 26, 1994 1.0A : First release
//
void AL_DLL_FAR * AL_PROTO ALFile::operator new
//
// ALFile::ALFile( const char *file_name = "",
// int buffer_size = 4096,
// ALCase name_case = AL_LOWER)
//
// ARGUMENTS:
//
// file_name : The initial file name of the ALFile object you are
// creating. By default, this is a blank string, which
// will get converted to a temporary name before opening
// the actual disk file.
//
// buffer_size : The size of the object's I/O buffer. The default of 4096
// should give very good performance.
//
// name_case : This parameter determines whether the file names will
// always be converted to upper case, lower case, or left
// in mixed case. Under MS-DOS, you shouldn't use mixed
// case, because the O/S file naming convention is case
// insensitive. ArchiveLib will think "TEMP.BAK" and
// "temp.bak" are different, when they really aren't.
//
// RETURNS
//
// Nothing, this is a constructor.
//
// DESCRIPTION
//
// This constructor is used to create a new ALFile object, which will
// usually be treated as an ALStorage object by ArchiveLib functions.
// It is important to note that not much happens during construction of
// this object, the real activity happens after you call the Open()
// function. Just creating this object *does not* create a file on disk!
//
// REVISION HISTORY
//
// May 26, 1994 1.0A : First release
//
AL_PROTO :
//
// ALFile::~ALFile()
//
// ARGUMENTS:
//
// None.
//
// RETURNS
//
// Nothing.
//
// DESCRIPTION
//
// The destructor for an ALFile object doesn't have to do much work.
// The base class destructor will take care of freeing the I/O buffer,
// and any other loose ends. All we have to do here is make sure
// the file gets closed, and that its buffers get flushed to the disk
// file.
//
// Note that in debug mode, the destructor also checks this for the
// correct class type. This helps flag erroneous or duplicated
// destructor calls.
//
// REVISION HISTORY
//
// May 26, 1994 1.0A : First release
//
AL_PROTO ALFile::~
//
// int ALFile::LoadBuffer( long address )
//
// ARGUMENTS:
//
// address : The long offset into the physical storage object. A
// seek/read combination will be executed at this location,
// so that subsequent calls to read data will start at
// the given address.
//
// RETURNS
//
// AL_SUCCESS, AL_SEEK_ERROR, AL_END_OF_FILE, or possibly another
// error code < AL_SUCCESS.
//
// DESCRIPTION
//
// This function is used in the library whenever a byte needs to be read
// that isn't present in the current I/O buffer. It has to use
// the C RTL function lseek() to go to the correct position in the library.
// If that works, it uses the C RTL function read() to read in an I/O
// buffer full of data.
//
// After that operation is performed, muReadIndex is set to 0, indicating
// that the next read from the I/O buffer will take place at location 0.
// mlFilePointer is set to address plus the number of bytes read, so
// we know where the next read from the file will take place. And
// muBufferValidData is set to the count of bytes read in from this
// location. That lets us know how far we can read in the I/O buffer
// before we run out of space.
//
// Note that if CRC checking has been turned on, we will update the
// current working CRC value with the new data that has been read
// in from the buffer.
//
// REVISION HISTORY
//
// May 26, 1994 1.0A : First release
//
// July 7, 1994 1.0B : Added a very minor #ifdef for UNIX.
//
int AL_PROTO
//
// int ALFile::FlushBuffer()
//
// ARGUMENTS:
//
// None.
//
// RETURNS
//
// An integer status value, AL_SUCCESS, AL_WRITE_ERROR, or possibly some
// status code < AL_SUCCESS.
//
// DESCRIPTION
//
// This function is the counterpart to LoadBuffer(). It gets called
// when a write operation is poised to overflow the I/O buffer. This
// means we need to flush the buffer out to disk, then reset some
// data members.
//
// Unlike LoadBuffer(), this function doesn't have an address argument,
// so we don't have to perform a seek(). Instead, the data will be
// written out to the current position of the file pointer. If the
// write is successful, muWriteIndex is set to 0, indicating that the
// next write to the I/O buffer can go to position 0. mlFilePointer is
// incremented by the length of the write, so we know where the next read
// or write will occur. Finally, muBufferValidData is set to 0, indicating
// that there is no data in the I/O buffer that has been written, and
// there is no data that can be read.
//
// REVISION HISTORY
//
// May 26, 1994 1.0A : First release
//
//
// July 7, 1994 1.0B : Added a very minor #ifdef for UNIX.
//
int AL_PROTO
//
// int ALFile::Seek( long address )
//
// ARGUMENTS:
//
// address : The address in the physical disk to seek to.
//
// RETURNS
//
// AL_SUCCESS, AL_SEEK_ERROR, or possibly some other status code < AL_SUCCESS.
//
// DESCRIPTION
//
// This is a function the user can call to position the read/write pointer
// to a new location in the disk file. If there is any data that has been
// written to the I/O buffer, it gets flushed first. After that, we do
// a seek, and update mlFilePointer to reflect the new reality. Note that
// the other important data members will have been updated by FlushBuffer().
//
// And no, this guy doesn't do a LoadBuffer(). Which is fine if you are
// going to do a bunch of writes afterwards. If you are going to read data
// immediately after Seek(), you would have been better of calling
// LoadBuffer().
//
// REVISION HISTORY
//
// May 26, 1994 1.0A : First release
//
int AL_PROTO
//
// int ALFile::Open()
//
// ARGUMENTS:
//
// None.
//
// RETURNS
//
// AL_CANT_OPEN_FILE, AL_SUCCESS, or possibly some other error code
// < AL_SUCCESS.
//
// DESCRIPTION
//
// This is an important function, because it converts the ALFile
// object from a dinky little unimportant object, to a big massive
// thing that is ready to do serious work.
//
// The first thing we do here is see if we can open the file. We try
// to open it with READ/WRITE privileges, but we give up and drop back
// to READ only if that doesn't work out.
//
// We then call the base class ALStorage::Open() who takes care of
// allocating buffers and initializing data members.
//
// Finally, we have to get the protection attributes and time date
// stamps for the file. After those are stored off, the file is ready
// for abuse.
//
// REVISION HISTORY
//
// May 26, 1994 1.0A : First release
//
// July 7, 1994 1.0B : Several changes to this routine for UNIX. A couple
// of them are very minor #ifdefs to handle different
// values for constants. But there is still one giant
// chunk missing. If you look down a few lines, you
// will see that the code to get the file attributes
// has not been filled in yet.
//
int AL_PROTO
//
// void ALFile::MakeTempName( int i )
//
// ARGUMENTS:
//
// i : A numeric argument that can somehow be incorporated into
// the temporary file name. Create() will call this function
// while incrementing this number in an attempt to find a unique
// name.
//
// RETURNS
//
// Nothing.
//
// DESCRIPTION
//
// This function is called by Create() and other functions when they
// decide they need to cook up a temporary file name. The single parameter
// i is incremented by the calling program so that repeated calls should
// eventually produce a unique name.
//
// All this function does to create that unique name is perform a sprintf()
// into a buffer using a simple template. The result is copied into the
// mName member, and is ready to be tried out.
//
// REVISION HISTORY
//
// May 26, 1994 1.0A : First release
//
void AL_PROTO
//
// int ALFile::Create()
//
// ARGUMENTS:
//
// None.
//
// RETURNS
//
// AL_SUCCESS, AL_CANT_OPEN_FILE, or possibly some other error code
// < AL_SUCCESS.
//
// DESCRIPTION
//
// This function is used to create a new file storage object. Since
// we are creating a new object, we must be able to open it with read
// access. We will also rudely obliterate any existing file.
//
// The first thing we do here is call the base class Create() function.
// It takes care of setting up the I/O buffer and initializing the
// data members used to support the class.
//
// Next, function checks to see if we have a valid filename. If
// not, a search is made for a valid temporary file name. In either
// case, the file is then opened with R/W access, in O_CREAT
// mode, obliterating any existing file with the same name.
//
// Once the file is open, everything is ready to go, and you can write
// to the file at will. Don't expect much to happen if you try to
// read, however.
//
// REVISION HISTORY
//
// May 26, 1994 1.0A : First release
//
// July 7, 1994 1.0B : Added a very minor #ifdef to handle UNIX.
//
int AL_PROTO
//
// int ALFile::Close()
//
// ARGUMENTS:
//
// None.
//
// RETURNS
//
// Any status code, hopefully AL_SUCCESS.
//
// DESCRIPTION
//
// This function is called when you are done accessing a file, and want
// to free up its resources. The first thing it does is check to see
// if the file was ever actually opened. If it was, we flush the output
// buffer, then calculate and store the file length. Finally, we close
// the disk file, then call the base class Close() function to clean up
// the buffers and deal with other miscellaneous dirty work.
//
// REVISION HISTORY
//
// May 26, 1994 1.0A : First release
//
// July 7, 1994 1.0B : Sun UNIX doesn't have filelength, so I had to
// improvise. We could probably use the same
// code under DOS and Windows, and get rid of an
// #ifdef.
//
int AL_PROTO
//
// int ALFile::RenameToBackup( int delete_on_clash = 1 )
//
// ARGUMENTS:
//
// delete_on_clash : If this flag is set, it means that we will overwrite
// an existing file with this file if the names clash.
// For example, if I am renaming TEMP.DAT to TEMP.BAK,
// and a TEMP.BAK already exists, I will delete it
// before renaming if this arg is set.
//
// RETURNS
//
// AL_SUCCESS or AL_RENAME_ERROR.
//
// DESCRIPTION
//
// This function is a quick way to rename a storage object. The new
// name created is the default name, which usually means changing the
// file extension to ".BAK", from whatever it was.
//
// You don't see it here, but both the mName member and the physical file
// name are both updated. That all happens in the Rename() function.
//
// REVISION HISTORY
//
// May 26, 1994 1.0A : First release
//
int AL_PROTO
//
// int ALFile::Rename( const char *new_name /* = 0 */,
// int delete_on_clash /* = 1 */ )
//
// ARGUMENTS:
//
// new_name : A character pointer to a new file name. If a name is
// defined here, the file is renamed to this new value.
// If this value is 0, it means that we expect that
// the mName member has already been updated with a
// new name. In this case, the old name of the
// file is renamed to the new name.
//
// delete_on_clash : If this flag is set, it means that we will overwrite
// an existing file with this file if the names clash.
// For example, if I am renaming TEMP.DAT to TEMP.BAK,
// and a TEMP.BAK already exists, I will delete it
// before renaming if this arg is set.
//
// RETURNS
//
// AL_SUCCESS or AL_RENAME_ERROR.
//
// DESCRIPTION
//
// This virtual function provides a way to rename a storage object's
// physical implementation. It first updates the mName member if a
// new_name argument is provided. After that, we unlink the clash file
// if one exists, then do a simple rename of mName.mszOldName to
// new_name.
//
// Note that this function does a lot of error checking in debug mode.
// It also does a little error checking in release mode.
//
// REVISION HISTORY
//
// May 26, 1994 1.0A : First release
//
// July 7, 1994 1.0B : Sun UNIX doesn't have stricmp(), so I have to
// substitute strcasecmp().
//
int AL_PROTO
//
// int ALFile::UnRename( int delete_on_clash /* = 1 */ )
//
// ARGUMENTS:
//
// delete_on_clash : If this flag is set, it means that we will overwrite
// an existing file with this file if the names clash.
// For example, if I am renaming TEMP.BAK to TEMP.DAT,
// and a TEMP.DAT already exists, I will delete it
// before renaming if this arg is set.
//
// RETURNS
//
// AL_SUCCESS or AL_RENAME_ERROR.
//
// DESCRIPTION
//
// This virtual function provides a way to undo a previous renaming of
// a storage object's physical name. We can do this because the mName
// member of ALStorage keeps track both of the current name of the file,
// and the old name. In this case, we just rename the current name to
// the old name. Then we update the mName member so it is accurate.
//
// Note that this function does a lot of error checking in debug mode.
// It also does a little error checking in release mode.
//
// REVISION HISTORY
//
// May 26, 1994 1.0A : First release
//
int AL_PROTO
//
// int ALFile::Delete()
//
// ARGUMENTS:
//
// None.
//
// RETURNS
//
// AL_DELETE_ERROR or AL_SUCCESS.
//
// DESCRIPTION
//
// This function is called to delete the physical object associated with
// a file. This simply means calling the unlink() function for the
// given name.
//
// REVISION HISTORY
//
// May 26, 1994 1.0A : First release
//
int AL_PROTO