Copyright Digital Equipment Corp. All rights reserved.

Example

#include <wchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

 /* This program verifies that two transformed strings,    */
 /* when passed through wcsxfrm and then compared, provide */
 /* the same result as if passed through wcscoll without   */
 /* any transformation.                                    */

#define  BUFF_SIZE  20

main()
{
    wchar_t w_string1[BUFF_SIZE];
    wchar_t w_string2[BUFF_SIZE];
    wchar_t w_string3[BUFF_SIZE];
    wchar_t w_string4[BUFF_SIZE];
    int errno;
    int coll_result;
    int wcscmp_result;
    size_t wcsxfrm_result1;
    size_t wcsxfrm_result2;

    /* setlocale to French locale */

    if (setlocale(LC_ALL, "fr_FR.ISO8859-1") == NULL) {
        perror("setlocale");
        exit(EXIT_FAILURE);
    }

    /* Convert each of the strings into wide-character format. */

    if (mbstowcs(w_string1, "<a`>bcd", BUFF_SIZE) == (size_t)-1) {

        perror("mbstowcs");
        exit(EXIT_FAILURE);
    }

    if (mbstowcs(w_string2, "abcz", BUFF_SIZE) == (size_t)-1) {

        perror("mbstowcs");
        exit(EXIT_FAILURE);
    }

    /* Collate string 1 and string 2 and store the result. */

    errno = 0;
    coll_result = wcscoll(w_string1, w_string2);
    if (errno) {
        perror("wcscoll");
        exit(EXIT_FAILURE);
    }
    else {

       /*  Transform the strings (using wcsxfrm) into  */
       /*  w_string3 and w_string4.                    */

       wcsxfrm_result1 = wcsxfrm(w_string3, w_string1, BUFF_SIZE);

       if (wcsxfrm_result1 == ((size_t) - 1))
           perror("wcsxfrm");
       else if (wcsxfrm_result1 > BUFF_SIZE) {
           perror("\n** String is too long **\n");
           exit(EXIT_FAILURE);
       }
       else {
          wcsxfrm_result2 = wcsxfrm(w_string4, w_string2, BUFF_SIZE);
          if (wcsxfrm_result2 == ((size_t) - 1)) {
              perror("wcsxfrm");
              exit(EXIT_FAILURE);
           }
          else if (wcsxfrm_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 wcscoll on */
        /* the original strings.                                */

          else {
              wcscmp_result = wcscmp(w_string3, w_string4);
              if (wcscmp_result == 0 && (coll_result == 0)) {
                  printf("\nReturn value from wcscoll() and return value"
                                       " from wcscmp() are both zero.");
                  printf("\nThe program was successful\n\n");
              }
              else if ((wcscmp_result < 0) && (coll_result < 0)) {
                  printf("\nReturn value from wcscoll() and return value"
                                  " from wcscmp() are less than zero.");
                  printf("\nThe program was successful\n\n");
              }
              else if ((wcscmp_result > 0) && (coll_result > 0)) {
                  printf("\nReturn value from wcscoll() and return value"
                                " from wcscmp() 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 wcscoll() and return value
          from wcscmp() are less than zero.
   The program was successful