Added Literals, Added growth procedure, Added error codes

Settled on an error-signaling logic, I'll use the Win32API one, and copy PureBasic's behaviour in its libs for the rest.
This commit is contained in:
2026-10-10 14:07:02 +02:00
parent 760e501f7c
commit ded9faba84
3 changed files with 213 additions and 5 deletions
+1 -1
View File
@@ -90,4 +90,4 @@ Procedure.b MsgPackFree(*MsgPackData.MsgPackData, FreeDataBuffer.b = #False)
EndIf
ProcedureReturn #True
EndProcedure
EndProcedure
+92 -4
View File
@@ -25,7 +25,19 @@
; ------------------------------------------------------------------------------
;- Compiler directive
;EnableExplicit
CompilerIf #PB_Compiler_IsMainFile
EnableExplicit
CompilerEndIf
; ------------------------------------------------------------------------------
;- Include Options
; Disables null checking on non-debug builds
CompilerIf Not Defined(MsgPack_DisableNullChecks, #PB_Constant)
#MsgPack_DisableNullChecks = #False
CompilerEndIf
@@ -116,14 +128,33 @@ Enumeration _MsgPack_FormatCodes
#MsgPack_FormatCode_Invalid = $C1
EndEnumeration
Enumeration _MsgPack_ErrorCodes
#MsgPack_Error_Success = 0
; Common errors
#MsgPack_Error_NullPointerGiven = 1
#MsgPack_Error_BufferTooSmall = 2
; Shouldn't re-use it.
#MsgPack_Error_FailedToGrowBuffer = 3
#MsgPack_Error_AtOrPastTheEndOfBuffer = 4
#MsgPack_Error_ReadPastEndOfBuffer = 5
#MsgPack_Error_InvalidFormatCode = 6
#MsgPack_Error_CannotGrowBufferDueToConfig = 100
EndEnumeration
; ------------------------------------------------------------------------------
;- Typedefs
Macro MsgPack_DataType : b : EndMacro
Macro MsgPack_DataType : a : EndMacro
Macro MsgPack_FormatCode : b : EndMacro
Macro MsgPack_FormatCode : a : EndMacro
Macro MsgPack_ErrorCode : u : EndMacro
@@ -141,7 +172,7 @@ Structure MsgPackData
BufferGrowthIncrements.i
; LastError code (Unused)
LastError.i
LastError.MsgPack_ErrorCode
; Pointer to a data buffer.
; Can be freed by the caller or this module.
@@ -160,3 +191,60 @@ EndMacro
Macro _MsgPackIsAtTheEnd(MsgPackWrapper)
(MsgPackWrapper\BufferOffset >= MsgPackWrapper\BufferSize)
EndMacro
; ------------------------------------------------------------------------------
;- Procedures
; Should be moved to allocators.
; But it would fuck up the splitting of responsibility.
; TODO: Add callback in the structure, maybe ?
Procedure.MsgPack_ErrorCode MsgPackGrow(*MsgPackData.MsgPackData, MinimalGrowthSize.i = -1)
Protected GrowthSize.i
Protected *NewBuffer
CompilerIf Not #MsgPack_DisableNullChecks
If Not *MsgPackData
DebuggerError("A #Null MsgPackData pointer was passed !")
ProcedureReturn #MsgPack_Error_NullPointerGiven
EndIf
CompilerEndIf
; Often set by the callers, but programmers are likely to use it too,
; so I'm leaving it here.
MsgPackData\LastError = #MsgPack_Error_Success
If *MsgPackData\BufferGrowthIncrements <= 0
DebuggerError("Cannot grow a buffer with no growth increments !")
MsgPackData\LastError = #MsgPack_Error_CannotGrowBufferDueToConfig
ProcedureReturn #MsgPack_Error_CannotGrowBufferDueToConfig
EndIf
; TODO: Improve logic later, i can't be bothered now
GrowthSize = *MsgPackData\BufferGrowthIncrements
If MinimalGrowthSize > GrowthSize
GrowthSize = ((MinimalGrowthSize + GrowthSize - 1) / GrowthSize) * GrowthSize
EndIf
*NewBuffer = ReAllocateMemory(*MsgPackData\Buffer, *MsgPackData\BufferSize + GrowthSize)
If Not *NewBuffer
DebuggerError("Failed to reallocate memory for buffer !")
MsgPackData\LastError = #MsgPack_Error_FailedToGrowBuffer
ProcedureReturn #MsgPack_Error_FailedToGrowBuffer
EndIf
*MsgPackData\Buffer = *NewBuffer
*MsgPackData\BufferSize + GrowthSize
ProcedureReturn #MsgPack_Error_Success
EndProcedure
; ------------------------------------------------------------------------------
;- Debugging
; TODO: Something for OutputDebugStringW on win32
+120
View File
@@ -0,0 +1,120 @@
;{- Code Header
; ==- Basic Info -================================
; Name: _Literals.pbi
; Version: 0.0.0
; Author: Herwin Bozet (NibblePoker)
;
; ==- Compatibility -=============================
; Tested compiler version:
; * //PureBasic 5.73 LTS (x86/x64)
; * //PureBasic 6.21 (x86/x64)
; * //PureBasic 6.21 C Backend (arm64)
;
; ==- Links & License -===========================
; License: CC0 1.0 Universal (Public Domain)
; GitHub: https://github.com/aziascreations/PB-MessagePack
;}
; ------------------------------------------------------------------------------
;- Compiler directive
XIncludeFile "./_Commons.pbi"
; ------------------------------------------------------------------------------
;- Procedures
;-> Getters
; Reads a boolean from the given data.
; Returns: The boolean value `#True` or `#False` as a `.b`
; OnError: Returns `#False` and sets the `MsgPackData\LastError` field.
; Will also raise a `DebuggerError`.
Procedure.b MsgPackReadBoolean(*MsgPackData.MsgPackData)
CompilerIf Not #MsgPack_DisableNullChecks
If Not *MsgPackData
DebuggerError("A #Null MsgPackData pointer was passed !")
ProcedureReturn #False
EndIf
CompilerEndIf
MsgPackData\LastError = #MsgPack_Error_Success
If _MsgPackIsAtTheEnd(*MsgPackData)
DebuggerError("End of buffer reached, cannot check data format code !")
MsgPackData\LastError = #MsgPack_Error_AtOrPastTheEndOfBuffer
ProcedureReturn #False
EndIf
If PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset) = #MsgPack_FormatCode_False
*MsgPackData\BufferOffset + 1
ProcedureReturn #False
ElseIf PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset) = #MsgPack_FormatCode_True
*MsgPackData\BufferOffset + 1
ProcedureReturn #True
Else
DebuggerError("The format code isn't the one for a boolean ! (" +
RSet(Hex(PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset)), 2, "0") +
" vs "+ RSet(Hex(#MsgPack_FormatCode_False), 2, "0") +
"/"+ RSet(Hex(#MsgPack_FormatCode_Else), 2, "0") + ")")
MsgPackData\LastError = #MsgPack_Error_InvalidFormatCode
ProcedureReturn #False
EndIf
EndProcedure
;-> Setters
;-> > Common
Procedure.MsgPack_ErrorCode _MsgPackWriteLiteral(*MsgPackData.MsgPackData, LiteralFormatCode.MsgPack_FormatCode)
CompilerIf Not #MsgPack_DisableNullChecks
If Not *MsgPackData
DebuggerError("A #Null MsgPackData pointer was passed !")
ProcedureReturn #MsgPack_Error_NullPointerGiven
EndIf
CompilerEndIf
MsgPackData\LastError = #MsgPack_Error_Success
If Not _MsgPackHasSpaceLeft(*MsgPackData, 1)
Protected GrowthErrorCode.MsgPack_ErrorCode
Debug("Growing buffer for literal with code 0x" + Hex(LiteralFormatCode) + "...")
GrowthErrorCode = MsgPackGrow(*MsgPackData, 1)
If GrowthErrorCode <> #MsgPack_Error_Success
; The structure's error code field is set by the called function !
ProcedureReturn GrowthErrorCode
EndIf
EndIf
PokeA(*MsgPackData\Buffer + *MsgPackData\BufferOffset, LiteralFormatCode)
*MsgPackData\BufferOffset + 1
ProcedureReturn #MsgPack_Error_Success
EndProcedure
;-> > Utility
; Procedure.b MsgPackWriteBoolean(*MsgPackData.MsgPackData, Boolean)
; If Boolean
; ProcedureReturn MsgPackWriteTrue(*MsgPackData)
; Else
; ProcedureReturn MsgPackWriteFalse(*MsgPackData)
; EndIf
; EndProcedure
;-> Macro aliases
Macro MsgPackWriteNull(MsgPackData) : _MsgPackWriteLiteral(MsgPackData, #MsgPack_FormatCode_Null) : EndMacro
Macro MsgPackWriteTrue(MsgPackData) : _MsgPackWriteLiteral(MsgPackData, #MsgPack_FormatCode_True) : EndMacro
Macro MsgPackWriteFalse(MsgPackData) : _MsgPackWriteLiteral(MsgPackData, #MsgPack_FormatCode_False) : EndMacro