CONST FORCE_UPDATE = -1
DIM blockCount AS LONG, currentRecID AS LONG
DIM fs AS QFILESTREAM, __s AS STRING
SUB dbtCreate( dbtArch AS STRING)
DIM streamEx AS QMEMORYSTREAM
IF fs.OPEN(dbtArch, fmCreate) = FALSE THEN
PRINT "Create fail .. "+dbtArch
END
END IF
streamEx.writeNUM(1,4)
streamEx.size = 512
fs.CopyFrom(streamEx, 0)
fs.CLOSE
END SUB
SUB dbtClose
fs.CLOSE
blockCount = 0
__s = ""
END SUB
SUB dbtOpen(dbtArch AS STRING, mode AS INTEGER)
IF fs.OPEN(dbtArch, mode) = FALSE THEN
PRINT "It can't be open " + dbtArch
END
END IF
blockCount = fs.readNUM(4)
currentRecID = -1
END SUB
private SUB readRecord
Eom = 0
__s = ""
WHILE Eom=0
__s = __s + fs.readSTR(512)
Eom = INSTR(__s, CHR$(&H1A))
WEND
END SUB
FUNCTION MemoAppend(text AS STRING) AS INTEGER
DIM streamEx AS QMEMORYSTREAM
IF LEN(text) = 0 THEN
EXIT FUNCTION
END IF
fs.position = fs.size
IF blockCount = 1 THEN
streamEx.WriteNUM(&H001A, 2)
streamEx.size = 512
fs.copyFrom(streamEx, 0)
INC blockCount
ELSE
streamEx.size = blockCount*512 - fs.position
fs.copyFrom(streamEx, 0)
END IF
result = blockCount
fs.write(text)
fs.writeNUM(&H1A, 1)
INC blockCount, 1+LEN(text) / 512
fs.position = 0
fs.writeNUM(blockCount, 4)
END FUNCTION
FUNCTION Memoget(n AS LONG) AS STRING
IF n < 0 OR n > blockCount THEN
EXIT FUNCTION
END IF
currentRecID = n
fs.position = n*512
readRecord
result = LEFT$( __s, INSTR(__s, CHR$(&H1A))-1 )
END FUNCTION
FUNCTION memoUpdate(newtext AS STRING) AS LONG
n& = currentRecID
IF n& = -1 THEN
GOTO leave
END IF
l& = LEN(__s)
blocks% = l&/512
rest% = l& MOD 512
fs.position = n&*512
SELECT CASE LEN(newText)
CASE IS <= l&
fs.write(newText)
fs.writeNUM(&H1A, 1)
CASE ELSE
SELECT CASE 1+LEN(newtext) / 512
CASE blocks%
SELECT CASE LEN(newtext) MOD 512
CASE IS > 0
fs.write(newText)
fs.writeNUM(&H1A, 1)
CASE 0
n& = MemoAppend(newtext)
END SELECT
CASE ELSE
n& = MemoAppend(newtext)
END SELECT
END SELECT
currentRecID = -1
leave:
result = n&
END FUNCTION
|