Copyright (c) Hyperion Entertainment and contributors.

Difference between revisions of "Utility Library"

From AmigaOS Documentation Wiki
Jump to navigation Jump to search
 
(54 intermediate revisions by 2 users not shown)
Line 2: Line 2:
 
== Utility Library ==
 
== Utility Library ==
   
Utility library is the home for all the OS functions which don't fit in the other libraries. Among the calls in utility library are calls to manage tags and tag lists, callback hooks, and some generic 32-bit math functions, all discussed below.
+
Utility library is the home for all the OS functions which don't fit in the other libraries. Among the calls in utility library are calls to manage tags and tag lists, callback hooks, strings, data structures and more.
   
  +
The various functions are grouped into several sections:
== Tags ==
 
  +
* [[Tags]]
 
  +
* [[Callback Hooks]]
Tags make it possible to add new parameters to system functions without interfering with the original parameters. They also make specifying parameter lists much clearer and easier.
 
  +
* [[String Functions]]
 
=== Tag Functions and Structures ===
+
* [[Date Functions]]
  +
* [[Named Objects]]
 
  +
* [[Data Structures]]
A tag is made up of an attribute/value pair as defined below (from <utility/tagitem.h>):
 
 
<syntaxhighlight>
 
struct TagItem
 
{
 
ULONG ti_Tag; /* identifies the type of this item */
 
ULONG ti_Data; /* type-specific data, can be a pointer */
 
};
 
</syntaxhighlight>
 
 
The ti_Tag field specifies an attribute to set. The possible values of ti_Tag are implementation specific. System tags are defined in the include files. The value the attribute is set to is specified in ti_Data. An example of the attribute/value pair that will specify a window's name is:
 
 
<syntaxhighlight>
 
ti_Tag = WA_Title;
 
ti_Data = "My Window's Name";
 
</syntaxhighlight>
 
 
The ti_Data field often contains 32-bit data as well as pointers.
 
 
These are brief descriptions of the utility functions you can use to manipulate and access tags. For complete descriptions, see the "Simple Tag Usage" and "Advanced Tag Usage" sections.
 
 
The following utility library calls are for supporting tags:
 
 
Utility Library Tag Functions
 
 
{| class="wikitable"
 
| AllocateTagItems() || Allocate a TagItem array (or chain).
 
|-
 
| FreeTagItems() || Frees allocated TagItem lists.
 
|-
 
| CloneTagItems() || Copies a TagItem list.
 
|-
 
| RefreshTagItemClone() || Rejuvenates a clone from the original.
 
|-
 
| FindTagItem() || Scans TagItem list for a tag.
 
|-
 
| GetTagData() || Obtain data corresponding to tag.
 
|-
 
| NextTagItem() || Iterate TagItem lists.
 
|-
 
| TagInArray() || Check if a tag value appears in a Tag array.
 
|-
 
| FilterTagChanges() || Eliminate TagItems which specify no change.
 
|-
 
| FilterTagItems() || Remove selected items from a TagItem list.
 
|-
 
| MapTags() || Convert ti_Tag values in a list via map pairing.
 
|-
 
| PackBoolTags() || Builds a "Flag" word from a TagItem list.
 
|}
 
 
=== Simple Tag Usage ===
 
 
One way tags are passed to system functions is in the form of tag lists. A tag list is an array or chain of arrays of TagItem structures. Within this array, different data items are identified by the value of ti_Tag. Items specific to a subsystem (Intuition, Graphics,...) have a ti_Tag value which has the TAG_USER bit set. Global system tags have a ti_Tag value with TAG_USER bit clear. The global system tags include:
 
 
Global System Tags
 
 
{| class="wikitable"
 
! Tag Value
 
! Meaning
 
|-
 
| TAG_IGNORE || A no-op. The data item is ignored.
 
|-
 
| TAG_MORE || The ti_Data points to another tag list, to support chaining of TagItem arrays.
 
|-
 
| TAG_DONE || Terminates the TagItem array (or chain).
 
|-
 
| TAG_SKIP || Ignore the current tag item, and skip the next ''n'' array elements, where ''n'' is kept in ti_Data.
 
|}
 
 
Note that user tags need only be unique within the particular context of their use. For example, the attribute tags defined for OpenWindow() have the same numeric value as some tags used by OpenScreen(), but the same numeric value has different meaning in the different contexts.
 
 
System functions receive TagItems in several ways. One way is illustrated in the Intuition function OpenWindow(). This function supports an extented NewWindow structure called ExtNewWindow. When the NW_EXTENDED flag is set in the ExtNewWindow.Flags field, OpenWindow() assumes that the ExtNewWindow.Extension field contains a pointer to a tag list.
 
 
Another method of passing a tag list is to directly pass a pointer to a tag list, as OpenWindowTagList() does in the following code fragment.
 
 
<syntaxhighlight>
 
struct TagItem tagitem[3];
 
struct Screen *screen;
 
struct Window *window;
 
 
tagitem[0].ti_Tag = WA_CustomScreen;
 
tagitem[0].ti_Data = screen; /* Open on my own screen */
 
tagitem[1].ti_Tag = WA_Title;
 
tagitem[1].ti_Data = "Amiga Test Window";
 
tagitem[2].ti_Tag = TAG_DONE; /* Marks the end of the tag array. */
 
 
/* Use defaults for everything else. Will open as big as the screen. */
 
/* Because all window parameters are specified using tags, we don't */
 
/* need a NewWindow structure */
 
 
if (window = IIntuition->OpenWindowTagList(NULL, tagitem))
 
{
 
/* rest of code */
 
IIntuition->CloseWindow(window);
 
}
 
