Oracle® Call Interface Programmer's Guide, 11g Release 2 (11.2) Part Number E10646-02 |
|
|
View PDF |
This section describes the string formatting functions.
Table 20-7 String Formatting Functions
Function/Page | Purpose |
---|---|
Initializes the OCIFormat package. |
|
Writes a text string into the supplied text buffer. |
|
Terminates the OCIFormat package. |
See Also:
For more information about using these functions, see Oracle Database Data Cartridge Developer's GuidePurpose
Initializes the OCIFormat package.
Syntax
sword OCIFormatInit( void *hndl, OCIError *err);
Parameters
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()
.
Comments
This routine must be called before calling any other OCIFormat routine and it must only be called once.
Returns
OCI_SUCCESS
,
OCI_INVALID_HANDLE
,
OCI_ERROR
.
Purpose
Terminates the OCIFormat package.
Syntax
sword OCIFormatTerm( void *hndl, OCIError *err);
Parameters
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()
.
Comments
This function must be called after the OCIFormat package is no longer being used and it must only be called once.
Returns
OCI_SUCCESS
,
OCI_INVALID_HANDLE
,
OCI_ERROR
.
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
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 '%' 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 the tables that follow.
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
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 may be in any order and are all optional.
Table 20-8 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 will always have a radix character.
Field Width
<w>
where <w>
is a number specifying a minimum field width. The converted argument will be 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 will be 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 in place of <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>
specifies a period followed by the number <p>
, specifying 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 zeroes will be added to make up the difference). The special character '*'
may be used in place of <p>
indicating 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 codes can appear in upper case, which will cause all alphabetic characters in the output to appear in upper case except for text strings, which are not converted.
Table 20-9 Codes
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
/* This example shows the power of arbitrary argument */ /* selection in the context of internationalization. A */ /* date is formatted in 2 different ways for 2 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
,
OCI_ERROR
.