unicode.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Fri, 27 Dec 2013 16:00:09 +0100
changeset 167 7c6b221900bf
parent 77 49e0babccb23
permissions -rw-r--r--
Proper Datatypes to leave the 80's
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
/**
77
49e0babccb23 HEADER TAGS
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 63
diff changeset
     2
 * unicode.c
49e0babccb23 HEADER TAGS
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 63
diff changeset
     3
 * Copyright (C) 2008 Markus Broeker
49e0babccb23 HEADER TAGS
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 63
diff changeset
     4
 *
62
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
 * Unicode Testcase
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
 */
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <stdio.h>
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <stdlib.h>
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
#include <wchar.h>
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
#include <locale.h>
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
#include <string.h>
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
#define FACTOR 1.42
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
63
5a82f89d607e uint vs size_t and two bugfixes in fak and unicode
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 62
diff changeset
    16
wchar_t *substr (const wchar_t * s, size_t start, size_t end)
62
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
    wchar_t *str;
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
63
5a82f89d607e uint vs size_t and two bugfixes in fak and unicode
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 62
diff changeset
    20
    size_t len = end - start;
62
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
    if (len < 1)
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
        return NULL;
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
    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
    26
        return NULL;
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
    wcsncpy (str, s + start, len);
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
    str[len] = L'\0';
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
    return str;
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
}
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
int main (int argc, char **argv)
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
{
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
    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
    37
    wchar_t *email_addr;
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
    wchar_t wide_euro_char = L'\u20ac';
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
    wchar_t wide_dollar_char = L'\u0024';
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
    char *locale;
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
    double price;
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
    if (argc != 2) {
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
        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
    48
        return EXIT_FAILURE;
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
    }
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
    if (!setlocale (LC_ALL, "")) {
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
        perror ("LOCALE");
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
        return EXIT_FAILURE;
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
    }
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
    locale = getenv ("LC_ALL");
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
    price = atof (argv[1]);
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
    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
    60
    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
    61
        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
    62
    }
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
    printf ("\n");
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
    email_addr = substr (wide_str, 23, 43);
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
    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
    69
    printf ("%ls\n", wide_str);
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    70
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    71
    if (email_addr)
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    72
        free (email_addr);
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    73
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    74
    return EXIT_SUCCESS;
b7061c0e239f unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    75
}