Implement string formatting into libc :)
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Of course, using <luna/Format.h> makes it so simple. No need for duplicate code!
This commit is contained in:
parent
367a2ce521
commit
80f5c790f8
@ -10,6 +10,11 @@ int main()
|
||||
{
|
||||
atexit(bye);
|
||||
|
||||
char buffer[64];
|
||||
snprintf(buffer, sizeof(buffer), "Welcome to %s!\n", "Luna");
|
||||
|
||||
console_print(buffer);
|
||||
|
||||
for (int i = 0; i < atoi("8"); i++) { console_print("."); }
|
||||
|
||||
console_print("\n");
|
||||
|
@ -36,7 +36,18 @@ extern "C"
|
||||
|
||||
int fprintf(FILE*, const char*, ...);
|
||||
int vfprintf(FILE*, const char*, va_list);
|
||||
int sprintf(char*, const char*, ...);
|
||||
|
||||
/* Write formatted output into a buffer. */
|
||||
int sprintf(char* buf, const char* format, ...);
|
||||
|
||||
/* Write up to max bytes of formatted output into a buffer. */
|
||||
int snprintf(char* buf, size_t max, const char* format, ...);
|
||||
|
||||
/* Write formatted output into a buffer. */
|
||||
int vsprintf(char*, const char*, va_list);
|
||||
|
||||
/* Write up to max bytes of formatted output into a buffer. */
|
||||
int vsnprintf(char*, size_t, const char*, va_list);
|
||||
|
||||
/* Output a message to the console. */
|
||||
int console_print(const char* msg);
|
||||
|
@ -1,4 +1,6 @@
|
||||
#define _LUNA_SYSTEM_ERROR_EXTENSIONS
|
||||
#include <bits/errno-return.h>
|
||||
#include <luna/Format.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
@ -12,4 +14,38 @@ extern "C"
|
||||
long rc = syscall(SYS_console_print, str);
|
||||
__errno_return(rc, int);
|
||||
}
|
||||
|
||||
int vsnprintf(char* buf, size_t max, const char* format, va_list ap)
|
||||
{
|
||||
return (int)vstring_format(buf, max, format, ap);
|
||||
}
|
||||
|
||||
int snprintf(char* buf, size_t max, const char* format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
|
||||
int rc = vsnprintf(buf, max, format, ap);
|
||||
|
||||
va_end(ap);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int vsprintf(char* buf, const char* format, va_list ap)
|
||||
{
|
||||
return vsnprintf(buf, (size_t)-1, format, ap);
|
||||
}
|
||||
|
||||
int sprintf(char* buf, const char* format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
|
||||
int rc = vsnprintf(buf, (size_t)-1, format, ap);
|
||||
|
||||
va_end(ap);
|
||||
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,9 @@
|
||||
char* const rc = new (std::nothrow) char[size];
|
||||
return (void*)rc;
|
||||
#else
|
||||
return malloc(size);
|
||||
// return malloc(size);
|
||||
(void)size;
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -20,11 +22,17 @@ void raw_free(void* ptr)
|
||||
char* const arr = (char*)ptr;
|
||||
delete[] arr;
|
||||
#else
|
||||
return free(ptr);
|
||||
// return free(ptr);
|
||||
(void)ptr;
|
||||
#endif
|
||||
}
|
||||
|
||||
void operator delete(void* ptr, usize size, std::align_val_t) noexcept
|
||||
{
|
||||
#ifdef USE_FREESTANDING
|
||||
operator delete(ptr, size);
|
||||
#else
|
||||
(void)ptr;
|
||||
(void)size;
|
||||
#endif
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user