### Author Topic: PowerBasic to PureBasic (PDF-attachement)  (Read 3754 times)

0 Members and 1 Guest are viewing this topic.

#### Theo Gottwald

• Hero Member
• Posts: 1058
• User-Rate: +30/-4
• Gender:
##### PowerBasic to PureBasic (PDF-attachement)
« on: February 15, 2016, 10:55:28 AM »
Additionally some Sample code in PureBasic.

In PowerBasic, you could:

Mid\$(PART1\$, 4, 10) = PART2

In PureBasic, you can do the below. Note that specifying a "0" (zero) in the fourth parameter will cause all of B\$
that will fit into the remaining lengh of the string pointed to by A to be utilized. This routine could be
reworked as a funcion but I suspect the below is faster because of its use of pointers. This is for ASCII only.

Usage: INS(@PART1\$, PART2\$, 4, 10)

Code: [Select]
`Define.IProcedure INS(*A, B\$, C, D)E = MemoryStringLength(*A): F = Len(B\$)If E = 0 Or F = 0 Or C < 1: RaiseError(5): EndIfIf D < 1 Or F < D: D = F: EndIfE = E - C + 1If E < D: D = E: EndIfPokeS(*A + C - 1, B\$, D, #PB_String_NoZero)EndProcedure`

Code: [Select]
`Procedure.s x_mid(x.s,p.l,l.l,y.s)  x=Left(x,p-1)+y+Mid(x,p+l,Len(x))  ProcedureReturn xEndProcedure`
Code: [Select]
`Procedure MidSet(*PtrStr.l, DestPos.l, DestLen.l, ReplaceStr.s)     aMemPos.l=*PtrStr+DestPos-1      If DestLen>1       LastChar.b=PeekB(aMemPos+DestLen)       PokeS(aMemPos, ReplaceStr, DestLen)       PokeB(aMemPos+DestLen,LastChar)     Else       PokeB(aMemPos,PeekB(@ReplaceStr))     EndIf   EndProcedure `
or
Code: [Select]
`Procedure MidSet(*PtrStr.l, DestPos.l, DestLen.l, ReplaceStr.s)   aMemPos.l=*PtrStr+DestPos-1  StrLen.l=MemoryStringLength(*PtrStr)  If aMemPos>=*PtrStr       If aMemPos<*PtrStr+StrLen        If DestLen>Len(ReplaceStr)        DestLen=Len(ReplaceStr)      EndIf            If DestPos+DestLen>StrLen        DestLen=StrLen-DestPos+1      EndIf            If DestLen>1        LastChar.b=PeekB(aMemPos+DestLen)        PokeS(aMemPos, ReplaceStr, DestLen)        PokeB(aMemPos+DestLen,LastChar)      ElseIf DestLen=1        PokeB(aMemPos,PeekB(@ReplaceStr))      EndIf          EndIf     EndIf  EndProcedure ;---------------------------------------------strTest.s="Hello"MessageRequester("Before",strTest)MidSet(@strTest, 3, 2, "Booh")MessageRequester("After",strTest)`
or

Code: [Select]
`Procedure.s MidSet(string\$,position,length,ReplaceString\$); - Replace a part in the string and return the result  ProcedureReturn Left(string\$,position-1)+ReplaceString\$+Right(string\$,Len(string\$)-position-length+1)EndProcedureProcedure MidSetDirect(*string.BYTE,position,*ReplaceString.BYTE); - Replace a part in the string direct!  *string+(position-1)  While *ReplaceString\b    *string\b=*ReplaceString\b:*string+1:*ReplaceString+1  WendEndProcedure`
or optimized for speed:

Code: [Select]
`Procedure MidSet_Fast(*PtrStr.BYTE, DestPos.l, DestLen.l, *ReplaceStr.BYTE)   If DestLen>4    CopyMemory(*ReplaceStr,*PtrStr+DestPos-1,DestLen)  ElseIf DestLen=1    *PtrStr+DestPos-1     *PtrStr\b=*ReplaceStr\b   ElseIf DestLen=2    *PtrStr+DestPos-1     *PtrStr\b=*ReplaceStr\b     *PtrStr+1    *ReplaceStr+1    *PtrStr\b=*ReplaceStr\b   ElseIf DestLen=3    *PtrStr+DestPos-1     *PtrStr\b=*ReplaceStr\b     *PtrStr+1    *ReplaceStr+1    *PtrStr\b=*ReplaceStr\b     *PtrStr+1    *ReplaceStr+1    *PtrStr\b=*ReplaceStr\b   ElseIf DestLen=4    *PtrStr+DestPos-1     *PtrStr\b=*ReplaceStr\b     *PtrStr+1    *ReplaceStr+1    *PtrStr\b=*ReplaceStr\b     *PtrStr+1    *ReplaceStr+1    *PtrStr\b=*ReplaceStr\b     *PtrStr+1    *ReplaceStr+1    *PtrStr\b=*ReplaceStr\b   EndIfEndProcedure`
Code: [Select]
`Procedure MidSet_Fast(*PtrStr.BYTE, DestPos.l, DestLen.l, *ReplaceStr.BYTE)     If DestLen>1     *PtrStr+DestPos-1+DestLen    LastChar.b=*PtrStr\b    PokeS(*PtrStr-DestLen, PeekS(*ReplaceStr, DestLen))     *PtrStr\b=LastChar  Else     *PtrStr+DestPos-1    *PtrStr\b=*ReplaceStr\b  EndIf   EndProcedure`

