
I'm interested in what other people have build up over time (and see what I can steal

To start, I've simply list a few procedures that I wrote once and (sometimes

So, what's your favourite subroutine that you keep using?
Code: Select all
Procedure.i x_nr() ; generates unique number
; Global x_nr.i, x_nr_n.i, x_nr_list()
;
; *** generates a new unique number
;
; in: - none
; retval: n - unused unique number
; out: x_nr - as x_retval
;
; pure sometimes uses 'numbers' to identify elements instead of windows handles
; these numbers are arbitrary and fully defined by the user, this can cause trouble
; when using multiple libraries or code from different people as they might be reusing
; these unique numbers, for this purpose, i've added a x_nr procedure that returns
; a new unique number on every call
;
; you decide yourself if you want to free them or not :-)
; i would suggest doing so if you don't 'recycle' them using x_freenr() because other procedures
; may end up with numbers that are 'too high' for the pb functions being called
;
If ListSize(x_nr_list()) > 0 ; which means there's some stuff on the list
x_nr = x_nr_list() ; then use that number
DeleteElement(x_nr_list()) ; and take it from the list
Else
x_nr = x_nr_n
x_nr_n = x_nr_n+1
EndIf
ProcedureReturn x_nr
;
EndProcedure
Procedure x_freenr(n.i) ; frees number generated by x_nr()
; Global x_nr.i, x_nr_n.i, x_nr_list()
;
; *** recycles unique numbers
;
; put this number into the 'free numbers' list so it can be reused by x_nr()
;
AddElement(x_nr_list())
x_nr_list()=n
;
EndProcedure
Code: Select all
;
; the ugly way
;
If a < b
c = b
Else
c = a
Endif
Code: Select all
;
; the *slightly* better way, IMHO etc. etc.
;
c = x_max(a,b)
Code: Select all
Procedure.i x_min(n1.i,n2.i) ; smallest of two vars
If n1 < n2
ProcedureReturn n1
Else
ProcedureReturn n2
EndIf
EndProcedure
Procedure.i x_max(n1.i,n2.i) ; largest of two vars
If n1 > n2
ProcedureReturn n1
Else
ProcedureReturn n2
EndIf
EndProcedure
Code: Select all
Global _regout
Macro regout(register)
!push register
!pop [v__regout]
!pushad
Debug _regout
!popad
EndMacro
; use
regout(esp)
Code: Select all
Global _DRAWING
Procedure Msg(T.S="",Title.S="")
If Title="" :Title="Stop !" :EndIf
Result = MessageRequester(Title,T+Chr(10)+Chr(10)+"Continue?",#PB_MessageRequester_YesNo)
If Result = #PB_MessageRequester_No ; else pressed Yes button (Result = 6)
End
EndIf
EndProcedure
Macro STOPDRAW :If _DRAWING :StopDrawing() :_DRAWING=0 :EndIf:EndMacro
Macro DraWin(Win=0)
STOPDRAW
_DRAWING=StartDrawing(WindowOutput(Win))
EndMacro
Macro DrawIMG(IMG)
STOPDRAW
_DRAWING=StartDrawing(ImageOutput(IMG))
EndMacro
Macro GadImg(ImGad=_ImGad,IMG=_Img)
STOPDRAW
SetGadgetState(ImGad,ImageID(IMG))
EndMacro
Macro Escape
If GetAsyncKeyState_(27)&$8000 : End : EndIf
EndMacro
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
OpenWindow(0, 100, 100,500,300 ,"")
DraWin()
Box(150,100,200,50,#green)
DrawText(150,170,"Box on window")
Msg("Drawing on Window"+#lf$+"next: drawing on Image")
CreateImage(0,WindowWidth(0),WindowHeight(0))
ImageGadget(0,0,0,0,0,0)
DrawIMG(0)
Box(150,100,200,50,#red)
DrawText(150,170,"Box on Image. <Escape> To Quit",#Yellow,0)
GadImg(0,0) ; put the image on ImageGadget
Repeat
Escape
EV=WaitWindowEvent()
Until EV=#PB_Event_CloseWindow
End
I think I should convert this little procedure to a macro, but macros and me don't always get along well
Code: Select all
Macro Max(A,B)
((Not A>B)*B)|((Not B>A)*A)
EndMacro
Macro Min(A,B)
((Not A<B)*B)|((Not B<A)*A)
EndMacro
Code: Select all
Procedure.s GetSelfPath()
Define.s ModulePath
ModulePath = Space(1024)
GetModuleFileName_(0, @ModulePath, 1024)
ProcedureReturn GetPathPart(Trim(ModulePath))
EndProcedure
Code: Select all
code removed pending upload somewhere - sorry - did not realize there was some sort of line count or size limit for this thread
... I would pare it like: c:\ -> c:\windows\ -> c:\windows\system32\ -> then I should be able to create a subfolder under system32 like this: x_createdirectory("c:\windows\system32\whatever")c:\
c:\windows\
c:\windows\system32\
Code: Select all
; modified version from IBSoftware (CodeArchiv)
; on vista and above check the Request for "User mode" or "Administrator mode" in compileroptions
; (no virtualisation!)
Procedure ForceDirectories(Dir.s)
Static tmpDir.s, Init
Protected result
If Len(Dir) = 0
ProcedureReturn #False
Else
If Not Init
tmpDir = Dir
Init = #True
EndIf
If (Right(Dir, 1) = "\")
Dir = Left(Dir, Len(Dir) - 1)
EndIf
If (Len(Dir) < 3) Or FileSize(Dir) = -2 Or GetPathPart(Dir) = Dir
If FileSize(tmpDir) = -2
result = #True
EndIf
tmpDir = "" : Init = #False
ProcedureReturn result
EndIf
ForceDirectories(GetPathPart(Dir))
ProcedureReturn CreateDirectory(Dir)
EndIf
EndProcedure