</syntaxhighlight>
 
 
Notice that window parameters need not be explicitly specified. Functions that utilize tags have reasonable defaults to fall back on in case no valid attribute/value pair was supplied for a particular parameter. This fall back capability is a useful feature. An application only has to specify the attributes that differ from the default, rather than unnecessarily listing all the possible attributes.
 
 
Many functions also support another way to pass TagItems. Rather than passing a tag list, the function OpenWindowTags() receives the attribute/value pairs in the argument list, much like the standard C function printf() receives its arguments. Any number of attribute/value pairs can be specified. This type of argument passing is called VarArgs. The following code fragment illustrates the usage of OpenWindowTags().
 
 
<syntaxhighlight>
 
struct Window *window;
 
 
/* Just pass NULL to show we aren't using a NewWindow */
 
window = IIntuition->OpenWindowTags( NULL,
 
WA_CustomScreen, screen,
 
WA_Title, "Amiga Test Window",
 
TAG_DONE );
 
</syntaxhighlight>
 
 
Tags are not exclusively for use with the operating system; the programmer can implement them as well. The run-time utility library contains several functions to make using tags easier.
 
 
=== Simple Tag Usage Example ===
 
 
The following example shows simple usage of tags. It shows how to allocate a tag array and use it, it also shows how to build a tag array on the stack.
 
 
<pre>;/* tag1.c - Execute me to compile me with SAS C 5.10
 
LC -b1 -cfis -j73 tag1.c
 
Blink FROM LIB:c.o,tag1.o TO tag1 LIBRARY LIB:LC.lib,LIB:Amiga.lib
 
quit
 
*/
 
 
#include &lt;exec/types.h&gt;
 
#include &lt;exec/libraries.h&gt;
 
#include &lt;utility/tagitem.h&gt;
 
#include &lt;intuition/intuition.h&gt;
 
#include &lt;clib/exec_protos.h&gt;
 
#include &lt;clib/intuition_protos.h&gt;
 
#include &lt;clib/utility_protos.h&gt;
 
 
extern struct Library *SysBase;
 
struct Library *IntuitionBase, *UtilityBase;
 
 
int main (int argc, char **argv)
 
{
 
struct TagItem *tags;
 
struct Window *win;
 
 
/* For this example we need Version 2.0 */
 
if (IntuitionBase = OpenLibrary (&quot;intuition.library&quot;, 37))
 
{
 
/* We need the utility library for this example */
 
if (UtilityBase = OpenLibrary (&quot;utility.library&quot;, 37))
 
{
 
 
/****************************************************************/
 
/* This section allocates a tag array, fills it in with values, */
 
/* and then uses it. */
 
/****************************************************************/
 
 
/* Allocate a tag array */
 
if (tags = AllocateTagItems (7))
 
{
 
/* Fill in our tag array */
 
tags[0].ti_Tag = WA_Width;
 
tags[0].ti_Data = 320;
 
tags[1].ti_Tag = WA_Height;
 
tags[1].ti_Data = 50;
 
tags[2].ti_Tag = WA_Title;
 
tags[2].ti_Data = (ULONG) &quot;Amiga Tag Example 1&quot;;
 
tags[3].ti_Tag = WA_IDCMP;
 
tags[3].ti_Data = IDCMP_CLOSEWINDOW;
 
tags[4].ti_Tag = WA_CloseGadget;
 
tags[4].ti_Data = TRUE;
 
tags[5].ti_Tag = WA_DragBar;
 
tags[5].ti_Data = TRUE;
 
tags[6].ti_Tag = TAG_DONE;
 
 
/* Open the window, using the tag attributes as the
 
* only description. */
 
if (win = OpenWindowTagList (NULL, tags))
 
{
 
/* Wait for an event to occur */
 
WaitPort (win-&gt;UserPort);
 
 
/* Close the window now that we're done with it */
 
CloseWindow (win);
 
}
 
 
/* Free the tag list now that we're done with it */
 
FreeTagItems(tags);
 
}
 
 
/****************************************************************/
 
/* This section builds the tag array on the stack, and passes */
 
/* the array to a function. */
 
/****************************************************************/
 
 
/* Now use the VarArgs (or stack based) version. */
 
if (win = OpenWindowTags ( NULL,
 
WA_Width, 320,
 
WA_Height, 50,
 
WA_Title, (ULONG)&quot;Amiga Tag Example 1&quot;,
 
WA_IDCMP, IDCMP_CLOSEWINDOW,
 
WA_CloseGadget, TRUE,
 
WA_DragBar, TRUE,
 
TAG_DONE))
 
{
 
/* Wait for an event to occur */
 
WaitPort (win-&gt;UserPort);
 
 
/* Close the window now that we're done with it */
 
CloseWindow (win);
 
}
 
 
/* Close the library now */
 
CloseLibrary (UtilityBase);
 
}
 
 
/* Close the library now that we're done with it */
 
CloseLibrary (IntuitionBase);
 
}
 
}</pre>
 
 
=== Advanced Tag Usage ===
 
 
The previous section provided the background material necessary to start using tags. This section will show how to use the more advanced features of tags using functions within utility library.
 
 
==== Creating a New Tag List ====
 
 
The AllocateTagItems() function can be used to create a new tag array ready for use. The tag array should be passed to FreeTagItems() when the application is done with it.
 
 
<syntaxhighlight>
 
struct TagItem *tags;
 
uint32 tags_needed;
 
 
/* Indicate how many tags we need */
 
tags_needed = 10;
 
 
/* Allocate a tag array */
 
if (tags = IUtility->AllocateTagItems(tags_needed))
 
{
 
/* ...do something with the array... */
 
 
/* Free the array when your done with it */
 
IUtility->FreeTagItems (tags);
 
}
 
</syntaxhighlight>
 
 
==== Copying an Existing Tag List ====
 
 
The CloneTagItems() function is used to copy an existing tag array into a new tag array.
 
 
<syntaxhighlight>
 
