Oracle® Call Interface Programmer's Guide 11g Release 2 (11.2) Part Number E10646-08 |
|
|
PDF · Mobi · ePub |
Table 20-6 lists the string formatting functions that are described in this section.
Table 20-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 functionsInitializes the OCIFormat package.
sword OCIFormatInit( void *hndl, OCIError *err);
The OCI environment or user session handle.
The OCI error handle. If there is an error, it is recorded in err
, and this function returns OCI_ERROR
. Diagnostic information can be obtained by calling OCIErrorGet().
This routine must be called before calling any other OCIFormat routine, and it must be called only once.
OCI_SUCCESS
; OCI_INVALID_HANDLE
; or OCI_ERROR
.
Writes a text string into the supplied text buffer using the argument list submitted to it and in accordance with the format string given.
sword OCIFormatString( void *hndl, OCIError *err, OraText *buffer, sbig_ora bufferLength, sbig_ora *returnLength, const OraText *formatString,... );
The OCI environment or user session handle.
The OCI error handle. If there is an error, it is recorded in err
, and this function returns OCI_ERROR
. Diagnostic information can be obtained by calling OCIErrorGet().
The buffer that contains the string.
The length of the buffer in bytes.
The number of bytes written to the buffer (excluding the terminating NULL
).
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 20-7 and Table 20-8.
Variable number of arguments of the form OCIFormat type wrapper
(variable
) where variable
must be a variable containing the value to be used. No constant values or expressions are allowed as arguments to the OCIFormat type wrappers
; The OCIFormat type wrappers
that are available are listed next. The argument list must be terminated with OCIFormatEnd
.
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
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.
A format modifier alters or extends the format specification, allowing more specialized output. The format modifiers, as described in Table 20-7, can be in any order and are all optional.
Table 20-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.
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.
<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.
.<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.
(<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()
.
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 20-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 20-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 20-2 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". */
OCI_SUCCESS
; OCI_INVALID_HANDLE
; or OCI_ERROR
.
Terminates the OCIFormat package.
sword OCIFormatTerm( void *hndl, OCIError *err);
The OCI environment or user session handle.
The OCI error handle. If there is an error, it is recorded in err
, and this function returns OCI_ERROR
. Diagnostic information can be obtained by calling OCIErrorGet().
This function must be called after the OCIFormat package is no longer being used, and it must be called only once.
OCI_SUCCESS
; OCI_INVALID_HANDLE
; or OCI_ERROR
.