~ubuntu-branches/ubuntu/gutsy/libwpd/gutsy

« back to all changes in this revision

Viewing changes to src/lib/WPXString.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Rene Engelhard
  • Date: 2007-01-11 15:15:57 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20070111151557-rn1kysabqbccx3as
Tags: 0.8.8-2
run make check for stream check; build-depend on libcppunit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* libwpd
2
2
 * Copyright (C) 2004 William Lachance (wrlach@gmail.com)
3
3
 * Copyright (C) 2005 Net Integration Technologies (http://www.net-itech.com)
 
4
 * Copyright (C) 2006 Fridrich Strba (fridrich.strba@bluewin.ch)
4
5
 *
5
6
 * This library is free software; you can redistribute it and/or
6
7
 * modify it under the terms of the GNU Library General Public
28
29
 
29
30
#include <string>
30
31
#include <stdarg.h>
31
 
//#include <string.h>
32
32
#include <stdio.h>
33
33
 
 
34
#define FIRST_BUF_SIZE 128
 
35
#ifdef _MSC_VER
 
36
#define vsnprintf _vsnprintf
 
37
#endif
 
38
 
34
39
static int g_static_utf8_strlen (const char *p);
35
40
static int g_static_unichar_to_utf8 (uint32_t c,  char *outbuf);
36
41
 
53
58
      ((Char) < 0x4000000 ? 5 : 6)))))
54
59
#define g_static_utf8_next_char(p) (char *)((p) + g_static_utf8_skip_data[*((uint8_t *)p)])
55
60
 
 
61
class WPXStringImpl
 
62
{
 
63
public:
 
64
        std::string m_buf;
 
65
};
 
66
 
56
67
WPXString::~WPXString()
57
68
{
58
 
        delete static_cast<std::string*>(m_buf);
 
69
        delete m_stringImpl;
59
70
}
60
71
 
61
72
WPXString::WPXString()
62
73
{
63
 
        m_buf = static_cast<void *>(new std::string());
 
74
        m_stringImpl = new WPXStringImpl;
 
75
        m_stringImpl->m_buf.reserve(FIRST_BUF_SIZE);
64
76
}
65
77
 
66
78
WPXString::WPXString(const WPXString &stringBuf)
67
79
{
68
 
        m_buf = static_cast<void *>(new std::string(*static_cast<std::string*>(stringBuf.m_buf)));
 
80
        m_stringImpl = new WPXStringImpl;
 
81
        m_stringImpl->m_buf = stringBuf.m_stringImpl->m_buf;
69
82
}
70
83
 
71
84
WPXString::WPXString(const WPXString &stringBuf, bool escapeXML) 
72
85
{
 
86
        m_stringImpl = new WPXStringImpl;
73
87
 
74
88
        if (escapeXML)
75
89
        {
76
 
                m_buf = static_cast<void *>(new std::string());
77
 
                int len = static_cast<std::string*>(stringBuf.m_buf)->length(); // strlen(stringBuf.cstr()); // want to use standard strlen
 
90
                int tmpLen = stringBuf.m_stringImpl->m_buf.length();
 
91
                m_stringImpl->m_buf.reserve(2*tmpLen);
78
92
                const char *p = stringBuf.cstr();
79
 
                const char *end = p + len; 
 
93
                const char *end = p + tmpLen; 
80
94
                while (p != end)
81
95
                {
82
96
                        const char *next = g_static_utf8_next_char(p);
111
125
                }
112
126
        }
113
127
        else
114
 
                m_buf = static_cast<void *>(new std::string(*static_cast<std::string*>(stringBuf.m_buf)));
 
128
                m_stringImpl->m_buf = stringBuf.m_stringImpl->m_buf;
115
129
}
116
130
 
117
131
WPXString::WPXString(const char *str)
118
132
{
119
 
        m_buf = static_cast<void *>(new std::string(str));
 
133
        m_stringImpl = new WPXStringImpl;
 
134
        m_stringImpl->m_buf = std::string(str);
120
135
}
121
136
 
122
137
const char * WPXString::cstr() const
123
138
{
124
 
    return static_cast<std::string *>(m_buf)->c_str();
 
139
    return m_stringImpl->m_buf.c_str();
125
140
}
126
141
 
127
 
#define FIRST_BUF_SIZE 128;
128
 
#ifdef _MSC_VER
129
 
#define vsnprintf _vsnprintf
130
 
#endif
131
 
 
132
142
void WPXString::sprintf(const char *format, ...)
133
143
{
134
144
        va_list args;
135
145
 
136
146
        int bufsize = FIRST_BUF_SIZE;
137
 
        char * buf = NULL;
 
147
        char firstBuffer[FIRST_BUF_SIZE];
 
148
        char * buf = firstBuffer;
138
149
 
139
150
        while(true)
140
151
        {
141
 
                        buf = new char[bufsize];
142
152
                        va_start(args, format);
143
153
                        int outsize = vsnprintf(buf, bufsize, format, args);
144
154
                        va_end(args);
150
160
                        else
151
161
                                break;
152
162
 
153
 
                        delete [] buf;
 
163
                        if (buf != firstBuffer)
 
164
                        {
 
165
                                delete [] buf;
 
166
                                buf = new char[bufsize];
 
167
                        }
154
168
        }
155
169
 
156
170
        clear();
157
171
        append(buf);
158
 
        delete [] buf;
 
172
        if (buf != firstBuffer)
 
173
                delete [] buf;
159
174
}
160
175
 
161
176
void WPXString::append(const WPXString &s)
162
177
{
163
 
        static_cast<std::string *>(m_buf)->append(*static_cast<std::string*>(s.m_buf));
 
178
        m_stringImpl->m_buf.append(s.m_stringImpl->m_buf);
164
179
}
165
180
 
166
181
void WPXString::append(const char *s)
167
182
{
168
 
        static_cast<std::string *>(m_buf)->append(s);
 
183
        m_stringImpl->m_buf.append(s);
169
184
}
170
185
 
171
186
void WPXString::append(const char c)
172
187
{
173
 
        *(static_cast<std::string *>(m_buf)) += c;
 
188
        m_stringImpl->m_buf.append(1, c);
174
189
}
175
190
 
176
191
void WPXString::clear()
177
192
{
178
 
        static_cast<std::string *>(m_buf)->erase(static_cast<std::string *>(m_buf)->begin(), static_cast<std::string *>(m_buf)->end());
 
193
        m_stringImpl->m_buf.erase(m_stringImpl->m_buf.begin(), m_stringImpl->m_buf.end());
179
194
}
180
195
 
181
196
int WPXString::len() const
185
200
 
186
201
WPXString& WPXString::operator=(const WPXString &stringBuf)
187
202
{
188
 
        *static_cast<std::string*>(m_buf) = *static_cast<std::string*>(stringBuf.m_buf);
 
203
        m_stringImpl->m_buf = stringBuf.m_stringImpl->m_buf;
189
204
        return *this;
190
205
}
191
206
 
192
207
bool WPXString::operator==(const char *str)
193
208
{
194
 
        return (*static_cast<std::string*>(m_buf) == str);
 
209
        return (m_stringImpl->m_buf == str);
195
210
}
196
211
 
197
212
bool WPXString::operator==(const WPXString &str)
198
213
{
199
 
        return (*static_cast<std::string*>(m_buf) == *static_cast<std::string*>(str.m_buf));
 
214
        return (m_stringImpl->m_buf == str.m_stringImpl->m_buf);
200
215
}
201
216
 
202
217
WPXString::Iter::Iter(const WPXString &str) :
203
218
        m_pos(0),
204
 
        m_curChar(NULL)
 
219
        m_curChar(0)
205
220
{
206
 
        m_buf = static_cast<void *>(new std::string(str.cstr()));
 
221
        m_stringImpl = new WPXStringImpl;
 
222
        m_stringImpl->m_buf = str.cstr();
207
223
 
208
224
}
209
225
 
210
226
WPXString::Iter::~Iter()
211
227
{
212
228
        delete [] m_curChar;
213
 
        delete (static_cast<std::string *>(m_buf));
 
229
        delete m_stringImpl;
214
230
}
215
231
 
216
232
void WPXString::Iter::rewind()
220
236
 
