Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <stdarg.h>
- #include <math.h>
- #include <iostream>
- #include <sstream>
- #include <fstream>
- #include <iterator>
- #include <vector>
- #include <string>
- #include <algorithm>
- static inline char _is_digit(char ch)
- {
- return (ch >= '0') && (ch <= '9');
- }
- static uint8_t getNum(char **format)
- {
- uint8_t ret = 0;
- if (_is_digit(**format))
- {
- //get max lenght
- ret = (**format) - '0';
- (*format)++;
- if (_is_digit(**format))
- {
- //get max lenght
- ret = ret * 10 + ((**format) - '0');
- (*format)++;
- }
- }
- return ret;
- }
- int _printFloat(char *buff, uint8_t bufLen, double val, uint8_t minLen, uint8_t precision)
- {
- //debug
- uint8_t buffPtr = 0;
- uint64_t digitPtr = 1;
- if (!isfinite(val))
- {
- if (isnan(val))
- {
- if (bufLen--)
- *buff++ = 'N';
- if (bufLen--)
- *buff++ = 'a';
- if (bufLen--)
- *buff++ = 'N';
- }
- else if (isinf(val))
- {
- if (bufLen--)
- *buff++ = 'I';
- if (bufLen--)
- *buff++ = 'n';
- if (bufLen--)
- *buff++ = 'f';
- }
- int8_t lenTmp = minLen - 3;
- minLen = lenTmp > 0 ? lenTmp : 0;
- while (buffPtr < bufLen && minLen > 0)
- {
- if (minLen)
- minLen--;
- buff[buffPtr++] = ' ';
- }
- return buffPtr;
- }
- if (val < 0)
- {
- val = -val;
- if (minLen)
- minLen--;
- if (buffPtr < bufLen)
- {
- buff[buffPtr++] = '-';
- }
- }
- while (val >= 10)
- {
- val /= 10;
- digitPtr++;
- }
- if (precision > 32)
- precision = 4;
- while (buffPtr < bufLen)
- {
- uint8_t digit;
- if (digitPtr > 1)
- {
- digit = val + 0.5; //rand
- buff[buffPtr++] = digit + '0';
- digitPtr--;
- val -= digit;
- val *= 10;
- if (minLen)
- minLen--;
- }
- else if (digitPtr == 1)
- {
- digit = val + 0.5;
- buff[buffPtr++] = digit + '0';
- val -= digit;
- val *= 10;
- digitPtr = 0;
- if (minLen)
- minLen--;
- if (precision == 0)
- return buffPtr;
- if (buffPtr < bufLen)
- {
- bufLen--;
- if (minLen)
- minLen--;
- buff[buffPtr++] = '.';
- }
- }
- else
- {
- if (precision-- == 0)
- {
- while (buffPtr < bufLen && minLen > 0)
- {
- if (minLen)
- minLen--;
- buff[buffPtr++] = ' ';
- }
- return buffPtr;
- }
- digit = val + 0.5;
- buff[buffPtr++] = digit + '0';
- val -= digit;
- val *= 10;
- //buff[buffPtr++] = (char) val + '0';
- if (minLen)
- minLen--;
- }
- }
- return buffPtr;
- }
- //put only i32/u32/ double
- void sprintf_simple(char *buff, int buffLen, char *format, ...)
- {
- if (buff == 0 || format == 0 || buffLen < 2)
- return;
- uint16_t buffPtr = 0;
- va_list argptr;
- va_start(argptr, format);
- while (format)
- {
- //if (buffPtr >= buffLen)break;
- if (*format == '%')
- {
- //char *bakcup = format;
- if (*(format + 1) == '%')
- {
- format += 2;
- if (buffPtr < buffLen - 1)
- {
- buff[buffPtr++] = '%';
- continue;
- }
- else
- {
- buff[buffPtr - 2] = 0;
- break;
- }
- }
- //print float;
- unsigned char space = 0;
- unsigned char precision = 0;
- format++;
- space = getNum(&format);
- //format++;
- if (*format == '.')
- {
- //get prec
- format++;
- precision = getNum(&format);
- }
- if (*format == 'f')
- {
- format++;
- buffPtr += _printFloat(&buff[buffPtr], buffLen - buffPtr, va_arg(argptr, double), space, precision);
- }
- else if (*format == 'i')
- {
- format++;
- buffPtr += _printFloat(&buff[buffPtr], buffLen - buffPtr, (double)va_arg(argptr, int32_t), space, precision);
- }
- else if (*format == 'u')
- {
- format++;
- buffPtr += _printFloat(&buff[buffPtr], buffLen - buffPtr, (double)va_arg(argptr, uint32_t), space, precision);
- }
- else
- {
- //error
- if (buffPtr < buffLen - 4)
- {
- buff[buffPtr++] = '#';
- buff[buffPtr++] = 'E';
- buff[buffPtr++] = 'R';
- buff[buffPtr++] = '#';
- }
- else
- {
- break;
- }
- }
- }
- else
- {
- if (buffPtr < buffLen)
- {
- char tmp = *format++;
- buff[buffPtr++] = tmp;
- }
- else
- break;
- }
- }
- if (buffPtr < buffLen)
- {
- buff[buffPtr] = 0;
- }
- else
- buff[buffLen - 1] = 0;
- va_end(argptr);
- }
- #define buffSize 32
- void printfTest()
- {
- using namespace std;
- char buff[buffSize];
- float pi = 3.14159265359;
- memset(buff, 0, 31);
- sprintf_simple(buff, buffSize, "%8.1f_%4.2f_%.6f_", pi, pi * 10, pi * 100);
- cout << buff << endl;
- memset(buff, 0, 32);
- sprintf_simple(buff, 30, "abc123.%%1.1%f&%4.2f&%.6f", pi, pi * 10, pi * 100);
- cout << buff << endl;
- memset(buff, 0, 32);
- sprintf_simple(buff, 8, "%f %4.2f %.6f", pi, pi * 10, pi * 100);
- cout << buff << endl;
- memset(buff, 0, 32);
- sprintf_simple(buff, 24, "%f %4.2f %.6f", pi / 100, pi / 10, pi / 100);
- cout << buff << endl;
- sprintf_simple(buff, 32, "%i %u %f", -123, 10000);
- cout << buff << endl;
- }
- int main()
- {
- printfTest();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement