Add a va_list printFormat
This commit is contained in:
		
							parent
							
								
									9680a2a9c5
								
							
						
					
					
						commit
						354e15012e
					
				
					 3 changed files with 34 additions and 22 deletions
				
			
		| 
						 | 
					@ -13,6 +13,7 @@
 | 
				
			||||||
#define __ATTRIBUTES_HH__
 | 
					#define __ATTRIBUTES_HH__
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define PACKED __attribute__((packed))
 | 
					#define PACKED __attribute__((packed))
 | 
				
			||||||
#define NORETURN __attribute((noreturn))
 | 
					#define NORETURN __attribute__((noreturn))
 | 
				
			||||||
 | 
					#define PRINTF(formatArg, variableArgStart) __attribute__((format (printf, formatArg, variableArgStart)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif /* __ATTRIBUTES_HH__ */
 | 
					#endif /* __ATTRIBUTES_HH__ */
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -9,8 +9,11 @@
 | 
				
			||||||
#ifndef __CONSOLE_HH__
 | 
					#ifndef __CONSOLE_HH__
 | 
				
			||||||
#define __CONSOLE_HH__
 | 
					#define __CONSOLE_HH__
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <stdarg.h>
 | 
				
			||||||
#include <stddef.h>
 | 
					#include <stddef.h>
 | 
				
			||||||