struct TagItem *otags; /* Original tag array */
 
struct TagItem *ntags; /* New tag array */
 
 
/* Make sure there is a TagItem array */
 
if (otags)
 
{
 
/* Copy the original tags into a new tag array */
 
if (ntags = IUtility->CloneTagItems(otags))
 
{
 
/* ...do something with the array... */
 
 
/* Free the array when your done with it */
 
IUtility->FreeTagItems (ntags);
 
}
 
}
 
</syntaxhighlight>
 
 
This function can also be used to implement a function that will insert tag items into an array.
 
 
<syntaxhighlight>
 
struct TagItem *otags; /* Original tag array */
 
struct TagItem *tags; /* New tag array */
 
 
/* Insert a couple of tags into an existing tag array */
 
if (tags = IUtility->MakeNewTagList (GA_LeftEdge, 10,
 
GA_TopEdge, 20,
 
TAG_MORE, otags))
 
{
 
/* ...do something with the array... */
 
 
/* Free the array when your done with it */
 
IUtility->FreeTagItems (tags);
 
}
 
 
/* This function will create a tag array from tag pairs placed on
 
* the stack */
 
struct TagItem *MakeNewTagList (uint32 data,...)
 
{
 
struct TagItem *tags = (struct TagItem *) &data;
 
 
return IUtility->CloneTagItems (tags);
 
}
 
</syntaxhighlight>
 
 
==== Filtering an Existing Tag List ====
 
 
Sometimes it is necessary to only allow certain attributes to be visible in a tag list. In order to achieve this, the tag array would need to be filtered.
 
 
A number of functions are provided for filtering items in a tag array. They are FilterTagChanges(), FilterTagItems() and RefreshTagItemClone().
 
 
<syntaxhighlight>
 
/* We want the text entry gadget to receive the following tags */
 
Tag string_attrs[] =
 
{
 
STRINGA_MaxChars,
 
STRINGA_Buffer,
 
STRINGA_TextVal,
 
TAG_END,
 
};
 
 
/* These are attributes that the model understands */
 
Tag model_attrs[] =
 
{
 
CGTA_Total,
 
CGTA_Visible,
 
CGTA_Top,
 
ICA_TARGET,
 
ICA_MAP,
 
TAG_END,
 
};
 
 
struct TagItem *otags; /* Original tag list */
 
struct TagItem *ntags; /* New, work, tag list */
 
 
/* Make a copy of the original for us to work with */
 
ntags = IUtility->CloneTagItems (otags);
 
 
/* Create a tag list that only contains attributes that are
 
* listed in the model_attrs list. */
 
if (IUtility->FilterTagItems (ntags, model_attrs, TAGFILTER_AND))
 
{
 
/* Work with filtered tag list (ntags) */
 
 
/* Restore the tag list */
 
IUtility->RefreshTagItemClones (ntags, otags);
 
 
/* Create a tag list that only contains attributes that
 
* aren't in the model_attrs list. */
 
if (IUtility->FilterTagItems (ntags, model_attrs, TAGFILTER_NOT))
 
{
 
/* Work with filtered tag list (ntags) */
 
}
 
 
/* Restore the tag list */
 
IUtility->RefreshTagItemClones (ntags, otags);
 
 
/* Create a tag list that only contains attributes that
 
* are in the string_attrs list. */
 
if (IUtility->FilterTagItems (ntags, string_attrs, TAGFILTER_AND))
 
{
 
/* Work with filtered tag list (ntags) */
 
}
 
}
 
 
/* Free work tag list. */
 
IUtility->FreeTagItems (ntags);
 
</syntaxhighlight>
 
 
==== Locating an Attribute ====
 
 
To see if an attribute is in a tag array, the TagInArray() function is used.
 
 
<syntaxhighlight>
 
/* See if the listview labels attribute is located in a tag array */
 
if (IUtility->TagItemArray(GTLV_Labels, tags))
 
{
 
/* Yes, the attribute is in the list */
 
}
 
else
 
{
 
/* No, the attribute isn't in the list */
 
}
 
</syntaxhighlight>
 
 
The FindTagItem() function will return a pointer to the actual tag that has the desired attribute. This allows you to manipulate the tag or to determine if the attribute exists but just has a NULL value.
 
 
<syntaxhighlight>
 
struct TagItem *tag;
 
 
/* See if they are trying to set a sound */
 
if (tag = IUtility->FindTagItem(MGA_Sound, attrs))
 
{
 
/* Set the sound attribute to point to the specified sound data */
 
tag->ti_Data = sound;
 
}
 
</syntaxhighlight>
 
 
==== Sequential Access of Tag Lists ====
 
 
In order to sequentially access the members of a tag array, the NextTagItem() function is used.
 
 
<syntaxhighlight>
 
struct TagItem *tags = msg->ops_AttrList;
 
struct TagItem *tstate;
 
struct TagItem *tag;
 
 
/* Start at the beginning */
 
tstate = tags;
 
 
/* Step through the tag list while there are still items in the
 
* list */
 
while (tag = NextTagItem (&tstate))
 
{
 
/* Cache the data for the current element */
 
uint32 tidata = tag->ti_Data;
 
 
/* Handle each attribute that we understand */
 
switch (tag->ti_Tag)
 
{
 
/* Put a case statement here for each attribute that your
 
* function understands */
 
case PGA_Freedom:
 
lod->lod_Flags |= tidata;
 
break;
 
 
case GTLV_Labels:
 
lod->lod_List = (struct List *) tidata;
 
break;
 
 
/* We don't understand this attribute */
 
default:
 
break;
 
}
 
}
 
</syntaxhighlight>
 
 
==== Random Access of Tag Lists ====
 
 
The GetTagData() function will return the data for the specified attribute. If there isn't a tag that matches, then the default value is returned.
 
 
<syntaxhighlight>
 
