utils.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. */
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <time.h>
  27. #include "../../sr_module.h"
  28. #include <jni.h>
  29. #include "global.h"
  30. #include "utils.h"
  31. #include "java_mod.h"
  32. #include "java_iface.h"
  33. #include "java_support.h"
  34. #include "java_native_methods.h"
  35. #include "java_sig_parser.h"
  36. char **split(char *str, char *sep)
  37. {
  38. char **buf = NULL;
  39. char *token = NULL;
  40. char *saveptr = NULL;
  41. int i;
  42. buf = (char **)pkg_malloc(sizeof(char *));
  43. if (!buf)
  44. {
  45. LM_ERR("%s: pkg_malloc() has failed. Not enough memory!\n", APP_NAME);
  46. return NULL;
  47. }
  48. memset(&buf, 0, sizeof(char *));
  49. if (str == NULL)
  50. return buf;
  51. if (strncmp(str, sep, strlen(sep)) <= 0)
  52. {
  53. // string doesn't contains a separator
  54. buf[0] = strdup(str);
  55. return buf;
  56. }
  57. token = strdup(str);
  58. for (i=0; token != NULL; token = saveptr, i++)
  59. {
  60. token = strtok_r(token, (const char *)sep, &saveptr);
  61. if (token == NULL || !strcmp(token, ""))
  62. break;
  63. buf = (char **)pkg_realloc(buf, (i+2) * sizeof(char *));
  64. if (!buf)
  65. {
  66. LM_ERR("%s: pkg_realloc() has failed. Not enough memory!\n", APP_NAME);
  67. return NULL;
  68. }
  69. buf[i] = strdup(token);
  70. }
  71. buf[i] = NULL;
  72. free(token);
  73. return buf;
  74. }