PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/xdebug_str.c

http://github.com/derickr/xdebug
C | 132 lines | 92 code | 17 blank | 23 comment | 17 complexity | 08edcec14b70c297f7c70bae9f1da235 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Xdebug |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2002-2016 Derick Rethans |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 1.0 of the Xdebug license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at |
  10. | http://xdebug.derickrethans.nl/license.php |
  11. | If you did not receive a copy of the Xdebug license and are unable |
  12. | to obtain it through the world-wide-web, please send a note to |
  13. | xdebug@derickrethans.nl so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Derick Rethans <derick@xdebug.org> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include <stdarg.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <locale.h>
  23. #include "php.h"
  24. #include "ext/standard/php_string.h"
  25. #include "xdebug_mm.h"
  26. #include "xdebug_str.h"
  27. void xdebug_str_add(xdebug_str *xs, char *str, int f)
  28. {
  29. int l = strlen(str);
  30. if (xs->l + l > xs->a - 1) {
  31. xs->d = xdrealloc(xs->d, xs->a + l + XDEBUG_STR_PREALLOC);
  32. xs->a = xs->a + l + XDEBUG_STR_PREALLOC;
  33. }
  34. if (!xs->l) {
  35. xs->d[0] = '\0';
  36. }
  37. memcpy(xs->d + xs->l, str, l);
  38. xs->d[xs->l + l] = '\0';
  39. xs->l = xs->l + l;
  40. if (f) {
  41. xdfree(str);
  42. }
  43. }
  44. void xdebug_str_addl(xdebug_str *xs, char *str, int le, int f)
  45. {
  46. if (xs->l + le > xs->a - 1) {
  47. xs->d = xdrealloc(xs->d, xs->a + le + XDEBUG_STR_PREALLOC);
  48. xs->a = xs->a + le + XDEBUG_STR_PREALLOC;
  49. }
  50. if (!xs->l) {
  51. xs->d[0] = '\0';
  52. }
  53. memcpy(xs->d + xs->l, str, le);
  54. xs->d[xs->l + le] = '\0';
  55. xs->l = xs->l + le;
  56. if (f) {
  57. xdfree(str);
  58. }
  59. }
  60. void xdebug_str_chop(xdebug_str *xs, int c)
  61. {
  62. if (c > xs->l) {
  63. /* Do nothing if the chop amount is larger than the buffer size */
  64. } else {
  65. xs->l -= c;
  66. xs->d[xs->l] = '\0';
  67. }
  68. }
  69. void xdebug_str_free(xdebug_str *s)
  70. {
  71. if (s->d) {
  72. xdfree(s->d);
  73. }
  74. }
  75. char *xdebug_sprintf(const char* fmt, ...)
  76. {
  77. char *new_str;
  78. int size = 1;
  79. va_list args;
  80. new_str = (char *) xdmalloc(size);
  81. for (;;) {
  82. int n;
  83. va_start(args, fmt);
  84. n = vsnprintf(new_str, size, fmt, args);
  85. va_end(args);
  86. if (n > -1 && n < size) {
  87. break;
  88. }
  89. if (n < 0) {
  90. size *= 2;
  91. } else {
  92. size = n + 1;
  93. }
  94. new_str = (char *) xdrealloc(new_str, size);
  95. }
  96. return new_str;
  97. }
  98. /**
  99. * Duplicate zend_strndup in core to avoid mismatches
  100. * in C-runtime libraries when xdebug and core are built
  101. * with different run-time libraries.
  102. */
  103. char *xdebug_strndup(const char *s, int length)
  104. {
  105. char *p;
  106. p = (char *) xdmalloc(length + 1);
  107. if (p == NULL) {
  108. return p;
  109. }
  110. if (length) {
  111. memcpy(p, s, length);
  112. }
  113. p[length] = 0;
  114. return p;
  115. }