; ; content: ; ; Macro _d(exp) : debug an expression's evaluation and the expression itself ; Macro quote(a) : embrace code by dqoutes ; Macro absM(exp) : abs() for integers ; Procedure.l absInt(val.l) : abs() for integers ; Macro bool(exp) : to-bool cast ; Macro dist(A,B) : abs of difference (distance) ; Macro if2(condition, A, B) : like an ?-operator for integer ; Macro StrIf( condition, sA, sB="") : like an ?-operator for strings ; Macro maxM(a,b) : retruns the greater ; Procedure max(a.l, b.l) : retruns the greater ; Macro isin(VAL,A,B) : if VAL is between A and B (or between B and A, inclusive) -> 1, else 0 ; Procedure.q valBin(s.s) : convert a binary string to integer ; Procedure valHex(string.s) : convert a hex string to integer ; Procedure.l initTime(ticks_per_second.l=1000) : init high resolution timing ; Procedure.l getTime() : get high resolution timing ; Procedure beep(nr.l=0) : 3 debugging sounds :) ; Procedure.s echo(s.s, n.l) : repeat a string n times ; Macro echoM(s, n) : repeat a string n times (single chars only) ; Procedure.s formatBin(n.l, seperator.s=" ", fill.l=#True, bytes.l=4) : convert a long value into a formatted binary string ; Procedure.s formatByteSize(n.q) : format 12345678 to "12.345.678" ; Procedure.s increaseFilename(file.s) : get a free filename not used in FS ; internals: ; ; Macro M_dquote ; Procedure if2proc(bool, A.l, B.l) ; Procedure.s StrIf_proc( condition.l, A.s, B.s) ; Define QPC_TICKS_PER_SECOND.l ; Define QPC_Time_freq.q ; Define QPC_Time_start.q ;XIncludeFile "E:\pbi\quote.pbi" ;{ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ E:\pbi\quote.pbi Macro M_dquote " EndMacro Macro quote(a) M_dquote#a#M_dquote EndMacro ; Debug quote(2+3+n*#PB_Any) ;} ; ________________________________/ [end] E:\pbi\quote.pbi ;XIncludeFile "E:\pbi\abs.pbi" ;{ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ E:\pbi\abs.pbi Macro absM(exp) (exp) - ( 0 Or ((exp)<0) ) * 2*(exp) EndMacro Procedure.l absInt(val.l) If val<0 ProcedureReturn -val Else ProcedureReturn val EndIf EndProcedure ; For i=0 To 20 ; r = Random(100)-50 ; Debug Str(r)+" "+Str(absM(r))+" "+Str(absint(r)) ; Next ;} ; ________________________________/ [end] E:\pbi\abs.pbi ;XIncludeFile "E:\pbi\beeps.pbi" ;{ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ E:\pbi\beeps.pbi Procedure beep(nr.l=0) Protected i.l Select nr Case 0;success For i=0 To 10 Beep_(1000+i*100,8) Next Case 1;failure For i=10 To 0 Step -1 Beep_(1000+i*100,8) Next Case 2;error Beep_( 79, 55 ) Beep_( 123, 55 ) Beep_( 79, 55 ) Beep_( 123, 55 ) EndSelect EndProcedure ; EXAMPLE ; ; beep() ; Delay(700) ; beep(1) ; Delay(700) ; beep(2) ;} ; ________________________________/ [end] E:\pbi\beeps.pbi ;XIncludeFile "E:\pbi\bool.pbi" ;{ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ E:\pbi\bool.pbi ; bool.pbi Macro bool(exp) (0 Or (exp)) EndMacro ; Debug "example" ; n=0 ; For i=0 To 20 ; Debug bool(i%4) ; Next ;} ; ________________________________/ [end] E:\pbi\bool.pbi ;XIncludeFile "E:\pbi\deb.pbi" ;{ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ E:\pbi\deb.pbi ; deb.pbi ; (debugging stuff) ;XIncludeFile "E:\pbi\M_quote.pbi" ;{ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ E:\pbi\M_quote.pbi ; [file already included] ;} ; ________________________________/ [end] E:\pbi\M_quote.pbi Macro _d(exp) Debug M_dquote#exp = M_dquote + Str(exp) EndMacro ; var=99 ; _d(var) ; _d(Pow(2,8)) ;} ; ________________________________/ [end] E:\pbi\deb.pbi ;XIncludeFile "E:\pbi\dist.pbi" ;{ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ E:\pbi\dist.pbi Macro dist(A,B) ( ((A)-(B)) + (0 Or (B)>(A))*-2*((A)-(B)) ) EndMacro ; Debug dist(8,8) ; 0 ; Debug dist( 4,6) ; 2 ; Debug dist(13,9) ; 4 ; Debug dist(-3,2) ; 5 ; Debug dist(3,-2) ; 5 ;} ; ________________________________/ [end] E:\pbi\dist.pbi ;XIncludeFile "E:\pbi\formatBin.pbi" ;{ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ E:\pbi\formatBin.pbi ; formatBin.pbi ; returns a formatted binary string ; 1.param: integer to be converted ; 2.param(opt.): seperator ; 3.param(opt.): use #False to suppress leading zeros ; 4.param(opt.): amount of bytes to pad leading zeros (default is 4 bytes = 32 bit) Procedure.s formatBin(n.l, seperator.s=" ", fill.l=#True, bytes.l=4) Protected ret.s Protected i.l Protected first.l Protected max=bytes*8-1 For i=max To 0 Step -1 If (n) & (1<5, 123, 321) ;} ; ________________________________/ [end] E:\pbi\if2.pbi ;XIncludeFile "E:\pbi\increaseFilename.pbi" ;{ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ E:\pbi\increaseFilename.pbi ; increaseFilename.pbi ; returns the first filename not used in ; file system, according to the sequence: ; ; filename.ext ; filename(2).ext ; filename(3).ext ; filename(4).ext ; filename(...).ext Procedure.s increaseFilename(file.s) Protected path.s Protected ext.s Protected fullname.s Protected name.s Protected i.l If FileSize(file)>=0 path=GetPathPart(file) fullname=GetFilePart(file) ext=GetExtensionPart(file) name=Left( fullname, Len(fullname)-Len(ext)-1 ) i=2 While FileSize( path + name + "(" + Str(i) + ")." + ext ) >= 0 i+1 Wend file= path + name + "(" + Str(i) + ")." + ext EndIf ProcedureReturn file EndProcedure ; ; ---- EXAMPLE - increaseFilename.pbi" ; datei$="C:\zzzzzzzz.txt" ; ; CreateFile(0, increaseFilename(datei$) ) ; CloseFile(0) ; ; CreateFile(0, increaseFilename(datei$) ) ; CloseFile(0) ; ; CreateFile(0, increaseFilename(datei$) ) ; CloseFile(0) ; ; CreateFile(0, increaseFilename(datei$) ) ; CloseFile(0) ; ; ---- ;} ; ________________________________/ [end] E:\pbi\increaseFilename.pbi ;XIncludeFile "E:\pbi\isin.pbi" ;{ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ E:\pbi\isin.pbi Macro isin(VAL,A,B) ( ( (A)<(B) And (VAL)<=(B) And (VAL)>=(A) ) Or ((A)>=(B) And (VAL)<=(A) And (VAL)>=(B)) ) EndMacro ; Debug isin( 3, -1, 6) ; Debug isin( -3, -1, 6) ; Debug isin( 20,-100, 6) ; Debug isin(-20, 100,-6) ; Debug isin( 20,-100,26) ; Debug isin( 3,3,3) ;} ; ________________________________/ [end] E:\pbi\isin.pbi ;XIncludeFile "E:\pbi\max.pbi" ;{ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ E:\pbi\max.pbi ; max.pbi Macro maxM(a,b) ( (0 Or (b)>(a)) * (b) + (Not (b)>(a)) * (a) ) EndMacro Procedure max(a.l, b.l) If a>b ProcedureReturn a Else ProcedureReturn b EndIf EndProcedure ; Debug "---- EXAMPLE - max.pbi" ; ; For i=0 To 5 ; a.l = Random(100)-50 ; b.l = (Random(100)-50) ; Debug Str(a)+" "+Str(b)+" --> " ; Debug Str(max(a,b)) ; Debug Str(maxM(a,b)) ; Debug "" ; Next ; Debug "----" ;} ; ________________________________/ [end] E:\pbi\max.pbi ;XIncludeFile "E:\pbi\echo.pbi" ;{ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ E:\pbi\echo.pbi ; echo.pbi ; to 'multiply' (repeat) a string ;as procedure Procedure.s echo(s.s, n.l) Protected ret.s While n>0 ret+s n-1 Wend ProcedureReturn ret EndProcedure ;as macro (only works with single chars) Macro echoM(s, n) LSet("",n,s) EndMacro ; Debug echo(":",20) + " EXAMPLE - mulStr.pbi" ; Debug echo("more ",3) ; Debug echo("Bla",2) ; Debug echo("~",20) ; Debug echoM("*",8) ;} ; ________________________________/ [end] E:\pbi\echo.pbi ;XIncludeFile "E:\pbi\strIf.pbi" ;{ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ E:\pbi\strIf.pbi ; strIf.pbi ; an ?-operator for stings; works like: ; expression ? string1 : string2 Macro StrIf( condition, sA, sB="") StrIf_proc( 0 Or (condition), sA, sB ) EndMacro Procedure.s StrIf_proc( condition.l, A.s, B.s) If condition ProcedureReturn A Else ProcedureReturn B EndIf EndProcedure ; Debug "---- EXAMPLE - StrIf.pbi" ; For i=0 To 10 ; r=Random(20)-10 ; Debug StrIf(r>0,"+") + Str(r) ; Next ; Debug "----" ; For i=0 To 10 ; r=Random(3) ; Debug StrIf(Not r,"no", Str(r)) + StrIf(r>1," seats", " seat") ; Next ; Debug "----" ;} ; ________________________________/ [end] E:\pbi\strIf.pbi ;XIncludeFile "E:\pbi\time.pbi" ;{ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ E:\pbi\time.pbi ; time.pbi ; (uses win-api) ; + thanks to Ligatur ; + pb-forum: http://www.purebasic.fr/german/viewtopic.php?p=140652#140652 Define QPC_TICKS_PER_SECOND.l Define QPC_Time_freq.q Define QPC_Time_start.q ; if an high-resolution performance counter is available ; the procedure initTime() returns 1, otherwise 0. ; it also sets the resolution for getTime() (default is 1000 ticks per second -> milliseconds) ; it also resets the clock for getTime() ; [it also gets the counter-frequency needed for internal calculation in getTime()] Procedure.l initTime(ticks_per_second.l=1000) Shared QPC_TICKS_PER_SECOND.l Shared QPC_Time_freq.q Shared QPC_Time_start.q If Not QueryPerformanceFrequency_(@QPC_Time_freq.q) ProcedureReturn 0 Else QueryPerformanceCounter_(@QPC_Time_start.q) QPC_TICKS_PER_SECOND.l = ticks_per_second ProcedureReturn 1 EndIf EndProcedure ; getTime() returns the amount of ticks elapsed ; since the last call of initTime() Procedure.l getTime() Shared QPC_TICKS_PER_SECOND.l Shared QPC_Time_freq.q Shared QPC_Time_start.q Static QPC_Time_now.q QueryPerformanceCounter_(@QPC_Time_now.q) ProcedureReturn (QPC_Time_now - QPC_Time_start) * QPC_TICKS_PER_SECOND / QPC_Time_freq EndProcedure ; Debug "---- EXMAPLE - time.pbi" ; If Not initTime() ; MessageRequester("error","no high-resolution performance counter available.") ; End ; EndIf ; ; initTime(10) ; now we get 10 ticks per second ; While tick < QPC_TICKS_PER_SECOND * 2 ; for two seconds ; tick=getTime() ; If tick<>tick_previous ; Debug Str(tick) ; tick_previous=tick ; EndIf ; Wend ; ; Debug "":Debug "########################":Debug "########################":Debug "" ; ; initTime(100000) ; now we get 100 ticks per millisecond ! ; tick=getTime() ; While tick < QPC_TICKS_PER_SECOND * 0.001 ; for one milliseconds ; tick=getTime() ; out$=Str(tick) + " ticks since initTime() [" + StrF(tick/QPC_TICKS_PER_SECOND, 8) + " seconds]" ; Debug out$ ; Wend ; Debug "----" ;} ; ________________________________/ [end] E:\pbi\time.pbi ;XIncludeFile "E:\pbi\valBin.pbi" ;{ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ E:\pbi\valBin.pbi ; valBin.pbi ; to get an integer value from a ; string containing "0", "1", and ; any seperators. ; only "0" and "1" are interpreted ; so you have to make shure yourself ; that your string makes (binary) sense. Procedure.q valBin(s.s) Protected ret.q Protected i.l Protected len.l=Len(s) For i=1 To len Select Mid( s,i,1 ) Case "1" ret<<1 ret+1 Case "0" ret<<1 EndSelect Next ProcedureReturn ret EndProcedure ; Debug "---- EXAMPLE - valBin.pbi" ; Debug valBin( "0") ; Debug valBin( "1") ; Debug valBin( "10") ; Debug valBin( "11") ; Debug valBin( "100") ; Debug valBin( "101") ; Debug valBin( "110") ; Debug valBin( "111") ; Debug valBin("1000") ; Debug "" ; Debug valBin("11111111000000001111111100000000") ; Debug %11111111000000001111111100000000 ; Debug "" ; Debug valBin("10000000 00000000 00000000 00000000") ; Debug %10000000000000000000000000000000 ; Debug "" ; Debug valBin("11111111_11111111_11111111_11111111") ; Debug %11111111111111111111111111111111 ; Debug "" ; Debug valBin(" -+- 11111111 -+- 11111111 -+- 11111111 -+- 11111111 -+- ") ; Debug %11111111111111111111111111111111 ; Debug "" ; Debug valBin("11100010=10000010=10101100") ; Debug %111000101000001010101100 ; Debug "----" ;} ; ________________________________/ [end] E:\pbi\valBin.pbi ;XIncludeFile "E:\pbi\valHex.pbi" ;{ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ E:\pbi\valHex.pbi ; valHex.pbi Procedure valHex(string.s) Protected ret.l Protected len.l = Len(string) Protected i.l Protected char Protected signif.l For i=0 To len-1 :;Debug Mid(string,len-i,1) char = Asc(UCase(Mid(string,len-i,1))) If (char>=48 And char<=57) char = char-48 ElseIf (char>=65 And char<=70) char = char-65 + 10 Else ;++++++++ Continue ;++++++++ EndIf ret | (char<<(4*(signif))) signif+1 Next ProcedureReturn ret EndProcedure ; ;EXAMPLE ; ; For i=0 To 20 ; r = Random($ffffff) ; Debug Hex(r) + " " + Str(r) + " " + Str( valHex( Hex(r))) ; Next ;} ; ________________________________/ [end] E:\pbi\valHex.pbi ; IDE Options = PureBasic 4.20 Beta 2 (Windows - x86) ; CursorPosition = 7 ; Folding = AAAAAAA- ; HideErrorLog ; CompileSourceDirectory