Nugget
Bare-metal libraries and examples for the original PlayStation
Loading...
Searching...
No Matches
xprintf.h
Go to the documentation of this file.
1/*
2
3MIT License
4
5Copyright (c) 2022 PCSX-Redux authors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27#pragma once
28
29#include <stdarg.h>
30#include <stddef.h>
31
32#include "common/libc/xprintf.h"
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39static inline int xprintf(void (*func)(const char *, int, void *), void *arg, const char *fmt, ...) {
40 va_list ap;
41 va_start(ap, fmt);
42 int ret = vxprintf(func, arg, fmt, ap);
43 va_end(ap);
44 return ret;
45}
46
47static inline int sprintf(char *buf, const char *fmt, ...) {
48 va_list ap;
49 va_start(ap, fmt);
50 int ret = vsprintf(buf, fmt, ap);
51 va_end(ap);
52 return ret;
53}
54
55static inline int snprintf(char *buf, size_t n, const char *fmt, ...) {
56 va_list ap;
57 va_start(ap, fmt);
58 int ret = vsnprintf(buf, n, fmt, ap);
59 va_end(ap);
60 return ret;
61}
62
63static inline int asprintf(char **out, const char *fmt, ...) {
64 va_list ap;
65 va_start(ap, fmt);
66 int ret = vasprintf(out, fmt, ap);
67 va_end(ap);
68 return ret;
69}
70
71static inline void writeToTTYCallback(const char *str, int len, void *opaque) { syscall_write(1, str, len); }
72static inline int vprintf(const char *fmt, va_list ap) { return vxprintf(writeToTTYCallback, NULL, fmt, ap); }
73static inline int printf(const char *fmt, ...) {
74 va_list ap;
75 va_start(ap, fmt);
76 int ret = vprintf(fmt, ap);
77 va_end(ap);
78 return ret;
79}
80
81#ifdef __cplusplus
82}
83
84#include <EASTL/fixed_string.h>
85#include <EASTL/string.h>
86
97template <int nodeCount, bool bEnableOverflow = true>
98static inline void vfsprintf(eastl::fixed_string<char, nodeCount, bEnableOverflow> &str, const char *fmt, va_list ap) {
99 str.clear();
100 vxprintf(
101 [](const char *str, int len, void *opaque) {
102 eastl::fixed_string<char, nodeCount, bEnableOverflow> *out =
103 (eastl::fixed_string<char, nodeCount, bEnableOverflow> *)opaque;
104 out->append(str, len);
105 },
106 &str, fmt, ap);
107}
108
109template <int nodeCount, bool bEnableOverflow = true>
110static inline void fsprintf(eastl::fixed_string<char, nodeCount, bEnableOverflow> &str, const char *fmt, ...) {
111 str.clear();
112 va_list ap;
113 va_start(ap, fmt);
114 vfsprintf<nodeCount, bEnableOverflow>(str, fmt, ap);
115 va_end(ap);
116}
117
128static inline eastl::string vsprintf(const char *fmt, va_list ap) {
129 eastl::string ret;
130 vxprintf(
131 [](const char *str, int len, void *opaque) {
132 eastl::string *ret = (eastl::string *)opaque;
133 ret->append(str, len);
134 },
135 &ret, fmt, ap);
136 return ret;
137}
138
139static inline eastl::string sprintf(const char *fmt, ...) {
140 va_list ap;
141 va_start(ap, fmt);
142 eastl::string ret = vsprintf(fmt, ap);
143 va_end(ap);
144 return ret;
145}
146
147#endif
int vsprintf(char *buf, const char *fmt, va_list ap)
Prints a formatted string to a string.
Definition xprintf.c:730
int vxprintf(void(*func)(const char *, int, void *), void *opaque, const char *fmt, va_list ap)
Prints a formatted string to a callback.
Definition xprintf.c:318
int vasprintf(char **out, const char *fmt, va_list ap)
Prints a formatted string to a newly allocated string.
Definition xprintf.c:794
int vsnprintf(char *buf, size_t n, const char *fmt, va_list ap)
Prints a formatted string to a length-limited string.
Definition xprintf.c:737
uint32_t out
Definition cpu.c:62
#define printf
Definition demo.c:35
static int ret
Definition syscalls.h:125
static const void * buf
Definition syscalls.h:114