avstring.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  3. * Copyright (c) 2007 Mans Rullgard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stdarg.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include "avstring.h"
  26. #include "mem.h"
  27. int av_strstart(const char *str, const char *pfx, const char **ptr)
  28. {
  29. while (*pfx && *pfx == *str) {
  30. pfx++;
  31. str++;
  32. }
  33. if (!*pfx && ptr)
  34. *ptr = str;
  35. return !*pfx;
  36. }
  37. int av_stristart(const char *str, const char *pfx, const char **ptr)
  38. {
  39. while (*pfx && toupper((unsigned)*pfx) == toupper((unsigned)*str)) {
  40. pfx++;
  41. str++;
  42. }
  43. if (!*pfx && ptr)
  44. *ptr = str;
  45. return !*pfx;
  46. }
  47. size_t av_strlcpy(char *dst, const char *src, size_t size)
  48. {
  49. size_t len = 0;
  50. while (++len < size && *src)
  51. *dst++ = *src++;
  52. if (len <= size)
  53. *dst = 0;
  54. return len + strlen(src) - 1;
  55. }
  56. size_t av_strlcat(char *dst, const char *src, size_t size)
  57. {
  58. size_t len = strlen(dst);
  59. if (size <= len + 1)
  60. return len + strlen(src);
  61. return len + av_strlcpy(dst + len, src, size - len);
  62. }
  63. size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)
  64. {
  65. int len = strlen(dst);
  66. va_list vl;
  67. va_start(vl, fmt);
  68. len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);
  69. va_end(vl);
  70. return len;
  71. }
  72. char *av_d2str(double d)
  73. {
  74. char *str= av_malloc(16);
  75. if(str) snprintf(str, 16, "%f", d);
  76. return str;
  77. }