Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <type_traits>
- void _pf_seek_direct_out(const char *&format)
- {
- for (; *format != '\0'; ++format) {
- if (*format == '%') {
- switch (*++format) {
- case '%':
- std::cout << '%';
- break;
- default:
- return;
- }
- } else {
- std::cout << *format;
- }
- }
- }
- void _pf_output_format(const char *&format)
- {
- /* Flags */
- bool stop = false;
- do {
- switch (*format) {
- case '#':
- std::cout << std::showbase;
- break;
- case '0':
- std::cout << std::setfill('0');
- break;
- case '-':
- std::cout << std::left;
- std::cout << std::setfill(' ');
- break;
- case ' ':
- std::cout << std::setfill(' ');
- break;
- case '+':
- std::cout << std::showpos;
- break;
- default:
- stop = true;
- break;
- }
- format++;
- } while (!stop);
- format--;
- /* Field width */
- int num = 0;
- while ('0' <= *format && *format <= '9') {
- num *= 10;
- num += *format++ - '0';
- }
- if (num)
- std::cout << std::setw(num);
- /* Precision */
- if (*format == '.') {
- int num = 0;
- while ('0' <= *format && *format <= '9') {
- num *= 10;
- num += *format++ - '0';
- }
- std::cout << std::setprecision(num);
- }
- }
- template<typename T, typename ...Ts>
- void printf(const char *format, T arg, Ts... args)
- {
- static_assert(std::is_integral<T>::value, "Implementation for integral types");
- // Seek normal text
- _pf_seek_direct_out(format);
- // Apply output format settings
- _pf_output_format(format);
- // Conversion specifiers
- switch (*format++) {
- case 'd':
- case 'i':
- std::cout << std::dec;
- break;
- case 'O':
- std::cout << std::uppercase;
- /* Fall-through */
- case 'o':
- std::cout << std::oct;
- break;
- case 'X':
- std::cout << std::uppercase;
- /* Fall-through */
- case 'x':
- std::cout << std::hex;
- break;
- default:
- ; /* Errör :c */
- }
- std::cout << arg;
- printf(format, args...);
- }
- int main()
- {
- printf("Das ist der %d. Test mit %X Format Anweisungen drinne: %08x, %-10d!\n", 123, 4, 0xff, 12345);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement