tool-stdio.inline.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #ifndef AFUN_STDIO_INLINE_H
  2. #define AFUN_STDIO_INLINE_H
  3. #include "tool-stdio.h"
  4. namespace aFuntool {
  5. static bool clear_ferror(FILE *file) {
  6. return ferror(file) && (clearerr(file), ferror(file));
  7. }
  8. static bool clear_stdin() {
  9. return (ferror(stdin) || feof(stdin)) &&
  10. (clearerr(stdin), (ferror(stdin) || feof(stdin)));
  11. }
  12. }
  13. #ifdef aFunWIN32_NO_CYGWIN
  14. namespace aFuntool {
  15. static int fputs_stdout(const char *str) {
  16. return fputs_std_(str, stdout);
  17. }
  18. static int fputs_stderr(const char *str) {
  19. return fputs_std_(str, stderr);
  20. }
  21. static size_t vprintf_stderr(size_t buf_len, const char *format, va_list ap) {
  22. return vprintf_std_(stderr, buf_len, format, ap);
  23. }
  24. static size_t vprintf_stdout(size_t buf_len, const char *format, va_list ap) {
  25. return vprintf_std_(stdout, buf_len, format, ap);
  26. }
  27. static size_t printf_stdout(size_t buf_len, const char *format, ...) {
  28. va_list ap;
  29. va_start(ap, format);
  30. size_t re = vprintf_std_(stdout, buf_len, format, ap);
  31. va_end(ap);
  32. return re;
  33. }
  34. static size_t printf_stderr(size_t buf_len, const char *format, ...) {
  35. va_list ap;
  36. va_start(ap, format);
  37. size_t re = vprintf_std_(stderr, buf_len, format, ap);
  38. va_end(ap);
  39. return re;
  40. }
  41. }
  42. #else
  43. namespace aFuntool {
  44. static int fgetc_stdin(){
  45. return fgetc(stdout);
  46. }
  47. static int fgets_stdin_(char *buf, int len, FILE *file){
  48. return fgets(buf, len, file) != nullptr;
  49. }
  50. static int fungetc_stdin(char ch){
  51. return ungetc(ch, stdin);
  52. }
  53. static int fputs_stdout(const char *str){
  54. return fputs(str, stdout);
  55. }
  56. static int fputs_stderr(const char *str){
  57. return fputs(str, stderr);
  58. }
  59. static int vprintf_stdout(size_t, const char *format, va_list ap){
  60. return vfprintf(stdout, format, ap);
  61. }
  62. static int vprintf_stderr(size_t, const char *format, va_list ap){
  63. return vfprintf(stderr, format, ap);
  64. }
  65. static size_t printf_stdout(size_t, const char *format, ...) {
  66. va_list ap;
  67. va_start(ap, format);
  68. size_t re = vfprintf(stdout, format, ap);
  69. va_end(ap);
  70. return re;
  71. }
  72. static size_t printf_stderr(size_t, const char *format, ...) {
  73. va_list ap;
  74. va_start(ap, format);
  75. size_t re = vfprintf(stderr, format, ap);
  76. va_end(ap);
  77. return re;
  78. }
  79. }
  80. #endif
  81. namespace aFuntool {
  82. inline OutStream::OutStream(PrintFunction *func_) : func {func_} {
  83. }
  84. inline OutStream &OutStream::operator<<(signed char a) {
  85. func(0, "%c", a);
  86. return *this;
  87. }
  88. inline OutStream &OutStream::operator<<(short a) {
  89. func(0, "%d", a);
  90. return *this;
  91. }
  92. inline OutStream &OutStream::operator<<(int a) {
  93. func(0, "%d", a);
  94. return *this;
  95. }
  96. inline OutStream &OutStream::operator<<(long a) {
  97. func(0, "%ld", a);
  98. return *this;
  99. }
  100. inline OutStream &OutStream::operator<<(long long a) {
  101. func(0, "%lld", a);
  102. return *this;
  103. }
  104. inline OutStream &OutStream::operator<<(unsigned char a) {
  105. func(0, "%c", a);
  106. return *this;
  107. }
  108. inline OutStream &OutStream::operator<<(unsigned short a) {
  109. func(0, "%u", a);
  110. return *this;
  111. }
  112. inline OutStream &OutStream::operator<<(unsigned int a) {
  113. func(0, "%u", a);
  114. return *this;
  115. }
  116. inline OutStream &OutStream::operator<<(unsigned long a) {
  117. func(0, "%lu", a);
  118. return *this;
  119. }
  120. inline OutStream &OutStream::operator<<(unsigned long long a) {
  121. func(0, "%llu", a);
  122. return *this;
  123. }
  124. inline OutStream &OutStream::operator<<(const char *a){
  125. func(0, "%s", a);
  126. return *this;
  127. }
  128. inline OutStream &OutStream::operator<<(const std::string &a) {
  129. func(0, "%s", a.c_str());
  130. return *this;
  131. }
  132. inline OutStream &OutStream::operator<<(const void *a) {
  133. func(0, "%p", a);
  134. return *this;
  135. }
  136. inline OutStream &OutStream::operator<<(float a) {
  137. func(0, "%f", a);
  138. return *this;
  139. }
  140. inline OutStream &OutStream::operator<<(double a) {
  141. func(0, "%f", a);
  142. return *this;
  143. }
  144. inline OutStream &OutStream::operator<<(long double a) {
  145. func(0, "%lf", a);
  146. return *this;
  147. }
  148. }
  149. #endif //AFUN_STDIO_INLINE_H