summaryrefslogtreecommitdiff
path: root/src/user/lib/libc/std
diff options
context:
space:
mode:
authorAlex AUVOLAT <alexis211@gmail.com>2012-05-19 15:56:25 +0200
committerAlex AUVOLAT <alexis211@gmail.com>2012-05-19 15:56:25 +0200
commit8e07c1db6ba4bedd0f8fe537a6fb0ca80e5d25f4 (patch)
tree08df626a28c290f5cde43424dd62dd43fc441a35 /src/user/lib/libc/std
parent0d2f7645c3fb45d83497faf2a4b6fff8c3f175d1 (diff)
downloadTCE-8e07c1db6ba4bedd0f8fe537a6fb0ca80e5d25f4.tar.gz
TCE-8e07c1db6ba4bedd0f8fe537a6fb0ca80e5d25f4.zip
Better sprintf, vsprintf, String::sprintf, etc.
Diffstat (limited to 'src/user/lib/libc/std')
-rw-r--r--src/user/lib/libc/std/stdio.c75
-rw-r--r--src/user/lib/libc/std/string.c54
2 files changed, 70 insertions, 59 deletions
diff --git a/src/user/lib/libc/std/stdio.c b/src/user/lib/libc/std/stdio.c
index da2f094..23ec989 100644
--- a/src/user/lib/libc/std/stdio.c
+++ b/src/user/lib/libc/std/stdio.c
@@ -4,8 +4,8 @@
#include <readline.h>
FILE term = 0;
-void print(char *s) { fprint(term, s); }
-void printf(char *format, ...) {
+void print(const char *s) { fprint(term, s); }
+void printf(const char *format, ...) {
va_list ap;
va_start(ap, format);
vfprintf(term, format, ap);
@@ -13,67 +13,24 @@ void printf(char *format, ...) {
}
char* readln() { return freadln(term); }
-void fprintf(FILE f, char* format, ...) {
+void fprint(FILE f, const char *s) {
+ write(f, 0, strlen(s), s);
+}
+
+void fprintf(FILE f, const char* format, ...) {
va_list ap;
va_start(ap, format);
vfprintf(f, format, ap);
va_end(ap);
}
-// FUNCTIONS
-
-void fprint(FILE f, char *s) {
- write(f, 0, strlen(s), s);
-}
-
-void fprint_int(FILE f, int number) {
- char s[32];
- char *v = format_int(s, number);
- *v = 0;
- fprint(f, s);
-}
-
-void fprint_hex(FILE f, unsigned v) {
- char s[11];
- char *e = format_hex(s, v);
- *e = 0;
- fprint(f, s);
-}
-
-void vfprintf(FILE f, char *format, va_list ap) {
- char bb[256];
- int bufl = 256;
-
- char *buf = bb;
- char *end = buf;
-
- while (*format) {
- if (*format == '%') {
- // ASSUMPTION : (TODO) WE HAVE ENOUGH SPACE - NOT THE CASE!!!
- format++;
- if (*format == 'd' || *format == 'i') {
- end = format_int(end, va_arg(ap, int));
- } else if (*format == 'p') {
- end = format_hex(end, va_arg(ap, uint32_t));
- } else if (*format == 's') {
- char *s = va_arg(ap, char*);
- strcpy(end, s);
- end += strlen(s);
- }
- format++;
- } else {
- *(end++) = *(format++);
- }
- if (end - buf > bufl - 2) {
- bufl *= 2;
- char *nbuf = (char*)malloc(bufl);
- memcpy(nbuf, buf, end - buf);
- end = nbuf + (end - buf);
- if (buf != bb) free(buf);
- buf = nbuf;
- }
- }
- *end = 0;
- fprint(f, buf);
- if (buf != bb) free(buf);
+void vfprintf(FILE f, const char *fmt, va_list ap) {
+ va_list ap2;
+ va_copy(ap2, ap);
+ int l = printf_str_len(fmt, ap2);
+ va_end(ap2);
+ char* buf = (char*) malloc(l+1);
+ l = vsprintf(buf, fmt, ap);
+ if (l > 0) write(f, 0, l, buf);
+ free(buf);
}
diff --git a/src/user/lib/libc/std/string.c b/src/user/lib/libc/std/string.c
index d8e4a48..619b8c6 100644
--- a/src/user/lib/libc/std/string.c
+++ b/src/user/lib/libc/std/string.c
@@ -119,3 +119,57 @@ char* format_hex(char *buf, unsigned v) {
}
return buf;
}
+
+int printf_str_len(const char *format, va_list ap) {
+ int l = 0;
+ while (*format) {
+ if (*format == '%') {
+ format++;
+ if (*format == 'd' || *format == 'i') {
+ l += 15;
+ va_arg(ap, int);
+ } else if (*format == 'p') {
+ l += 11;
+ va_arg(ap, void*);
+ } else if (*format == 's') {
+ l += strlen(va_arg(ap, const char*));
+ }
+ } else {
+ l++;
+ }
+ format++;
+ }
+ return l;
+}
+
+int sprintf(char *buf, const char *fmt, ...) {
+ va_list ap;
+ va_start(ap, fmt);
+ int ret = vsprintf(buf, fmt, ap);
+ va_end(ap);
+ return ret;
+}
+
+int vsprintf(char *buf, const char *format, va_list ap) {
+ char *end = buf;
+
+ while (*format) {
+ if (*format == '%') {
+ format++;
+ if (*format == 'd' || *format == 'i') {
+ end = format_int(end, va_arg(ap, int));
+ } else if (*format == 'p') {
+ end = format_hex(end, va_arg(ap, uint32_t));
+ } else if (*format == 's') {
+ const char *s = va_arg(ap, const char*);
+ strcpy(end, s);
+ end += strlen(s);
+ }
+ format++;
+ } else {
+ *(end++) = *(format++);
+ }
+ }
+ *end = 0;
+ return end - buf;
+}