221
237
bool WPXString::Iter::next()
222
238
{
223
 
        int len = static_cast<std::string *>(m_buf)->length();
 
239
        int len = m_stringImpl->m_buf.length();
224
240
 
225
241
        if (m_pos == (-1)) 
226
242
                m_pos++;
227
243
        else if (m_pos < len)
228
244
        {
229
 
                m_pos+=(int32_t) (g_static_utf8_next_char(&(static_cast<std::string *>(m_buf)->c_str()[m_pos])) - 
230
 
                                  &(static_cast<std::string *>(m_buf)->c_str()[m_pos]));
 
245
                m_pos+=(int32_t) (g_static_utf8_next_char(&(m_stringImpl->m_buf.c_str()[m_pos])) - 
 
246
                                  &(m_stringImpl->m_buf.c_str()[m_pos]));
231
247
        }
232
248
 
233
249
        if (m_pos < len)
237
253
 
238
254
bool WPXString::Iter::last()
239
255
{
240
 
        if (m_pos >= g_static_utf8_strlen(static_cast<std::string *>(m_buf)->c_str()))
 
256
        if (m_pos >= g_static_utf8_strlen(m_stringImpl->m_buf.c_str()))
241
257
                return true;
242
258
        return false;
243
259
}
244
260
 
245
261
const char * WPXString::Iter::operator()() const
246
262
247
 
        if (m_pos == (-1)) return NULL; 
 
263
        if (m_pos == (-1)) return 0; 
248
264
 
249
 
        delete [] m_curChar; m_curChar = NULL;
250
 
        int32_t charLength =(int32_t) (g_static_utf8_next_char(&(static_cast<std::string *>(m_buf)->c_str()[m_pos])) - 
251
 
                                       &(static_cast<std::string *>(m_buf)->c_str()[m_pos]));
 
265
        if (m_curChar) delete [] m_curChar; m_curChar = 0;
 
266
        int32_t charLength =(int32_t) (g_static_utf8_next_char(&(m_stringImpl->m_buf.c_str()[m_pos])) - 
 
267
                                       &(m_stringImpl->m_buf.c_str()[m_pos]));
252
268
        m_curChar = new char[charLength+1];
253
269
        for (int i=0; i<charLength; i++)
254
 
                m_curChar[i] = (*(static_cast<std::string *>(m_buf)))[m_pos+i];
 
270
                m_curChar[i] = m_stringImpl->m_buf[m_pos+i];
255
271
        m_curChar[charLength]='\0';
256
272
 
257
273
        return m_curChar;
261
277
 
262
278
void appendUCS4(WPXString &str, uint32_t ucs4)
263
279
{
264
 
        int charLength = g_static_unichar_to_utf8(ucs4, NULL);
265
 
        char *utf8 = new(char[charLength+1]);
 
280
        int charLength = g_static_unichar_to_utf8(ucs4, 0);
 
281
        char *utf8 = new char[charLength+1];
266
282
        utf8[charLength] = '\0';
267
283
        g_static_unichar_to_utf8(ucs4, utf8);
268
284
        str.append(utf8);
277
293
 *
278
294
 * @c: a ISO10646 character code
279
295
 * @outbuf: output buffer, must have at least 6 bytes of space.
280
 
 *       If %NULL, the length will be computed and returned
 
296
 *       If %0, the length will be computed and returned
281
297
 *       and nothing will be written to @outbuf.
282
298
 * 
283
299
 * Converts a single character to UTF-8.
288
304
g_static_unichar_to_utf8 (uint32_t c,
289
305
                          char   *outbuf)
290
306
{
291
 
        unsigned int len = 0;    
292
 
        int first;
293
 
        int i;
 
307
        uint8_t len = 1;    
 
308
        uint8_t first = 0;
294
309
    
295
310
        if (c < 0x80)
296
311
        {
325
340
    
326
341
        if (outbuf)
327
342
        {
328
 
                for (i = len - 1; i > 0; --i)
 
343
                for (uint8_t i = len - 1; i > 0; --i)
329
344
                {
330
 
                        outbuf[i] = (c & 0x3f) | 0x80;
 
345
                        outbuf[i] = (char)((c & 0x3f) | 0x80);
331
346
                        c >>= 6;
332
347
                }
333
 
                outbuf[0] = c | first;
 
348
                outbuf[0] = (char)(c | first);
334
349
        }
335
350
    
336
351
        return len;
349
364
g_static_utf8_strlen (const char *p)
350
365
{
351
366
        long len = 0;
352
 
        if (p == NULL)
 
367
        if (!p)
353
368
                return 0;
354
369
 
355
370
        while (*p)