bprint.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2012 Nicolas George
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/bprint.c"
  21. #undef printf
  22. static void bprint_pascal(AVBPrint *b, unsigned size)
  23. {
  24. unsigned i, j;
  25. unsigned p[42];
  26. av_assert0(size < FF_ARRAY_ELEMS(p));
  27. p[0] = 1;
  28. av_bprintf(b, "%8d\n", 1);
  29. for (i = 1; i <= size; i++) {
  30. p[i] = 1;
  31. for (j = i - 1; j > 0; j--)
  32. p[j] = p[j] + p[j - 1];
  33. for (j = 0; j <= i; j++)
  34. av_bprintf(b, "%8d", p[j]);
  35. av_bprintf(b, "\n");
  36. }
  37. }
  38. int main(void)
  39. {
  40. AVBPrint b;
  41. char buf[256];
  42. struct tm testtime = { .tm_year = 100, .tm_mon = 11, .tm_mday = 20 };
  43. av_bprint_init(&b, 0, -1);
  44. bprint_pascal(&b, 5);
  45. printf("Short text in unlimited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
  46. printf("%s\n", b.str);
  47. av_bprint_finalize(&b, NULL);
  48. av_bprint_init(&b, 0, -1);
  49. bprint_pascal(&b, 25);
  50. printf("Long text in unlimited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
  51. av_bprint_finalize(&b, NULL);
  52. av_bprint_init(&b, 0, 2048);
  53. bprint_pascal(&b, 25);
  54. printf("Long text in limited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
  55. av_bprint_finalize(&b, NULL);
  56. av_bprint_init(&b, 0, 1);
  57. bprint_pascal(&b, 5);
  58. printf("Short text in automatic buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
  59. av_bprint_init(&b, 0, 1);
  60. bprint_pascal(&b, 25);
  61. printf("Long text in automatic buffer: %u/%u\n", (unsigned)strlen(b.str)/8*8, b.len);
  62. /* Note that the size of the automatic buffer is arch-dependent. */
  63. av_bprint_init(&b, 0, 0);
  64. bprint_pascal(&b, 25);
  65. printf("Long text count only buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
  66. av_bprint_init_for_buffer(&b, buf, sizeof(buf));
  67. bprint_pascal(&b, 25);
  68. printf("Long text count only buffer: %u/%u\n", (unsigned)strlen(buf), b.len);
  69. av_bprint_init(&b, 0, -1);
  70. av_bprint_strftime(&b, "%Y-%m-%d", &testtime);
  71. printf("strftime full: %u/%u \"%s\"\n", (unsigned)strlen(buf), b.len, b.str);
  72. av_bprint_finalize(&b, NULL);
  73. av_bprint_init(&b, 0, 8);
  74. av_bprint_strftime(&b, "%Y-%m-%d", &testtime);
  75. printf("strftime truncated: %u/%u \"%s\"\n", (unsigned)strlen(buf), b.len, b.str);
  76. return 0;
  77. }