vsnprintf.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2017,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. *
  5. * This file is part of GNU Mes.
  6. *
  7. * GNU Mes is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * GNU Mes is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <mes/lib.h>
  21. #include <ctype.h>
  22. #include <stdarg.h>
  23. #include <string.h>
  24. int
  25. vsnprintf (char *str, size_t size, char const *format, va_list ap)
  26. {
  27. char const *p = format;
  28. int count = 0;
  29. char c;
  30. while (*p)
  31. if (*p != '%')
  32. {
  33. c = *p++;
  34. if (count < size)
  35. *str++ = c;
  36. count++;
  37. }
  38. else
  39. {
  40. p++;
  41. c = *p;
  42. int left_p = 0;
  43. int precision = -1;
  44. int width = -1;
  45. if (c == '-')
  46. {
  47. left_p = 1;
  48. c = *++p;
  49. }
  50. char pad = ' ';
  51. if (c == ' ')
  52. {
  53. pad = c;
  54. c = *p++;
  55. }
  56. if (c == '0')
  57. {
  58. pad = c;
  59. c = *p++;
  60. }
  61. if (c >= '0' && c <= '9')
  62. {
  63. width = abtol (&p, 10);
  64. c = *p;
  65. }
  66. else if (c == '*')
  67. {
  68. width = va_arg (ap, long);
  69. c = *++p;
  70. }
  71. if (c == '.')
  72. {
  73. c = *++p;
  74. if (c >= '0' && c <= '9')
  75. {
  76. precision = abtol (&p, 10);
  77. c = *p;
  78. }
  79. else if (c == '*')
  80. {
  81. precision = va_arg (ap, long);
  82. c = *++p;
  83. }
  84. }
  85. if (c == 'l')
  86. c = *++p;
  87. if (c == 'l')
  88. c = *++p;
  89. if (c == 'l')
  90. {
  91. eputs ("vsnprintf: skipping second: l\n");
  92. c = *++p;
  93. }
  94. switch (c)
  95. {
  96. case '%':
  97. {
  98. if (count < size)
  99. *str++ = *p;
  100. count++;
  101. break;
  102. }
  103. case 'c':
  104. {
  105. c = va_arg (ap, long);
  106. if (count < size)
  107. *str++ = c;
  108. count++;
  109. break;
  110. }
  111. case 'd':
  112. case 'i':
  113. case 'o':
  114. case 'u':
  115. case 'x':
  116. case 'X':
  117. {
  118. long d = va_arg (ap, long);
  119. int base = c == 'o' ? 8 : c == 'x' || c == 'X' ? 16 : 10;
  120. char *s = ntoab (d, base, c != 'u' && c != 'x' && c != 'X');
  121. if (c == 'X')
  122. strupr (s);
  123. int length = strlen (s);
  124. if (precision == -1)
  125. precision = length;
  126. if (!left_p)
  127. {
  128. while (width-- > precision)
  129. {
  130. if (count < size)
  131. *str++ = pad;
  132. count++;
  133. }
  134. while (precision > length)
  135. {
  136. if (count < size)
  137. *str++ = '0';
  138. precision--;
  139. width--;
  140. count++;
  141. }
  142. }
  143. while (*s)
  144. {
  145. if (precision-- <= 0)
  146. break;
  147. width--;
  148. c = *s++;
  149. if (count < size)
  150. *str++ = c;
  151. count++;
  152. }
  153. while (width > 0)
  154. {
  155. width--;
  156. if (count < size)
  157. *str++ = pad;
  158. count++;
  159. }
  160. break;
  161. }
  162. case 's':
  163. {
  164. char *s = va_arg (ap, char *);
  165. int length = s ? strlen (s) : 0;
  166. if (precision == -1)
  167. precision = length;
  168. if (!left_p)
  169. {
  170. while (width-- > precision)
  171. {
  172. if (count < size)
  173. *str++ = pad;
  174. count++;
  175. }
  176. while (width > length)
  177. {
  178. if (count < size)
  179. *str++ = ' ';
  180. precision--;
  181. width--;
  182. count++;
  183. }
  184. }
  185. while (s && *s)
  186. {
  187. if (precision-- <= 0)
  188. break;
  189. width--;
  190. c = *s++;
  191. if (count < size)
  192. *str++ = c;
  193. count++;
  194. }
  195. while (width > 0)
  196. {
  197. width--;
  198. if (count < size)
  199. *str++ = pad;
  200. count++;
  201. }
  202. break;
  203. }
  204. case 'f':
  205. case 'e':
  206. case 'E':
  207. case 'g':
  208. case 'G':
  209. {
  210. double d = va_arg8 (ap, double);
  211. char *s = dtoab (d, 10, 1);
  212. if (c == 'E' || c == 'G')
  213. strupr (s);
  214. int length = strlen (s);
  215. if (precision == -1)
  216. precision = length;
  217. if (!left_p)
  218. {
  219. while (width-- > precision)
  220. {
  221. if (count < size)
  222. *str++ = pad;
  223. count++;
  224. }
  225. while (precision > length)
  226. {
  227. if (count < size)
  228. *str++ = ' ';
  229. precision--;
  230. width--;
  231. count++;
  232. }
  233. }
  234. while (*s)
  235. {
  236. if (precision-- <= 0)
  237. break;
  238. width--;
  239. c = *s++;
  240. if (count < size)
  241. *str++ = c;
  242. count++;
  243. }
  244. while (width > 0)
  245. {
  246. width--;
  247. if (count < size)
  248. *str++ = pad;
  249. count++;
  250. }
  251. break;
  252. }
  253. case 'n':
  254. {
  255. int *n = va_arg (ap, int *);
  256. *n = count;
  257. break;
  258. }
  259. default:
  260. {
  261. eputs ("vsnprintf: not supported: %:");
  262. eputc (c);
  263. eputs ("\n");
  264. p++;
  265. }
  266. }
  267. p++;
  268. }
  269. va_end (ap);
  270. if (count < size)
  271. *str = 0;
  272. return count;
  273. }