123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- #ifndef AFUN_STDIO_INLINE_H
- #define AFUN_STDIO_INLINE_H
- #include "tool-stdio.h"
- namespace aFuntool {
- static bool clear_ferror(FILE *file) {
- return ferror(file) && (clearerr(file), ferror(file));
- }
- static bool clear_stdin() {
- return (ferror(stdin) || feof(stdin)) &&
- (clearerr(stdin), (ferror(stdin) || feof(stdin)));
- }
- }
- #ifdef aFunWIN32_NO_CYGWIN
- namespace aFuntool {
- static int fputs_stdout(const char *str) {
- return fputs_std_(str, stdout);
- }
- static int fputs_stderr(const char *str) {
- return fputs_std_(str, stderr);
- }
- static size_t vprintf_stderr(size_t buf_len, const char *format, va_list ap) {
- return vprintf_std_(stderr, buf_len, format, ap);
- }
- static size_t vprintf_stdout(size_t buf_len, const char *format, va_list ap) {
- return vprintf_std_(stdout, buf_len, format, ap);
- }
- static 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;
- }
- static 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;
- }
- }
- #else
- namespace aFuntool {
- static int fgetc_stdin(){
- return fgetc(stdout);
- }
- static int fgets_stdin_(char *buf, int len, FILE *file){
- return fgets(buf, len, file) != nullptr;
- }
- static int fungetc_stdin(char ch){
- return ungetc(ch, stdin);
- }
- static int fputs_stdout(const char *str){
- return fputs(str, stdout);
- }
- static int fputs_stderr(const char *str){
- return fputs(str, stderr);
- }
- static int vprintf_stdout(size_t, const char *format, va_list ap){
- return vfprintf(stdout, format, ap);
- }
- static int vprintf_stderr(size_t, const char *format, va_list ap){
- return vfprintf(stderr, format, ap);
- }
- static 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;
- }
- static 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;
- }
- }
- #endif
- namespace aFuntool {
- inline OutStream::OutStream(PrintFunction *func_) : func {func_} {
- }
- inline OutStream &OutStream::operator<<(signed char a) {
- func(0, "%c", a);
- return *this;
- }
- inline OutStream &OutStream::operator<<(short a) {
- func(0, "%d", a);
- return *this;
- }
- inline OutStream &OutStream::operator<<(int a) {
- func(0, "%d", a);
- return *this;
- }
- inline OutStream &OutStream::operator<<(long a) {
- func(0, "%ld", a);
- return *this;
- }
- inline OutStream &OutStream::operator<<(long long a) {
- func(0, "%lld", a);
- return *this;
- }
- inline OutStream &OutStream::operator<<(unsigned char a) {
- func(0, "%c", a);
- return *this;
- }
- inline OutStream &OutStream::operator<<(unsigned short a) {
- func(0, "%u", a);
- return *this;
- }
- inline OutStream &OutStream::operator<<(unsigned int a) {
- func(0, "%u", a);
- return *this;
- }
- inline OutStream &OutStream::operator<<(unsigned long a) {
- func(0, "%lu", a);
- return *this;
- }
- inline OutStream &OutStream::operator<<(unsigned long long a) {
- func(0, "%llu", a);
- return *this;
- }
- inline OutStream &OutStream::operator<<(const char *a){
- func(0, "%s", a);
- return *this;
- }
- inline OutStream &OutStream::operator<<(const std::string &a) {
- func(0, "%s", a.c_str());
- return *this;
- }
- inline OutStream &OutStream::operator<<(const void *a) {
- func(0, "%p", a);
- return *this;
- }
- inline OutStream &OutStream::operator<<(float a) {
- func(0, "%f", a);
- return *this;
- }
- inline OutStream &OutStream::operator<<(double a) {
- func(0, "%f", a);
- return *this;
- }
- inline OutStream &OutStream::operator<<(long double a) {
- func(0, "%lf", a);
- return *this;
- }
- }
- #endif //AFUN_STDIO_INLINE_H
|