APTR sound;
 
 
/* Get the sound data that our function will use. */
 
sound = (APTR) GetTagData (MGA_Sound, (uint32) DefaultSound, attrs);
 
</syntaxhighlight>
 
 
==== Obtaining Boolean Values ====
 
 
Often times data is best represented as simple boolean (TRUE or FALSE) values. The PackBoolTags() function provides an easy method for converting a tag list to bit fields.
 
 
<pre>/* These are the attributes that we understand, with the
 
* corresponding flag value. */
 
struct TagItem activation_bools[] =
 
{
 
/* Attribute Flags */
 
{GA_ENDGADGET, ENDGADGET},
 
{GA_IMMEDIATE, GADGIMMEDIATE},
 
{GA_RELVERIFY, RELVERIFY},
 
{GA_FOLLOWMOUSE, FOLLOWMOUSE},
 
{GA_RIGHTBORDER, RIGHTBORDER},
 
{GA_LEFTBORDER, LEFTBORDER},
 
{GA_TOPBORDER, TOPBORDER},
 
{GA_BOTTOMBORDER, BOTTOMBORDER},
 
{GA_TOGGLESELECT, TOGGLESELECT},
 
 
/* Terminate the array */
 
{TAG_END}
 
};
 
 
/* Set the activation field, based on the attributes passed */
 
g-&gt;Activation = PackBoolTags(g-&gt;Activation, tags, activation_bools);</pre>
 
==== Mapping Tag Attributes ====
 
 
To translate all occurrences of an attribute to another attribute, the MapTags() function is used.
 
 
For Release 2, the third parameter of this function is always TRUE (tags remain in the array even if they can’t be mapped).
 
 
<pre>struct TagItem map_list[] =
 
{
 
/* Original New */
 
{MGA_LeftEdge, GA_LeftEdge},
 
{MGA_TopEdge, GA_TopEdge},
 
{MGA_Width, GA_Width},
 
{MGA_Height, GA_Height},
 
 
/* Terminate the array */
 
{TAG_END},
 
}
 
 
/* Map the tags to the new attributes, keeping all attributes that
 
* aren't included in the mapping array */
 
MapTags(tags, map_list, TRUE);</pre>
 
 
== Callback Hooks ==
 
 
The callback feature provides a standard means for applications to extend the functionality of libraries, devices, and applications. This standard makes it easy for the operating system to use custom modules from different high level programming languages as part of the operating system. For example, the layers library, which takes care of treating a display as a series of layered regions, allows an application to attach a pattern function to a display layer. Instead of filling in the background of a layer with the background color, the layers library calls the custom pattern function which fills in the layer display with a custom background pattern.
 
 
=== Callback Hook Structure and Function ===
 
 
An application passes a custom function in the form of a callback Hook (from &lt;utility/hooks.h&gt;):
 
 
<pre>
 
struct Hook
 
{
 
struct MinNode h_MinNode;
 
ULONG (*h_Entry)(); /* stub function entry point */
 
ULONG (*h_SubEntry)(); /* the custom function entry point */
 
VOID *h_Data; /* owner specific */
 
};
 
</pre>
 
 
This field is reserved for use by the module that will call the Hook.
 
 
This is the address of the Hook stub. When the OS calls a callback function, it puts parameters for the callback function in CPU registers A0, A1, and A2. This makes it tough for higher level language programmers to use a callback function because most higher level languages don't have a way to manipulate CPU registers directly. The solution is a stub function which first copies the parameters from the CPU registers to a place where a high level language function can get to them. The stub function then calls the callback function. Typically, the stub pushes the registers onto the stack in a specific order and the callback function pops them off the stack.
 
 
This is the address of the actual callback function that the application has defined. The stub calls this function.
 
 
This field is for the application to use. It could point to a global storage structure that the callback function utilizes.
 
 
There is only one function defined in utility library that relates to callback hooks:
 
 
<pre>
 
ULONG CallHookPkt(struct Hook *hook, APTR object, APTR paramPkt);
 
</pre>
 
 
This function calls a standard callback Hook function.
 
 
==== Simple Callback Hook Usage ====
 
 
A Hook function must accept the following three parameters in these specific registers:
 
 
A0
 
 
Pointer to the Hook structure.<br />
 
A2 Pointer to an object to manipulate. The object is context specific.<br />
 
A1 Pointer to a message packet. This is also context specific.
 
 
For a callback function written in C, the parameters should appear in this order:
 
 
<pre>
 
myCallbackFunction(Pointer to Hook (A0),
 
Pointer to Object (A2),
 
Pointer to message (A1));
 
</pre>
 
 
This is because the standard C stub pushes the parameters onto the stack in the following order: A1, A2, A0. The following assembly language routine is a callback stub for C:
 
 
<pre>
 
INCLUDE 'exec/types.i'
 
INCLUDE 'utility/hooks.i'
 
 
xdef _hookEntry
 
 
_hookEntry:
 
move.l a1,-(sp) ; push message packet pointer
 
move.l a2,-(sp) ; push object pointer
 
move.l a0,-(sp) ; push hook pointer
 
move.l h_SubEntry(a0),a0 ; fetch actual Hook entry point ...
 
jsr (a0) ; and call it
 
lea 12(sp),sp ; fix stack
 
rts
 
</pre>
 
 
If your C compiler supports registerized parameters, your callback functions can get the parameters directly from the CPU registers instead of having to use a stub to push them on the stack. The following C language routine uses registerized parameters to put parameters in the right registers. This routine requires a C compiler that supports registerized parameters.
 
 
<pre>
 
#include &lt;exec/types.h&gt;
 
#include &lt;utility/hooks.h&gt;
 
 
#define ASM __asm
 
