您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

178 行
4.7KB

  1. /* FatLib Library
  2. * Copyright (C) 2013 by William Greiman
  3. *
  4. * This file is part of the FatLib Library
  5. *
  6. * This Library is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This Library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the FatLib Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #include <string.h>
  21. #include "ostream.h"
  22. #ifndef PSTR
  23. #define PSTR(x) x
  24. #endif
  25. //------------------------------------------------------------------------------
  26. void ostream::do_fill(unsigned len) {
  27. for (; len < width(); len++) putch(fill());
  28. width(0);
  29. }
  30. //------------------------------------------------------------------------------
  31. void ostream::fill_not_left(unsigned len) {
  32. if ((flags() & adjustfield) != left) {
  33. do_fill(len);
  34. }
  35. }
  36. //------------------------------------------------------------------------------
  37. char* ostream::fmtNum(uint32_t n, char *ptr, uint8_t base) {
  38. char a = flags() & uppercase ? 'A' - 10 : 'a' - 10;
  39. do {
  40. uint32_t m = n;
  41. n /= base;
  42. char c = m - base * n;
  43. *--ptr = c < 10 ? c + '0' : c + a;
  44. } while (n);
  45. return ptr;
  46. }
  47. //------------------------------------------------------------------------------
  48. void ostream::putBool(bool b) {
  49. if (flags() & boolalpha) {
  50. if (b) {
  51. putPgm(PSTR("true"));
  52. } else {
  53. putPgm(PSTR("false"));
  54. }
  55. } else {
  56. putChar(b ? '1' : '0');
  57. }
  58. }
  59. //------------------------------------------------------------------------------
  60. void ostream::putChar(char c) {
  61. fill_not_left(1);
  62. putch(c);
  63. do_fill(1);
  64. }
  65. //------------------------------------------------------------------------------
  66. void ostream::putDouble(double n) {
  67. uint8_t nd = precision();
  68. double round = 0.5;
  69. char sign;
  70. char buf[13]; // room for sign, 10 digits, '.', and zero byte
  71. char *end = buf + sizeof(buf) - 1;
  72. char *str = end;
  73. // terminate string
  74. *end = '\0';
  75. // get sign and make nonnegative
  76. if (n < 0.0) {
  77. sign = '-';
  78. n = -n;
  79. } else {
  80. sign = flags() & showpos ? '+' : '\0';
  81. }
  82. // check for larger than uint32_t
  83. if (n > 4.0E9) {
  84. putPgm(PSTR("BIG FLT"));
  85. return;
  86. }
  87. // round up and separate int and fraction parts
  88. for (uint8_t i = 0; i < nd; ++i) round *= 0.1;
  89. n += round;
  90. uint32_t intPart = n;
  91. double fractionPart = n - intPart;
  92. // format intPart and decimal point
  93. if (nd || (flags() & showpoint)) *--str = '.';
  94. str = fmtNum(intPart, str, 10);
  95. // calculate length for fill
  96. uint8_t len = sign ? 1 : 0;
  97. len += nd + end - str;
  98. // extract adjust field
  99. fmtflags adj = flags() & adjustfield;
  100. if (adj == internal) {
  101. if (sign) putch(sign);
  102. do_fill(len);
  103. } else {
  104. // do fill for internal or right
  105. fill_not_left(len);
  106. if (sign) *--str = sign;
  107. }
  108. putstr(str);
  109. // output fraction
  110. while (nd-- > 0) {
  111. fractionPart *= 10.0;
  112. int digit = static_cast<int>(fractionPart);
  113. putch(digit + '0');
  114. fractionPart -= digit;
  115. }
  116. // do fill if not done above
  117. do_fill(len);
  118. }
  119. //------------------------------------------------------------------------------
  120. void ostream::putNum(int32_t n) {
  121. bool neg = n < 0 && flagsToBase() == 10;
  122. if (neg) n = -n;
  123. putNum(n, neg);
  124. }
  125. //------------------------------------------------------------------------------
  126. void ostream::putNum(uint32_t n, bool neg) {
  127. char buf[13];
  128. char* end = buf + sizeof(buf) - 1;
  129. char* num;
  130. char* str;
  131. uint8_t base = flagsToBase();
  132. *end = '\0';
  133. str = num = fmtNum(n, end, base);
  134. if (base == 10) {
  135. if (neg) {
  136. *--str = '-';
  137. } else if (flags() & showpos) {
  138. *--str = '+';
  139. }
  140. } else if (flags() & showbase) {
  141. if (flags() & hex) {
  142. *--str = flags() & uppercase ? 'X' : 'x';
  143. }
  144. *--str = '0';
  145. }
  146. uint8_t len = end - str;
  147. fmtflags adj = flags() & adjustfield;
  148. if (adj == internal) {
  149. while (str < num) putch(*str++);
  150. }
  151. if (adj != left) {
  152. do_fill(len);
  153. }
  154. putstr(str);
  155. do_fill(len);
  156. }
  157. //------------------------------------------------------------------------------
  158. void ostream::putPgm(const char* str) {
  159. int n;
  160. for (n = 0; pgm_read_byte(&str[n]); n++) {}
  161. fill_not_left(n);
  162. for (uint8_t c; (c = pgm_read_byte(str)); str++) {
  163. putch(c);
  164. }
  165. do_fill(n);
  166. }
  167. //------------------------------------------------------------------------------
  168. void ostream::putStr(const char *str) {
  169. unsigned n = strlen(str);
  170. fill_not_left(n);
  171. putstr(str);
  172. do_fill(n);
  173. }