3bf626aa41e99321d12da3c07b13e11b35daad7e
[swftools.git] / pdf2swf / xpdf / GString.cc
1 //========================================================================
2 //
3 // GString.cc
4 //
5 // Simple variable-length string type.
6 //
7 // Copyright 1996-2002 Glyph & Cog, LLC
8 //
9 //========================================================================
10
11 #ifdef __GNUC__
12 #pragma implementation
13 #endif
14
15 #include <aconf.h>
16 #include <stdlib.h>
17 #include <stddef.h>
18 #include <string.h>
19 #include <ctype.h>
20 #include "gtypes.h"
21 #include "GString.h"
22
23 static inline int size(int len) {
24   int delta;
25
26   delta = len < 256 ? 7 : 255;
27   return ((len + 1) + delta) & ~delta;
28 }
29
30 inline void GString::resize(int length1) {
31   char *s1;
32
33   if (!s) {
34     s = new char[size(length1)];
35   } else if (size(length1) != size(length)) {
36     s1 = new char[size(length1)];
37     memcpy(s1, s, length + 1);
38     delete[] s;
39     s = s1;
40   }
41 }
42
43 GString::GString() {
44   s = NULL;
45   resize(length = 0);
46   s[0] = '\0';
47 }
48
49 GString::GString(const char *sA) {
50   int n = strlen(sA);
51
52   s = NULL;
53   resize(length = n);
54   memcpy(s, sA, n + 1);
55 }
56
57 GString::GString(const char *sA, int lengthA) {
58   s = NULL;
59   resize(length = lengthA);
60   memcpy(s, sA, length * sizeof(char));
61   s[length] = '\0';
62 }
63
64 GString::GString(GString *str, int idx, int lengthA) {
65   s = NULL;
66   resize(length = lengthA);
67   memcpy(s, str->getCString() + idx, length);
68   s[length] = '\0';
69 }
70
71 GString::GString(GString *str) {
72   s = NULL;
73   resize(length = str->getLength());
74   memcpy(s, str->getCString(), length + 1);
75 }
76
77 GString::GString(GString *str1, GString *str2) {
78   int n1 = str1->getLength();
79   int n2 = str2->getLength();
80
81   s = NULL;
82   resize(length = n1 + n2);
83   memcpy(s, str1->getCString(), n1);
84   memcpy(s + n1, str2->getCString(), n2 + 1);
85 }
86
87 GString *GString::fromInt(int x) {
88   char buf[24]; // enough space for 64-bit ints plus a little extra
89   GBool neg;
90   Guint y;
91   int i;
92
93   i = 24;
94   if (x == 0) {
95     buf[--i] = '0';
96   } else {
97     if ((neg = x < 0)) {
98       y = (Guint)-x;
99     } else {
100       y = (Guint)x;
101     }
102     while (i > 0 && y > 0) {
103       buf[--i] = '0' + y % 10;
104       y /= 10;
105     }
106     if (neg && i > 0) {
107       buf[--i] = '-';
108     }
109   }
110   return new GString(buf + i, 24 - i);
111 }
112
113 GString::~GString() {
114   delete[] s;
115 }
116
117 GString *GString::clear() {
118   s[length = 0] = '\0';
119   resize(0);
120   return this;
121 }
122
123 GString *GString::append(char c) {
124   resize(length + 1);
125   s[length++] = c;
126   s[length] = '\0';
127   return this;
128 }
129
130 GString *GString::append(GString *str) {
131   int n = str->getLength();
132
133   resize(length + n);
134   memcpy(s + length, str->getCString(), n + 1);
135   length += n;
136   return this;
137 }
138
139 GString *GString::append(const char *str) {
140   int n = strlen(str);
141
142   resize(length + n);
143   memcpy(s + length, str, n + 1);
144   length += n;
145   return this;
146 }
147
148 GString *GString::append(const char *str, int lengthA) {
149   resize(length + lengthA);
150   memcpy(s + length, str, lengthA);
151   length += lengthA;
152   s[length] = '\0';
153   return this;
154 }
155
156 GString *GString::insert(int i, char c) {
157   int j;
158
159   resize(length + 1);
160   for (j = length + 1; j > i; --j)
161     s[j] = s[j-1];
162   s[i] = c;
163   ++length;
164   return this;
165 }
166
167 GString *GString::insert(int i, GString *str) {
168   int n = str->getLength();
169   int j;
170
171   resize(length + n);
172   for (j = length; j >= i; --j)
173     s[j+n] = s[j];
174   memcpy(s+i, str->getCString(), n);
175   length += n;
176   return this;
177 }
178
179 GString *GString::insert(int i, const char *str) {
180   int n = strlen(str);
181   int j;
182
183   resize(length + n);
184   for (j = length; j >= i; --j)
185     s[j+n] = s[j];
186   memcpy(s+i, str, n);
187   length += n;
188   return this;
189 }
190
191 GString *GString::insert(int i, const char *str, int lengthA) {
192   int j;
193
194   resize(length + lengthA);
195   for (j = length; j >= i; --j)
196     s[j+lengthA] = s[j];
197   memcpy(s+i, str, lengthA);
198   length += lengthA;
199   return this;
200 }
201
202 GString *GString::del(int i, int n) {
203   int j;
204
205   if (n > 0) {
206     for (j = i; j <= length - n; ++j)
207       s[j] = s[j + n];
208     resize(length -= n);
209   }
210   return this;
211 }
212
213 GString *GString::upperCase() {
214   int i;
215
216   for (i = 0; i < length; ++i) {
217     if (islower(s[i]))
218       s[i] = toupper(s[i]);
219   }
220   return this;
221 }
222
223 GString *GString::lowerCase() {
224   int i;
225
226   for (i = 0; i < length; ++i) {
227     if (isupper(s[i]))
228       s[i] = tolower(s[i]);
229   }
230   return this;
231 }