comments
Uses: FileMap2.inc and FileMemMang2.inc
Allows dynamic string storage in a file.
Stored strings accessed with a handle.
Strings can be any length, nulls OK.
FileMemMang takes care of storage, getting and recovering file space is string deleted.
Uses: FileMap2.inc and FileMemMang2.inc
Allows dynamic string storage in a file.
Stored strings accessed with a handle.
Strings can be any length, nulls OK.
FileMemMang takes care of storage, getting and recovering file space is string deleted.
Code:
' PB 5/9 ' FStr16.inc ' ' File Based: Dynamic String Storage Manager ' ' - uses File Mapping for fast file In/Out ' ' - unlimited strings ' - unlimited string length ' - any kind of data - nulls OK ' ' handle returned when string stored ' use handle to retrieve OR remove string ' ' requires UDT File Memory Manager ' used to manage file storage ' #Include Once "C:\PB9\FileMapping\FileMemMang2.inc" ' $FStr16_Err_ModuleNotInitiated = "FStr16: module not initiated" $FStr16_Err_FileNotOpen = "FStr16: file not open" $FStr16_Err_NullString = "FStr16: null strings not stored" $FStr16_Err_NullHandle = "FStr16: null string handle" $FStr16_Err_NullPointer = "FStr16: null pointer" $FStr16_Err_MemAllocFailed = "FStr16: file memory allocation failed" ' Macro FStr16_ExitFalse(test, procedure, msg) If test Then Else #Debug Print FuncName$ +": "+ msg t.isErr = %TRUE t.errMsg = msg Exit procedure End If End Macro Macro FStr16_ExitTrue(test, procedure, msg) If test Then #Debug Print FuncName$ +": "+ msg t.isErr = %TRUE t.errMsg = msg Exit procedure End If End Macro Macro FStr16_GoFalse(test, MARKER, msg) If test Then Else #Debug Print FuncName$ +": "+ msg t.isErr = %TRUE t.errMsg = msg GoTo MARKER End If End Macro Macro FStr16_GoTrue(test, MARKER, msg) If test Then #Debug Print FuncName$ +": "+ msg t.isErr = %TRUE t.errMsg = msg GoTo MARKER End If End Macro ' ' %FStr16StrSize = 16 %FStr16FirstSize = 24 %FStr16NextSize = 20 ' Type FStr16FirstT 'first string segment Next As Long str As String * %FStr16StrSize Count As Long 'string len End Type Type FStr16NextT 'subsequent string segments Next As Long str As String * %FStr16StrSize End Type Type FStr16T pMem As FMemT Ptr isErr As Long errMsg As Asciiz * 256 End Type ' ' Sub FStr16_Initiate(t As FStr16T, FileMemManager As FMemT) 'File Memory Manager must be set before use ' File Memory Manager must have an open file before use ' all file In/Out handled by File Memory Manager t.isErr = %FALSE t.pMem = VarPtr(FileMemManager) End Sub ' Function FStr16_Put(t As FStr16T, ByRef s As String) As Long 'store string - return handle ' handle must be saved to access string 'Note: if s = "" then Method = False : null strings not stored ' Local strLen, strPos, hStr, hStr2, returnValue As Long ' t.isErr = %FALSE FStr16_ExitFalse(t.pMem, Function, $FStr16_Err_ModuleNotInitiated) FStr16_ExitFalse([EMAIL="[email protected]"][email protected][/EMAIL], Function, $FStr16_Err_FileNotOpen) FStr16_ExitTrue(s = "", Function, $FStr16_Err_NullString) ' strLen = Len(s) strPos = 1 hStr = FMem_Alloc([email protected], %FStr16FirstSize) : FStr16_ExitFalse(hStr, Function, $FStr16_Err_MemAllocFailed) returnValue = hStr 'return string's first block FStr16_First_SetCount t, hStr, strLen FStr16_First_SetStr t, hStr, Mid$(s, strPos, Min&(%FStr16StrSize, strLen)) FStr16_First_SetNext t, hStr, %NULL strLen -= %FStr16StrSize strPos += %FStr16StrSize If strLen > 0 Then hStr2 = FMem_Alloc([email protected], %FStr16NextSize) : FStr16_ExitFalse(hStr, Function, $FStr16_Err_MemAllocFailed) FStr16_Next_SetNext t, hStr2, %NULL FStr16_First_SetNext t, hStr, hStr2 hStr = hStr2 End If While strLen > 0 FStr16_Next_SetStr t, hStr, Mid$(s, strPos, Min&(%FStr16StrSize, strLen)) strLen -= %FStr16StrSize strPos += %FStr16StrSize If strLen > 0 Then hStr2 = FMem_Alloc([email protected], %FStr16NextSize) : FStr16_ExitFalse(hStr, Function, $FStr16_Err_MemAllocFailed) FStr16_Next_SetNext t, hStr2, %NULL FStr16_Next_SetNext t, hStr, hStr2 hStr = hStr2 End If Wend Function = returnValue End Function ' Function FStr16_Get(t As FStr16T, ByVal hStr As Long) As String 'get stored string Local strLen, totalStrLen As Long Local s As String ' t.isErr = %FALSE FStr16_ExitFalse(t.pMem, Function, $FStr16_Err_ModuleNotInitiated) FStr16_ExitFalse([EMAIL="[email protected]"][email protected][/EMAIL], Function, $FStr16_Err_FileNotOpen) FStr16_ExitFalse(hStr, Function, $FStr16_Err_NullHandle) ' strLen = FStr16_First_GetCount(t, hStr) : FStr16_ExitTrue(strLen < 1, Function, $FStr16_Err_NullString) totalStrLen = strLen s = FStr16_First_GetStr(t, hStr) hStr = FStr16_First_GetNext(t, hStr) strLen -= %FStr16StrSize While strLen > 0 FStr16_ExitFalse(hStr, Function, $FStr16_Err_NullHandle) s += FStr16_Next_GetStr(t, hStr) hStr = FStr16_Next_GetNext(t, hStr) strLen -= %FStr16StrSize Wend Function = Left$(s, totalStrLen) End Function ' Function FStr16_Remove(t As FStr16T, ByVal hStr As Long) As Long 'remove string from file ' hStr = FStr16_Remove(t, hStr) ' (should always null handles to deleted strings) Local hDel As Long ' t.isErr = %FALSE FStr16_ExitFalse(t.pMem, Function, $FStr16_Err_ModuleNotInitiated) FStr16_ExitFalse([EMAIL="[email protected]"][email protected][/EMAIL], Function, $FStr16_Err_FileNotOpen) FStr16_ExitFalse(hStr, Function, $FStr16_Err_NullHandle) ' hDel = hStr hStr = FStr16_First_GetNext(t, hDel) FMem_Free [email protected], hDel, %FStr16FirstSize While hStr hDel = hStr hStr = FStr16_Next_GetNext(t, hDel) FMem_Free [email protected], hDel, %FStr16NextSize Wend Function = %NULL End Function ' Function FStr16_IsErr(t As FStr16T) As Long 'True/False if last operation caused an error Function = t.isErr End Function ' Function FStr16_ErrMsg(t As FStr16T) As String 'get error message If t.isErr Then Function = t.errMsg End Function ' ' ----------------------------------- ' internal ' ----------------------------------- ' Sub FStr16_First_SetCount(t As FStr16T, ByVal hFirst As Long, ByVal Count As Long) Local pFirst As FStr16FirstT Ptr FStr16_ExitFalse(hFirst, Sub, $FStr16_Err_NullHandle) pFirst = FMem_Get([email protected], hFirst) : FStr16_ExitFalse(pFirst, Sub, $FStr16_Err_NullPointer) @pFirst.count = Count End Sub ' Function FStr16_First_GetCount(t As FStr16T, ByVal hFirst As Long) As Long Local pFirst As FStr16FirstT Ptr FStr16_ExitFalse(hFirst, Function, $FStr16_Err_NullHandle) pFirst = FMem_Get([email protected], hFirst) : FStr16_ExitFalse(pFirst, Function, $FStr16_Err_NullPointer) Function = @pFirst.count End Function ' Sub FStr16_First_SetNext(t As FStr16T, ByVal hFirst As Long, ByVal x As Long) Local pFirst As FStr16FirstT Ptr FStr16_ExitFalse(hFirst, Sub, $FStr16_Err_NullHandle) pFirst = FMem_Get([email protected], hFirst) : FStr16_ExitFalse(pFirst, Sub, $FStr16_Err_NullPointer) @pFirst.next = x End Sub ' Function FStr16_First_GetNext(t As FStr16T, ByVal hFirst As Long) As Long Local pFirst As FStr16FirstT Ptr FStr16_ExitFalse(hFirst, Function, $FStr16_Err_NullHandle) pFirst = FMem_Get([email protected], hFirst) : FStr16_ExitFalse(pFirst, Function, $FStr16_Err_NullPointer) Function = @pFirst.next End Function ' Sub FStr16_First_SetStr(t As FStr16T, ByVal hFirst As Long, ByRef s As String) Local pFirst As FStr16FirstT Ptr FStr16_ExitFalse(hFirst, Sub, $FStr16_Err_NullHandle) pFirst = FMem_Get([email protected], hFirst) : FStr16_ExitFalse(pFirst, Sub, $FStr16_Err_NullPointer) @pFirst.str = s End Sub ' Function FStr16_First_GetStr(t As FStr16T, ByVal hFirst As Long) As String Local pFirst As FStr16FirstT Ptr FStr16_ExitFalse(hFirst, Function, $FStr16_Err_NullHandle) pFirst = FMem_Get([email protected], hFirst) : FStr16_ExitFalse(pFirst, Function, $FStr16_Err_NullPointer) Function = @pFirst.str End Function ' Sub FStr16_Next_SetNext(t As FStr16T, ByVal hNext As Long, ByVal x As Long) Local pNext As FStr16NextT Ptr FStr16_ExitFalse(hNext, Sub, $FStr16_Err_NullHandle) pNext = FMem_Get([email protected], hNext) : FStr16_ExitFalse(pNext, Sub, $FStr16_Err_NullPointer) @pNext.next = x End Sub ' Function FStr16_Next_GetNext(t As FStr16T, ByVal hNext As Long) As Long Local pNext As FStr16NextT Ptr FStr16_ExitFalse(hNext, Function, $FStr16_Err_NullHandle) pNext = FMem_Get([email protected], hNext) : FStr16_ExitFalse(pNext, Function, $FStr16_Err_NullPointer) Function = @pNext.next End Function ' Sub FStr16_Next_SetStr(t As FStr16T, ByVal hNext As Long, ByRef s As String) Local pNext As FStr16NextT Ptr FStr16_ExitFalse(hNext, Sub, $FStr16_Err_NullHandle) pNext = FMem_Get([email protected], hNext) : FStr16_ExitFalse(pNext, Sub, $FStr16_Err_NullPointer) @pNext.str = s End Sub ' Function FStr16_Next_GetStr(t As FStr16T, ByVal hNext As Long) As String Local pNext As FStr16NextT Ptr FStr16_ExitFalse(hNext, Function, $FStr16_Err_NullHandle) pNext = FMem_Get([email protected], hNext) : FStr16_ExitFalse(pNext, Function, $FStr16_Err_NullPointer) Function = @pNext.str End Function '
Comment