elog_utils.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * This file is part of the EasyLogger Library.
  3. *
  4. * Copyright (c) 2015-2018, Armink, <armink.ztl@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * 'Software'), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * Function: Some utils for this library.
  26. * Created on: 2015-04-28
  27. */
  28. #include <elog.h>
  29. #include <string.h>
  30. /**
  31. * another copy string function
  32. *
  33. * @param cur_len current copied log length, max size is ELOG_LINE_BUF_SIZE
  34. * @param dst destination
  35. * @param src source
  36. *
  37. * @return copied length
  38. */
  39. size_t elog_strcpy(size_t cur_len, char *dst, const char *src) {
  40. const char *src_old = src;
  41. assert(dst);
  42. assert(src);
  43. while (*src != 0) {
  44. /* make sure destination has enough space */
  45. if (cur_len++ < ELOG_LINE_BUF_SIZE) {
  46. *dst++ = *src++;
  47. } else {
  48. break;
  49. }
  50. }
  51. return src - src_old;
  52. }
  53. /**
  54. * Copy line log split by newline sign. It will copy all log when the newline sign isn't find.
  55. *
  56. * @param line line log buffer
  57. * @param log origin log buffer
  58. * @param len origin log buffer length
  59. *
  60. * @return copy size
  61. */
  62. size_t elog_cpyln(char *line, const char *log, size_t len) {
  63. size_t newline_len = strlen(ELOG_NEWLINE_SIGN), copy_size = 0;
  64. assert(line);
  65. assert(log);
  66. while (len--) {
  67. *line++ = *log++;
  68. copy_size++;
  69. if (copy_size >= newline_len && !strncmp(log - newline_len, ELOG_NEWLINE_SIGN, newline_len)) {
  70. break;
  71. }
  72. }
  73. return copy_size;
  74. }
  75. /**
  76. * This function will copy memory content from source address to destination
  77. * address.
  78. *
  79. * @param dst the address of destination memory
  80. * @param src the address of source memory
  81. * @param count the copied length
  82. *
  83. * @return the address of destination memory
  84. */
  85. void *elog_memcpy(void *dst, const void *src, size_t count) {
  86. char *tmp = (char *) dst, *s = (char *) src;
  87. assert(dst);
  88. assert(src);
  89. while (count--)
  90. *tmp++ = *s++;
  91. return dst;
  92. }