123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- #ifndef AFUN_STDIO_INLINE_H
- #define AFUN_STDIO_INLINE_H
- #include "tool_stdio.h"
- #ifndef AFUN_TOOL_C
- namespace aFuntool {
- #endif
- bool clear_ferror(FILE *file) {
- return ferror(file) && (clearerr(file), ferror(file));
- }
- bool clear_stdin() {
- return (ferror(stdin) || feof(stdin)) &&
- (clearerr(stdin), (ferror(stdin) || feof(stdin)));
- }
- #ifndef AFUN_TOOL_C
- }
- #endif
- #ifdef AFUN_WIN32_NO_CYGWIN
- #ifndef AFUN_TOOL_C
- namespace aFuntool {
- #endif
- int fputs_stdout(const char *str) {
- return fputs_std_(str, stdout);
- }
- int fputs_stderr(const char *str) {
- return fputs_std_(str, stderr);
- }
- size_t vprintf_stderr(size_t buf_len, const char *format, va_list ap) {
- return vprintf_std_(stderr, buf_len, format, ap);
- }
- size_t vprintf_stdout(size_t buf_len, const char *format, va_list ap) {
- return vprintf_std_(stdout, buf_len, format, ap);
- }
- size_t printf_stdout(size_t buf_len, const char *format, ...) {
- va_list ap;
- va_start(ap, format);
- size_t re = vprintf_std_(stdout, buf_len, format, ap);
- va_end(ap);
- return re;
- }
- size_t printf_stderr(size_t buf_len, const char *format, ...) {
- va_list ap;
- va_start(ap, format);
- size_t re = vprintf_std_(stderr, buf_len, format, ap);
- va_end(ap);
- return re;
- }
- #ifndef AFUN_TOOL_C
- }
- #endif
- #else
- #ifndef AFUN_TOOL_C
- namespace aFuntool {
- #endif
- #ifndef __cplusplus
- #define nullptr NULL
- #endif
- int fgetc_stdin(){
- return fgetc(stdout);
- }
- int fgets_stdin_(char *buf, int len, FILE *file){
- return fgets(buf, len, file) != nullptr;
- }
- int fungetc_stdin(char ch){
- return ungetc(ch, stdin);
- }
- int fputs_stdout(const char *str){
- return fputs(str, stdout);
- }
- int fputs_stderr(const char *str){
- return fputs(str, stderr);
- }
- #ifdef __cplusplus
- int vprintf_stdout(size_t, const char *format, va_list ap){
- return vfprintf(stdout, format, ap);
- }
- int vprintf_stderr(size_t, const char *format, va_list ap){
- return vfprintf(stderr, format, ap);
- }
- size_t printf_stdout(size_t, const char *format, ...) {
- va_list ap;
- va_start(ap, format);
- size_t re = vfprintf(stdout, format, ap);
- va_end(ap);
- return re;
- }
- size_t printf_stderr(size_t, const char *format, ...) {
- va_list ap;
- va_start(ap, format);
- size_t re = vfprintf(stderr, format, ap);
- va_end(ap);
- return re;
- }
- #else // C 不允许省略形参名
- int vprintf_stdout(size_t _, const char *format, va_list ap){
- (void)_; // 确保参数被使用
- return vfprintf(stdout, format, ap);
- }
- int vprintf_stderr(size_t _, const char *format, va_list ap){
- (void)_; // 确保参数被使用
- return vfprintf(stderr, format, ap);
- }
- size_t printf_stdout(size_t _, const char *format, ...) {
- (void)_; // 确保参数被使用
- va_list ap;
- va_start(ap, format);
- size_t re = vfprintf(stdout, format, ap);
- va_end(ap);
- return re;
- }
- size_t printf_stderr(size_t _, const char *format, ...) {
- (void)_; // 确保参数被使用
- va_list ap;
- va_start(ap, format);
- size_t re = vfprintf(stderr, format, ap);
- va_end(ap);
- return re;
- }
- #endif
- #ifndef AFUN_TOOL_C
- }
- #endif
- #endif
- #ifdef __cplusplus
- #ifndef AFUN_TOOL_C
- namespace aFuntool {
- #endif
- OutStream::OutStream(PrintFunction *func_) noexcept : func {func_} {
- }
- OutStream &OutStream::operator<<(char a) {
- func(0, "%c", a);
- return *this;
- }
- OutStream &OutStream::operator<<(signed char a) {
- func(0, "%c", a);
- return *this;
- }
- OutStream &OutStream::operator<<(short a) {
- func(0, "%d", a);
- return *this;
- }
- OutStream &OutStream::operator<<(int a) {
- func(0, "%d", a);
- return *this;
- }
- OutStream &OutStream::operator<<(long a) {
- func(0, "%ld", a);
- return *this;
- }
- OutStream &OutStream::operator<<(long long a) {
- func(0, "%lld", a);
- return *this;
- }
- OutStream &OutStream::operator<<(unsigned char a) {
- func(0, "%c", a);
- return *this;
- }
- OutStream &OutStream::operator<<(unsigned short a) {
- func(0, "%u", a);
- return *this;
- }
- OutStream &OutStream::operator<<(unsigned int a) {
- func(0, "%u", a);
- return *this;
- }
- OutStream &OutStream::operator<<(unsigned long a) {
- func(0, "%lu", a);
- return *this;
- }
- OutStream &OutStream::operator<<(unsigned long long a) {
- func(0, "%llu", a);
- return *this;
- }
- OutStream &OutStream::operator<<(const char *a){
- func(0, "%s", a);
- return *this;
- }
- OutStream &OutStream::operator<<(const std::string &a) {
- func(0, "%s", a.c_str());
- return *this;
- }
- OutStream &OutStream::operator<<(const void *a) {
- func(0, "%p", a);
- return *this;
- }
- OutStream &OutStream::operator<<(float a) {
- func(0, "%f", a);
- return *this;
- }
- OutStream &OutStream::operator<<(double a) {
- func(0, "%f", a);
- return *this;
- }
- OutStream &OutStream::operator<<(long double a) {
- func(0, "%lf", a);
- return *this;
- }
- #ifndef AFUN_TOOL_C
- }
- #endif
- #endif // __cplusplus
- #endif //AFUN_STDIO_INLINE_H
|