#define REG(x) register __ ## x
 
 
/* This function converts register-parameter hook calling
 
* convention into standard C conventions. It requires a C
 
* compiler that supports registerized parameters, such as
 
* SAS/C 5.xx or greater.
 
*/
 
ULONG ASM
 
hookEntry(REG(a0) struct Hook *h, REG(a2) VOID *o, REG(a1) VOID *msg)
 
{
 
return ((*h-&gt;h_SubEntry)(h, o, msg));
 
}
 
</pre>
 
 
A callback function is executed on the context of the module that invoked it. This usually means that callback functions cannot call functions that need to look at environment specific data. For example, printf() needs to look at the current process's input and output stream. Entities like Intuition have no input and output stream. This also means that in order for the function to access any of its global data, it needs to make sure the CPU can find the function's data segment. It does this by forcing the function to load the offset for the program's data segment into CPU register A4. See your compiler documentation for details.
 
 
The following is a simple function that can be used in a callback hook.
 
 
<pre>
 
ULONG MyFunction (struct Hook *h, VOID *o, VOID *msg)
 
{
 
/* A SASC and Manx function that obtains access to the global
 
data segment */
 
geta4();
 
 
/* Debugging function to send a string to the serial port */
 
KPrintF(&quot;Inside MyFunction()\n&quot;);
 
 
return (1);
 
}
 
</pre>
 
 
The next step is to initialize the Hook for use. This basically means that the fields of the Hook structure must be filled with appropriate values.
 
 
The following simple function initializes a Hook structure.
 
 
<pre>
 
/* This simple function is used to initialize a Hook */
 
VOID InitHook (struct Hook *h, ULONG (*func)(), VOID *data)
 
{
 
/* Make sure a pointer was passed */
 
if (h)
 
{
 
/* Fill in the hook fields */
 
h-&gt;h_Entry = (ULONG (*)()) hookEntry;
 
h-&gt;h_SubEntry = func;
 
h-&gt;h_Data = data;
 
}
 
}
 
</pre>
 
 
The following is a simple example of a callback hook function.
 
 
<pre>
 
;/* hooks1.c - Execute me to compile me with SAS C 5.10
 
LC -b1 -cfis -j73 hooks1.c
 
Blink FROM LIB:c.o,hooks1.o TO hooks1 LIBRARY LIB:LC.lib,LIB:Amiga.lib
 
quit
 
*/
 
 
#include &lt;exec/types.h&gt;
 
#include &lt;exec/libraries.h&gt;
 
#include &lt;utility/hooks.h&gt;
 
#include &lt;dos.h&gt;
 
 
#include &lt;clib/exec_protos.h&gt;
 
#include &lt;clib/dos_protos.h&gt;
 
#include &lt;clib/utility_protos.h&gt;
 
 
#include &lt;stdio.h&gt;
 
 
extern struct Library *SysBase;
 
struct Library *UtilityBase;
 
 
#define ASM __asm
 
#define REG(x) register __ ## x
 
 
/* This function converts register-parameter Hook calling
 
* convention into standard C conventions. It requires a C
 
* compiler that supports registerized parameters, such as
 
* SAS/C 5.xx or greater.
 
*/
 
 
ULONG ASM
 
hookEntry(REG(a0) struct Hook *h, REG(a2) VOID *o, REG(a1) VOID *msg)
 
{
 
return ((*(ULONG (*)(struct Hook *,VOID *,VOID *))(h-&gt;h_SubEntry))(h, o, msg));
 
}
 
 
/* This simple function is used to initialize a Hook */
 
VOID InitHook (struct Hook *h, ULONG (*func)(), VOID *data)
 
{
 
/* Make sure a pointer was passed */
 
if (h)
 
{
 
/* Fill in the Hook fields */
 
h-&gt;h_Entry = (ULONG (*)()) hookEntry;
 
h-&gt;h_SubEntry = func;
 
h-&gt;h_Data = data;
 
}
 
}
 
 
/* This function only prints out a message indicating that we are
 
* inside the callback function.
 
*/
 
 
ULONG MyFunction (struct Hook *h, VOID *o, VOID *msg)
 
{
 
/* Obtain access to the global data segment */
 
geta4();
 
 
/* Debugging function to send a string to the serial port */
 
printf(&quot;Inside MyFunction()\n&quot;);
 
 
return (1);
 
}
 
 
int main (int argc, char **argv)
 
