*-------- metin in eki ------
lPARAMETERS xtable
LOCAL xrepair
xrepair=CREATEOBJECT("_repair")
xrepair.repairheaderandmemo(m.xtable)
*-------- end metin in eki ------
Define CLASS _repair AS custom
*-- Reads given size of bytes at offsett specified by pos from a file opened
Procedure readbytes
**************************************************************************
* ReadBytes(nHandle, nPos, nBytes[, lLR]) :
* Reads given size of bytes at offsett specified by pos from a file opened
* with low level functions (handle comes from that function)
* Parameters :
* nHandle : (Numeric) Handle of a file to read bytes from
* nPos : (Numeric) File offsett to start reading
* nBytes : (Numeric) Byte count to read
* (typically 1 for single byte, 2 for word and 4 for dword)
* lLR : (Logical)
* if .T. left-to-right format used
* (Default) if .F. right-to-left format used
* (more likely to be used with low level)
* EXAMPLE :
* ReadBytes(handle,12,2) - returns 62313 if bytes at offset 12 are ( 69 F3 )
* ReadBytes(handle,12,2,.T.) - returns 62313 if bytes at offset 12 are ( F3 69 )
* Remark :
* Doesn't save and restore current file pos. Caller should take care of it
**************************************************************************
Lparameters tnHandle, tnPos, tnSize, tlLR
Local lcString, lnRetValue,ix
=fseek(tnHandle, tnPos,0) && Go to pos
lcString = fread(tnHandle, tnSize) && Read tnSize bytes
lnRetValue = 0
For ix=0 to tnSize-1 && Convert to a number
lnRetValue = lnRetValue + asc(substr(lcString,ix+1)) * ;
iif(tlLR,256^(tnSize-1-ix),256^ix)
Endfor
Return int(lnRetValue)
Endproc
*-- Writes integer with given size of bytes at offsett specified by pos to a file opened
Procedure writebytes
**************************************************************************
* WriteBytes
* Same as ReadBytes except writes to file
* 64 bits not available in this version
**************************************************************************
Lparameters tnHandle, tnPos, tnSize, tnNumber, tlLR
Local lcString, lnLowDword, lnHighDword,ix
lcString=''
If tlLR
For ix=tnSize-1 to 0 step -1
lcString=lcString+chr(tnNumber/256^ix%256)
Endfor
Else
For ix=0 to tnSize-1
lcString=lcString+chr(tnNumber/256^ix%256)
Endfor
Endif
=fseek(tnHandle, tnPos,0) && Go to pos
Return fwrite(tnHandle,lcString)
Endproc
*-- Repairs the header of a table and its memo. Frees the table removing backlink info and clears out has CDX flag.
Procedure repairheaderandmemo
* RepairHeaderAndMemo
* Parameters
* tcDBF : Full path and name of dbf file to repair
* Remarks :
* Table is freed (backlink info cleared if exists)
* Has CDX flag is cleared && New copy has no CDX
Lparameters tcDBF
Local handle, lnFileSize, lnReccount, lnHeaderSize, lnRecordSize, ;
lnCalculatedReccount, llHasMemo
handle=fopen(tcDBF,12) && Opened readwrite
lnFileSize = fseek(handle,0,2) && Get file size
* Read header info
With this
lnReccount = .readbytes(handle, 4,4)
lnHeaderSize = .readbytes(handle, 8,2)
lnRecordSize = .readbytes(handle,10,2)
lnCalculatedReccount = floor((lnFileSize-(lnHeaderSize+1))/lnRecordSize)
*-------- metin in müdahalesi ------
if lnCalculatedReccount<0
lnCalculatedReccount=0
ENDIF
*-------- end metin in müdahalesi ------
If lnCalculatedReccount # lnReccount && If calculated # recorded fix it
.writebytes(handle, 4,4,lnCalculatedReccount)
Endif
.writebytes(handle, 28,1,0) && Clear has CDX, has memo, isDBC flags
=fseek(handle,lnHeaderSize-263 ,0) && Go backlink info start
=fwrite(handle,replicate(chr(0),263),263) && Clear backlink info and free table
llHasMemo=file(forceext(tcDBF,'FPT'))
If llHasMemo
.RepairMemo(forceext(tcDBF,'FPT'))
.writebytes(handle, 28,1,0x02) && Set has memo flag
Endif
Endwith
=fclose(handle)
Endproc
*-- Fixes next block pointer, blocksize and filesize
Procedure RepairMemo
* RepairMemo
* Simply fixes next block pointer, blocksize and filesize
Lparameters tcMemoFilename
Local handle, lnFileSize, lnNextBlockPointer, lnBlockSize, lnFirstBlock, lnCalculatedFileSize
handle=fopen(tcMemoFilename,12) && Opened readwrite
lnFileSize = fseek(handle,0,2) && Get file size
With this
* Read header info
lnNextBlockPointer = .readbytes(handle, 0,4,.T.) && Stored in left-to-right format
lnBlockSize = .readbytes(handle, 6,2,.T.) && Stored in left-to-right format
* Specific to me - no blocksize setting to something other than default 0x40
If lnBlockSize # 0x40
.writebytes(handle, 6,2,0x40,.T.)
lnBlockSize=0x40
Endif
*
lnFirstBlock = ceiling(512/lnBlockSize) && Possible min lnNextblockpointer
lnCalculatedFileSize = lnNextBlockPointer*lnBlockSize
* Fix if needs repair
If !(lnFileSize >= 512 ;
and lnNextBlockPointer >= lnFirstBlock ;
and lnCalculatedFileSize >= lnFileSize) && Memo needs repair
lnNextBlockPointer = max(lnNextBlockPointer, lnFirstBlock)
lnFileSize = lnNextBlockPointer * lnBlockSize
.writebytes(handle, 0,4,lnNextBlockPointer,.T.) && Fix next block pointer
=fchsize(handle, lnFileSize) && Fix filesize
Endif
Endwith
=fclose(handle)
Endproc
Enddefine