Hollywood Code Snippets

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.
  1. Global base
  2. base = {}
You should also use the following code in your Hollywood projects as they can be very useful to you.
  1. Global application
  2. application = {}
  3.  
  4. application.debug = False
  5. application.debugtofile = False
  6. application.version = "1.0"
  7. application.platform = GetVersion().platform
  8. Local t = GetFileAttributes("PROGDIR:TheNameOfYourExecutableProgram")
  9. 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.
  10. application.screenwidth = GetAttribute(#DISPLAY, 0, #ATTRHOSTWIDTH)
  11. application.screenheight = GetAttribute(#DISPLAY, 0, #ATTRHOSTHEIGHT)
  1. ; Convert seconds into Hours, Minutes and Seconds
  2. Function base:SecsToHMS(s)
  3. Local h, m
  4.  
  5. h=Int(s/3600)
  6. s=s-(h*3600)
  7. m=Int(s/60)
  8. s=Int(s-(m*60))
  9.  
  10. Return(h, m, s)
  11. EndFunction
  1. ; Convert a given filesize in bytes into written notation eg; 1.1 TB, 10.5 GB, 11 MB, 309 KB, 90 bytes
  2. Function base:ConvertBytes(filesize)
  3. Local bytename
  4.  
  5. If filesize >= 1099511627776 ; greater than a tetrabyte
  6. filesize = filesize / 1099511627776
  7. filesize = MidStr(ToString(filesize),0,FindStr(ToString(filesize),".")+3)
  8. bytename = "TB"
  9.  
  10. ElseIf filesize >= 1073741824 ; greater than a gigabyte
  11. filesize = filesize / 1073741824
  12. filesize = MidStr(ToString(filesize),0,FindStr(ToString(filesize),".")+3)
  13. bytename = "GB"
  14.  
  15. ElseIf filesize >= 1048576 ; greater than a megabyte
  16. filesize = filesize / 1048576
  17. filesize = MidStr(ToString(filesize),0,FindStr(ToString(filesize),".")+3)
  18. bytename = "MB"
  19.  
  20. ElseIf filesize >= 1024 ; greater than a kilobyte
  21. filesize = filesize / 1024
  22. filesize = MidStr(ToString(filesize),0,FindStr(ToString(filesize),".")+3)
  23. bytename = "KB"
  24.  
  25. Else
  26. bytename = "bytes"
  27.  
  28. EndIf
  29.  
  30. return(filesize, bytename)
  31. 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.
  1. Function base:SystemRequest(title,body,gadgets,icon)
  2. Local res = SystemRequest(base:StripUnderlineFormatting(title),base:StripUnderlineFormatting(body),base:StripUnderlineFormatting(gadgets),icon)
  3. Return (res)
  4. EndFunction
  1. ;; base:StripUnderlineFormatting(gadgets$)
  2. ; Removes hollywood underline formatting from a string
  3. Function base:StripUnderlineFormatting(gadgets$)
  4. gadgets$ = ToString(gadgets$)
  5. ; please remove all spaces from the [ u ] and [ / u] searchstring, this was
  6. ; add so the forum could display the strings correctly
  7. If FindStr(gadgets$," [ u ]") > -1
  8. gadgets$ = ReplaceStr(gadgets$," [ u ]","")
  9. EndIf
  10.  
  11. If FindStr(gadgets$,"[ / u ]") > -1
  12. gadgets$ = ReplaceStr(gadgets$,"[ / u]","")
  13. EndIf
  14.  
  15. Return (gadgets$)
  16. 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.
  1. Function base:CheckLibrary(library$,warn,appquit)
  2. If FindStr(library$,".library") = -1
  3. library$ = library$ .. ".library"
  4. EndIf
  5.  
  6. If Exists("Libs:" .. library$) = True
  7. Return (True)
  8. Else
  9. If warn = True
  10. base:SystemRequest(applet_locale[6],applet_locale[3] .. " " .. library$,applet_locale[8],#REQICON_WARNING)
  11. EndIf
  12. If appquit = True
  13. End()
  14. Else
  15. Return (False)
  16. EndIf
  17. EndIf
  18. 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.
  1. Global application
  2. application = {}
  3. application.debug = True
  4. application.debugtofile = True
  5.  
  6. Function base:DebugPrint(message)
  7. If application.debug = True
  8. If application.debugtofile = False ; send to console window
  9. debugprint(message)
  10.  
  11. Else ; send to log file
  12. OpenFile(9,"T:yourprogram_debug",#MODE_READWRITE)
  13. Seek(9,#EOF)
  14. WriteLine(9,message)
  15. CloseFile(9)
  16. EndIf
  17. EndIf
  18. EndFunction
  19.  
  20. 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.
  1. Function base:DeleteFile(filename$)
  2. Local err
  3. ExitOnError(False)
  4. If Exists(filename$) = True
  5. err = False
  6. DeleteFile(filename$)
  7. Else
  8. err = True
  9. EndIf
  10. ExitOnError(True)
  11.  
  12. Return (err)
  13. EndFunction
A replacement for IsOnline() making use of base:SystemRequest to warn the user if a connection isn't found.
  1. Global locale
  2. locale = {}
  3. locale[0]= "OK"
  4. locale[1] = "body text of the error message to display to the user"
  5.  
  6. ; This will detect if an internet connection is found and return true or false.
  7. ; It will also display a warning message, the argument required is for the window
  8. ; title of the dialog box.
  9. Function base:IsOnline(message)
  10. If IsOnline() = True
  11. Return(True)
  12. Else
  13. base:SystemRequest(locale[message],locale[0],locale[1],#REQICON_INFORMATION)
  14. Return(False)
  15. EndIf
  16. 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.
  1. Global locale
  2. locale = {}
  3. locale[4] = "OK"
  4.  
  5. ; Download a file
  6. Function base:DownloadFile(url, savefile, callfunction, userdata)
  7. ExitOnError(False)
  8. Local connected, status, handle, count
  9.  
  10. Local online = base:IsOnline(122)
  11.  
  12. If online = True
  13. connected = True
  14. If callfunction <> nil
  15. handle, count = DownloadFile(url, {File = savefile, Fail404 = True, SilentFail = True}, callfunction, userdata )
  16. Else
  17. handle, count = DownloadFile(url, {File = savefile, Fail404 = True, SilentFail = True} )
  18. EndIf
  19.  
  20. Local code = GetLastError()
  21.  
  22. ; 404 File Not Found or other error in downloading file
  23. If code > 0 or count = -1
  24. status = False
  25. If code > 0
  26. base:SystemRequest(locale[122],locale[800],locale[4],#REQICON_WARNING)
  27. Else
  28. base:SystemRequest(locale[122],locale[344],locale[4],#REQICON_WARNING)
  29. EndIf
  30. Else
  31. status = True
  32. EndIf
  33. Else
  34. connected = False
  35. status = False
  36. handle = ""
  37. count = 0
  38. EndIf
  39.  
  40. ExitOnError(True)
  41. Return (connected, status, handle, count)
  42. 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.
  1. ;; Escape characters
  2. Function base:EscapeCharsinArexxString(string)
  3. string = base:FindAndReplace(string,"'","''")
  4. string = base:FindAndReplace(string,"\"","''")
  5.  
  6. Return (string)
  7. EndFunction
Another useful gem:
  1. ; Searches a string for the first occurence of an underlined letter and returns the letter in question
  2. ; Used for labels and in keyboard shortcuts to make locale independence possible
  3. Function base:GetUnderlineChar(string$)
  4.  
  5. Local string$ = MidStr(string$,FindStr(string$,"[u]")+3,1)
  6.  
  7. Return (string$)
  8. EndFunction
Masking a string into asterisks.
  1. ;; base:Maskpassword(password$)
  2. Function base:Maskpassword(password$)
  3. Local mask = ""
  4. If password$ <> ""
  5. For Local a = 0 to StrLen(password$)-1
  6. mask = mask .. "*"
  7. Next
  8. EndIf
  9.  
  10. Return (mask)
  11. EndFunction
  1. ; Convert milliseconds into Days, Hours, Minutes and Seconds
  2. Function base:Milliseconds(milliseconds)
  3. Local d, h, m, s
  4.  
  5. milliseconds = ToNumber(milliseconds)
  6.  
  7. d = milliseconds / 86400000
  8. h = milliseconds / (1000*60*60)
  9. m = (milliseconds % (1000*60*60)) / (1000*60)
  10. s = ((milliseconds % (1000*60*60)) % (1000*60)) / 1000
  11.  
  12. If d > 1
  13. If h > 24
  14. h = h / 24
  15. EndIf
  16. EndIf
  17.  
  18. Return(d, h, m, s)
  19. EndFunction
  1. ; Round a float to a number of decimal places
  2. 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.

Tags: 

Blog post type: 

Comments

stephenix1015's picture
Any additional information is accessed using "Eproc" Interface functions with the Interface pointer being stored into r12 to execute.-Missed Fortune