[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tyndur-devel] [PATCH 1/3] libc: fgetwc, fputwc und verwandte Funktionen



* libc: wchar.h nach modules/include verschoben, da sie stdio.h
        inkludieren muss.
+ libc: fgetwc, getwc, getchar: Breite Zeichen aus Datei lesen
+ libc: fputwc, putwc, putwchar: Breite Zeichen in Datei schreiben
---
 src/include/wchar.h              |   37 ----------
 src/modules/include/wchar.h      |   96 +++++++++++++++++++++++++
 src/modules/lib/stdlibc/wstdio.c |  142 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 238 insertions(+), 37 deletions(-)
 delete mode 100644 src/include/wchar.h
 create mode 100644 src/modules/include/wchar.h
 create mode 100644 src/modules/lib/stdlibc/wstdio.c

diff --git a/src/include/wchar.h b/src/include/wchar.h
deleted file mode 100644
index f9cd89b..0000000
--- a/src/include/wchar.h
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * Copyright (c) 2008 The tyndur Project. All rights reserved.
- *
- * This code is derived from software contributed to the tyndur Project
- * by Antoine Kaufmann.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#ifndef _WCHAR_H_
-#define _WCHAR_H_
-
-#include <stddef.h>
-
-typedef wchar_t wint_t;
-
-#endif //ifndef _WCHAR_H_
-
diff --git a/src/modules/include/wchar.h b/src/modules/include/wchar.h
new file mode 100644
index 0000000..d173bce
--- /dev/null
+++ b/src/modules/include/wchar.h
@@ -0,0 +1,96 @@
+/*
+ * Copyright (c) 2008-2009 The tyndur Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the tyndur Project
+ * by Antoine Kaufmann.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _WCHAR_H_
+#define _WCHAR_H_
+
+#include <stddef.h>
+#include <stdio.h>
+
+#define WEOF ((wint_t) -1)
+
+typedef wchar_t wint_t;
+
+/* WSTDIO */
+
+/**
+ * Breites Zeichen aus einer Datei lesen.
+ *
+ * @param stream Die geoeffnete Datei
+ *
+ * @return Das gelesene Zeichen oder WEOF im Fehlerfall.
+ */
+wint_t fgetwc(FILE* stream);
+
+/**
+ * Wie fgetwc, mit dem Unterschied, dass getwc als Makro implementiert werden
+ * darf, das den Parameter mehrmals auswertet
+ *
+ * @see fgetwc
+ */
+wint_t getwc(FILE* stream);
+
+/**
+ * Breites Zeichen von der Standardeingabe lesen.
+ *
+ * @see fgetwc
+ */
+wint_t getwchar(void);
+
+
+/**
+ * Ein breites Zeichen in eine Datei schreiben, dabei wird das Zeichen
+ * entsprechend codiert.
+ *
+ * @param wc     Das zu schreibende Zeichen
+ * @param stream Die geoeffnete Datei
+ *
+ * @return Das geschriebene Zeichen oder WEOF im Fehlerfall
+ */
+wint_t fputwc(wchar_t wc, FILE* stream);
+
+/**
+ * Wie fputwc, mit dem Unterschied, dass putwc als Makro implementiert werden
+ * darf, das seine Parameter mehrmals auswertet.
+ *
+ * @see fputwc
+ */
+wint_t putwc(wchar_t wc, FILE* stream);
+
+/**
+ * Breites Zeichen auf die Standardausgabe schreiben
+ *
+ * @see fputwc
+ * @param wc Das zu schreibende Zeichen
+ *
+ * @return Das geschriebene Zeichen oder WEOF im Fehlerfall
+ */
+wint_t putwchar(wchar_t wc);
+
+#endif //ifndef _WCHAR_H_
+
diff --git a/src/modules/lib/stdlibc/wstdio.c b/src/modules/lib/stdlibc/wstdio.c
new file mode 100644
index 0000000..6d61666
--- /dev/null
+++ b/src/modules/lib/stdlibc/wstdio.c
@@ -0,0 +1,142 @@
+/*
+ * Copyright (c) 2009 The tyndur Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the tyndur Project
+ * by Antoine Kaufmann.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <wchar.h>
+#include <stdlib.h>
+#include <errno.h>
+
+/**
+ * Breites Zeichen aus einer Datei lesen.
+ *
+ * @param stream Die geoeffnete Datei
+ *
+ * @return Das gelesene Zeichen oder WEOF im Fehlerfall.
+ */
+wint_t fgetwc(FILE* stream)
+{
+    char mbc[MB_CUR_MAX];
+    int len = 0;
+    int mbres;
+    int c;
+    wint_t result;
+
+    do {
+        if ((c = fgetc(stream)) == EOF) {
+            return WEOF;
+        }
+
+        mbc[len++] = c;
+    } while (((mbres = mbtowc(&result, mbc, len)) == -1) && (len < MB_CUR_MAX));
+
+    // Das Zeichen konnte nicht konvertiert werden. Wir schicken alle bytes bis
+    // aufs erste mittels ungetc zurueck
+    if (mbres < 0) {
+        int i;
+        for (i = len - 1; i > 0; i--) {
+            ungetc(mbc[i], stream);
+        }
+
+        errno = EILSEQ;
+        return WEOF;
+    }
+
+    return result;
+}
+
+/**
+ * Wie fgetwc, mit dem Unterschied, dass getwc als Makro implementiert werden
+ * darf, das den Parameter mehrmals auswertet
+ *
+ * @see fgetwc
+ */
+wint_t getwc(FILE* stream)
+{
+    return fgetwc(stream);
+}
+
+/**
+ * Breites Zeichen von der Standardeingabe lesen.
+ *
+ * @see fgetwc
+ */
+wint_t getwchar()
+{
+    return fgetwc(stdin);
+}
+
+
+/**
+ * Ein breites Zeichen in eine Datei schreiben, dabei wird das Zeichen
+ * entsprechend codiert.
+ *
+ * @param wc     Das zu schreibende Zeichen
+ * @param stream Die geoeffnete Datei
+ *
+ * @return Das geschriebene Zeichen oder WEOF im Fehlerfall
+ */
+wint_t fputwc(wchar_t wc, FILE* stream)
+{
+    char mbc[MB_CUR_MAX];
+    int len;
+
+    if ((len = wctomb(mbc, wc)) < 0) {
+        errno = EILSEQ;
+        return WEOF;
+    }
+
+    if (!fwrite(mbc, len, 1, stream)) {
+        return WEOF;
+    }
+
+    return wc;
+}
+
+/**
+ * Wie fputwc, mit dem Unterschied, dass putwc als Makro implementiert werden
+ * darf, das seine Parameter mehrmals auswertet.
+ *
+ * @see fputwc
+ */
+wint_t putwc(wchar_t wc, FILE* stream)
+{
+    return fputwc(wc, stream);
+}
+
+/**
+ * Breites Zeichen auf die Standardausgabe schreiben
+ *
+ * @see fputwc
+ * @param wc Das zu schreibende Zeichen
+ *
+ * @return Das geschriebene Zeichen oder WEOF im Fehlerfall
+ */
+wint_t putwchar(wchar_t wc)
+{
+    return fputwc(wc, stdout);
+}
+
-- 
1.6.0.6