| 1 | #ifndef TPRINTF_H |
| 2 | #define TPRINTF_H |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | // Written by Emery Berger |
| 7 | |
| 8 | #if !defined(_WIN32) |
| 9 | |
| 10 | #include <unistd.h> |
| 11 | #include <string.h> |
| 12 | #include <cmath> |
| 13 | #include <cstdint> |
| 14 | |
| 15 | using namespace std; |
| 16 | |
| 17 | namespace tprintf { |
| 18 | |
| 19 | const int MAXBUF = 65536; |
| 20 | static int FD { 1 }; // 2 |
| 21 | |
| 22 | template <typename T> |
| 23 | inline void writeval(T v); |
| 24 | |
| 25 | |
| 26 | template <typename T> |
| 27 | inline unsigned int itoa(char * buf, T v) { |
| 28 | long n = (long) v; |
| 29 | auto startbuf = buf; |
| 30 | if (n < 0) { |
| 31 | *buf++ = '-'; |
| 32 | n = -n; |
| 33 | } |
| 34 | if (n == 0) { |
| 35 | *buf++ = '0'; |
| 36 | return static_cast<unsigned int>(buf - startbuf); |
| 37 | } |
| 38 | long tens = 1L; |
| 39 | while (n / (10 * tens)) { |
| 40 | tens *= 10; |
| 41 | } |
| 42 | while (tens) { |
| 43 | *buf++ = '0' + static_cast<char>(n / tens); |
| 44 | n = n - (n / tens) * tens; |
| 45 | tens /= 10; |
| 46 | } |
| 47 | return (unsigned int) (buf - startbuf); |
| 48 | } |
| 49 | |
| 50 | inline unsigned int ftoa(char * buf, double n, int decimalPlaces = 4) { |
| 51 | // Extract integer part |
| 52 | auto ipart = (long) n; |
| 53 | |
| 54 | // Extract floating part |
| 55 | auto fpart = n - (double) ipart; |
| 56 | if (fpart < 0.0) { |
| 57 | fpart = -fpart; |
| 58 | } |
| 59 | |
| 60 | // convert integer part to string |
| 61 | auto i = itoa(buf, ipart); |
| 62 | |
| 63 | if (decimalPlaces > 0) { |
| 64 | buf[i] = '.'; |
| 65 | auto multiple = pow(10, decimalPlaces); |
| 66 | fpart = fpart * multiple; |
| 67 | multiple /= 10; |
| 68 | while ((fpart < multiple) && (decimalPlaces > 0)) { |
| 69 | buf[++i] = '0'; |
| 70 | multiple /= 10; |
| 71 | decimalPlaces--; |
| 72 | } |
| 73 | if (fpart > 0) { |
| 74 | i = i + itoa(buf + i + 1, (long) fpart) + 1; |
| 75 | } |
| 76 | } |
| 77 | return i; |
| 78 | } |
| 79 | |
| 80 | inline void writeval(double n) { |
| 81 | char buf[MAXBUF]; |
| 82 | unsigned long len = ftoa(buf, n); |
| 83 | auto _ __attribute__((unused)) = write(FD, buf, len); |
| 84 | } |
| 85 | |
| 86 | inline void writeval(float n) { |
| 87 | char buf[MAXBUF]; |
| 88 | auto len = ftoa(buf, n); |
| 89 | auto _ __attribute__((unused)) = write(FD, buf, len); |
| 90 | } |
| 91 | |
| 92 | inline void writeval(const char * str) { |
| 93 | auto _ __attribute__((unused)) = write(FD, str, strlen(str)); |
| 94 | } |
| 95 | |
| 96 | inline void writeval(char * str) { |
| 97 | auto _ __attribute__((unused)) = write(FD, str, strlen(str)); |
| 98 | } |
| 99 | |
| 100 | inline void writeval(const char c) { |
| 101 | char buf[1]; |
| 102 | buf[0] = c; |
| 103 | auto _ __attribute__((unused)) = write(FD, buf, 1); |
| 104 | } |
| 105 | |
| 106 | inline void writeval(uint64_t n) { |
| 107 | char buf[MAXBUF]; |
| 108 | auto len = itoa(buf, n); |
| 109 | auto _ __attribute__((unused)) = write(FD, buf, len); |
| 110 | } |
| 111 | |
| 112 | template <class T> |
| 113 | inline void writeval(T n) { |
| 114 | char buf[MAXBUF]; |
| 115 | auto len = itoa(buf, n); |
| 116 | auto _ __attribute__((unused)) = write(FD, buf, len); |
| 117 | } |
| 118 | |
| 119 | inline void tprintf(const char* format) // base function |
| 120 | { |
| 121 | writeval(format); |
| 122 | } |
| 123 | |
| 124 | template<typename T, typename... Targs> |
| 125 | inline void tprintf(const char * format, T value, Targs... Fargs) |
| 126 | { |
| 127 | for ( ; *format != '\0'; format++ ) { |
| 128 | if ( *format == '@' ) { |
| 129 | if ( *(format + 1) == '@') { |
| 130 | writeval("@"); |
| 131 | format = format+2; |
| 132 | } else { |
| 133 | writeval(value); |
| 134 | tprintf(format+1, Fargs...); |
| 135 | return; |
| 136 | } |
| 137 | } |
| 138 | writeval(*format); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | } |
| 143 | |
| 144 | #endif // !_WIN32 |
| 145 | |
| 146 | #endif |