You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.8KB

  1. /*
  2. * Various simple utilities for ffmpeg system
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. #include <ctype.h>
  21. #if !defined(CONFIG_NOCUTILS)
  22. /**
  23. * Return TRUE if val is a prefix of str. If it returns TRUE, ptr is
  24. * set to the next character in 'str' after the prefix.
  25. *
  26. * @param str input string
  27. * @param val prefix to test
  28. * @param ptr updated after the prefix in str in there is a match
  29. * @return TRUE if there is a match
  30. */
  31. int strstart(const char *str, const char *val, const char **ptr)
  32. {
  33. const char *p, *q;
  34. p = str;
  35. q = val;
  36. while (*q != '\0') {
  37. if (*p != *q)
  38. return 0;
  39. p++;
  40. q++;
  41. }
  42. if (ptr)
  43. *ptr = p;
  44. return 1;
  45. }
  46. /**
  47. * Return TRUE if val is a prefix of str (case independent). If it
  48. * returns TRUE, ptr is set to the next character in 'str' after the
  49. * prefix.
  50. *
  51. * @param str input string
  52. * @param val prefix to test
  53. * @param ptr updated after the prefix in str in there is a match
  54. * @return TRUE if there is a match */
  55. int stristart(const char *str, const char *val, const char **ptr)
  56. {
  57. const char *p, *q;
  58. p = str;
  59. q = val;
  60. while (*q != '\0') {
  61. if (toupper(*(unsigned char *)p) != toupper(*(unsigned char *)q))
  62. return 0;
  63. p++;
  64. q++;
  65. }
  66. if (ptr)
  67. *ptr = p;
  68. return 1;
  69. }
  70. /**
  71. * Copy the string str to buf. If str length is bigger than buf_size -
  72. * 1 then it is clamped to buf_size - 1.
  73. * NOTE: this function does what strncpy should have done to be
  74. * useful. NEVER use strncpy.
  75. *
  76. * @param buf destination buffer
  77. * @param buf_size size of destination buffer
  78. * @param str source string
  79. */
  80. void pstrcpy(char *buf, int buf_size, const char *str)
  81. {
  82. int c;
  83. char *q = buf;
  84. if (buf_size <= 0)
  85. return;
  86. for(;;) {
  87. c = *str++;
  88. if (c == 0 || q >= buf + buf_size - 1)
  89. break;
  90. *q++ = c;
  91. }
  92. *q = '\0';
  93. }
  94. /* strcat and truncate. */
  95. char *pstrcat(char *buf, int buf_size, const char *s)
  96. {
  97. int len;
  98. len = strlen(buf);
  99. if (len < buf_size)
  100. pstrcpy(buf + len, buf_size - len, s);
  101. return buf;
  102. }
  103. #endif