#include <stdint.h>
 | 
					#include <stdint.h>
 | 
				
			||||||
 | 
					#include "Attributes.hh"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace kernel {
 | 
					namespace kernel {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -62,8 +65,9 @@ struct Console
 | 
				
			||||||
     * Write a format string to the current cursor position. Returns the number
 | 
					     * Write a format string to the current cursor position. Returns the number
 | 
				
			||||||
     * of characters printed.
 | 
					     * of characters printed.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    int printFormat(const char *format, ...)
 | 
					    int printFormat(const char *format, ...) PRINTF(2, 3);
 | 
				
			||||||
        __attribute__((format (printf, 2, 3)));
 | 
					    int printFormat(const char *format, va_list args);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    void setColor(Color fg, Color bg);
 | 
					    void setColor(Color fg, Color bg);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -185,8 +185,8 @@ is_specifier(char c)
 | 
				
			||||||
namespace kernel {
 | 
					namespace kernel {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int
 | 
					int
 | 
				
			||||||
Console::printFormat(const char* fmt,
 | 
					Console::printFormat(const char* format,
 | 
				
			||||||
                     ...)
 | 
					                     va_list args)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    enum {
 | 
					    enum {
 | 
				
			||||||
        Default = 0,
 | 
					        Default = 0,
 | 
				
			||||||
| 
						 | 
					@ -195,13 +195,10 @@ Console::printFormat(const char* fmt,
 | 
				
			||||||
        Size
 | 
					        Size
 | 
				
			||||||
    } state = Default;
 | 
					    } state = Default;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    va_list vl;
 | 
					 | 
				
			||||||
    va_start(vl, fmt);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    int nchars = 0;
 | 
					    int nchars = 0;
 | 
				
			||||||
    Spec spec;
 | 
					    Spec spec;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (const char* p = fmt; *p != 0; p++) {
 | 
					    for (const char* p = format; *p != 0; p++) {
 | 
				
			||||||
        switch (state) {
 | 
					        switch (state) {
 | 
				
			||||||
            case Default:
 | 
					            case Default:
 | 
				
			||||||
                if (*p == '%') {
 | 
					                if (*p == '%') {
 | 
				
			||||||
| 
						 | 
					@ -273,19 +270,19 @@ Console::printFormat(const char* fmt,
 | 
				
			||||||
            case 'i':
 | 
					            case 'i':
 | 
				
			||||||
                switch (spec.size) {
 | 
					                switch (spec.size) {
 | 
				
			||||||
                    case Spec::Size::Normal:
 | 
					                    case Spec::Size::Normal:
 | 
				
			||||||
                        spec.value.d = va_arg(vl, int);
 | 
					                        spec.value.d = va_arg(args, int);
 | 
				
			||||||
                        break;
 | 
					                        break;
 | 
				
			||||||
                    case Spec::Size::DoubleShort:
 | 
					                    case Spec::Size::DoubleShort:
 | 
				
			||||||
                        spec.value.hhd = (signed char)va_arg(vl, int);
 | 
					                        spec.value.hhd = (signed char)va_arg(args, int);
 | 
				
			||||||
                        break;
 | 
					                        break;
 | 
				
			||||||
                    case Spec::Size::Short:
 | 
					                    case Spec::Size::Short:
 | 
				
			||||||
                        spec.value.hd = (short int)va_arg(vl, int);
 | 
					                        spec.value.hd = (short int)va_arg(args, int);
 | 
				
			||||||
                        break;
 | 
					                        break;
 | 
				
			||||||
                    case Spec::Size::Long:
 | 
					                    case Spec::Size::Long:
 | 
				
			||||||
                        spec.value.ld = va_arg(vl, long int);
 | 
					                        spec.value.ld = va_arg(args, long int);
 | 
				
			||||||
                        break;
 | 
					                        break;
 | 
				
			||||||
                    case Spec::Size::DoubleLong:
 | 
					                    case Spec::Size::DoubleLong:
 | 
				
			||||||
                        spec.value.lld = va_arg(vl, long long int);
 | 
					                        spec.value.lld = va_arg(args, long long int);
 | 
				
			||||||
                        break;
 | 
					                        break;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                spec.type = Spec::Type::Int;
 | 
					                spec.type = Spec::Type::Int;
 | 
				
			||||||
| 
						 | 
					@ -296,29 +293,29 @@ Console::printFormat(const char* fmt,
 | 
				
			||||||
            case 'x':
 | 
					            case 'x':
 | 
				
			||||||
                switch (spec.size) {
 | 
					                switch (spec.size) {
 | 
				
			||||||
                    case Spec::Size::Normal:
 | 
					                    case Spec::Size::Normal:
 | 
				
			||||||
                        spec.value.x = va_arg(vl, unsigned int);
 | 
					                        spec.value.x = va_arg(args, unsigned int);
 | 
				
			||||||
                        break;
 | 
					                        break;
 | 
				
			||||||
                    case Spec::Size::DoubleShort:
 | 
					                    case Spec::Size::DoubleShort:
 | 
				
			||||||
                        spec.value.hhx = (unsigned char)va_arg(vl, unsigned int);
 | 
					                        spec.value.hhx = (unsigned char)va_arg(args, unsigned int);
 | 
				
			||||||
                        break;
 | 
					                        break;
 | 
				
			||||||
                    case Spec::Size::Short:
 | 
					                    case Spec::Size::Short:
 | 
				
			||||||
                        spec.value.hx = (unsigned short int)va_arg(vl, unsigned int);
 | 
					                        spec.value.hx = (unsigned short int)va_arg(args, unsigned int);
 | 
				
			||||||
                        break;
 | 
					                        break;
 | 
				
			||||||
                    case Spec::Size::Long:
 | 
					                    case Spec::Size::Long:
 | 
				
			||||||
                        spec.value.lx = va_arg(vl, unsigned long int);
 | 
					                        spec.value.lx = va_arg(args, unsigned long int);
 | 
				
			||||||
                        break;
 | 
					                        break;
 | 
				
			||||||
                    case Spec::Size::DoubleLong:
 | 
					                    case Spec::Size::DoubleLong:
 | 
				
			||||||
                        spec.value.llx = va_arg(vl, unsigned long long int);
 | 
					                        spec.value.llx = va_arg(args, unsigned long long int);
 | 
				
			||||||
                        break;
 | 
					                        break;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                spec.type = Spec::Type::Hex;
 | 
					                spec.type = Spec::Type::Hex;
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
            case 'c':
 | 
					            case 'c':
 | 
				
			||||||
                spec.value.c = va_arg(vl, int);
 | 
					                spec.value.c = va_arg(args, int);
 | 
				
			||||||
                spec.type = Spec::Type::Char;
 | 
					                spec.type = Spec::Type::Char;
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
            case 's':
 | 
					            case 's':
 | 
				
			||||||
                spec.value.s = va_arg(vl, char*);
 | 
					                spec.value.s = va_arg(args, char*);
 | 
				
			||||||
                spec.type = Spec::Type::String;
 | 
					                spec.type = Spec::Type::String;
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					@ -326,8 +323,18 @@ Console::printFormat(const char* fmt,
 | 
				
			||||||
        continue;
 | 
					        continue;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    va_end(vl);
 | 
					    return nchars;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int
 | 
				
			||||||
 | 
					Console::printFormat(const char* format,
 | 
				
			||||||
 | 
					                     ...)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    va_list args;
 | 
				
			||||||
 | 
					    va_start(args, format);
 | 
				
			||||||
 | 
					    int nchars = printFormat(format, args);
 | 
				
			||||||
 | 
					    va_end(args);
 | 
				
			||||||
    return nchars;
 | 
					    return nchars;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue