home *** CD-ROM | disk | FTP | other *** search
-
- REM $INCLUDE: 'QBTREE.BI'
-
- DEFINT A-Z
-
- 'QBTree sample 1
- '-read data from an ASCII fixed-length record file
- '-create a QBTree data and index file
- '-count all records to measure key access time
- '-list all records just to look at them
-
- '31-Jul-91
- 'Cornel Huth
- 'C>bc XBTREE1/o;
- 'C>link /noe XBTREE1+nocom,XBTREE1.EXE,nul,QBTREE
- '
- 'C>XBTREE1 [/NL] [/DP]
- '/NL=no list all
- '/DP=do delete on every other key and record
-
- 'common data structure in both the raw and the QBTree file
- 'doing it this way makes it easier to assign one to the other
-
- TYPE CommonRecordTYPE
- partno AS STRING * 8
- desc AS STRING * 20
- cost AS STRING * 8
- pkgqty AS STRING * 2
- status AS STRING * 1
- END TYPE '39
-
- 'raw data file FIXED.DAT has this record layout
- 'raw data happens to be already sorted, matters not
-
- TYPE RawRecordTYPE
- info AS CommonRecordTYPE
- crlf AS STRING * 2
- END TYPE '41
- DIM RawRecord AS RawRecordTYPE
-
- 'QBTree translation of FIXED.DAT record layout and a temporary string buffer
-
- TYPE QBTRecordTYPE
- info AS CommonRecordTYPE
- END TYPE '39
- DIM QBTRecord AS QBTRecordTYPE
- DIM XferBuff AS STRING * 39
-
- DIM LowValue AS STRING * 2
- DIM HiValue AS STRING * 2
- LowValue = CHR$(0) + CHR$(0)
- HiValue = CHR$(255) + CHR$(255)
-
- cl$ = COMMAND$
-
- CLS
- nul = QBTreeVer(ver)
- PRINT "WELCOME TO QBTree"; ver; "doing a little work..."
-
- 'initialize QBTree to 1 key file, 1 data file
-
- stat = InitQBTREE(1, 1)
- IF stat THEN GOTO Abend
-
- 'create the data file (CATALOG.QBD)
- 'if it exists delete it
-
- dbfile$ = "catalog.qbd"
- IF FileExists(dbfile$ + CHR$(0)) = -1 THEN KILL dbfile$
- stat = CreateDataFile(dbfile$, 39)
- IF stat THEN GOTO Abend
-
- 'open the data file using QBTree file handle dbfile
- 'open for compatible mode read/write access
-
- dbfile = FreeDataFile: IF dbfile = -1 THEN GOTO Abend
- OpenMode = 2
- stat = OpenDataFile(dbfile$, dbfile, OpenMode)
- IF stat THEN GOTO Abend
-
- 'create the key file (CATALOG.QBX)
- 'if it exists delete it
-
- kyfile$ = "catalog.qbx"
- IF FileExists(kyfile$ + CHR$(0)) = -1 THEN KILL kyfile$
- stat = CreateKeyFile(kyfile$, 8 + 2)'+2 for the 16-bit enumerator
- IF stat THEN GOTO Abend
-
- 'open the key file using QBTree file handle kyfile
- 'open for compatible mode read/write access
-
- kyfile = FreeKeyFile: IF kyfile = -1 THEN GOTO Abend
- OpenMode = 2
- stat = OpenKeyFile(kyfile$, kyfile, OpenMode)
- IF stat THEN GOTO Abend
-
- 'setup pointer to QBTRecord and temporary transfer buffer
-
- vseg = VARSEG(QBTRecord)
- voff = VARPTR(QBTRecord)
- xferseg = VARSEG(XferBuff)
- xferoff = VARPTR(XferBuff)
-
- 'everything's setup to go
- 'we could use QBTree file I/O like ReadDevice(), DeleteFile(), etc., but
- 'for this example BASIC file I/O is used for simplicity
-
- rawfile$ = "xdata1.dat"
- rawfile = FREEFILE
- OPEN rawfile$ FOR BINARY AS rawfile
-
- 'read a fixed-length record from raw file and add it to the dbfile
-
- PRINT "Importing records from "; rawfile$; ". Creating QBTree data and index files."
- PRINT " Records added";
-
- 'preload first raw record
-
- s1! = TIMER
- GET rawfile, , RawRecord
- DO WHILE NOT EOF(rawfile)
-
- 'update QBTRecord only with the meaningful data in RawRecord
- 'transfer the data to a string that QBTree can use
- '(first transfered to a fixed-len string so that this code example)
- '(can be used in either QuickBASIC or BASIC PDS using far strings)
- 'write the QBTree record and key
-
- QBTRecord.info = RawRecord.info
- MemCopy vseg, voff, xferseg, xferoff, LEN(QBTRecord)
- Qrec$ = XferBuff
- Qkey$ = UCASE$(LEFT$(XferBuff, 8)) + LowValue
-
- stat = AddKeyRecord(kyfile, dbfile, Qkey$, Qrec$)
-
- 'if this key already exists handle it
-
- IF stat = 201 THEN
- stat = GetEqual(kyfile, dbfile, LEFT$(Qkey$, 8) + HiValue, Qrec$)
- stat = GetPrev(kyfile, dbfile, Qkey$, Qrec$)
- enum$ = RIGHT$(Qkey$, 2)
- enum$ = RIGHT$(enum$, 1) + LEFT$(enum$, 1)
- enum = CVI(enum$)
- enum = enum + 1
- enum$ = MKI$(enum)
- Qkey$ = LEFT$(Qkey$, 8) + RIGHT$(enum$, 1) + LEFT$(enum$, 1)
- stat = AddKeyRecord(kyfile, dbfile, Qkey$, Qrec$)
- END IF
-
- cnt& = cnt& + 1
- LOCATE , 15: PRINT cnt&;
- IF stat THEN EXIT DO
-
- 'load next raw record
-
- GET rawfile, , RawRecord
- LOOP
- e1! = TIMER
- CLOSE rawfile
- PRINT USING " (####.# secs)"; e1! - s1!
- IF stat THEN GOTO Abend
-
- 'delete every other record
-
- IF INSTR(cl$, "/DP") THEN
- PRINT "Deleting every other key and its data record."
- PRINT " Records deleted";
- cnt& = 0&
- stat = GetFirst(kyfile, dbfile, Qkey$, Qrec$) 'leave odd records
- DO UNTIL stat
- stat = GetNext(kyfile, dbfile, Qkey$, Qrec$)
- IF stat = 0 THEN
- stat = DeleteKeyRecord(kyfile, dbfile, Qkey$)
- cnt& = cnt& + 1
- LOCATE , 17: PRINT cnt&;
- IF stat = 0 THEN stat = GetNext(kyfile, dbfile, Qkey$, Qrec$)
- END IF
- LOOP
-
- 'stat=202 is normal in the case above, it means end of file reached
- IF stat = 202 THEN stat = 0
- PRINT
-
- END IF
-
- IF stat THEN GOTO Abend
-
- 'count all keys inorder, raw index speed--data file not accessed
-
- cnt& = 0&
- PRINT "Counting all keys."
- PRINT " Keys counted";
- s1! = TIMER
- stat = RetrieveFirst(kyfile, Qkey$, Qrecno&)
- DO UNTIL stat
- cnt& = cnt& + 1
- LOCATE , 17: PRINT cnt&;
- stat = RetrieveNext(kyfile, Qkey$, Qrecno&)
- LOOP
- e1! = TIMER
- PRINT USING " (####.# secs)"; e1! - s1!
-
- 'stat=202 is normal in the case above, it means end of file reached
- IF stat = 202 THEN stat = 0
- IF stat THEN GOTO Abend
-
- 'list all records of dbfile$ inorder by key
-
- IF INSTR(cl$, "/NL") = 0 THEN
- PRINT "Listing all records."
- PRINT "RECORD PARTNO-- DESC---------------- COST---- PKGQTY STATUS"
- use$ = " ##### \ \ \ \ \ \ \\ \\"
- VIEW PRINT CSRLIN TO 24
-
- stat = GetFirst(kyfile, dbfile, Qkey$, Qrec$)
- DO UNTIL stat
- nul = GetPosition(kyfile, recno&)
-
- 'copy the data record to the QBTRecord type
-
- XferBuff = Qrec$
- MemCopy xferseg, xferoff, vseg, voff, LEN(QBTRecord)
-
- PRINT USING use$; recno&; QBTRecord.info.partno; QBTRecord.info.desc; QBTRecord.info.cost; QBTRecord.info.pkgqty; QBTRecord.info.status
-
- stat = GetNext(kyfile, dbfile, Qkey$, Qrec$)
- LOOP
- VIEW PRINT
- LOCATE 24, 1
- PRINT "Done.";
-
- 'stat=202 is normal in the case above, it means end of file reached
- IF stat = 202 THEN stat = 0
- END IF
-
- Abend:
- IF stat THEN
- stat2 = GetXEInfo(class, action, locus)
- PRINT
- PRINT "I/O error"; stat; "occured ( extended info error:"; stat2;
- PRINT "class:"; class; "action:"; action; "locus:"; locus; ")"
- END IF
-
- 'no stat check on the QBTree closes though it would be better to do so
-
- stat = CloseDataFile(dbfile)
- stat = CloseKeyFile(kyfile)
- SYSTEM
-
-