2
0

tool_stdio.inline.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #ifndef AFUN_STDIO_INLINE_H
  2. #define AFUN_STDIO_INLINE_H
  3. #include "tool_stdio.h"
  4. #ifndef AFUN_TOOL_C
  5. namespace aFuntool {
  6. #endif
  7. bool clear_ferror(FILE *file) {
  8. return ferror(file) && (clearerr(file), ferror(file));
  9. }
  10. bool clear_stdin() {
  11. return (ferror(stdin) || feof(stdin)) &&
  12. (clearerr(stdin), (ferror(stdin) || feof(stdin)));
  13. }
  14. #ifndef AFUN_TOOL_C
  15. }
  16. #endif
  17. #ifdef AFUN_WIN32_NO_CYGWIN
  18. #ifndef AFUN_TOOL_C
  19. namespace aFuntool {
  20. #endif
  21. int fputs_stdout(const char *str) {
  22. return fputs_std_(str, stdout);
  23. }
  24. int fputs_stderr(const char *str) {
  25. return fputs_std_(str, stderr);
  26. }
  27. size_t vprintf_stderr(size_t buf_len, const char *format, va_list ap) {
  28. return vprintf_std_(stderr, buf_len, format, ap);
  29. }
  30. size_t vprintf_stdout(size_t buf_len, const char *format, va_list ap) {
  31. return vprintf_std_(stdout, buf_len, format, ap);
  32. }
  33. size_t printf_stdout(size_t buf_len, const char *format, ...) {
  34. va_list ap;
  35. va_start(ap, format);
  36. size_t re = vprintf_std_(stdout, buf_len, format, ap);
  37. va_end(ap);
  38. return re;
  39. }
  40. size_t printf_stderr(size_t buf_len, const char *format, ...) {
  41. va_list ap;
  42. va_start(ap, format);
  43. size_t re = vprintf_std_(stderr, buf_len, format, ap);
  44. va_end(ap);
  45. return re;
  46. }
  47. #ifndef AFUN_TOOL_C
  48. }
  49. #endif
  50. #else
  51. #ifndef AFUN_TOOL_C
  52. namespace aFuntool {
  53. #endif
  54. #ifndef __cplusplus
  55. #define nullptr NULL
  56. #endif
  57. int fgetc_stdin(){
  58. return fgetc(stdout);
  59. }
  60. int fgets_stdin_(char *buf, int len, FILE *file){
  61. return fgets(buf, len, file) != nullptr;
  62. }
  63. int fungetc_stdin(char ch){
  64. return ungetc(ch, stdin);
  65. }
  66. int fputs_stdout(const char *str){
  67. return fputs(str, stdout);
  68. }
  69. int fputs_stderr(const char *str){
  70. return fputs(str, stderr);
  71. }
  72. #ifdef __cplusplus
  73. int vprintf_stdout(size_t, const char *format, va_list ap){
  74. return vfprintf(stdout, format, ap);
  75. }
  76. int vprintf_stderr(size_t, const char *format, va_list ap){
  77. return vfprintf(stderr, format, ap);
  78. }
  79. size_t printf_stdout(size_t, const char *format, ...) {
  80. va_list ap;
  81. va_start(ap, format);
  82. size_t re = vfprintf(stdout, format, ap);
  83. va_end(ap);
  84. return re;
  85. }
  86. size_t printf_stderr(size_t, const char *format, ...) {
  87. va_list ap;
  88. va_start(ap, format);
  89. size_t re = vfprintf(stderr, format, ap);
  90. va_end(ap);
  91. return re;
  92. }
  93. #else // C 不允许省略形参名
  94. int vprintf_stdout(size_t _, const char *format, va_list ap){
  95. (void)_; // 确保参数被使用
  96. return vfprintf(stdout, format, ap);
  97. }
  98. int vprintf_stderr(size_t _, const char *format, va_list ap){
  99. (void)_; // 确保参数被使用
  100. return vfprintf(stderr, format, ap);
  101. }
  102. size_t printf_stdout(size_t _, const char *format, ...) {
  103. (void)_; // 确保参数被使用
  104. va_list ap;
  105. va_start(ap, format);
  106. size_t re = vfprintf(stdout, format, ap);
  107. va_end(ap);
  108. return re;
  109. }
  110. size_t printf_stderr(size_t _, const char *format, ...) {
  111. (void)_; // 确保参数被使用
  112. va_list ap;
  113. va_start(ap, format);
  114. size_t re = vfprintf(stderr, format, ap);
  115. va_end(ap);
  116. return re;
  117. }
  118. #endif
  119. #ifndef AFUN_TOOL_C
  120. }
  121. #endif
  122. #endif
  123. #ifdef __cplusplus
  124. #ifndef AFUN_TOOL_C
  125. namespace aFuntool {
  126. #endif
  127. OutStream::OutStream(PrintFunction *func_) noexcept : func {func_} {
  128. }
  129. OutStream &OutStream::operator<<(char a) {
  130. func(0, "%c", a);
  131. return *this;
  132. }
  133. OutStream &OutStream::operator<<(signed char a) {
  134. func(0, "%c", a);
  135. return *this;
  136. }
  137. OutStream &OutStream::operator<<(short a) {
  138. func(0, "%d", a);
  139. return *this;
  140. }
  141. OutStream &OutStream::operator<<(int a) {
  142. func(0, "%d", a);
  143. return *this;
  144. }
  145. OutStream &OutStream::operator<<(long a) {
  146. func(0, "%ld", a);
  147. return *this;
  148. }
  149. OutStream &OutStream::operator<<(long long a) {
  150. func(0, "%lld", a);
  151. return *this;
  152. }
  153. OutStream &OutStream::operator<<(unsigned char a) {
  154. func(0, "%c", a);
  155. return *this;
  156. }
  157. OutStream &OutStream::operator<<(unsigned short a) {
  158. func(0, "%u", a);
  159. return *this;
  160. }
  161. OutStream &OutStream::operator<<(unsigned int a) {
  162. func(0, "%u", a);
  163. return *this;
  164. }
  165. OutStream &OutStream::operator<<(unsigned long a) {
  166. func(0, "%lu", a);
  167. return *this;
  168. }
  169. OutStream &OutStream::operator<<(unsigned long long a) {
  170. func(0, "%llu", a);
  171. return *this;
  172. }
  173. OutStream &OutStream::operator<<(const char *a){
  174. func(0, "%s", a);
  175. return *this;
  176. }
  177. OutStream &OutStream::operator<<(const std::string &a) {
  178. func(0, "%s", a.c_str());
  179. return *this;
  180. }
  181. OutStream &OutStream::operator<<(const void *a) {
  182. func(0, "%p", a);
  183. return *this;
  184. }
  185. OutStream &OutStream::operator<<(float a) {
  186. func(0, "%f", a);
  187. return *this;
  188. }
  189. OutStream &OutStream::operator<<(double a) {
  190. func(0, "%f", a);
  191. return *this;
  192. }
  193. OutStream &OutStream::operator<<(long double a) {
  194. func(0, "%lf", a);
  195. return *this;
  196. }
  197. #ifndef AFUN_TOOL_C
  198. }
  199. #endif
  200. #endif // __cplusplus
  201. #endif //AFUN_STDIO_INLINE_H