-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23d7925
commit 45ec901
Showing
12 changed files
with
170 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,118 @@ | ||
#include "altc/altio.h" | ||
|
||
#include <stdarg.h> | ||
#include <stddef.h> | ||
|
||
int alt_printf(const char *fmt, ...) | ||
#define ALT_PRINTF_BUF_SIZE 128 | ||
|
||
static char *write_dec(char *restrict dst, const char *end, | ||
unsigned long long val) | ||
{ | ||
if (!val && dst != end) { | ||
*(dst++) = '0'; | ||
} else if (dst != end) { | ||
int i = 0; | ||
char buf[32]; | ||
while (val) { | ||
int tmp = val % 10; | ||
buf[i++] = '0' + tmp; | ||
val /= 10; | ||
} | ||
while (i > 0 && dst != end) | ||
*(dst++) = buf[--i]; | ||
} | ||
return dst; | ||
} | ||
|
||
static char *write_hex(char *restrict dst, const char *end, | ||
unsigned long long val) | ||
{ | ||
if (!val && dst != end) { | ||
*(dst++) = '0'; | ||
} else if (dst != end) { | ||
int i = 0; | ||
char buf[16]; | ||
while (val) { | ||
int tmp = val & 0xF; | ||
buf[i++] = tmp < 10 ? ('0' + tmp) : 'A' + (tmp - 10); | ||
val >>= 4; | ||
} | ||
while (i > 0 && dst != end) | ||
*(dst++) = buf[--i]; | ||
} | ||
return dst; | ||
} | ||
|
||
static char *write_str(char *restrict dst, const char *end, char *restrict src) | ||
{ | ||
static const char hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', | ||
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; | ||
unsigned long long x; | ||
int len; | ||
va_list args; | ||
va_start(args, fmt); | ||
len = 0; | ||
while (dst != end && *src != '\0') | ||
*(dst++) = *(src++); | ||
return dst; | ||
} | ||
|
||
while (*fmt != '\0') { | ||
if (*fmt++ != '%') { | ||
alt_putchar(*(fmt - 1)); | ||
len++; | ||
static char *write_char(char *restrict dst, const char *end, char c) | ||
{ | ||
if (dst != end) | ||
*(dst++) = c; | ||
return dst; | ||
} | ||
|
||
int alt_vsnprintf(char *restrict str, size_t size, const char *restrict fmt, | ||
va_list ap) | ||
{ | ||
char *s = str; | ||
const char *end = str + size - 1; | ||
while (*fmt != '\0' && s != end) { | ||
if (*(fmt++) != '%') { | ||
s = write_char(s, end, *(fmt - 1)); | ||
continue; | ||
} | ||
switch (*fmt++) { | ||
switch (*(fmt++)) { | ||
case '%': | ||
s = write_char(s, end, '%'); | ||
break; | ||
case 'c': | ||
alt_putchar((char)va_arg(args, int)); | ||
len++; | ||
s = write_char(s, end, va_arg(ap, int)); | ||
break; | ||
case 's': | ||
len += alt_putstr(va_arg(args, char *)); | ||
s = write_str(s, end, va_arg(ap, char *)); | ||
break; | ||
case 'x': | ||
x = va_arg(args, unsigned int); | ||
if (!x) { | ||
alt_putchar('0'); | ||
len++; | ||
break; | ||
} | ||
for (int i = 28; i >= 0; i -= 4) { | ||
if (x >> i) { | ||
alt_putchar(hex[(x >> i) & 0xF]); | ||
len++; | ||
} | ||
} | ||
s = write_hex(s, end, va_arg(ap, unsigned int)); | ||
break; | ||
case 'X': | ||
x = va_arg(args, unsigned long long); | ||
if (!x) { | ||
alt_putchar('0'); | ||
len++; | ||
break; | ||
} | ||
for (int i = 60; i >= 0; i -= 4) { | ||
if (x >> i) { | ||
alt_putchar(hex[(x >> i) & 0xF]); | ||
len++; | ||
} | ||
} | ||
s = write_hex(s, end, va_arg(ap, unsigned long long)); | ||
break; | ||
case '%': | ||
alt_putchar('%'); | ||
len++; | ||
case 'd': | ||
s = write_dec(s, end, va_arg(ap, unsigned int)); | ||
break; | ||
case 'D': | ||
s = write_dec(s, end, va_arg(ap, unsigned long long)); | ||
break; | ||
case '\0': | ||
break; | ||
} | ||
} | ||
va_end(args); | ||
*s = '\0'; | ||
return s - str; | ||
} | ||
|
||
int alt_snprintf(char *restrict str, size_t size, const char *restrict fmt, ...) | ||
{ | ||
va_list ap; | ||
va_start(ap, fmt); | ||
int len = alt_vsnprintf(str, size, fmt, ap); | ||
va_end(ap); | ||
return len; | ||
} | ||
|
||
int alt_printf(const char *restrict fmt, ...) | ||
{ | ||
char buf[ALT_PRINTF_BUF_SIZE + 1]; | ||
va_list ap; | ||
va_start(ap, fmt); | ||
int len = alt_vsnprintf(buf, ALT_PRINTF_BUF_SIZE + 1, fmt, ap); | ||
va_end(ap); | ||
alt_puts(buf); | ||
return len; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#pragma once | ||
void kputstr(char *s); | ||
void kprintf(char *restrict fmt, ...); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.