29.7 Cartridge Services — String Formatting Interface
Lists and describes cartridge services string formatting functions.
Table 29-6 lists the string formatting functions that are described in this section.
Table 29-6 String Formatting Functions
Function | Purpose |
---|---|
Initialize the OCIFormat package |
|
Write a text string into the supplied text buffer |
|
Terminate the OCIFormat package |
See Also:
Oracle Database Data Cartridge Developer's Guide for more information about using these functions
29.7.1 OCIFormatInit()
Initializes the OCIFormat package.
Purpose
Initializes the OCIFormat package.
Syntax
sword OCIFormatInit( void *hndl, OCIError *err);
Parameters
Comments
This routine must be called before calling any other OCIFormat routine, and it must be called only once.
Returns
OCI_SUCCESS
; OCI_INVALID_HANDLE
; or OCI_ERROR
.
Related Topics
29.7.2 OCIFormatString()
Writes a text string into the supplied text buffer using the argument list submitted to it and in accordance with the format string given.
Purpose
Writes a text string into the supplied text buffer using the argument list submitted to it and in accordance with the format string given.
Syntax
sword OCIFormatString( void *hndl, OCIError *err, OraText *buffer, sbig_ora bufferLength, sbig_ora *returnLength, const OraText *formatString,... );
Parameters
- hndl (IN)
-
The OCI environment or user session handle.
- err (IN/OUT)
-
The OCI error handle. If there is an error, it is recorded in
err
, and this function returnsOCI_ERROR
. Diagnostic information can be obtained by callingOCIErrorGet()
. - buffer (OUT)
-
The buffer that contains the string.
- bufferLength (IN)
-
The length of the buffer in bytes.
- returnLength (OUT)
-
The number of bytes written to the buffer (excluding the terminating
NULL
). - formatString (IN)
-
The format string, which can be any combination of literal text and format specifications. A format specification is delimited by the percent character (%) and is followed by any number (including none) of optional format modifiers, and terminated by a mandatory format code. If the format string ends with %, that is, with no format modifiers, or format specifier following it, then no action is taken. The format modifiers and format codes available are described in Table 29-7 and Table 29-8.
- ... (IN)
-
Variable number of arguments of the form
OCIFormat type wrapper
(variable
) wherevariable
must be a variable containing the value to be used. No constant values or expressions are allowed as arguments to theOCIFormat type wrappers
; TheOCIFormat type wrappers
that are available are listed next. The argument list must be terminated withOCIFormatEnd
.OCIFormatUb1(ub1 variable);
OCIFormatUb2(ub2 variable);
OCIFormatUb4(ub4 variable);
OCIFormatUword(uword variable);
OCIFormatUbig_ora(ubig_ora variable);
OCIFormatSb1(sb1 variable);
OCIFormatSb2(sb2 variable);
OCIFormatSb4(sb4 variable);
OCIFormatSword(sword variable);
OCIFormatSbig_ora(sbig_ora variable);
OCIFormatEb1(eb1 variable);
OCIFormatEb2(eb2 variable);
OCIFormatEb4(eb4 variable);
OCIFormatEword(eword variable);
OCIFormatChar (text variable);
OCIFormatText(const text *variable);
OCIFormatDouble(double variable);
OCIFormatDvoid(const dvoid *variable);
OCIFormatEnd
Comments
The first call to this routine must be preceded by a call to the OCIFormatInit()
routine that initializes the OCIFormat
package for use. When this routine is no longer needed terminate the OCIFormat
package by a call to the OCIFormatTerm()
routine.
Format Modifiers
A format modifier alters or extends the format specification, allowing more specialized output. The format modifiers, as described in Table 29-7, can be in any order and are all optional.
Table 29-7 Format Modifier Flags
Flag | Operation |
---|---|
|
Left-justify the output in the field. |
|
Always print a sign ('+' or '–') for numeric types. |
|
If a number's sign is not printed, then print a space in the sign position. |
|
Pad numeric output with zeros, not spaces. |
-
If both the
'+'
and' '
flags are used in the same format specification, then the' '
flag is ignored. -
If both the
'-'
and'0'
flags are used in the same format specification, then the'-'
flag is ignored.
Alternate output:
-
For the octal format code, add a leading zero.
-
For the hexadecimal format code, add a leading '0x'.
-
For floating-point format codes, the output always has a radix character.
Field Width
<w>
where <w>
is a number specifying a minimum field width. The converted argument is printed in a field at least this wide, and wider if necessary. If the converted argument takes up fewer display positions than the field width, it is padded on the left (or right for left justification) to make up the field width. The padding character is normally a space, but it is a zero if the zero padding flag was specified. The special character '*'
may be used for <w>
and indicates the current argument is to be used for the field width value; the actual field or precision follows as the next sequential argument.
Precision
.<p>
(a period followed by the number <p>
), specifies the maximum number of display positions to print from a string, or digits after the radix point for a decimal number, or the minimum number of digits to print for an integer type (leading zeros are added to make up the difference). The special character '*'
may be used for <p>
, indicating that the current argument contains the precision value.
Argument Index
(<n>
) where <n>
is an integer index into the argument list with the first argument being 1. If no argument index is specified in a format specification, the first argument is selected. The next time no argument index is specified in a format specification, the second argument is selected, and so on. Format specifications with and without argument indexes can be in any order and are independent of each other in operation.
For example, the format string "%u %(4)u %u %(2)u %u"
selects the first, fourth, second, second, and third arguments given to OCIFormatString()
.
Format Codes
A format code specifies how to format an argument that is being written to a string.
Note that these format codes, as described in Table 29-8, can appear in uppercase, which causes all alphabetic characters in the output to appear in uppercase except for text strings, which are not converted.
Table 29-8 Format Codes to Specify How to Format an Argument Written to a String
Codes | Operation |
---|---|
|
Single-byte character in the compiler character set |
|
Signed decimal integer |
|
Exponential (scientific) notation of the form [ |
|
Fixed decimal notation of the form |
|
Variable floating-point notation; chooses |
|
Identical to |
|
Unsigned octal integer |
|
Operating system-specific pointer printout |
|
Prints an argument using the default format code for its type:
The format code used is 'u'.
The format code used is 'd'.
The format code used is 'c'.
Prints text until trailing
The format code used is 'g'.
The format code used is 'p'.
|
|
Unsigned decimal integer |
'x' |
Unsigned hexadecimal integer |
Example
Using OCIFormatString() to Format a Date Two Different Ways for Two Countries
/* This example shows the power of arbitrary argument */ /* selection in the context of internationalization. A */ /* date is formatted in two different ways for two different */ /* countries according to the format string, yet the */ /* argument list submitted to OCIFormatString remains */ /* invariant. */ text buffer[255]; ub1 day, month, year; OCIError *err; void *hndl; sbig_ora returnLen; /* Set the date. */ day = 10; month = 3; year = 97; /* Work out the date in United States style: mm/dd/yy */ OCIFormatString(hndl, err, buffer, (sbig_ora)sizeof(buffer), &returnLen (const text *)"%(2)02u/%(1)02u/%(3)02u", OCIFormatUb1(day), OCIFormatUb1(month), OCIFormatUb1(year), OCIFormatEnd); /* Buffer is "03/10/97". */ /* Work out the date in New Zealand style: dd/mm/yy */ OCIFormatString(hndl, err, buffer, (sbig_ora)sizeof(buffer), &returnLen (const text *)"%(1)02u/%(2)02u/%(3)02u", OCIFormatUb1(day), OCIFormatUb1(month), OCIFormatUb1(year), OCIFormatEnd); /* Buffer is "10/03/97". */
Returns
OCI_SUCCESS
; OCI_INVALID_HANDLE
; or OCI_ERROR
.
Related Topics
29.7.3 OCIFormatTerm()
Terminates the OCIFormat package.
Purpose
Terminates the OCIFormat package.
Syntax
sword OCIFormatTerm( void *hndl, OCIError *err);
Parameters
Comments
This function must be called after the OCIFormat package is no longer being used, and it must be called only once.
Returns
OCI_SUCCESS
; OCI_INVALID_HANDLE
; or OCI_ERROR
.
Related Topics