utils.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2013 Konstantin Mosesov
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * This file is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. *
  14. * This file is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <time.h>
  27. #include "utils.h"
  28. char **split(char *str, char *sep)
  29. {
  30. char **buf = NULL;
  31. char *token = NULL;
  32. char *saveptr = NULL;
  33. int i;
  34. buf = (char **)calloc(1, sizeof(char *));
  35. if (!buf)
  36. {
  37. return '\0';
  38. }
  39. if (str == NULL)
  40. return buf;
  41. if (strncmp(str, sep, strlen(sep)) <= 0)
  42. {
  43. // string doesn't contains a separator
  44. buf[0] = strdup(str);
  45. return buf;
  46. }
  47. token = strdup(str);
  48. for (i=0; token != NULL; token = saveptr, i++)
  49. {
  50. token = strtok_r(token, (const char *)sep, &saveptr);
  51. if (token == NULL || !strcmp(token, ""))
  52. break;
  53. buf = (char **)realloc(buf, (i+1) * sizeof(char *));
  54. buf[i] = strdup(token);
  55. }
  56. buf[i] = '\0';
  57. free(token);
  58. return buf;
  59. }
  60. char *rand_string(const int len)
  61. {
  62. char *buf;
  63. const char alphanum[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  64. int i;
  65. buf = (char *)calloc(len+1, sizeof(char));
  66. if (!buf)
  67. return NULL;
  68. srand(time(NULL));
  69. for (i=0; i<len; i++)
  70. {
  71. buf[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
  72. }
  73. buf[len] = '\0';
  74. return buf;
  75. }
  76. char *str_replace(char *original, char *pattern, char *replacement)
  77. {
  78. char *oriptr, *patloc, *retptr, *returned;
  79. size_t replen, patlen, orilen, patcnt, retlen, skplen;
  80. replen = strlen(replacement);
  81. patlen = strlen(pattern);
  82. orilen = strlen(original);
  83. patcnt = 0;
  84. // find how many times the pattern occurs in the original string
  85. for (oriptr = original; (patloc = strstr(oriptr, pattern)); oriptr = patloc + patlen)
  86. {
  87. patcnt++;
  88. }
  89. // allocate memory for the new string
  90. retlen = orilen + patcnt * (replen - patlen);
  91. returned = (char *)malloc((retlen+1) * sizeof(char));
  92. if (returned != NULL)
  93. {
  94. // copy the original string,
  95. // replacing all the instances of the pattern
  96. retptr = returned;
  97. for (oriptr = original; (patloc = strstr(oriptr, pattern)); oriptr = patloc + patlen)
  98. {
  99. skplen = patloc - oriptr;
  100. // copy the section until the occurence of the pattern
  101. strncpy(retptr, oriptr, skplen);
  102. retptr += skplen;
  103. // copy the replacement
  104. strncpy(retptr, replacement, replen);
  105. retptr += replen;
  106. }
  107. // copy the rest of the string.
  108. strcpy(retptr, oriptr);
  109. }
  110. return returned;
  111. }