unicode.c
changeset 62 b7061c0e239f
child 63 5a82f89d607e
equal deleted inserted replaced
61:4b4c97f179da 62:b7061c0e239f
       
     1 /**
       
     2  * Unicode Testcase
       
     3  * Copyright (C) 2008 Markus Broeker
       
     4  */
       
     5 
       
     6 #include <stdio.h>
       
     7 #include <stdlib.h>
       
     8 #include <wchar.h>
       
     9 #include <locale.h>
       
    10 #include <string.h>
       
    11 
       
    12 #define FACTOR 1.42
       
    13 
       
    14 wchar_t *substr (const wchar_t * s, int start, int end)
       
    15 {
       
    16     wchar_t *str;
       
    17 
       
    18     int len = end - start;
       
    19 
       
    20     if (len < 1)
       
    21         return NULL;
       
    22 
       
    23     if ((str = malloc (sizeof (wchar_t) * (len + 1))) == NULL)
       
    24         return NULL;
       
    25 
       
    26     wcsncpy (str, s + start, len);
       
    27     str[len] = L'\0';
       
    28 
       
    29     return str;
       
    30 }
       
    31 
       
    32 int main (int argc, char **argv)
       
    33 {
       
    34     wchar_t *wide_str = L"Report Bugs to mbroeker@largo.homelinux.org ...";
       
    35     wchar_t *email_addr;
       
    36 
       
    37     wchar_t wide_euro_char = L'\u20ac';
       
    38     wchar_t wide_dollar_char = L'\u0024';
       
    39 
       
    40     char *locale;
       
    41 
       
    42     double price;
       
    43 
       
    44     if (argc != 2) {
       
    45         printf ("Usage: [LC_ALL=C] %s <PRICE>\n", argv[0]);
       
    46         return EXIT_FAILURE;
       
    47     }
       
    48 
       
    49     if (!setlocale (LC_ALL, "")) {
       
    50         perror ("LOCALE");
       
    51         return EXIT_FAILURE;
       
    52     }
       
    53 
       
    54     locale = getenv ("LC_ALL");
       
    55     price = atof (argv[1]);
       
    56 
       
    57     printf ("Default Locale=%s, Current LOCALE=%s\n", getenv ("LANG"), (locale ? locale : "default"));
       
    58     if (printf ("Best Price: %3.2f%lc = %3.2f%lc", price, wide_euro_char, price * FACTOR, wide_dollar_char) == -1) {
       
    59         printf ("\rBest Price: %3.2f EUR = %3.2f USD", price, price * FACTOR);
       
    60     }
       
    61 
       
    62     printf ("\n");
       
    63 
       
    64     email_addr = substr (wide_str, 23, 43);
       
    65 
       
    66     printf ("Do not report bugs to webmaster%ls\n", email_addr);
       
    67     printf ("%ls\n", wide_str);
       
    68 
       
    69     if (email_addr)
       
    70         free (email_addr);
       
    71 
       
    72     return EXIT_SUCCESS;
       
    73 }