avstring.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. int av_strstart(const char *str, const char *pfx, const char **ptr)
  27. {
  28. while (*pfx && *pfx == *str) {
  29. pfx++;
  30. str++;
  31. }
  32. if (!*pfx && ptr)
  33. *ptr = str;
  34. return !*pfx;
  35. }
  36. int av_stristart(const char *str, const char *pfx, const char **ptr)
  37. {
  38. while (*pfx && toupper((unsigned)*pfx) == toupper((unsigned)*str)) {
  39. pfx++;
  40. str++;
  41. }
  42. if (!*pfx && ptr)
  43. *ptr = str;
  44. return !*pfx;
  45. }
  46. size_t av_strlcpy(char *dst, const char *src, size_t size)
  47. {
  48. size_t len = 0;
  49. while (++len < size && *src)
  50. *dst++ = *src++;
  51. if (len <= size)
  52. *dst = 0;
  53. return len + strlen(src) - 1;
  54. }
  55. size_t av_strlcat(char *dst, const char *src, size_t size)
  56. {
  57. size_t len = strlen(dst);
  58. if (size <= len + 1)
  59. return len + strlen(src);
  60. return len + av_strlcpy(dst + len, src, size - len);
  61. }
  62. size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)
  63. {
  64. int len = strlen(dst);
  65. va_list vl;
  66. va_start(vl, fmt);
  67. len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);
  68. va_end(vl);
  69. return len;
  70. }