unicode.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Fri, 01 May 2009 18:27:06 +0200
changeset 89 66f0244c2863
parent 77 49e0babccb23
permissions -rw-r--r--
nearest: more templates fun small improvements and a new template function committer: Markus Bröker <mbroeker@largo.homelinux.org>

/**
 * unicode.c
 * Copyright (C) 2008 Markus Broeker
 *
 * Unicode Testcase
 */

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

#define FACTOR 1.42

wchar_t *substr (const wchar_t * s, size_t start, size_t end)
{
    wchar_t *str;

    size_t len = end - start;

    if (len < 1)
        return NULL;

    if ((str = malloc (sizeof (wchar_t) * (len + 1))) == NULL)
        return NULL;

    wcsncpy (str, s + start, len);
    str[len] = L'\0';

    return str;
}

int main (int argc, char **argv)
{
    wchar_t *wide_str = L"Report Bugs to mbroeker@largo.homelinux.org ...";
    wchar_t *email_addr;

    wchar_t wide_euro_char = L'\u20ac';
    wchar_t wide_dollar_char = L'\u0024';

    char *locale;

    double price;

    if (argc != 2) {
        printf ("Usage: [LC_ALL=C] %s <PRICE>\n", argv[0]);
        return EXIT_FAILURE;
    }

    if (!setlocale (LC_ALL, "")) {
        perror ("LOCALE");
        return EXIT_FAILURE;
    }

    locale = getenv ("LC_ALL");
    price = atof (argv[1]);

    printf ("Default Locale=%s, Current LOCALE=%s\n", getenv ("LANG"), (locale ? locale : "default"));
    if (printf ("Best Price: %3.2f%lc = %3.2f%lc", price, wide_euro_char, price * FACTOR, wide_dollar_char) == -1) {
        printf ("\rBest Price: %3.2f EUR = %3.2f USD", price, price * FACTOR);
    }

    printf ("\n");

    email_addr = substr (wide_str, 23, 43);

    printf ("Do not report bugs to webmaster%ls\n", email_addr);
    printf ("%ls\n", wide_str);

    if (email_addr)
        free (email_addr);

    return EXIT_SUCCESS;
}