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