Hello again readers,
In this blog I present to you a few code snippets I have written in Hollywood for various projects that I have been involved in, you are free to use them in any of your own projects; all I ask is that perhaps you give me a mention in the credits of your program somewhere.
These snippets might be useful for adapting into other programming languages especially some of the more mundane functions such as base:SecsToHMS() which will be our first code snippet.
As with all these base:library snippets below your Hollywood program needs to declare the following somewhere in the top-level of your code before being called.
Global base base = {}
You should also use the following code in your Hollywood projects as they can be very useful to you.
Global application application = {} application.debug = False application.debugtofile = False application.version = "1.0" application.platform = GetVersion().platform Local t = GetFileAttributes("PROGDIR:TheNameOfYourExecutableProgram") application.path = PathPart(t.path) ; the full path to your executable, useful if you need to specify it in Execute() or Run()- better than forcing an unnecessary Assign: on the end-user. application.screenwidth = GetAttribute(#DISPLAY, 0, #ATTRHOSTWIDTH) application.screenheight = GetAttribute(#DISPLAY, 0, #ATTRHOSTHEIGHT)
; Convert seconds into Hours, Minutes and Seconds Function base:SecsToHMS(s) Local h, m h=Int(s/3600) s=s-(h*3600) m=Int(s/60) s=Int(s-(m*60)) Return(h, m, s) EndFunction
; Convert a given filesize in bytes into written notation eg; 1.1 TB, 10.5 GB, 11 MB, 309 KB, 90 bytes Function base:ConvertBytes(filesize) Local bytename If filesize >= 1099511627776 ; greater than a tetrabyte filesize = filesize / 1099511627776 filesize = MidStr(ToString(filesize),0,FindStr(ToString(filesize),".")+3) bytename = "TB" ElseIf filesize >= 1073741824 ; greater than a gigabyte filesize = filesize / 1073741824 filesize = MidStr(ToString(filesize),0,FindStr(ToString(filesize),".")+3) bytename = "GB" ElseIf filesize >= 1048576 ; greater than a megabyte filesize = filesize / 1048576 filesize = MidStr(ToString(filesize),0,FindStr(ToString(filesize),".")+3) bytename = "MB" ElseIf filesize >= 1024 ; greater than a kilobyte filesize = filesize / 1024 filesize = MidStr(ToString(filesize),0,FindStr(ToString(filesize),".")+3) bytename = "KB" Else bytename = "bytes" EndIf return(filesize, bytename) EndFunction
I use base:SystemRequest() in several of these snippets mentioned in this blog. This particular function strips away any Hollywood square bracket underlining you might supply it in the 'title' argument. Foreinstance this is useful if you want to have labelled buttons on-screen where one letter is underlined to provide keyboard support- you never know when you might have to show that label within a system requester at some point in the future.
Function base:SystemRequest(title,body,gadgets,icon) Local res = SystemRequest(base:StripUnderlineFormatting(title),base:StripUnderlineFormatting(body),base:StripUnderlineFormatting(gadgets),icon) Return (res) EndFunction
;; base:StripUnderlineFormatting(gadgets$) ; Removes hollywood underline formatting from a string Function base:StripUnderlineFormatting(gadgets$) gadgets$ = ToString(gadgets$) ; please remove all spaces from the [ u ] and [ / u] searchstring, this was ; add so the forum could display the strings correctly If FindStr(gadgets$," [ u ]") > -1 gadgets$ = ReplaceStr(gadgets$," [ u ]","") EndIf If FindStr(gadgets$,"[ / u ]") > -1 gadgets$ = ReplaceStr(gadgets$,"[ / u]","") EndIf Return (gadgets$) EndFunction
The base:CheckLibrary functions accepts 3 arguments, the name of the AmigaOS library to check exists, if it doesn't exist you can warn the user by setting 'warn' to True and/or you can set 'appquit' to True if you want the application to quit as a result of the library not being found.
Function base:CheckLibrary(library$,warn,appquit) If FindStr(library$,".library") = -1 library$ = library$ .. ".library" EndIf If Exists("Libs:" .. library$) = True Return (True) Else If warn = True base:SystemRequest(applet_locale[6],applet_locale[3] .. " " .. library$,applet_locale[8],#REQICON_WARNING) EndIf If appquit = True End() Else Return (False) EndIf EndIf EndFunction
I even have a base: function for the standard DebugPrint command with added support for writing debug information to disk so you can review it later.
Global application application = {} application.debug = True application.debugtofile = True Function base:DebugPrint(message) If application.debug = True If application.debugtofile = False ; send to console window debugprint(message) Else ; send to log file OpenFile(9,"T:yourprogram_debug",#MODE_READWRITE) Seek(9,#EOF) WriteLine(9,message) CloseFile(9) EndIf EndIf EndFunction base:DebugPrint("Hello World!")
Deleting a file can be a common practise in any program, this code snippet is a replacement for the standard DeleteFile() command. This will check the file exists first before attempting to delete it. Just because a file exists doesn't always mean that your program can just go ahead and delete it, sometimes the file might be locked. Normally if you try and delete a locked file your program will exit complaining. base:DeleteFile will ensure that even if the file is locked your program will still continue. However please bare in mind if the file is locked, it will still remain on disk.
Function base:DeleteFile(filename$) Local err ExitOnError(False) If Exists(filename$) = True err = False DeleteFile(filename$) Else err = True EndIf ExitOnError(True) Return (err) EndFunction
A replacement for IsOnline() making use of base:SystemRequest to warn the user if a connection isn't found.
Global locale locale = {} locale[0]= "OK" locale[1] = "body text of the error message to display to the user" ; This will detect if an internet connection is found and return true or false. ; It will also display a warning message, the argument required is for the window ; title of the dialog box. Function base:IsOnline(message) If IsOnline() = True Return(True) Else base:SystemRequest(locale[message],locale[0],locale[1],#REQICON_INFORMATION) Return(False) EndIf EndFunction
How about an improved DownloadFile() base:function?
This one will not only download the file for you, but will also check the important stuff like:
This base: function is also dual-purpose, you can download the file with or without a callback function (for showing or not showing a progress bar).
Simply replace the locale[] values with your own.
Global locale locale = {} locale[4] = "OK" ; Download a file Function base:DownloadFile(url, savefile, callfunction, userdata) ExitOnError(False) Local connected, status, handle, count Local online = base:IsOnline(122) If online = True connected = True If callfunction <> nil handle, count = DownloadFile(url, {File = savefile, Fail404 = True, SilentFail = True}, callfunction, userdata ) Else handle, count = DownloadFile(url, {File = savefile, Fail404 = True, SilentFail = True} ) EndIf Local code = GetLastError() ; 404 File Not Found or other error in downloading file If code > 0 or count = -1 status = False If code > 0 base:SystemRequest(locale[122],locale[800],locale[4],#REQICON_WARNING) Else base:SystemRequest(locale[122],locale[344],locale[4],#REQICON_WARNING) EndIf Else status = True EndIf Else connected = False status = False handle = "" count = 0 EndIf ExitOnError(True) Return (connected, status, handle, count) EndFunction
Sometimes issuing a string to Arexx can be a bit fiddly when it comes to single quotation marks, this base: function can make it somewhat easier.
;; Escape characters Function base:EscapeCharsinArexxString(string) string = base:FindAndReplace(string,"'","''") string = base:FindAndReplace(string,"\"","''") Return (string) EndFunction
Another useful gem:
; Searches a string for the first occurence of an underlined letter and returns the letter in question ; Used for labels and in keyboard shortcuts to make locale independence possible Function base:GetUnderlineChar(string$) Local string$ = MidStr(string$,FindStr(string$,"[u]")+3,1) Return (string$) EndFunction
Masking a string into asterisks.
;; base:Maskpassword(password$) Function base:Maskpassword(password$) Local mask = "" If password$ <> "" For Local a = 0 to StrLen(password$)-1 mask = mask .. "*" Next EndIf Return (mask) EndFunction
; Convert milliseconds into Days, Hours, Minutes and Seconds Function base:Milliseconds(milliseconds) Local d, h, m, s milliseconds = ToNumber(milliseconds) d = milliseconds / 86400000 h = milliseconds / (1000*60*60) m = (milliseconds % (1000*60*60)) / (1000*60) s = ((milliseconds % (1000*60*60)) % (1000*60)) / 1000 If d > 1 If h > 24 h = h / 24 EndIf EndIf Return(d, h, m, s) EndFunction
; Round a float to a number of decimal places Function base:RoundTo(value, DecimalPlaces) Return(Round(value*10^DecimalPlaces)/10^DecimalPlaces) EndFunction
This is just the tip of the iceberg. If you want to see some more, including function to develop AmigaOS-like applications complete with buttons, icon bars, option boxes and other event handling, please download the following archive from OS4Depot:
http://www.os4depot.net/share/development/example/hw_app_template.lha
The archive is a little out of date, but contains a whole host of base functions that you might want to explore and make some use of.
Thanks for reading.
Comments
Submitted by stephenix1015 (not verified) on
Any additional information is accessed using "Eproc" Interface functions with the Interface pointer being stored into r12 to execute.-Missed Fortune