aboutsummaryrefslogtreecommitdiff
path: root/Src/Wasabi/bfc/string/bigstring.cpp
blob: 20c7c09fc9bcc34b9a9e3ea3ffc5fdc932cf0544 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "precomp_wasabi_bfc.h"
#include "bigstring.h"

BigString::BigString() {
  mem = NULL;
  m_linecount = 0;
}

BigString::~BigString() {
  if (mem != NULL) {
    FREE(mem);
  }
  strings.deleteAll();
}

const char *BigString::getValue() /*const*/ {
  if (mem != NULL) return mem;
  size_t l = 0;
  foreach(strings)
    l += strings.getfor()->len();
  endfor;
  mem = (char *)MALLOC(l+1);
  char *p = mem;
  String *s = NULL;
  size_t sl = 0;
  foreach(strings)
    s = strings.getfor();
    sl = s->len();
    if (sl > 0) MEMCPY((void *)p, (void *)s->getValue(), sl);
    p += sl;
  endfor;
  *p = 0;
  return mem;
}

void BigString::setValue(const char *val) {
  if (mem != NULL) {
    FREE(mem);
    mem = NULL;
  }
  strings.deleteAll();
  cat(val);
}

int BigString::isempty() {
  if (strings.getNumItems() == 0) return 1;
  foreach(strings)
    if (!strings.getfor()->isempty()) return 0;
  endfor;
  return 1;
}

void BigString::reset() {
  if (mem != NULL) {
    FREE(mem);
    mem = NULL;
  }
  strings.deleteAll();
  m_linecount = 0;
}

void BigString::catn(const char *s, int n) {
  String *str = new String();
  str->catn(s, n);
  cat(str->getValue());
}

void BigString::cat(const char *s) {
  if (mem != NULL) {
    FREE(mem);
    mem = NULL;
  }
  char *p = (char *)s;
  while (p && *p) {
    if (*p == '\r' || *p == '\n') {
      if (*(p+1) == '\n' && *p == '\r') p++;
      m_linecount++;
    }
    p++;
  }
  strings.addItem(new String(s));
}

char BigString::lastChar() {
  return strings.getLast()->lastChar();
}

char BigString::firstChar() {
  const char *s = strings.getFirst()->getValue();
  return s ? *s : 0;
}

int BigString::getLineCount() {
  return m_linecount;
}