Nugget
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages Concepts
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
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
76int vxprintf(void (*func)(const char *, int, void *), void *opaque, const char *fmt, va_list ap);
77
90int vsprintf(char *buf, const char *fmt, va_list ap);
91
106int vsnprintf(char *buf, size_t n, const char *fmt, va_list ap);
107
121int vasprintf(char **out, const char *fmt, va_list ap);
122
123static inline int xprintf(void (*func)(const char *, int, void *), void *arg, const char *fmt, ...) {
124 va_list ap;
125 va_start(ap, fmt);
126 int ret = vxprintf(func, arg, fmt, ap);
127 va_end(ap);
128 return ret;
129}
130
131static inline int sprintf(char *buf, const char *fmt, ...) {
132 va_list ap;
133 va_start(ap, fmt);
134 int ret = vsprintf(buf, fmt, ap);
135 va_end(ap);
136 return ret;
137}
138
139static inline int snprintf(char *buf, size_t n, const char *fmt, ...) {
140 va_list ap;
141 va_start(ap, fmt);
142 int ret = vsnprintf(buf, n, fmt, ap);
143 va_end(ap);
144 return ret;
145}
146
147static inline int asprintf(char **out, const char *fmt, ...) {
148 va_list ap;
149 va_start(ap, fmt);
150 int ret = vasprintf(out, fmt, ap);
151 va_end(ap);
152 return ret;
153}
154
155static inline void writeToTTYCallback(const char *str, int len, void *opaque) { syscall_write(1, str, len); }
156static inline int vprintf(const char *fmt, va_list ap) { return vxprintf(writeToTTYCallback, NULL, fmt, ap); }
157static inline int printf(const char *fmt, ...) {
158 va_list ap;
159 va_start(ap, fmt);
160 int ret = vprintf(fmt, ap);
161 va_end(ap);
162 return ret;
163}
164
165#ifdef __cplusplus
166}
167
168#include <EASTL/fixed_string.h>
169#include <EASTL/string.h>
170
181template <int nodeCount, bool bEnableOverflow = true>
182static inline void vfsprintf(eastl::fixed_string<char, nodeCount, bEnableOverflow> &str, const char *fmt, va_list ap) {
183 str.clear();
184 vxprintf(
185 [](const char *str, int len, void *opaque) {
186 eastl::fixed_string<char, nodeCount, bEnableOverflow> *out =
187 (eastl::fixed_string<char, nodeCount, bEnableOverflow> *)opaque;
188 out->append(str, len);
189 },
190 &str, fmt, ap);
191}
192
193template <int nodeCount, bool bEnableOverflow = true>
194static inline void fsprintf(eastl::fixed_string<char, nodeCount, bEnableOverflow> &str, const char *fmt, ...) {
195 str.clear();
196 va_list ap;
197 va_start(ap, fmt);
198 vfsprintf<nodeCount, bEnableOverflow>(str, fmt, ap);
199 va_end(ap);
200}
201
212static inline eastl::string vsprintf(const char *fmt, va_list ap) {
213 eastl::string ret;
214 vxprintf(
215 [](const char *str, int len, void *opaque) {
216 eastl::string *ret = (eastl::string *)opaque;
217 ret->append(str, len);
218 },
219 &ret, fmt, ap);
220 return ret;
221}
222
223static inline eastl::string sprintf(const char *fmt, ...) {
224 va_list ap;
225 va_start(ap, fmt);
226 eastl::string ret = vsprintf(fmt, ap);
227 va_end(ap);
228 return ret;
229}
230
231#endif
uint32_t out
Definition cpu.c:62
#define printf
Definition demo.c:34
static int ret
Definition syscalls.h:72
static const void * buf
Definition syscalls.h:60
int vsprintf(char *buf, const char *fmt, va_list ap)
Prints a formatted string to a string.
Definition xprintf.c:728
int vxprintf(void(*func)(const char *, int, void *), void *opaque, const char *fmt, va_list ap)
Prints a formatted string to a callback.
int vasprintf(char **out, const char *fmt, va_list ap)
Prints a formatted string to a newly allocated string.
Definition xprintf.c:793
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:735