2
0

utils.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (C) 2013 Konstantin Mosesov
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * This file is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. *
  12. * This file 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
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <time.h>
  25. #include "../../sr_module.h"
  26. #include <jni.h>
  27. #include "global.h"
  28. #include "utils.h"
  29. #include "app_java_mod.h"
  30. #include "java_iface.h"
  31. #include "java_support.h"
  32. #include "java_native_methods.h"
  33. #include "java_sig_parser.h"
  34. char **split(char *str, char *sep)
  35. {
  36. char **buf = NULL;
  37. char *token = NULL;
  38. char *saveptr = NULL;
  39. int i;
  40. buf = (char **)pkg_malloc(sizeof(char *));
  41. if (!buf)
  42. {
  43. LM_ERR("%s: pkg_malloc() has failed. Not enough memory!\n", APP_NAME);
  44. return NULL;
  45. }
  46. memset(&buf, 0, sizeof(char *));
  47. if (str == NULL)
  48. return buf;
  49. if (strncmp(str, sep, strlen(sep)) <= 0)
  50. {
  51. // string doesn't contains a separator
  52. buf[0] = strdup(str);
  53. return buf;
  54. }
  55. token = strdup(str);
  56. for (i=0; token != NULL; token = saveptr, i++)
  57. {
  58. token = strtok_r(token, (const char *)sep, &saveptr);
  59. if (token == NULL || !strcmp(token, ""))
  60. break;
  61. buf = (char **)pkg_realloc(buf, (i+2) * sizeof(char *));
  62. if (!buf)
  63. {
  64. LM_ERR("%s: pkg_realloc() has failed. Not enough memory!\n", APP_NAME);
  65. return NULL;
  66. }
  67. buf[i] = strdup(token);
  68. }
  69. buf[i] = NULL;
  70. free(token);
  71. return buf;
  72. }