|
VMS Help CRTL, strxfrm, Example *Conan The Librarian |
/* This program verifies that two transformed strings when */
/* passed through strxfrm and then compared, provide the same */
/* result as if passed through strcoll without any */
/* transformation.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#define BUFF_SIZE 256
main()
{
char string1[BUFF_SIZE];
char string2[BUFF_SIZE];
int errno;
int coll_result;
int strcmp_result;
size_t strxfrm_result1;
size_t strxfrm_result2;
/* setlocale to French locale */
if (setlocale(LC_ALL, "fr_FR.ISO8859-1") == NULL) {
perror("setlocale");
exit(EXIT_FAILURE);
}
/* collate string 1 and string 2 and store the result */
errno = 0;
coll_result = strcoll("<a`>bcd", "abcz");
if (errno) {
perror("strcoll");
exit(EXIT_FAILURE);
}
else {
/* Transform the strings (using strxfrm) into string1 */
/* and string2 */
strxfrm_result1 = strxfrm(string1, "<a`>bcd", BUFF_SIZE);
if (strxfrm_result1 == ((size_t) - 1)) {
perror("strxfrm");
exit(EXIT_FAILURE);
}
else if (strxfrm_result1 > BUFF_SIZE) {
perror("\n** String is too long **\n");
exit(EXIT_FAILURE);
}
else {
strxfrm_result2 = strxfrm(string2, "abcz", BUFF_SIZE);
if (strxfrm_result2 == ((size_t) - 1)) {
perror("strxfrm");
exit(EXIT_FAILURE);
}
else if (strxfrm_result2 > BUFF_SIZE) {
perror("\n** String is too long **\n");
exit(EXIT_FAILURE);
}
/* Compare the two transformed strings and verify */
/* that the result is the same as the result from */
/* strcoll on the original strings */
else {
strcmp_result = strcmp(string1, string2);
if (strcmp_result == 0 && (coll_result == 0)) {
printf("\nReturn value from strcoll() and "
"return value from strcmp() are both zero.");
printf("\nThe program was successful\n\n");
}
else if ((strcmp_result < 0) && (coll_result < 0)) {
printf("\nReturn value from strcoll() and "
"return value from strcmp() are less than zero.");
printf("\nThe program successful\n\n");
}
else if ((strcmp_result > 0) && (coll_result > 0)) {
printf("\nReturn value from strcoll() and "
"return value from strcmp() are greater than zero.");
printf("\nThe program was successful\n\n");
}
else {
printf("** Error **\n");
printf("\nReturn values are not of the same type");
}
}
}
}
}
Running the example program produces the following result:
Return value from strcoll() and return value
from strcmp() are less than zero.
The program was successful
|
|