VistaIOattribute(3) | Library Functions Manual | VistaIOattribute(3) |
VistaIOattribute - representation of a list of name/value pairs
VistaIOAttrList list;
VistaIOAttrListPosn posn;
An attribute associates a value with a name. Generally, various properties of an object such as an image are described by attributes, each naming one property and specifying that property's associated value. Attributes are collected into lists, and Vista library routines are available for accessing and manipulating these lists. A list is referred to by a pointer of type VistaIOAttrList.
The attribute list representation is closely related to the Vista data file format. Among other things, attribute lists can represent in memory the contents of Vista data files.
An attribute's name is an alphanumeric string — specifically, a sequence of characters matching the regular expression [a-zA-Z0-9_.+-]+. (Regular expressions are described in grep(1).) Attribute names are represented in memory as null-terminated character strings. They may be 50 character in length.
An attribute's value may be a character string, a (nested) attribute list, or a pointer to data of a specified type.
The elements of an attribute list may be accessed by attribute name, in which case each element should have a unique name and the order of attributes within the list is not significant. In this form of access the attribute list is treated as a set. Alternatively, the elements of an attribute list may be accessed by their position within the list, in which case attribute names needn't be unique and the ordering of list elements is significant. In this form of access the attribute list is treated as a sequence. The library provides support for both forms of access.
A position within an attribute list is identified by a structure of type VistaIOAttrListPosn. Its value can denote a particular attribute within a list, or no attribute (analogous to NULL
The following routines will create, copy, and destroy attribute lists.
VistaIOAttrList VistaIOCreateAttrList (void)
VistaIOAttrList VistaIOCopyAttrList (VistaIOAttrList list)
void VistaIODestroyAttrList (VistaIOAttrList list)
The following macros are used to sequence through an attribute list in either direction. They are appropriate when accessing an attribute list representing a sequence (as opposed to a set) of attributes.
void VistaIOFirstAttr (VistaIOAttrList list, VistaIOAttrListPosn *posn)
void VistaIOLastAttr (VistaIOAttrList list, VistaIOAttrListPosn *posn)
void VistaIONextAttr (VistaIOAttrListPosn *posn)
void VistaIOPrevAttr (VistaIOAttrList list, VistaIOAttrListPosn *posn)
VistaIOBoolean VistaIOAttrExists (VistaIOAttrListPosn *posn)
VistaIOBoolean VistaIOAttrListEmpty (VistaIOAttrList *list)
The following macros and routines access the attribute at a specified position within an attribute list.
VistaIOStringConst VistaIOGetAttrName (VistaIOAttrListPosn *posn)
VistaIORepnKind VistaIOGetAttrRepn (VistaIOAttrListPosn *posn)
VistaIOBoolean VistaIOGetAttrValue (VistaIOAttrListPosn *posn, VistaIODictEntry *dict, VistaIORepnKind repn, VistaIOPointer value)
void VistaIOSetAttrValue (VistaIOAttrListPosn *posn, VistaIODictEntry *dict, VistaIORepnKind repn, type value)
The following routines access attributes by name. They are appropriate when accessing an attribute list representing a set of attributes, in which each attribute name is present at most once.
VistaIOBoolean VistaIOLookupAttr (VistaIOAttrList list, VistaIOStringConst name, VistaIOAttrListPosn *posn)
VistaIOGetAttrResult VistaIOGetAttr (VistaIOAttrList *list, VistaIOStringConst name, VistaIODictEntry *dict, VistaIORepnKind repn, VistaIOPointer value)
void VistaIOSetAttr (VistaIOAttrList *list, VistaIOStringConst name, VistaIODictEntry *dict, VistaIORepnKind repn, type value)
The following routines add attributes to lists and remove them.
void VistaIOInsertAttr (VistaIOAttrListPosn *posn, VistaIOBoolean after, VistaIOStringConst name, VistaIODictEntry *dict, VistaIORepnKind repn, type value)
void VistaIODeleteAttr (VistaIOAttrListPosn *posn)
void VistaIOAppendAttr (VistaIOAttrList *list, VistaIOStringConst name, VistaIODictEntry *dict, VistaIORepnKind repn, type value)
void VistaIOPrependAttr (VistaIOAttrList *list, VistaIOStringConst name, VistaIODictEntry *dict, VistaIORepnKind repn, type value)
VistaIOBoolean VistaIOExtractAttr (VistaIOAttrList *list, VistaIOStringConst name, VistaIODictEntry *dict, VistaIORepnKind repn, VistaIOPointer value, VistaIOBoolean required)
The following routines translate character-string attribute values to and from other representations.
VistaIOStringConst VistaIOEncodeAttrValue (VistaIODictEntry *dict, VistaIORepnKind repn, type value)
VistaIOBoolean VistaIODecodeAttrValue (VistaIOStringConst str, VistaIODictEntry *dict, VistaIORepnKind repn, VistaIOPointer value)
The following routines read and write attribute lists.
VistaIOAttrList VistaIOReadFile (FILE *file, VistaIOBoolean (*filter) ())
VistaIOBoolean VistaIOWriteFile (FILE *file, VistaIOAttrList list)
To program with attribute lists it is necessary to know something about how storage is managed for attribute values. The general rule is that when individual attributes are accessed, values aren't created, copied, or destroyed — only references to them are manipulated. On the other hand when an entire attribute list is create, copied, or destroyed, then all the values will be created, copied, or destroyed along with it. Unfortunately, the exact behavior must depend on both the type of value an attribute has, and the type of operation being performed with the attribute. The following table summarizes the various cases.
For an attribute whose value is a character string:
For an attribute whose value is an attribute list, object, or VistaIOBundle:
For an attribute whose value is a VistaIOPointer:
typedef struct _VAttrRec {
VistaIOAttrRec *next; /* next in list */ VistaIOAttrRec *prev; /* previous in list */ VistaIORepnKind repn; /* rep'n of attribute value */ VistaIOPointer value; /* pointer to attribute value */ char name[1]; /* beginning of name string */
} VistaIOAttrRec, *VistaIOAttrList;
typedef struct {
VistaIOAttrList list; /* the list */ VistaIOAttrRec *ptr; /* position within the list */
} VistaIOAttrListPosn;
Programmers using attribute lists will usually not need to access components of the attribute list data structure directly from their code since there are library routines and macros available for most purposes. However, when debugging, one may occasionally need to examine an attribute list directly.
In a list of attributes, each attribute is represented by a VistaIOAttrRec record. Records are allocated with a variable size sufficient to contain the attribute name and, if the attribute value is stored as a character string, the attribute value also.
The record's name field is a variable-length character array containing the attribute's name terminated by a NULL character. The repn field specifies whether the attribute's value is stored as a character string (VistaIOStringRepn), an attribute list (VistaIOAttrListRepn), a pointer to an opaque data structure (VistaIOPointerRepn), or some other object type (VistaIOBundle, VistaIOImage, VistaIOEdges, etc.). Accordingly, the value field will point to a null-terminated character string, an attribute list, an opaque data structure, a VistaIOBundleRec structure, etc. If the value is a string, that string immediately follows the name string in the variable length record. The next and prev fields form a doubly-linked list, with NULL pointers marking the list's ends.
An attribute list contains, in addition to a VistaIOAttrRec record for each of its attributes, one VistaIOAttrRec record serving as a list header. The only significant fields of this header record are the next and prev fields, which point to the first and last attributes of the list. An attribute list is of type VistaIOAttrList, which is a pointer to the list's header record.
The library includes routines and macros that allow one to step through an attribute list, and to access an attribute by its position in a list. These employ the VistaIOAttrListPosn type, which acts as a pointer to a particular attribute within a list. The VistaIOAttrListPosn's list field identifies an entire list, and its ptr field refers to a specific attribute within that list.
The examples in this section assume the following variables:
VistaIOAttrList list; VistaIOAttrListPosn posn; VistaIOImage image, result;
This code fragment creates an attribute list, sets some attributes in it, writes the list to the standard output stream, and finally destroys the list.
list = VistaIOCreateAttrList (); VistaIOSetAttr (list, "name", NULL, VistaIOStringRepn, "Socrates"); VistaIOSetAttr (list, "comment", NULL, VistaIOStringRepn, "Greek philosopher"); VistaIOSetAttr (list, "age", NULL, VistaIOShortRepn, (VistaIOShort) 37); VistaIOWriteFile (stdout, list); VistaIODestroyAttrList (list);
This reads an attribute list from the standard input stream and prints the name of each of its attributes in sequence:
list = VistaIOReadFile (stdin, NULL); for (VistaIOFirstAttr (list, & posn); VistaIOAttrExists (& posn); VistaIONextAttr (& posn)) printf ("%s\n", VistaIOGetAttrName (& posn));
To add a series of images to an attribute list:
while (...) { image = VistaIOCreateImage (...); VistaIOAppendAttr (list, ..., VistaIOImageRepn, image); /* Note: don't discard image. */ }
To modify each image in an attribute list without replacing it:
for (VistaIOFirstAttr (list, & posn); VistaIOAttrExists (& posn); VistaIONextAttr (& posn)) { if (VistaIOGetAttrRepn (& posn) != VistaIOImageRepn) continue; VistaIOGetAttrValue (& posn, NULL, VistaIOImageRepn, & image); ProcessImage (image, image, ...); /* Note: modifications to image are inherited by the attribute. */ }
To replace each image in an attribute list:
for (VistaIOFirstAttr (list, & posn); VistaIOAttrExists (& posn); VistaIONextAttr (& posn)) { if (VistaIOGetAttrRepn (& posn) != VistaIOImageRepn) continue; VistaIOGetAttrValue (& posn, NULL, VistaIOImageRepn, & image); result = ProcessImage (image, NULL, ...); VistaIOSetAttrValue (& posn, NULL, VistaIOImageRepn, result); /* Note: the attribute now refers to result, not image. */ VistaIODestroyImage (image); }
To delete a named image from an attribute list:
if (VistaIOLookupAttr (list, "...", & posn)) { VistaIOGetAttrValue (& posn, NULL, VistaIOImageRepn, & image); VistaIODeleteAttr (& posn); VistaIODestroyImage (image); }
VistaIOBundle(3), VistaIOdictionary(3),
VistaIOtype(3), ,
VistaIOGetAttrValue(3), VistaIOSetAttrValue(3),
VistaIOGetAttr(3), VistaIOSetAttr(3),
VistaIOInsertAttr(3), VistaIOAppendAttr(3),
VistaIOPrependAttr(3), VistaIOExtractAttr(3),
VistaIOEncodeAttrValue(3), VistaIODecodeAttrValue(3),
VistaIOReadFile(3), VistaIOWriteFile(3)
Art Pope <pope@cs.ubc.ca>
Adaption to vistaio: Gert Wollny <gw.fossdev@gmail.com>
28 January 1994 | VistaIO Version 1.2.14 |