| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
| 2 | /* librevenge |
| 3 | * Version: MPL 2.0 / LGPLv2.1+ |
| 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | * |
| 9 | * Major Contributor(s): |
| 10 | * Copyright (C) 2004 William Lachance ([email protected]) |
| 11 | * Copyright (C) 2006 Fridrich Strba ([email protected]) |
| 12 | * |
| 13 | * For minor contributions see the git repository. |
| 14 | * |
| 15 | * Alternatively, the contents of this file may be used under the terms |
| 16 | * of the GNU Lesser General Public License Version 2.1 or later |
| 17 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 18 | * applicable instead of those above. |
| 19 | */ |
| 20 | |
| 21 | #include <librevenge/librevenge.h> |
| 22 | |
| 23 | #include "librevenge_internal.h" |
| 24 | |
| 25 | #include <cassert> |
| 26 | #include <cstring> |
| 27 | #include <memory> |
| 28 | #include <string> |
| 29 | #include <stdarg.h> |
| 30 | #include <stdio.h> |
| 31 | #include <utility> |
| 32 | |
| 33 | #define FIRST_BUF_SIZE 128 |
| 34 | #ifdef _MSC_VER |
| 35 | #define vsnprintf _vsnprintf |
| 36 | #endif |
| 37 | |
| 38 | namespace librevenge |
| 39 | { |
| 40 | |
| 41 | namespace |
| 42 | { |
| 43 | |
| 44 | static const unsigned char librvng_utf8_skip_data[256] = |
| 45 | { |
| 46 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 47 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 48 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 49 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 50 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 51 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 52 | 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, |
| 53 | 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1 |
| 54 | }; |
| 55 | |
| 56 | #define librvng_utf8_next_char(p) (char const *)((p) + librvng_utf8_skip_data[*((unsigned char const *)p)]) |
| 57 | |
| 58 | static int librvng_utf8_strlen(const char *p, const char *end) |
| 59 | { |
| 60 | if (!p) |
| 61 | return 0; |
| 62 | |
| 63 | int len = 0; |
| 64 | while (p < end && *p) |
| 65 | { |
| 66 | p = librvng_utf8_next_char(p); |
| 67 | ++len; |
| 68 | } |
| 69 | |
| 70 | return len; |
| 71 | } |
| 72 | |
| 73 | } // anonymous namespace |
| 74 | |
| 75 | class RVNGStringImpl |
| 76 | { |
| 77 | public: |
| 78 | RVNGStringImpl() : m_buf() {} |
| 79 | bool empty() const |
| 80 | { |
| 81 | return m_buf.empty(); |
| 82 | } |
| 83 | int len() const |
| 84 | { |
| 85 | if (empty()) return 0; |
| 86 | return librvng_utf8_strlen(m_buf.c_str(), m_buf.c_str()+m_buf.length()); |
| 87 | } |
| 88 | unsigned long size() const |
| 89 | { |
| 90 | return (unsigned long)m_buf.size(); |
| 91 | } |
| 92 | void appendEscapedXML(const char *s, const unsigned long sz); |
| 93 | void append(const char *s); |
| 94 | void append(char c); |
| 95 | std::string m_buf; |
| 96 | }; |
| 97 | |
| 98 | void RVNGStringImpl::appendEscapedXML(const char *s, const unsigned long sz) |
| 99 | { |
| 100 | m_buf.reserve(m_buf.size() + 2 * sz); |
| 101 | const char *p = s; |
| 102 | const char *const end = p + sz; |
| 103 | while (p != end) |
| 104 | { |
| 105 | const auto *const next = librvng_utf8_next_char(p); |
| 106 | if (next > end) |
| 107 | { |
| 108 | RVNG_DEBUG_MSG(("RVNGStringImpl::appendEscapedXML: oops, we have a problem\n")); |
| 109 | break; |
| 110 | } |
| 111 | switch (*p) |
| 112 | { |
| 113 | case '&': |
| 114 | m_buf.append("&"); |
| 115 | break; |
| 116 | case '<': |
| 117 | m_buf.append("<"); |
| 118 | break; |
| 119 | case '>': |
| 120 | m_buf.append(">"); |
| 121 | break; |
| 122 | case '\'': |
| 123 | m_buf.append("'"); |
| 124 | break; |
| 125 | case '"': |
| 126 | m_buf.append("""); |
| 127 | break; |
| 128 | default: |
| 129 | while (p != next) |
| 130 | { |
| 131 | m_buf.append(1, *p); |
| 132 | ++p; |
| 133 | } |
| 134 | break; |
| 135 | } |
| 136 | |
| 137 | p = next; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void RVNGStringImpl::append(const char *s) |
| 142 | { |
| 143 | assert(s); |
| 144 | |
| 145 | // Find the true end of the string (one past the last code unit of |
| 146 | // the last complete UTF-8 character) |
| 147 | const char *v = s; |
| 148 | const char *p = s; |
| 149 | const char *u = s; |
| 150 | while (*p) |
| 151 | { |
| 152 | u = librvng_utf8_next_char(u); |
| 153 | while (p < u && *p) ++p; |
| 154 | if (p == u) |
| 155 | v = u; |
| 156 | } |
| 157 | |
| 158 | if (*v) |
| 159 | { |
| 160 | RVNG_DEBUG_MSG(("RVNGStringImpl::append: the string is not a valid UTF-8 string\n")); |
| 161 | } |
| 162 | |
| 163 | if (v > s) |
| 164 | m_buf.append(s, v - s); |
| 165 | } |
| 166 | |
| 167 | void RVNGStringImpl::append(char c) |
| 168 | { |
| 169 | if (librvng_utf8_skip_data[static_cast<unsigned char>(c)] == 1) |
| 170 | { |
| 171 | m_buf.append(1, c); |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | RVNG_DEBUG_MSG(("RVNGStringImpl::append: '%c' is not a valid 1-byte UTF-8 char\n", c)); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | RVNGString::~RVNGString() |
| 180 | { |
| 181 | delete m_stringImpl; |
| 182 | } |
| 183 | |
| 184 | RVNGString::RVNGString() : |
| 185 | m_stringImpl(new RVNGStringImpl) |
| 186 | { |
| 187 | m_stringImpl->m_buf.reserve(FIRST_BUF_SIZE); |
| 188 | } |
| 189 | |
| 190 | RVNGString::RVNGString(const RVNGString &other) : |
| 191 | m_stringImpl(new RVNGStringImpl) |
| 192 | { |
| 193 | m_stringImpl->m_buf = other.m_stringImpl->m_buf; |
| 194 | } |
| 195 | |
| 196 | RVNGString::RVNGString(const char *str) : |
| 197 | m_stringImpl(new RVNGStringImpl) |
| 198 | { |
| 199 | if (str) |
| 200 | m_stringImpl->append(str); |
| 201 | } |
| 202 | |
| 203 | RVNGString RVNGString::escapeXML(const RVNGString &s) |
| 204 | { |
| 205 | RVNGString escaped; |
| 206 | escaped.appendEscapedXML(s); |
| 207 | return escaped; |
| 208 | } |
| 209 | |
| 210 | RVNGString RVNGString::escapeXML(const char *const s) |
| 211 | { |
| 212 | RVNGString escaped; |
| 213 | escaped.appendEscapedXML(s); |
| 214 | return escaped; |
| 215 | } |
| 216 | |
| 217 | const char *RVNGString::cstr() const |
| 218 | { |
| 219 | return m_stringImpl->m_buf.c_str(); |
| 220 | } |
| 221 | |
| 222 | void RVNGString::sprintf(const char *format, ...) |
| 223 | { |
| 224 | va_list args; |
| 225 | |
| 226 | int bufsize = FIRST_BUF_SIZE; |
| 227 | char firstBuffer[FIRST_BUF_SIZE]; |
| 228 | std::unique_ptr<char[]> workBuffer; |
| 229 | char *buf = firstBuffer; |
| 230 | |
| 231 | while (true) |
| 232 | { |
| 233 | va_start(args, format); |
| 234 | int outsize = vsnprintf(buf, size_t(bufsize), format, args); |
| 235 | va_end(args); |
| 236 | |
| 237 | if ((outsize == -1) || (outsize == bufsize) || (outsize == bufsize - 1)) |
| 238 | bufsize = bufsize * 2; |
| 239 | else if (outsize > bufsize) |
| 240 | bufsize = outsize + 2; |
| 241 | else |
| 242 | break; |
| 243 | |
| 244 | workBuffer.reset(new char[bufsize]); |
| 245 | buf = workBuffer.get(); |
| 246 | } |
| 247 | |
| 248 | clear(); |
| 249 | append(buf); |
| 250 | } |
| 251 | |
| 252 | void RVNGString::append(const RVNGString &s) |
| 253 | { |
| 254 | m_stringImpl->m_buf.append(s.m_stringImpl->m_buf); |
| 255 | } |
| 256 | |
| 257 | void RVNGString::append(const char *s) |
| 258 | { |
| 259 | if (s) |
| 260 | m_stringImpl->append(s); |
| 261 | } |
| 262 | |
| 263 | void RVNGString::append(const char c) |
| 264 | { |
| 265 | m_stringImpl->m_buf.append(1, c); |
| 266 | } |
| 267 | |
| 268 | void RVNGString::appendEscapedXML(const RVNGString &s) |
| 269 | { |
| 270 | m_stringImpl->appendEscapedXML(s.cstr(), s.size()); |
| 271 | } |
| 272 | |
| 273 | void RVNGString::appendEscapedXML(const char *const s) |
| 274 | { |
| 275 | m_stringImpl->appendEscapedXML(s, std::strlen(s)); |
| 276 | } |
| 277 | |
| 278 | void RVNGString::clear() |
| 279 | { |
| 280 | m_stringImpl->m_buf.erase(m_stringImpl->m_buf.begin(), m_stringImpl->m_buf.end()); |
| 281 | } |
| 282 | |
| 283 | int RVNGString::len() const |
| 284 | { |
| 285 | return m_stringImpl->len(); |
| 286 | } |
| 287 | |
| 288 | unsigned long RVNGString::size() const |
| 289 | { |
| 290 | return m_stringImpl->size(); |
| 291 | } |
| 292 | |
| 293 | bool RVNGString::empty() const |
| 294 | { |
| 295 | return m_stringImpl->empty(); |
| 296 | } |
| 297 | |
| 298 | RVNGString &RVNGString::operator=(const RVNGString &stringBuf) |
| 299 | { |
| 300 | m_stringImpl->m_buf = stringBuf.m_stringImpl->m_buf; |
| 301 | return *this; |
| 302 | } |
| 303 | |
| 304 | RVNGString &RVNGString::operator=(const char *s) |
| 305 | { |
| 306 | clear(); |
| 307 | if (s) |
| 308 | m_stringImpl->append(s); |
| 309 | return *this; |
| 310 | } |
| 311 | |
| 312 | bool RVNGString::operator==(const char *str) const |
| 313 | { |
| 314 | if (!str) |
| 315 | return false; |
| 316 | return (m_stringImpl->m_buf == str); |
| 317 | } |
| 318 | |
| 319 | bool RVNGString::operator==(const RVNGString &str) const |
| 320 | { |
| 321 | return (m_stringImpl->m_buf == str.m_stringImpl->m_buf); |
| 322 | } |
| 323 | |
| 324 | bool RVNGString::operator<(const char *str) const |
| 325 | { |
| 326 | if (!str) |
| 327 | return false; |
| 328 | return (m_stringImpl->m_buf < str); |
| 329 | } |
| 330 | |
| 331 | bool RVNGString::operator<(const RVNGString &str) const |
| 332 | { |
| 333 | return (m_stringImpl->m_buf < str.m_stringImpl->m_buf); |
| 334 | } |
| 335 | |
| 336 | RVNGString::Iter::Iter(const RVNGString &str) : |
| 337 | m_stringImpl(new RVNGStringImpl), |
| 338 | m_pos(0), |
| 339 | m_curChar(nullptr) |
| 340 | { |
| 341 | m_stringImpl->m_buf = str.cstr(); |
| 342 | } |
| 343 | |
| 344 | RVNGString::Iter::~Iter() |
| 345 | { |
| 346 | delete [] m_curChar; |
| 347 | delete m_stringImpl; |
| 348 | } |
| 349 | |
| 350 | void RVNGString::Iter::rewind() |
| 351 | { |
| 352 | m_pos = (-1); |
| 353 | } |
| 354 | |
| 355 | bool RVNGString::Iter::next() |
| 356 | { |
| 357 | auto len = (int) m_stringImpl->m_buf.length(); |
| 358 | |
| 359 | if (m_pos == (-1)) |
| 360 | m_pos++; |
| 361 | else if (m_pos < len) |
| 362 | { |
| 363 | m_pos+=(int)(librvng_utf8_next_char(&(m_stringImpl->m_buf.c_str()[m_pos])) - |
| 364 | &(m_stringImpl->m_buf.c_str()[m_pos])); |
| 365 | } |
| 366 | |
| 367 | if (m_pos < len) |
| 368 | return true; |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | bool RVNGString::Iter::last() |
| 373 | { |
| 374 | assert(m_pos >= -1); |
| 375 | if (m_pos == -1) |
| 376 | return false; |
| 377 | return unsigned(m_pos) >= m_stringImpl->m_buf.length(); |
| 378 | } |
| 379 | |
| 380 | const char *RVNGString::Iter::operator()() const |
| 381 | { |
| 382 | if (m_pos == (-1)) return nullptr; |
| 383 | |
| 384 | auto charLength =(int)(librvng_utf8_next_char(&(m_stringImpl->m_buf.c_str()[m_pos])) - |
| 385 | &(m_stringImpl->m_buf.c_str()[m_pos])); |
| 386 | const int curCharLength = m_curChar ? int(unsigned(std::strlen(m_curChar))) : 0; |
| 387 | if (curCharLength < charLength) |
| 388 | { |
| 389 | auto *newChar = new char[charLength+1]; |
| 390 | std::swap(m_curChar, newChar); |
| 391 | delete[] newChar; |
| 392 | } |
| 393 | for (int i=0; i<charLength; i++) |
| 394 | m_curChar[i] = m_stringImpl->m_buf[size_t(m_pos+i)]; |
| 395 | m_curChar[charLength]='\0'; |
| 396 | |
| 397 | return m_curChar; |
| 398 | } |
| 399 | |
| 400 | } |
| 401 | |
| 402 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |