structs.e


version 1.0a
written by Chris Bensler and Jason Mirwald
LAST MODIFIED : 10/02/02

 


Arrays:

An array of structures can be created either by a call to array, or by specifying a value other than 1 as the number of times a structure should be repeated when define_c_struct is called. The individual elements in an array of structures can be accessed using integer values in the elem argument of  peek_struct or poke_struct to specify an indice within the array. If the indice in the array is the only id provided in the argument elem in either routine, it must be enclosed in curly braces.

For example; the fifth element in an array of RGBQUAD structures could be accessed with; {5}.

Unions:

When reading and writing data within a union, and a specific element within the union is not specified in the elem argument of peek_struct or poke_struct, the first data type that was defined within the union will be used by default.

If you were previously using an earlier version of structs.e, please see the Version Differences comments at the end of the this file.

 


Predefined Data Types:

Following is a list of the predefined data types that can be used when defining a structure:

BYTE    = 8-bit unsigned integer
CHAR    = 8-bit signed integer
UCHAR   = 8-bit unsigned integer
SHORT   = 16-bit signed integer
USHORT  = 16-bit unsigned integer
INT     = 32-bit signed integer
UINT    = 32-bit unsigned integer
FLOAT   = IEEE floating-point number, 32-bit format
DOUBLE  = IEEE floating-point number, 64-bit format
LPSZ    = 32-bit pointer to a null terminated string
LONG    = 32-bit signed integer
ULONG   = 32-bit unsigned integer
POINTER = 32-bit pointer to a location in memory
BOOL    = 8-bit unsigned integer
WORD    = 16-bit unsigned integer
DWORD   = 32-bit unsigned integer
PTR     = 32-bit pointer to a location in memory
FLOAT32 = IEEE floating-point number, 32-bit format
FLOAT64 = IEEE floating-point number, 64-bit format
 


Function Index

define_c_struct
array
union
sizeOf
addressOf
alloc_struct
poke_struct
peek_struct


define_c_struct( sequence s )

Description: Define a C-style structure by specifying a list of elements that the structure is to contain. Each element can be one of the predefined data types, or another structure (including arrays and unions).

Each item listed in the sequence s should be a sequence containing three values; a string-id to identify the element, the type of data or a structure handle, and the number of times that data type is to be repeated within the structure.

Example:

   RGBQUAD = define_c_struct({
    { "Red",      BYTE, 1 },
    { "Blue",     BYTE, 1 },
    { "Green",    BYTE, 1 },
    { "Reserved", BYTE, 1 }
   })

Return: Returns an atom representing the handle of the new structure.


array( atom str, integer n )

Description: Defines an array, or a series, of like structures. The atom str is a structure handle returned from a previous call to define_c_struct, and the integer n specifies the number of times to repeat the structure str.

Example:

   palette = array( RGBQUAD, 256 )

Return: Returns an atom representing the handle of the new structure.


union( sequence s )

Description: Defines a union of different data types or structures. The number of bytes reserved for the union will always be equal to the number of bytes needed by the largest element defined within the union.

Each item listed in the sequence s should be a sequence containing three values; a string-id for the element, the type of data or a structure handle, and the number of times that data type is to be repeated within the structure.

Example:

   RGB_UNION = union({
    { "RGBQUAD",  RGBQUAD, 1 },
    { "colorval", DWORD,   1 }
   })

Return: Returns an atom representing the handle of the new structure.


sizeOf( integer str, sequence elem )

Description: Return the size, in bytes, of a structure or an element within a structure.

Arguments:

str: The argument str must be the handle of a structure that was returned from a call to define_c_struct.

elem: If the argument elem is a sequence of length zero, or {}, the size of the entire structure will be returned. By providing the string-id of one of the elements within the structure, or in the case of nested structures, a sequence of string-ids, the size of that element will be returned.

Return: Returns an atom representing the number of bytes needed to store the structure in memory.


addressOf( integer str, sequence elem )

Description: Retrieve a pointer to the memory address of specific element within a structure.

Arguments:

str: If the argument str is an atom, it must be the pointer to a structure that was returned from a previous call to alloc_struct. To retrieve the address of an element in a structure that was not previously allocated using alloc_struct, the argument str can be a sequence containing two atoms, the address in memory where the structure is located, and a structure handle that was returned by define_c_struct. The sequence should have the format: { addr, str }.

elem: If the argument elem is a sequence of length zero, or {}, the address of the structure will be returned. By providing the string-id of one of the elements within the structure, or in the case of nested structures, a sequence of string-ids, the address of that element will be returned.

Return: Returns an atom representing the address of the specified element within the structure.


alloc_struct( atom str )

Description: Allocates space for a structure in memory. The function dealloc (in heaps.e) should be used to deallocate the memory reserved for the structure when it is no longer needed.

Return: Returns a pointer to the structure in memory that can be used in calls to peek_struct or poke_struct to read or write data within the structure.


peek_struct( object str, sequence elem )

Description: Return the value or values stored in a given structure, or element within a structure, from memory.

Arguments:

str: If the argument str is an atom, it must be the pointer to a structure that was returned from a previous call to alloc_struct. To read data from a structure that was not previously allocated using alloc_struct, the argument str can be a sequence containing two atoms, the address in memory where the structure is located, and a structure handle that was returned by define_c_struct. The sequence should have the format: { addr, str }.

elem: If the argument elem is a sequence of length zero, or {}, the data from the entire structure will be returned as a sequence of values, one for each element in the structure. By providing the string-id of one of the elements within the structure to be read, or in the case of nested structures, a sequence of string-ids, a single element within a structure can be read. In the case of an array of structures, and integer can be passed to specify the indice within the array.

Return: The type of data that is returned depends upon the arguments supplied in the arguments to the routine. If an atom value is specified by str and elem, and atom will be returned. In the case of arrays or structures, a sequence of values will be returned.


poke_struct( object str, sequence element, object data )

Description: Poke the value or values in the object data into the structure str.

Arguments:

str: If the argument str is an atom, it must be the pointer to a structure that was returned from a previous call to alloc_struct. To write data into a structure that was not previously allocated using alloc_struct, the argument str can be a sequence containing two atoms, the address in memory where the structure is located, and a structure handle that was returned by define_c_struct. The sequence should have the format; {addr,str}.

elem: By providing the string-id of one of the elements within the structure to be written, or in the case of nested structures, a sequence of string-ids, a single element within a structure can be written. In the case of an array of structures, and integer can be provided to specify the indice within the array that is to be written to.

data: The argument data should represent the type, number, and size of the value or values that are to be poked into memory. In the case of nested structures, the data for the nested structure should also be nested within the object data.

Return: This procedure does not return a value.


Version Differences

V1.0a:

Official release of the structs.e

V0.12b:

Due to the reformatting of the library internally, the routines peek_aquired and poke_aquired are no longer needed, and have been removed. An application can use peek_struct and poke_struct and simply provide the address and structure handle in the str argument of the routine.

Unions and arrays are now supported, and the routines union and array were added to support them.