[ Previous | Next | Contents | Glossary | Home | Search ]
Motif 2.1 Programmer's Guide



Parse Tables

A parse mapping (data type XmParseMapping) is a fragment of multibyte text paired with a compound string component. When translating text into a compound string, using the XmParse functions, any occurrences of the parse mapping text will be translated into the compound string component with which it is paired in a parse mapping. Likewise, when converting a compound string back into regular text, each compound string component that matches a parse mapping will be translated into the corresponding text string. A parse table (data type XmParseTable) is nothing more than an ordered array of XmParseMappings.

Structure of a Parse Mapping

Though the parse mapping is at heart nothing more than the text/compound string pairs, Motif stores each parse mapping with resources that indicate how the parsing is to proceed. Note that these are not widget resources, but the widget resource model provides a convenient way to specify and recall parse mapping instructions.

  1. XmParseMappingCreate

  2. XmParseMappingGetValues

  3. XmParseMappingSetValues

    Following this model, an application uses the XtSetArg function to build a resource-style argument list to submit to the XmParseMappingCreate, XmParseMappingGetValues, or XmParseMappingSetValues in the same manner that would be used to create or change the resource values of some widget.

    The parse mapping "resources" are outlined briefly below. For more information about the XmParseMapping data type and its resources, please see the Motif Programmer's Reference.

    XmNpattern
    Specifies a pattern to be matched in the text being parsed. At present, this is a maximum of one character.

    XmNpatternType
    Specifies the type of the pattern that is the value of XmNpattern. Following are the possible values:

    1. XmCHARSET_TEXT (default)

    2. XmMULTIBYTE_TEXT

    3. XmWIDECHAR_TEXT

      XmNsubstitute
      Specifies the compound string to be included in the compound string being constructed when XmNincludeStatus is XmINSERT or XmTERMINATE.

      XmNincludeStatus
      Specifies how the result of the mapping is to be included in the compound string being constructed. Unless the value is XmINVOKE, the result of the mapping is the value of XmNsubstitute. Following are the possible values for XmNincludeStatus:

      XmINSERT
      Concatenate the result to the compound string being constructed and continue parsing. This is the default value.

      XmINVOKE
      Invoke the XmNinvokeParseProc on the text being parsed and use the returned compound string instead of XmNsubstitute as the result to be inserted into the compound string being constructed. The include status returned by the parse procedure (XmINSERT or XmTERMINATE) determines how the returned compound string is included.

      XmTERMINATE
      Concatenate the result to the compound string being constructed and terminate parsing.

      XmNinvokeParseProc
      Specifies the parse procedure to be invoked when XmNincludeStatus is XmINVOKE. This defaults to NULL. See the following section about the parse procedure.

      XmNclientData
      This is a XtPointer used to indicate data to be used by the parse procedure. The default value is NULL.

    4. Parse Procedures

      The Motif parsing mechanism allows for the execution of an arbitrarily complex parsing algorithm. If the simple substitution of compound string components for text strings is not powerful enough for a particular application, a programmer can define a parse procedure that will be invoked when certain text characters are read.

      While parsing a text string, if an XmNpattern attached to a non-NULL XmNinvokeParseProc resource, matches part of the input string, the nominated parse procedure is invoked.

      The XmNinvokeParseProc resource is a function of type XmParseProc, which is defined as follows:

      XmIncludeStatus (*XmParseProc)(
              XtPointer       *
      text_in_out,
              XtPointer       
      text_end,
              XmTextType      
      type,
              XmStringTag     
      tag,
              XmParseMapping  
      entry,
              int     
      pattern_length,
              XmString        *
      str_include,
              XtPointer       
      call_data)

      The input text is a pointer to the first byte of the pattern that was matched to trigger the call to the parse procedure. The parse procedure may use, modify, or remove as many bytes of the input string as it needs, as long as upon return the input text pointer is advanced to the following byte. It returns a compound string to be included in the compound string being constructed, as well as an XmIncludeStatus, corresponding to the parse mapping resource, indicating how the returned compound string should be handled.

      text_in_out
      Specifies the text being parsed. The value is a pointer to the first byte of text matching the pattern that triggered the call to the parse procedure. When the parse procedure returns, this argument is set to the position in the text where parsing should resume--that is, to the byte following the last character parsed by the parse procedure.

      text_end
      Specifies a pointer to the end of the text_in_out string. If text_end is NULL, the string is scanned until a NULL character is found. Otherwise, the string is scanned up to but not including the character whose address is text_end.

      type
      Specifies the type of text and the tag type. If a locale tag should be created, type has a value of either XmMULTIBYTE_TEXT or XmWIDECHAR_TEXT. If a charset should be created, type has a value of XmCHARSET_TEXT.

      tag
      Specifies the tag to be used in creating the result string. The type of string tag created (charset or locale) depends on the text type and the tag value passed to the parse procedure. If the tag value is NULL and if type indicates that a charset tag should be created, the string tag has the value Xm_FONTLIST_DEFAULT_TAG. If type indicates a locale tag, the string tag has the value _MOTIF_DEFAULT_LOCALE.

      entry
      Specifies the parse mapping that triggered the call to the parse procedure.

      pattern_length
      Specifies the number of bytes in the input text, following text_in_out, that constitute the matched pattern.

      str_include
      Specifies a pointer to a compound string. The parse procedure creates a compound string to be included in the compound string being constructed. The parse procedure then returns the compound string in this argument.

      call_data
      Specifies data passed by the application to the parsing routine.

      The parse procedure returns an XmIncludeStatus indicating how str_include is to be included in the compound string being constructed. Following are the possible values:

      XmINSERT
      Concatenate the result to the compound string being constructed and continue parsing.

      XmTERMINATE
      Concatenate the result to the compound string being constructed and terminate parsing.

      Upon return, the function that invoked the parse procedure will continue looking for matching patterns from the position of the text pointer. If the parse procedure has not advanced the pointer, and the text and parse mappings also remain the same, the parse procedure will be called again, perhaps infinitely.

      Two parse procedures are provided as part of Motif.

      1. XmeGetNextCharacter

      2. XmeGetDirection

        The XmeGetNextCharacter procedure consumes the byte the input pointer indicates, and the following byte, and returns a compound string component containing only the second byte. The effect is to copy the byte following the trigger byte directly into the output compound string, even if the normal parsing would not have done so. In other words, the trigger byte has served as an "escape" character. The XmeGetNextCharacter return value is XmINSERT, so parsing will continue normally from the same spot.

        The other parse procedure included as a part of Motif is XmeGetDirection. This function simply returns an XmString component of type XmSTRING_COMPONENT_DIRECTION, whose value depends on the input text.

      3. Use of a Parse Table

        The following functions use a parse table to translate text into the compound string format and the reverse.

        1. XmStringParseText

        2. XmStringUnparse

          To use a parse table, send it and a companion text string to the XmStringParseText function. This function also accepts a pointer to data that may be passed to a parse procedure, if one exists. The function will walk through the input text string, copying characters from it into a compound string; when it finds a pattern (specified by XmNpattern in the parse mappings) to match a pattern in the input parse table, it will not copy the pattern, but write the corresponding compound string (indicated by XmNsubstitute in the parse mapping) to the output XmString.

          In the event it is desirable to translate a compound string back into a text string, the XmStringUnparse function is provided. This provides the precise converse to the XmStringParseText function, stepping through the input compound string and substituting text patterns for XmString components that match the XmNsubstitute resources in the parse table. If there is no parse procedure nominated in the parse table, the XmStringUnparse function called on the output of the XmStringParseText function will equal the input of XmStringParseText. A compound string created with a parse table that includes a parse procedure will not be correctly translated into simple text by a call to XmStringUnparse.

          1. XmParseMappingFree

          2. XmParseTableFree

            The routines that create parse mappings allocate memory in which to store those mappings. It is the responsibility of the application, however, to free that memory when the mappings and tables are no longer needed. Use XmParseMappingFree and XmParseTableFree to free the memory for other uses.


          3. [ Previous | Next | Contents | Glossary | Home | Search ]