{
 
struct Hook h;
 
 
/* Open the utility library */
 
if (UtilityBase = OpenLibrary (&quot;utility.library&quot;, 36))
 
{
 
/* Initialize the callback Hook */
 
InitHook (&amp;h, MyFunction, NULL);
 
 
/* Use the utility library function to invoke the Hook */
 
CallHookPkt (&amp;h, NULL, NULL);
 
 
/* Close utility library now that we're done with it */
 
CloseLibrary (UtilityBase);
 
}
 
else printf (&quot;Couldn't open utility.library\n&quot;);
 
}
 
</pre>
 
 
== 32-bit Integer Math Functions ==
 
 
 
 
Utility library contains some high-speed math functions for 32-bit integer division and multiplication. These functions will take advantage of available processor instructions (like DIVU.L), if a ''68020'' processor or higher is present. If not, these functions will mimic those instructions in ''68000'' only instructions, thus providing processor independency.
 
 
Currently the following functions are implemented:
 
 
[h] Utility Library 32-bit Math Functions
 
 
<table>
 
<tbody>
 
<tr class="odd">
 
<td align="left">SDivMod32()</td>
 
<td align="left">Signed 32 by 32-bit division and modulus.</td>
 
</tr>
 
<tr class="even">
 
<td align="left">SMult32()</td>
 
<td align="left">Signed 32 by 32-bit multiplication.</td>
 
</tr>
 
<tr class="odd">
 
<td align="left">UDivMod32()</td>
 
<td align="left">Unsigned 32 by 32-bit division and modulus.</td>
 
</tr>
 
<tr class="even">
 
<td align="left">UMult32()</td>
 
<td align="left">Unsigned 32 by 32-bit multiplication.</td>
 
</tr>
 
</tbody>
 
</table>
 
 
The division functions return the quotient in D0 and the remainder in D1. To obtain the remainder in a higher level language, either a compiler specific instruction to fetch the contents of a specific register must be used (like getreg() in SAS C) or a small assembler stub.
 
 
Following a simple example of the usage of the 32-bit integer math functions in C.
 
 
<pre>;/* uptime.c - Execute me to compile me with SAS C 5.10
 
LC -b1 -cfis -j73 uptime.c
 
Blink FROM LIB:c.o,uptime.o TO uptime LIBRARY LIB:LC.lib,LIB:Amiga.lib
 
quit
 
*/
 
 
/* Uses SAS C getreg() */
 
 
#include &lt;exec/types.h&gt;
 
#include &lt;exec/memory.h&gt;
 
#include &lt;dos/dos.h&gt;
 
#include &lt;dos/dosextens.h&gt;
 
#include &lt;dos/datetime.h&gt;
 
#include &lt;utility/date.h&gt;
 
#include &lt;dos.h&gt;
 
 
#include &lt;clib/exec_protos.h&gt;
 
#include &lt;clib/dos_protos.h&gt;
 
#include &lt;clib/utility_protos.h&gt;
 
 
#include &lt;stdlib.h&gt;
 
 
struct Library *UtilityBase;
 
 
LONG main(void);
 
 
LONG main(void)
 
{
 
struct InfoData *infodata;
 
struct DeviceList *ramdevice;
 
struct DateStamp *now;
 
LONG currenttime, boottime;
 
BPTR lock;
 
LONG vargs[3];
 
LONG rc = RETURN_FAIL;
 
 
/* Fails silently if &lt; 37 */
 
if (UtilityBase = OpenLibrary(&quot;utility.library&quot;, 37))
 
{
 
if (infodata = AllocMem(sizeof(struct InfoData), MEMF_CLEAR))
 
{
 
if (now = AllocMem(sizeof(struct DateStamp), MEMF_CLEAR))
 
{
 
if (lock = Lock(&quot;RAM:&quot;, SHARED_LOCK))
 
{
 
if ((Info(lock, infodata)) == DOSTRUE)
 
{
 
/* Make C pointer */
 
 
ramdevice = BADDR(infodata-&gt;id_VolumeNode);
 
 
boottime = SMult32(ramdevice-&gt;dl_VolumeDate.ds_Days, 86400) +
 
SMult32(ramdevice-&gt;dl_VolumeDate.ds_Minute, 60) +
 
SDivMod32(ramdevice-&gt;dl_VolumeDate.ds_Tick,
 
TICKS_PER_SECOND );
 
 
DateStamp(now);
 
 
currenttime = SMult32(now-&gt;ds_Days, 86400) +
 
SMult32(now-&gt;ds_Minute, 60) +
 
SDivMod32(now-&gt;ds_Tick, TICKS_PER_SECOND);
 
 
currenttime -= boottime;
 
 
if (currenttime &gt; 0)
 
{
 
vargs[0] = UDivMod32(currenttime, 86400);
 
 
vargs[1] = getreg(1);
 
vargs[1] = UDivMod32(vargs[1], 3600);
 
 
vargs[2] =getreg(1);
 
vargs[2] = UDivMod32(vargs[2], 60);
 
 
/*
 
* Passing the address of the array allows the VPrintf()
 
* function to access the array contents. Keep in mind
 
* that VPrintf() does _NOT_ know how many elements are
 
* really valid in the final parameter, and will gleefully
 
* run past the valid arguments.
 
*/
 
VFPrintf(Output(),
 
&quot;up for %ld days, %ld hours, %ld minutes\n&quot;,
 
vargs );
 
 
rc = RETURN_OK;
 
}
 
}
 
UnLock(lock);
 
}
 
FreeMem(now, sizeof(struct DateStamp));
 
}
 
FreeMem(infodata, sizeof(struct InfoData));
 
}
 
CloseLibrary(UtilityBase);
 
}
 
exit(rc);
 
}</pre>
 
== International String Functions ==
 
 
The international string functions in utility library are a way to make use of a future localization library which the AmigaOS development team will provide. When the localization library is opened, the functions will be replaced by ones which will take the locale as defined by the user into account. This means that the compare order may change according to the locale, so care should be taken not to rely on obtaining specific compare sequences.
 
 
Currently implemented are:
 
 
Utility Library International String Functions
 
 
{| class="wikitable"
 
| Stricmp() || Compare string case-insensitive.
 
|-
 
| Strnicmp() || Compare string case-insensitive, with a specified length.
 
|-
 
| ToLower() || Convert a character to lower case.
 
|-
 
| ToUpper() || Convert a character to upper case.
 
|}
 
 
These functions operate in the same manner as their ANSI C equivalents, for the most part. For more information, see the "Utility Library" Autodocs in the SDK. Here is a simple example of the usage of the international string functions.
 
 
<pre>
 
;/* istr.c - Execute me to compile me with SAS C 5.10
 
LC -b1 -cfis -j73 istr.c
 
Blink FROM LIB:c.o,istr.o TO istr LIBRARY LIB:LC.lib,LIB:Amiga.lib
 
quit
 
*/
 
 
#include &lt;exec/types.h&gt;
 
#include &lt;stdio.h&gt;
 
#include &lt;string.h&gt;
 
 
#include &lt;clib/exec_protos.h&gt;
 
#include &lt;clib/utility_protos.h&gt;
 
 
void main(void);
 
struct Library *UtilityBase;
 
 
void main(void)
 
{
 
UBYTE *butter = &quot;B√∏tervl√∏√∏t&quot;;
 
UBYTE *bread = &quot;Knåckerbrøt&quot;;
 
UBYTE ch1, ch2;
 
LONG result;
 
 
/* Fails silently if &lt; 37 */
 
if (UtilityBase = OpenLibrary(&quot;utility.library&quot;, 37))
 
{
 
result = Stricmp(butter, bread);
 
 
printf(&quot;comparing %s with %s yields %ld\n&quot;, butter, bread, result );
 
 
result = Strnicmp(bread, butter, strlen(bread));
 
 
printf(&quot;comparing (with length) %s with %s yields %ld\n&quot;, bread, butter, result );
 
 
ch1 = ToUpper(0xE6); æ /* ASCII character 230 ae ligature */
 
ch2 = ToLower(0xD0); Ð /* ASCII character 208 Icelandic Eth */
 
 
printf(&quot;Chars %c %c\n&quot;, ch1, ch2);
 
}
 
}
 
</pre>
 
 
== Date Functions ==
 
 
To ease date-related calculations, the utility library has some functions to convert a date, specified in a ClockData structure, in the number of seconds since 00:00:00 01-Jan-78 and vice versa.
 
 
To indicate the date, the ClockData structure (in &lt;utility/date.h&gt;) is used.
 
 
<pre>
 
struct ClockData
 
{
 
UWORD sec; /* seconds (0 - 59)*/
 
UWORD min; /* minutes (0 - 59) */
 
UWORD hour; /* hour (0 - 23) */
 
UWORD mday; /* day of the month (1 - 31) */
 
UWORD month; /* month of the year (1 - 12)
 
UWORD year; /* 1978 - */
 
UWORD wday; /* day of the week (0 - 6, where 0 is Sunday) */
 
};
 
</pre>
 
 
The following functions are available to operate on ClockData:
 
 
Utility Library Date Functions
 
 
{| class="wikitable"
 
| Amiga2Date() || Calculate the date from the specified timestamp (in seconds).
 
|-
 
| CheckDate() || Check the legality of a date.
 
|-
 
| Date2Amiga() || Calculate the timestamp from the specified date.</td>
 
|}
 
 
Amiga2Date() takes a number of seconds from 01-Jan-78 as argument and fills in the supplied ClockData structure with the date and time.
 
 
CheckDate() checks if the supplied ClockData structure is valid, and returns the number of seconds from 01-Jan-78 if it is. Note that this function currently does not take the supplied day of the week in account.
 
 
Date2Amiga() takes a ClockData structure as argument and returns the number of seconds since 01-Jan-78. The supplied ClockData structure ''must'' be valid, since no checking is done.
 
 
The following example shows various uses of the utility library date functions.
 
 
<pre>
 
;/* a2d.c - Execute me to compile me with SAS C 5.10
 
LC -b1 -cfis -j73 a2d.c
 
Blink FROM LIB:c.o,a2d.o TO a2d LIBRARY LIB:LC.lib,LIB:Amiga.lib
 
quit
 
*/
 
#include &lt;exec/types.h&gt;
 
#include &lt;exec/memory.h&gt;
 
#include &lt;dos/datetime.h&gt;
 
#include &lt;devices/timer.h&gt;
 
 
#include &lt;clib/exec_protos.h&gt;
 
#include &lt;clib/timer_protos.h&gt;
 
#include &lt;clib/utility_protos.h&gt;
 
 
#include &lt;stdio.h&gt;
 
 
LONG main(void);
 
 
struct Library *TimerBase;
 
struct Library *UtilityBase;
 
 
LONG main(void)
 
{
 
struct ClockData *clockdata;
 
struct timerequest *tr;
 
struct timeval *tv;
 
LONG seconds;
 
 
if (UtilityBase = OpenLibrary(&quot;utility.library&quot;, 37))
 
{
 
if (tr = AllocMem(sizeof(struct timerequest), MEMF_CLEAR))
 
{
 
if (tv = AllocMem(sizeof(struct timeval), MEMF_CLEAR))
 
{
 
if (clockdata = AllocMem(sizeof(struct ClockData), MEMF_CLEAR))
 
{
 
if (!(OpenDevice(&quot;timer.device&quot;, UNIT_VBLANK, (struct IORequest *)tr, 0) ))
 
{
 
TimerBase = tr-&gt;tr_node.io_Device;
 
 
GetSysTime(tv);
 
 
printf(&quot;GetSysTime():\t%d %d\n&quot;, tv-&gt;tv_secs, tv-&gt;tv_micro);
 
 
Amiga2Date(tv-&gt;tv_secs, clockdata);
 
 
printf(&quot;Amiga2Date(): sec %d min %d hour %d\n&quot;, clockdata-&gt;sec,
 
clockdata-&gt;min, clockdata-&gt;hour);
 
 
printf(&quot; mday %d month %d year %d wday %d\n&quot;, clockdata-&gt;mday,
 
clockdata-&gt;month, clockdata-&gt;year, clockdata-&gt;wday);
 
 
seconds = CheckDate(clockdata);
 
 
printf(&quot;CheckDate():\t%ld\n&quot;, seconds);
 
 
seconds = Date2Amiga(clockdata);
 
 
printf(&quot;Date2Amiga():\t%ld\n&quot;, seconds);
 
 
CloseDevice((struct IORequest *)tr);
 
}
 
FreeMem(clockdata, sizeof(struct ClockData));
 
}
 
FreeMem(tv, sizeof(struct timeval));
 
}
 
FreeMem(tr, sizeof(struct timerequest));
 
}
 
CloseLibrary(UtilityBase);
 
}
 
}
 
</pre>
 
 
== Function Reference ==
 
 
The tables which follow contain breif descriptions of the functions inside the utility library. See the SDK for details on each function call.
 
 
=== Tag Function Reference ===
 
 
The following are brief descriptions of the utility library functions which pertain to tags and tag lusts.
 
 
<table>
 
<tr class="header">
 
<th align="left">'''Function'''</th>
 
<th align="left">'''Description'''</th>
 
</tr>
 
<tr class="odd">
 
<td align="left">AllocateTagItems()</td>
 
<td align="left">Allocate a TagItem array (or chain).</td>
 
</tr>
 
<tr class="even">
 
<td align="left">FreeTagItems()</td>
 
<td align="left">Frees allocated TagItem lists.</td>
 
</tr>
 
<tr class="odd">
 
<td align="left">CloneTagItems()</td>
 
<td align="left">Copies a TagItem list.</td>
 
</tr>
 
<tr class="even">
 
<td align="left">RefreshTagItemClone()</td>
 
<td align="left">Rejuvenates a clone from the original.</td>
 
</tr>
 
<tr class="odd">
 
<td align="left">FindTagItem()</td>
 
<td align="left">Scans TagItem list for a tag.</td>
 
</tr>
 
<tr class="even">
 
<td align="left">GetTagData()</td>
 
<td align="left">Obtain data corresponding to tag.</td>
 
</tr>
 
<tr class="odd">
 
<td align="left">NextTagItem()</td>
 
<td align="left">Iterate TagItem lists.</td>
 
</tr>
 
<tr class="even">
 
<td align="left">TagInArray()</td>
 
<td align="left">Check if a tag value appears in a Tag array.</td>
 
</tr>
 
<tr class="odd">
 
<td align="left">FilterTagChanges()</td>
 
<td align="left">Eliminate TagItems which specify no change.</td>
 
</tr>
 
<tr class="even">
 
<td align="left">FilterTagItems()</td>
 
<td align="left">Remove selected items from a TagItem list.</td>
 
</tr>
 
<tr class="odd">
 
<td align="left">MapTags()</td>
 
<td align="left">Convert ti_Tag values in a list via map pairing.</td>
 
</tr>
 
<tr class="even">
 
<td align="left">PackBoolTags()</td>
 
<td align="left">Builds a “Flag” word from a TagItem list.</td>
 
</tr>
 
</table>
 
 
=== Callback Hook Function Reference ===
 
 
The following are brief descriptions of the utility library functions which pertain to callback hooks.
 
 
<table>
 
<tr class="header">
 
<th align="left">'''Function'''</th>
 
<th align="left">'''Description'''</th>
 
</tr>
 
<tr class="odd">
 
<td align="left">CallHookPkt()</td>
 
<td align="left">Call a standard callback Hook function.</td>
 
</tr>
 
</table>
 
 
=== 32-Bit Integer Math Function Reference ===
 
 
The following are brief descriptions of the utility library functions which pertain to 32-bit integer math.
 
 
<table>
 
<tr class="header">
 
<th align="left">'''Function'''</th>
 
<th align="left">'''Description'''</th>
 
</tr>
 
<tr class="odd">
 
<td align="left">SDivMod32()</td>
 
<td align="left">Signed 32 by 32-bit division and modulus.</td>
 
</tr>
 
<tr class="even">
 
<td align="left">SMult32()</td>
 
<td align="left">Signed 32 by 32-bit multiplication.</td>
 
</tr>
 
<tr class="odd">
 
<td align="left">UDivMod32()</td>
 
<td align="left">Unsigned 32 by 32-bit division modulus.</td>
 
</tr>
 
<tr class="even">
 
<td align="left">UMult32()</td>
 
<td align="left">Unsigned 32 by 32-bit multiplication.</td>
 
</tr>
 
</table>
 
 
=== International String Function Reference ===
 
 
The following are brief descriptions of the utility library functions which pertain to string operations using the international ASCII character set.
 
 
<table>
 
<tr class="header">
 
<th align="left">'''Function'''</th>
 
<th align="left">'''Description'''</th>
 
</tr>
 
<tr class="odd">
 
<td align="left">Stricmp()</td>
 
<td align="left">Compare strings, case-insensitive.</td>
 
</tr>
 
<tr class="even">
 
<td align="left">Strnicmp()</td>
 
<td align="left">Compare strings, case-insensitive, with specified length.</td>
 
</tr>
 
<tr class="odd">
 
<td align="left">ToLower()</td>
 
<td align="left">Convert a character to lower case.</td>
 
</tr>
 
<tr class="even">
 
<td align="left">ToUpper()</td>
 
<td align="left">Convert a character to upper case.</td>
 
</tr>
 
</table>
 
 
=== Date Function Reference ===
 
 
The following are brief descriptions of the utility library functions which pertain to date conversion.
 
 
<table>
 
<tr class="header">
 
<th align="left">'''Function'''</th>
 
<th align="left">'''Description'''</th>
 
</tr>
 
<tr class="odd">
 
<td align="left">CheckDate()</td>
 
<td align="left">Check the legality of a date.</td>
 
</tr>
 
<tr class="even">
 
<td align="left">Amiga2Date()</td>
 
<td align="left">Calculate the date from a specified timestamp.</td>
 
</tr>
 
<tr class="odd">
 
<td align="left">Date2Amiga()</td>
 
<td align="left">Calculate the timestamp from a specified date.</td>
 
</tr>
 
</table>
 

Latest revision as of 22:28, 26 March 2014

WIP.png This page is currently being updated to AmigaOS 4.x. Some of the information contained here may not yet be applicable in part or totally.

Utility Library

Utility library is the home for all the OS functions which don't fit in the other libraries. Among the calls in utility library are calls to manage tags and tag lists, callback hooks, strings, data structures and more.

The various functions are grouped into several sections: