unicode.c added and myprintf improved
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:49:13 +0200
changeset 62 b7061c0e239f
parent 61 4b4c97f179da
child 63 5a82f89d607e
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>
Makefile
myprintf.c
unicode.c
--- a/Makefile
+++ b/Makefile
@@ -52,7 +52,8 @@
 	base10 \
 	files \
 	fork \
-	duff
+	duff \
+	unicode
 
 .SUFFIXES: .c .cc .asm
 
@@ -280,6 +281,12 @@
 	@echo Linking $< ...
 	@$(CC) -o $@ $<
 
+unicode: unicode.c
+	@echo Compiling $<...
+	@$(CC) -c $(CFLAGS) -std=c99 $<
+	@echo Linking $< ...
+	@$(CC) -std=c99 -o $@ $<
+
 .PHONY: clean uninstall
 
 clean:
--- a/myprintf.c
+++ b/myprintf.c
@@ -10,7 +10,7 @@
 
 #define BUF_SIZE 1024
 
-int myprintf (char *fmt, ...)
+int myvfprintf (FILE * fp, const char *fmt, va_list list)
 {
     int i, value, pos = 0, size = 1;
     char c;
@@ -20,14 +20,10 @@
     char *s_str;
     char v_str[28];
 
-    va_list list;
-
     if ((s_str = malloc (BUF_SIZE)) == NULL) {
         return EOF;
     }
 
-    va_start (list, fmt);
-
     while (*fmt) {
         switch (*fmt) {
         case '%':
@@ -42,7 +38,7 @@
                 while ((pos + strlen (s)) > (size * BUF_SIZE)) {
                     if ((ptr = realloc (s_str, ++size * BUF_SIZE)) == NULL) {
                         s_str[pos] = '\0';
-                        fputs (s_str, stdout);  /* print it anyway... */
+                        fputs (s_str, fp);  /* print it anyway... */
                         return EOF;
                     }
                     s_str = ptr;
@@ -75,17 +71,41 @@
             }
             s_str = ptr;
         }
-
-        va_end (list);
     }
 
+    va_end (list);
+
     s_str[pos] = '\0';
-    fputs (s_str, stdout);
+    fputs (s_str, fp);
     free (s_str);
 
     return pos;
 }
 
+int myfprintf (FILE * fp, const char *fmt, ...)
+{
+    va_list list;
+    int ret;
+
+    va_start (list, fmt);
+    ret = myvfprintf (fp, fmt, list);
+    va_end (list);
+
+    return ret;
+}
+
+int myprintf (const char *fmt, ...)
+{
+    va_list list;
+    int ret;
+
+    va_start (list, fmt);
+    ret = myvfprintf (stdout, fmt, list);
+    va_end (list);
+
+    return ret;
+}
+
 int main (int argc, char **argv)
 {
     int len;
new file mode 100644
--- /dev/null
+++ b/unicode.c
@@ -0,0 +1,73 @@
+/**
+ * Unicode Testcase
+ * Copyright (C) 2008 Markus Broeker
+ */
+
+#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, int start, int end)
+{
+    wchar_t *str;
+
+    int 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;
+}