String Replacement:

Code: [Select]
`Macro SRep(StringVar, Position, NewString, MaxChars = -1)  PokeS(@StringVar + ((Position)-1)*SizeOf(CHARACTER), (NewString), MaxChars, #PB_String_NoZero)EndMacrotest\$ = "Hello World!"Debug test\$SRep(test\$,  2, "ond")SRep(test\$,  8, "eir???", 3)Debug test\$`

Code: [Select]
`; that first one can only "mid" single char of string; problems will be if Pos set incorrectlyMacro PoorMidEmulation (pStr, Pos, Char)   CompilerIf #PB_Compiler_Unicode      PokeC(@pStr + (Pos - 1)  * 2, Asc(Char))   CompilerElse      PokeC(@pStr + (Pos - 1), Asc(Char))   CompilerEndIfEndMacro; that second is a bit improved, it can "mid" a whole part of string; again, problems will be if Pos set incorrectlyMacro PoorMidEmulation2 (pStr, Pos, Char)   CompilerIf #PB_Compiler_Unicode      CopyMemory(@Char, @pStr + (Pos - 1) * 2, Len(Char) * 2)   CompilerElse      CopyMemory(@Char, @pStr + (Pos - 1), Len(Char))   CompilerEndIfEndMacro;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Define.s TEST = "test"Debug TEST ; clear variablePoorMidEmulation(TEST, 1, "R")Debug TEST   ; mid = single charPoorMidEmulation2(TEST, 2, "ock")Debug TEST   ; mid = part of string`
TRIM\$ in PureBasic:

Code: [Select]
`EnableExplicit#WHITESPACE\$ = " " + #TAB\$ + #CRLF\$Procedure.s LTrimAny (source\$, charlist\$=#WHITESPACE\$)   ; removes from source\$ any leading character which is contained in charlist\$   Protected p.i, *s.Character      p = 1   *s = @source\$   While *s\c <> 0 And FindString(charlist\$, Chr(*s\c)) <> 0      p + 1      *s + SizeOf(Character)   Wend      ProcedureReturn Mid(source\$, p)EndProcedureProcedure.s RTrimAny (source\$, charlist\$=#WHITESPACE\$)   ; removes from source\$ any trailing character which is contained in charlist\$   Protected p.i, *s.Character      p = Len(source\$)   *s = @source\$ + (p-1) * SizeOf(Character)   While p >= 1 And FindString(charlist\$, Chr(*s\c)) <> 0      p - 1      *s - SizeOf(Character)   Wend      ProcedureReturn Left(source\$, p)EndProcedureMacro TrimAny (_source_, _charlist_=#WHITESPACE\$)   ; removes from source\$ any leading or trailing character which is contained in charlist\$   LTrimAny(RTrimAny(_source_, _charlist_), _charlist_)EndMacro; == Demo ==Macro ShowTrimming (_source_)   Debug "#" +          _source_  + "#"   Debug "#" + LTrimAny(_source_) + "#"   Debug "#" + RTrimAny(_source_) + "#"   Debug "#" + TrimAny (_source_) + "#"   Debug ""EndMacroShowTrimming("Hello")ShowTrimming(#TAB\$ + " Hello " + #TAB\$)ShowTrimming(#TAB\$ + "  " + #TAB\$)ShowTrimming("")`
In PowerBasic, you could:

A\$ = "C:\SOMEDIR\SOMEFILE.*": If Dir\$(A\$) <> "" Then KILL A\$

In PureBasic you can do the below.

Usage: A\$ = "C:\SOMEDIR\SOMEFILE.*": DeleteFile(A\$)

Code: [Select]
`Procedure DELETEFILE(PATH\$)FOLDER\$ = GetPathPart(PATH\$)MASK\$ = GetFilePart(PATH\$)DIRN = ExamineDirectory(#PB_Any, FOLDER\$, MASK\$)If DIRN = 0: ProcedureReturn: EndIfWhile NextDirectoryEntry(DIRN) > 0DeleteFile(FOLDER\$ + "\" + DirectoryEntryName(DIRN))WendFinishDirectory(DIRN)EndProcedure`
« Last Edit: February 15, 2016, 11:16:19 AM by Theo Gottwald »