bigstring.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "precomp_wasabi_bfc.h"
  2. #include "bigstring.h"
  3. BigString::BigString() {
  4. mem = NULL;
  5. m_linecount = 0;
  6. }
  7. BigString::~BigString() {
  8. if (mem != NULL) {
  9. FREE(mem);
  10. }
  11. strings.deleteAll();
  12. }
  13. const char *BigString::getValue() /*const*/ {
  14. if (mem != NULL) return mem;
  15. size_t l = 0;
  16. foreach(strings)
  17. l += strings.getfor()->len();
  18. endfor;
  19. mem = (char *)MALLOC(l+1);
  20. char *p = mem;
  21. String *s = NULL;
  22. size_t sl = 0;
  23. foreach(strings)
  24. s = strings.getfor();
  25. sl = s->len();
  26. if (sl > 0) MEMCPY((void *)p, (void *)s->getValue(), sl);
  27. p += sl;
  28. endfor;
  29. *p = 0;
  30. return mem;
  31. }
  32. void BigString::setValue(const char *val) {
  33. if (mem != NULL) {
  34. FREE(mem);
  35. mem = NULL;
  36. }
  37. strings.deleteAll();
  38. cat(val);
  39. }
  40. int BigString::isempty() {
  41. if (strings.getNumItems() == 0) return 1;
  42. foreach(strings)
  43. if (!strings.getfor()->isempty()) return 0;
  44. endfor;
  45. return 1;
  46. }
  47. void BigString::reset() {
  48. if (mem != NULL) {
  49. FREE(mem);
  50. mem = NULL;
  51. }
  52. strings.deleteAll();
  53. m_linecount = 0;
  54. }
  55. void BigString::catn(const char *s, int n) {
  56. String *str = new String();
  57. str->catn(s, n);
  58. cat(str->getValue());
  59. }
  60. void BigString::cat(const char *s) {
  61. if (mem != NULL) {
  62. FREE(mem);
  63. mem = NULL;
  64. }
  65. char *p = (char *)s;
  66. while (p && *p) {
  67. if (*p == '\r' || *p == '\n') {
  68. if (*(p+1) == '\n' && *p == '\r') p++;
  69. m_linecount++;
  70. }
  71. p++;
  72. }
  73. strings.addItem(new String(s));
  74. }
  75. char BigString::lastChar() {
  76. return strings.getLast()->lastChar();
  77. }
  78. char BigString::firstChar() {
  79. const char *s = strings.getFirst()->getValue();
  80. return s ? *s : 0;
  81. }
  82. int BigString::getLineCount() {
  83. return m_linecount;
  84. }