;XIncludeFile "F:\Programming\PureBasicCode\Includes\string.pbi" Structure MemoryArray Byte.c[0] word.w[0] EndStructure Structure HexBin StructureUnion word.w Str.s{2} EndStructureUnion EndStructure Structure StringBytes Bytes.b[64000] EndStructure ;======================================================================= Declare.s String(Char.s, Length.l) Declare.s Bin2Hex(*pdata, lLen.l) Declare.s PML_GetVar(PLMText.s, Var.s) Declare.s GetCMDParam(Param.s) Declare.l GetCMDSwitch(Switch.s) Declare.l Quicksearch(StartPos.l, *MainMem, MainLen.l, *FindMem, FindLen.l) Declare.s FormatStr(n.l, format.s) Declare.l Split(StringArray.s(1), Text2Split.s, Delim.s) ;return count Declare.l RevSearch(Start.l, MainStr.s, MatchStr.s) ;start = 0 = from end of file though Declare.s GetTaggedText(Start.l, Tag1.s, Tag2.s, MainText.s) Declare.s RemoveWhiteSpace(Text.s) Declare.s GetTagTillLineEnd(Start.l, Tag.s, Maintext.s) ;======================================================================= Procedure.s FormatStr(n.l, format.s) ;from fweil *MyString.StringBytes = @format ipt = Len(format) - 1 If n < 0 Sign = #True n = -n EndIf Repeat If n <> 0 d = n % 10 n = (n - d) / 10 If *MyString\Bytes[ipt] = '#' *MyString\Bytes[ipt] = d + 48 Else ipt - 1 *MyString\Bytes[ipt] = d + 48 EndIf Else If Sign *MyString\Bytes[ipt] = '-' Sign = #False Else *MyString\Bytes[ipt] = ' ' EndIf EndIf ipt - 1 Until ipt < 0 ProcedureReturn format EndProcedure ;Debug FormatString(6789, "###,###,###") Procedure.l Quicksearch(StartPos.l, *MainMem, MainLen.l, *FindMem, FindLen.l) *MainByteArray.MemoryArray = *MainMem *FindByteArray.MemoryArray = *FindMem ;StartPos =- 1 ; Build BadChr Array Dim BadChar.l(255) ; set all alphabet to max shift pos (length of find string plus 1) For i = 0 To 255 BadChar(i) = FindLen + 1 Next ;Update chars that are in the find string to their position from the end. For i = 0 To findlen -1 BadChar(*FindByteArray\byte[i]) = findlen - i Next MainArrayLoop.l = StartPos EndSearchPos.l = MainLen - (FindLen -1) While MainArrayLoop <= EndSearchPos If CompareMemory(*MainMem + MainArrayLoop, *FindMem, FindLen) = 1 FoundPos = MainArrayLoop + 1 Break EndIf ;Didn't find the string so shift as per the table. MainArrayLoop + BadChar(*MainByteArray.MemoryArray\byte[MainArrayLoop + FindLen]) Wend ProcedureReturn FoundPos EndProcedure ;======================================================================= Procedure.l RevSearch(Start.l, MainStr.s, MatchStr.s) ;start = 0 = from end of file though MainLen.l = Len(MainStr) MatchLen.l = Len(MatchStr) If start = 0 Start = MainLen - Matchlen EndIf For i = Start To 1 Step -1 If Mid(MainStr,i,Matchlen) = MatchStr ProcedureReturn i EndIf Next ;not found ProcedureReturn 0 EndProcedure ;======================================================================= Procedure.s GetTaggedText(Start.l, Tag1.s, Tag2.s, MainText.s) Tag1Pos = FindString(maintext, tag1, start) + Len(tag1) Tag2pos = FindString(maintext, tag2, Tag1pos) If FindString(maintext, tag1, start) = 0 Or Tag2Pos = 0 ProcedureReturn "" Else ProcedureReturn Mid(maintext, tag1pos, tag2pos-tag1pos) EndIf EndProcedure ;======================================================================= Procedure.s String(Char.s, Length.l) If Len(Char) = 1 TempStr.s = Space(Length) ReplaceString(TempStr," ",Char,2) ProcedureReturn TempStr Else ProcedureReturn "" EndIf EndProcedure ;======================================================================= Procedure.s GetTagTillLineEnd(Start.l, Tag.s, Maintext.s) TagPos = FindString(maintext, tag, start) + Len(tag) CRLFPos = FindString(maintext, #CRLF$, TagPos) If FindString(maintext, tag, start) = 0 ProcedureReturn "" Else ProcedureReturn Mid(maintext, tagpos, CRLFPos - tagpos) EndIf EndProcedure ;======================================================================= Procedure.s RemoveWhiteSpace(Text.s) Text = RemoveString(Text,#CRLF$) Text = RemoveString(Text,#TAB$) ProcedureReturn Trim(Text) EndProcedure ;======================================================================= Procedure.s Bin2Hex(*pdata, lLen.l) Dim Hexs.HexBin(255) ReturnBuffer.s = Space(lLen * 2) For I = 0 To 255 hexs(i)\Str = RSet(Hex(i),2,"0") ;Debug hexs(i)\byte Next *StrByteArray.MemoryArray = *pdata *HexByteArray.MemoryArray = @ReturnBuffer For i = 0 To lLen - 1 *HexByteArray\word[i] = hexs(*StrByteArray\byte[i])\word Next ProcedureReturn ReturnBuffer EndProcedure ;======================================================================= Procedure.s PML_GetVar(PLMText.s, Var.s) VarStart.l VarEnd.l VarLen.l VarLen = Len(Var) var = UCase(var) SearchText.s = UCase(PLMText) VarStart = FindString(SearchText, "<" + var + ">",1) + VarLen + 2 VarEnd = FindString(SearchText, "",1) ProcedureReturn Mid(PLMText,VarStart, VarEnd - VarStart) EndProcedure ;using exe commandline: -i infile -o outfile -switch (you could catch /switch too I suppose) ; Returns ; outfile ; infile ; 1 ; 0 ; Debug GetCMDParam("-O") ; Debug GetCMDParam("-i") ; Debug GetCMDSwitch("-switch") ; Debug GetCMDSwitch("-MissingSwitch") ;=========================================== Procedure.s GetCMDParam(Param.s) Param = LCase(Param) For i = 0 To CountProgramParameters() -1 If LCase(ProgramParameter(i)) = Param ProcedureReturn ProgramParameter(i+1) EndIf Next ProcedureReturn "" EndProcedure ;=========================================== Procedure.l GetCMDSwitch(Switch.s) Switch = LCase(Switch) For i = 0 To CountProgramParameters() -1 If LCase(ProgramParameter(i)) = Switch ProcedureReturn #True EndIf Next ProcedureReturn #False EndProcedure ;=========================================== Procedure.l Split(StringArray.s(1), Text2Split.s, Delim.s) ;return count FindLen.l = Len(Delim) MainLen.l = Len(Text2Split) StringArray.s(0) StringCount.l = 0 *MainByteArray.MemoryArray = @Text2Split ;*MainMem *FindByteArray.MemoryArray = @Delim ;*FindMem PrevPos.l = 1 ; Build BadChr Array Dim BadChar.l(255) ; set all alphabet to max shift pos (length of find string plus 1) For i = 0 To 255 BadChar(i) = FindLen + 1 Next ;Update chars that are in the find string to their position from the end. For i = 0 To findlen -1 BadChar(*FindByteArray\byte[i]) = findlen - i Next MainArrayLoop.l = 1 EndSearchPos.l = MainLen - (FindLen -1) While MainArrayLoop <= EndSearchPos If CompareMemory(@Text2Split + MainArrayLoop, @Delim, FindLen) = 1 FoundPos = MainArrayLoop + 1 ReDim StringArray.s(StringCount ) ;emulate fast mid() function inline, If Foundpos - PrevPos > 0 *RetMem = AllocateMemory(Foundpos - PrevPos) CopyMemory(@Text2Split + Prevpos -1, *RetMem, Foundpos - PrevPos) MidStr.s = PeekS(*RetMem,Foundpos - PrevPos) FreeMemory(*RetMem) Else MidStr.s = "" EndIf StringArray(StringCount) = MidStr StringCount = StringCount + 1 PrevPos = foundpos + Findlen EndIf ;Didn't find the string so shift as per the table. MainArrayLoop + BadChar(*MainByteArray.MemoryArray\byte[MainArrayLoop + FindLen]) Wend ;catch end ReDim StringArray.s(StringCount) StringArray(StringCount) = Mid(Text2Split, Prevpos, MainLen - PrevPos +1) StringCount = StringCount + 1 ReDim StringArray.s(StringCount) ProcedureReturn StringCount EndProcedure ; IDE Options = PureBasic 4.20 (Windows - x86) ; CursorPosition = 32 ; FirstLine = 15 ; Folding = A9-