string_brom.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* string.c - common string routines */
  2. /*
  3. * Copyright (c) 2014 Wind River Systems, Inc.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. //#include <string.h>
  8. #include <stdint.h>
  9. #include <sys/types.h>
  10. #include <brom_interface.h>
  11. #ifdef __cplusplus
  12. #define _MLIBC_RESTRICT __restrict__
  13. #else
  14. #define _MLIBC_RESTRICT restrict
  15. #endif
  16. /**
  17. *
  18. * @brief Copy a string
  19. *
  20. * @return pointer to destination buffer <d>
  21. */
  22. char *strcpy(char *_MLIBC_RESTRICT d, const char *_MLIBC_RESTRICT s)
  23. {
  24. return pbrom_libc_api->p_strcpy(d, s);
  25. }
  26. /**
  27. *
  28. * @brief Copy part of a string
  29. *
  30. * @return pointer to destination buffer <d>
  31. */
  32. char *strncpy(char *_MLIBC_RESTRICT d, const char *_MLIBC_RESTRICT s, size_t n)
  33. {
  34. return pbrom_libc_api->p_strncpy(d, s, n);
  35. }
  36. /**
  37. *
  38. * @brief String scanning operation
  39. *
  40. * @return pointer to 1st instance of found byte, or NULL if not found
  41. */
  42. char *strchr(const char *s, int c)
  43. {
  44. return pbrom_libc_api->p_strchr(s, c);
  45. }
  46. /**
  47. *
  48. * @brief String scanning operation
  49. *
  50. * @return pointer to last instance of found byte, or NULL if not found
  51. */
  52. char *strrchr(const char *s, int c)
  53. {
  54. return pbrom_libc_api->p_strrchr(s, c);
  55. }
  56. /**
  57. *
  58. * @brief Get string length
  59. *
  60. * @return number of bytes in string <s>
  61. */
  62. size_t strlen(const char *s)
  63. {
  64. return pbrom_libc_api->p_strlen(s);
  65. }
  66. /**
  67. *
  68. * @brief Get fixed-size string length
  69. *
  70. * @return number of bytes in fixed-size string <s>
  71. */
  72. size_t strnlen(const char *s, size_t maxlen)
  73. {
  74. return pbrom_libc_api->p_strnlen(s, maxlen);
  75. }
  76. /**
  77. *
  78. * @brief Compare two strings
  79. *
  80. * @return negative # if <s1> < <s2>, 0 if <s1> == <s2>, else positive #
  81. */
  82. int strcmp(const char *s1, const char *s2)
  83. {
  84. return pbrom_libc_api->p_strcmp(s1, s2);
  85. }
  86. /**
  87. *
  88. * @brief Compare part of two strings
  89. *
  90. * @return negative # if <s1> < <s2>, 0 if <s1> == <s2>, else positive #
  91. */
  92. int strncmp(const char *s1, const char *s2, size_t n)
  93. {
  94. return pbrom_libc_api->p_strncmp(s1, s2, n);
  95. }
  96. /**
  97. * @brief Separate `str` by any char in `sep` and return NULL terminated
  98. * sections. Consecutive `sep` chars in `str` are treated as a single
  99. * separator.
  100. *
  101. * @return pointer to NULL terminated string or NULL on errors.
  102. */
  103. #if 0
  104. char *strtok_r(char *str, const char *sep, char **state)
  105. {
  106. return pbrom_libc_api->p_strtok_r(str, sep, state);
  107. }
  108. #endif
  109. char *strcat(char *_MLIBC_RESTRICT dest, const char *_MLIBC_RESTRICT src)
  110. {
  111. return pbrom_libc_api->p_strcat(dest, src);
  112. }
  113. char *strncat(char *_MLIBC_RESTRICT dest, const char *_MLIBC_RESTRICT src,
  114. size_t n)
  115. {
  116. return pbrom_libc_api->p_strncat(dest, src, n);
  117. }
  118. /**
  119. *
  120. * @brief Compare two memory areas
  121. *
  122. * @return negative # if <m1> < <m2>, 0 if <m1> == <m2>, else positive #
  123. */
  124. int memcmp(const void *m1, const void *m2, size_t n)
  125. {
  126. return pbrom_libc_api->p_memcmp(m1, m2, n);
  127. }
  128. /**
  129. *
  130. * @brief Copy bytes in memory with overlapping areas
  131. *
  132. * @return pointer to destination buffer <d>
  133. */
  134. void *memmove(void *d, const void *s, size_t n)
  135. {
  136. return pbrom_libc_api->p_memmove(d, s, n);
  137. }
  138. /**
  139. *
  140. * @brief Copy bytes in memory
  141. *
  142. * @return pointer to start of destination buffer
  143. */
  144. void *memcpy(void *_MLIBC_RESTRICT d, const void *_MLIBC_RESTRICT s, size_t n)
  145. {
  146. return pbrom_libc_api->p_memcpy(d, s, n);
  147. }
  148. /**
  149. *
  150. * @brief Set bytes in memory
  151. *
  152. * @return pointer to start of buffer
  153. */
  154. void *memset(void *buf, int c, size_t n)
  155. {
  156. return pbrom_libc_api->p_memset(buf, c, n);
  157. }
  158. /**
  159. *
  160. * @brief Scan byte in memory
  161. *
  162. * @return pointer to start of found byte
  163. */
  164. void *memchr(const void *s, int c, size_t n)
  165. {
  166. return pbrom_libc_api->p_memchr(s, c, n);
  167. }