{--------------------------------|--------------------------------------------------------------------|---------------------------------------------------------|------------------------}
{--------------------------------|--------------------------------------------------------------------|---------------------------------------------------------|------------------------}
{ }
{ GENERIC RESOURCES }
{ ````````````````````````````````` }
{ }
{ A Generic Utilities unit, for use by any program.
Copyright © by David Sinclair, 1990 2001.
I am releasing these units to the Pascal community. Feel free to use them in whole or part in your Pascal programs.
You are also welcome to modify these units to suit your needs. If you wish to re-distribute the sources with your changes,
please clearly indicate that you have changed them. In all cases, you must leave these comments and the copyright notice intact.
If you use a significant portion of these units, I would appreciate acknowledgement in your About dialog and/or documentation,
e.g. łDejal Generic Utilities copyright © by David Sinclair, 1990 - 2001.˛
Iąd appreciate it if you also e-mail me at if you find these units useful. If you have any questions about
these units, you can e-mail me at that address and I will do my best to help, time permitting. However, these units are provided
łas is˛ and I do not guarantee their reliablity or suitability for any particular purpose.
These units have been used extensively in my Dejal shareware and freeware products over the years. Most of the code was written
many years ago, and the code and style may not be optimal in all cases, but unless otherwise noted all routines have been used
in released software, so should work as described.
Please visit and try out Dejal QuickEncrypt and/or my other shareware products. If you want to show
your appreciation for these units financially, registrations for my shareware are always welcome! Or you can make a donation to
me via my online order form: .
I hope you find these units useful, and good luck in your Pascal endeavors!
- David Sinclair, Dejal
}
{ * * * }
{ UNIT HISTORY: }
{ }
{ Version: Start - finish dates: Comments / changes: }
{ }
{ 1.0: 8 April 1991 Moved resReallyBadError & resInit from }
{ genDebug, moved resStrListLength here }
{ from genStrings, and added resInitStrList & }
{ resAddToStrList routines to allow the }
{ creation of string lists in memory. }
{ 1.0.1: 9 April 1991 Debugged string list routines and added end }
{ function. }
{ 2.0: 20 December 1991 First public release, in library form. }
{ 2.1: 6 July 1994 Modified resInitStrList and resAddToStrList }
{ to use PtrAndHand instead of a large fixed- }
{ size buffer; more memory efficient. }
{ 27 October 2001 Public release as source code. }
{ * * * }
{ Written in THINKąs Lightspeed Pascal version 4.0, so portions of the }
{ unit are copyright © 1991 by Symantec Corporation. Also, I have used }
{ routines written by several other authors, both in modified and }
{ un-modified form, so they are copyright (or not, as the case may be) the }
{ respective authors. The appropriate details of this are documented at the }
{ end of the routines implementation comments. }
{ * * * }
{ N.B: Most of my units require the compile-time variables Śapplicationą }
{ and Śdebugą, both of which are booleans. }
{--------------------------------|--------------------------------------------------------------------|---------------------------------------------------------|------------------------}
{--------------------------------|--------------------------------------------------------------------|---------------------------------------------------------|------------------------}
UNIT genResources;
INTERFACE
USES
genToolbox;
CONST
genResFileName = 'Dejaląs Stuff:Development ź:Generic Utilities ź:genResFile.rsrc';
{--------------------------------|--------------------------------------------------------------------|---------------------------------------------------------|------------------------}
procedure resReallyBadError;
procedure resInit;
function resStrListLength (strListResID: integer): integer;
function resBeginStrList (var strListData: handle): osErr;
function resAddToStrList (strListData: handle; theString: str255): osErr;
{--------------------------------|--------------------------------------------------------------------|---------------------------------------------------------|------------------------}
{--------------------------------|--------------------------------------------------------------------|---------------------------------------------------------|------------------------}
IMPLEMENTATION
type
intPtr = ^integer;
intHandle = ^intPtr;
{--------------------------------|--------------------------------------------------------------------|---------------------------------------------------------|------------------------}
procedure resReallyBadError;
{ Handles errors so bad that we canąt even access an alert or dialog resource safely. }
{ Simply inverts the menu bar, beeps three times and quits the application. }
{ Written by David Sinclair, 11 August 1990. }
{ Modified so only exitToShell if application, 17 November 1990. }
{ Modified so uses THINKąs halt command to stop, 17 December 1990. }
{ Modified so it inverts the menubar, 22 March 1991. }
const
beepPeriod = 20;
begin
flashMenuBar(0); { Invert the menu bar }
sysBeep(beepPeriod); { Beep three times }
sysBeep(beepPeriod);
sysBeep(beepPeriod);
{$IFC application}
halt; { Exit the program }
sysError(41) { This will never be executed!! }
{$ENDC}
end;
{--------------------------------|--------------------------------------------------------------------|---------------------------------------------------------|------------------------}
procedure resInit;
{ Allows all programs which use my Generic Utilities to have resources which }
{ consistently match up with the corresponding utility unit, e.g. the string lists, dialogs }
{ and alerts used by genDialogs. Call this routine very early in the program (once only) }
{ to open the resource file, regardless of whether it is a final version or not, as it will check }
{ that the generic resources are available. When doing a alpha, beta or release build, set }
{ debug to false in the Compile Options dialog and once built, copy the resource from the }
{ file to the application file or whatever, but donąt ever copy the generic resources to the }
{ programąs private ResEdit resource file. }
{ P.S: Make sure that the correct file/path name is specified in the constant above! }
{ Written by David Sinclair, 16 March 1991. }
const
osErrorAlertID = 600; { ID number of the DLOG resource used by dlogOSError. }
begin
{$IFC debug}
if openResFile(genResFileName) = -1 then
resReallyBadError;
{$ENDC}
if getResource('DLOG', osErrorAlertID) = nil then
resReallyBadError
end;
{--------------------------------|--------------------------------------------------------------------|---------------------------------------------------------|------------------------}
function resStrListLength (strListResID: integer): integer;
{ Returns the number of strings present in the specified string list resource, or zero if the }
{ STR# was not found. }
{ Written by David Sinclair, 4 April 1991. }
type
intPtr = ^integer; { These can be removed if I ever decide to link in genNumerics }
intHandle = ^intPtr;
var
lengthHandle: intHandle;
begin
lengthHandle:= intHandle(getResource('STR#', strListResID));
if lengthHandle <> nil then
resStrListLength:= lengthHandle^^
else
resStrListLength:= 0
end;
{--------------------------------|--------------------------------------------------------------------|---------------------------------------------------------|------------------------}
function resBeginStrList (var strListData: handle): osErr;
{ Begins the process of building the data for a string list (STR#) in memory. Call this }
{ function before the following one, and report any errors returned from it to the }
{ user. Pass a declared but uninitialised handle variable, and this function will }
{ return a valid handle (if no error), which can then be passed to the following }
{ routine. }
{ Written by David Sinclair, 8 April 1991; upgraded to use PtrAndHand instead of }
{ using a massive fixed buffer, 6 July 1994. }
begin
strListData:= newHandle(2);
if strListData <> nil then
intHandle(strListData)^^:= 0; { Initialise the string count to zero }
resBeginStrList:= memError
end;
{--------------------------------|--------------------------------------------------------------------|---------------------------------------------------------|------------------------}
function resAddToStrList (strListData: handle; theString: str255): osErr;
{ Adds the specified string to the currently being built string list‹see and call }
{ resBeginStrList above for more information. Call this procedure repeatedly within }
{ a loop to add a sequence of strings to the list, checking for errors of course. Once }
{ successfully finished, pass the resulting handle to AddResource (or prefsSaveData) }
{ to create a genuine STR# resource! }
{ Written by David Sinclair, 8 April 1991; upgraded to use PtrAndHand instead of }
{ using a massive fixed buffer, 6 July 1994. }
var
err: osErr;
begin
err:= memWZErr;
if strListData <> nil then { Ensure valid parameters }
begin
hLock(strListData); { Lock down the handle }
err:= ptrAndHand(@theString, strListData, length(theString) + 1);
{ Append the string to the handle }
intHandle(strListData)^^:= intHandle(strListData)^^ + 1;
{ Increment the string count }
hUnlock(strListData) { Unlock the handle }
end;
resAddToStrList:= err
end;
{--------------------------------|--------------------------------------------------------------------|---------------------------------------------------------|------------------------}
{€ function resBeginStrList (var strListData: handle; var offset: integer): osErr;€}
{€€}
{€{ Begins the process of building the data for a string list (STR#) in memory. Call this €]}
{€{ function before either of the two following ones, and report any errors returned from it to €]}
{€{ the user. Pass declared but uninitialised variables for both parameters, and this function €]}
{€{ will return a valid handle and integer (if no error), which can then be passed to the €]}
{€{ following routines. €]}
{€{ Written by David Sinclair, 8 April 1991. €]}
{€€}
{€ const€}
{€ chunkSize = 32000;€}
{€€}
{€ begin€}
{€ strListData:= newHandle(chunkSize);€}
{€ if strListData <> nil then€}
{€ intHandle(strListData)^^:= 0; { Initialise the string count to zero €]}
{€€}
{€ offset:= 2; { Initialise offset to the first string position €]}
{€ resBeginStrList:= memError€}
{€ end;€}
{€€}
{€{ --------------------------------------------------------------------------------------------------------- €]}
{€€}
{€ procedure resAddToStrList (strListData: handle; var offset: integer; theString: str255);€}
{€€}
{€{ Adds the specified string to the currently being built string list‹see and call resBeginStrList €]}
{€{ above for more information. Call this procedure repeatedly within a loop to add a sequence €]}
{€{ of strings to the list. Once finished, call resEndStrList below, then if there is no error, pass €]}
{€{ the resulting handle to AddResource (or prefsSaveData) to create a genuine STR# resource! €]}
{€{ Written by David Sinclair, 8 April 1991. €]}
{€€}
{€ var€}
{€ offsetPtr: ptr;€}
{€€}
{€ begin€}
{€ if (strListData <> nil) & (offset > 1) then { Ensure valid parameters €]}
{€ begin€}
{€ hLock(strListData); { Lock down the handle €]}
{€€}
{€ offsetPtr:= pointer(ord4(@strListData^^) + offset); { Get ptr to place to insert str €]}
{€ stringPtr(offsetPtr)^:= theString; { Insert string €]}
{€ offset:= offset + length(theString) + 1; { Increase offset by added length €]}
{€€}
{€ intHandle(strListData)^^:= intHandle(strListData)^^ + 1; { Increment the string count €]}
{€€}
{€ hUnlock(strListData) { Unlock the handle €]}
{€ end€}
{€ end;€}
{€€}
{€{ --------------------------------------------------------------------------------------------------------- €]}
{€€}
{€ function resEndStrList (strListData: handle; offset: integer): osErr;€}
{€€}
{€{ Completes the process of building a string list in memory by compacting the handle down €]}
{€{ to the correct size. Donąt call this routine until after both of the above two: see them for €]}
{€{ more information. €]}
{€{ Written by David Sinclair, 9 April 1991. €]}
{€€}
{€ begin€}
{€ setHandleSize(strListData, offset);€}
{€ resEndStrList:= memError€}
{€ end;€}
{--------------------------------|--------------------------------------------------------------------|---------------------------------------------------------|------------------------}
{--------------------------------|--------------------------------------------------------------------|---------------------------------------------------------|------------------------}
end.