cCMDL. Commandline Class.
Posted: Sat Nov 03, 2007 2:03 pm
Hi,
I made a little commandline tool-class for one of my projects.
With it, it is very easy to get/check arguments of the current commandline.
@Procedural developers
It is not necessary to start flamewars against sense of OOP
Best regards
Wolf
I made a little commandline tool-class for one of my projects.
With it, it is very easy to get/check arguments of the current commandline.
@Procedural developers
It is not necessary to start flamewars against sense of OOP

Code: Select all
; PureBasic-Lounge.de
; Author: Hroudtwolf
; Date: 03. November 2007
; OS: Windows, Linux, Mac
; Demo: Yes
Interface IcCMDL
IsArg (sKey.s)
GetArg.s (lIndex.l)
GetArgIndex (sKey.s)
CountArgs ()
Done ()
EndInterface
Structure cCMDL
*VTABLE
*ArgList
lArgs .l
EndStructure
Structure tCMD
sValue .s
lHash .l
EndStructure
; **************************************************************
; * Internal function
; **************************************************************
Procedure.l _cCMDL_Hash (sString.s)
Protected *Source .CHARACTER = @sString
Protected lHash .l = #Null
Protected lX .l = #Null
While *Source\c
lHash = (lHash << 4) + *Source\c
lX = lHash & $F0000000
*Source + SizeOf (CHARACTER)
Wend
ProcedureReturn lHash
EndProcedure
; **************************************************************
; * *Obj\IsArg (sKey.s)
; **************************************************************
; * Checks whether a key exist in commandline arguments.
; *
; * sKey - In Example "/debug".
; **************************************************************
; * Returns:
; * True/False
; **************************************************************
Procedure cCMDL_IsArg (*This.cCMDL , sKey.s)
Protected lCount .l
Protected lKeyHash .l = _cCMDL_Hash (sKey)
Protected *CurrentKey.tCMD
For lCount = 0 To *This\lArgs - 1
*CurrentKey = *This\ArgList + (lCount * SizeOf (tCMD))
If lKeyHash = *CurrentKey\lHash
ProcedureReturn #True
EndIf
Next lCount
ProcedureReturn #False
EndProcedure
; **************************************************************
; * *Obj\GetArg (lIndex.l)
; **************************************************************
; * Retrieves an argument from given index.
; *
; * lIndex - The index of the argument you want to get
; **************************************************************
; * Returns:
; * Argument string
; **************************************************************
Procedure.s cCMDL_GetArg (*This.cCMDL , lIndex.l)
Protected *CurrentKey.tCMD
If lIndex < 0 Or lIndex > *This\lArgs - 1
ProcedureReturn ""
EndIf
*CurrentKey = *This\ArgList + (lIndex * SizeOf (tCMD))
ProcedureReturn *CurrentKey\sValue
EndProcedure
; **************************************************************
; * *Obj\GetArgIndex (sKey.s)
; **************************************************************
; * Retrieves the index of a given argument key.
; *
; * sKey - In Example "/debug".
; **************************************************************
; * Returns:
; * LONG | Index number
; **************************************************************
Procedure cCMDL_GetArgIndex (*This.cCMDL , sKey.s)
Protected lCount .l
Protected lKeyHash .l = _cCMDL_Hash (sKey)
Protected *CurrentKey.tCMD
For lCount = 0 To *This\lArgs - 1
*CurrentKey = *This\ArgList + (lCount * SizeOf (tCMD))
If lKeyHash = *CurrentKey\lHash
ProcedureReturn lCount
EndIf
Next lCount
ProcedureReturn -1
EndProcedure
; **************************************************************
; * *Obj\CountArgs ()
; **************************************************************
; * Retrieves the number if commandline arguments
; *
; **************************************************************
; * Returns:
; * LONG
; **************************************************************
Procedure cCMDL_CountArgs (*This.cCMDL)
ProcedureReturn *This\lArgs
EndProcedure
; **************************************************************
; * *Obj\Done ()
; **************************************************************
; * Releases the whole object.
; *
; **************************************************************
; * Returns:
; * null
; **************************************************************
Procedure cCMDL_Done (*This.cCMDL)
FreeMemory (*This\ArgList)
FreeMemory (*This)
ProcedureReturn #Null
EndProcedure
; **************************************************************
; * Constructor
; **************************************************************
ProcedureDLL CreateObject_cCMDL ()
Protected *This .cCMDL = AllocateMemory (SizeOf (cCMDL))
Protected lCountOf.l = CountProgramParameters ()
Protected lCount .l
Protected *TempLst.tCMD
Static *VTABLE_cCMDL
If Not lCountOf
FreeMemory (*This)
ProcedureReturn #Null
EndIf
If Not *VTABLE_cCMDL
*VTABLE_cCMDL = AllocateMemory (SizeOf (IcCMDL ))
If Not *VTABLE_cCMDL
ProcedureReturn #Null
EndIf
PokeL (*VTABLE_cCMDL + OffsetOf (IcCMDL\IsArg ()) , @cCMDL_IsArg ())
PokeL (*VTABLE_cCMDL + OffsetOf (IcCMDL\GetArg ()) , @cCMDL_GetArg ())
PokeL (*VTABLE_cCMDL + OffsetOf (IcCMDL\GetArgIndex ()) , @cCMDL_GetArgIndex ())
PokeL (*VTABLE_cCMDL + OffsetOf (IcCMDL\CountArgs ()) , @cCMDL_CountArgs ())
PokeL (*VTABLE_cCMDL + OffsetOf (IcCMDL\Done ()) , @cCMDL_Done ())
EndIf
*This\VTABLE = *VTABLE_cCMDL
For lCount = 0 To lCountOf - 1
If Not *TempLst
*This\ArgList = AllocateMemory (SizeOf (tCMD))
*TempLst = *This\ArgList
*TempLst\sValue = ProgramParameter (lCount)
*TempLst\lHash = _cCMDL_Hash (*TempLst\sValue)
Else
*TempLst = ReAllocateMemory (*This\ArgList , (lCount + 1) * SizeOf (tCMD))
If Not *TempLst
If *This\ArgList
FreeMemory (*This\ArgList)
EndIf
FreeMemory (*This)
ProcedureReturn #Null
EndIf
*This\ArgList = *TempLst
*TempLst = *This\ArgList + (lCount * SizeOf (tCMD))
*TempLst\sValue = ProgramParameter (lCount)
*TempLst\lHash = _cCMDL_Hash (*TempLst\sValue)
EndIf
Next lCount
*This\lArgs = lCountOf
ProcedureReturn *This
EndProcedure
;-------------- Test -------------------------------------------------
; Put '/debug /load "myfile.txt"' to your commandline for example.
*test.IcCMDL = CreateObject_cCMDL ()
OpenConsole ()
If *test\IsArg ("/debug")
PrintN ("Debug mode: On")
Else
PrintN ("Debug mode: Off")
EndIf
If *test\IsArg ("/load")
lIndex = *test\GetArgIndex ("/load")
PrintN ("Loading file: " + *test\GetArg (lIndex + 1))
EndIf
Input ()
CloseConsole ()
Wolf