* device/lib/z80/printf.c (sprintf): Added.
[fw/sdcc] / device / lib / gbz80 / printf.c
index 40a57ecc15f1c0462dabfa2b8fc6b25cc4228971..370a4e42173dd31a38abcf55d7917885a2c7c04e 100644 (file)
@@ -2,27 +2,15 @@
     Again a stub - will use the std one later...
 */
 
+#include <stdarg.h>
 #include <stdio.h>
 
-#define NULL 0
+#define STATIC
 
-/* A hack because I dont understand how va_arg works...
-   sdcc pushes right to left with the real sizes, not cast up
-   to an int.
-   so printf(int, char, long)
-   results in push long, push char, push int
-   On the z80 the stack grows down, so the things seem to be in
-   the correct order.
- */
+/* PENDING */
+#define NULL   0
 
-typedef char * va_list;
-#define va_start(list, last)   list = (char *)&last + sizeof(last)
-#define va_arg(list, type)     *(type *)list; list += sizeof(type);
-
-typedef void EMIT(char c, void *pData);
-
-
-static void _printn(unsigned u, unsigned base, char issigned, EMIT *emitter, void *pData)
+STATIC void _printn(unsigned u, unsigned base, char issigned, void (*emitter)(char, void *), void *pData)
 {
     const char *_hex = "0123456789ABCDEF";
     if (issigned && ((int)u < 0)) {
@@ -34,15 +22,13 @@ static void _printn(unsigned u, unsigned base, char issigned, EMIT *emitter, voi
     (*emitter)(_hex[u%base], pData);
 }
 
-static void _printf(const char *format, EMIT *emitter, void *pData, va_list va)
+STATIC void _printf(const char *format, void (*emitter)(char, void *), void *pData, va_list va)
 {
     while (*format) {
-       putchar(*format);
-#if 0
        if (*format == '%') {
            switch (*++format) {
            case 'c': {
-               char c = va_arg(va, char);
+               char c = (char)va_arg(va, int);
                (*emitter)(c, pData);
                break;
            }
@@ -77,20 +63,44 @@ static void _printf(const char *format, EMIT *emitter, void *pData, va_list va)
        else {
            (*emitter)(*format, pData);
        }
-#endif
        format++;
     }
 }
 
-static void _char_emitter(char c, void *pData)
+void putchar(char c);
+
+STATIC void _char_emitter(char c, void *pData)
 {
+    /* PENDING: Make the compiler happy. */
+    pData = 0;
+
     putchar(c);
 }
 
-void printf(const char *format, ...)
+int printf(const char *format, ...)
 {
     va_list va;
     va_start(va, format);
 
     _printf(format, _char_emitter, NULL, va);
+
+    /* PENDING: What to return? */
+    return 0;
+}
+
+STATIC void _buf_emitter(char c, void *pData)
+{
+  *((*((char **)pData)))++ = c;
+}
+
+int sprintf(char *pInto, const char *format, ...)
+{
+    va_list va;
+    va_start(va, format);
+
+    _printf(format, _buf_emitter, &pInto, va);
+    *pInto++ = '\0';
+
+    /* PENDING: What to return? */
+    return 0;
 }