| 1 |
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the libqxp project. |
| 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 |
|
| 10 |
#include "libqxp_utils.h" |
| 11 |
|
| 12 |
#include <unicode/ucnv.h> |
| 13 |
#include <unicode/utypes.h> |
| 14 |
|
| 15 |
#ifdef DEBUG |
| 16 |
#include <cstdarg> |
| 17 |
#include <cstdio> |
| 18 |
#endif |
| 19 |
|
| 20 |
#include <boost/math/constants/constants.hpp> |
| 21 |
|
| 22 |
using std::string; |
| 23 |
|
| 24 |
namespace libqxp |
| 25 |
{ |
| 26 |
|
| 27 |
namespace |
| 28 |
{ |
| 29 |
|
| 30 |
void checkStream(librevenge::RVNGInputStream *const input) |
| 31 |
{ |
| 32 |
if (!input || input->isEnd()) |
| 33 |
throw EndOfStreamException(); |
| 34 |
} |
| 35 |
|
| 36 |
struct SeekFailedException {}; |
| 37 |
|
| 38 |
static void _appendUCS4(librevenge::RVNGString &text, unsigned ucs4Character) |
| 39 |
{ |
| 40 |
unsigned char first; |
| 41 |
int len; |
| 42 |
if (ucs4Character < 0x80) |
| 43 |
{ |
| 44 |
first = 0; |
| 45 |
len = 1; |
| 46 |
} |
| 47 |
else if (ucs4Character < 0x800) |
| 48 |
{ |
| 49 |
first = 0xc0; |
| 50 |
len = 2; |
| 51 |
} |
| 52 |
else if (ucs4Character < 0x10000) |
| 53 |
{ |
| 54 |
first = 0xe0; |
| 55 |
len = 3; |
| 56 |
} |
| 57 |
else if (ucs4Character < 0x200000) |
| 58 |
{ |
| 59 |
first = 0xf0; |
| 60 |
len = 4; |
| 61 |
} |
| 62 |
else if (ucs4Character < 0x4000000) |
| 63 |
{ |
| 64 |
first = 0xf8; |
| 65 |
len = 5; |
| 66 |
} |
| 67 |
else |
| 68 |
{ |
| 69 |
first = 0xfc; |
| 70 |
len = 6; |
| 71 |
} |
| 72 |
|
| 73 |
unsigned char outbuf[6] = { 0, 0, 0, 0, 0, 0 }; |
| 74 |
int i; |
| 75 |
for (i = len - 1; i > 0; --i) |
| 76 |
{ |
| 77 |
outbuf[i] = (ucs4Character & 0x3f) | 0x80; |
| 78 |
ucs4Character >>= 6; |
| 79 |
} |
| 80 |
outbuf[0] = (ucs4Character & 0xff) | first; |
| 81 |
|
| 82 |
for (i = 0; i < len; i++) |
| 83 |
text.append(char(outbuf[i])); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
#ifdef DEBUG |
| 90 |
void debugPrint(const char *format, ...) |
| 91 |
{ |
| 92 |
va_list args; |
| 93 |
va_start(args, format); |
| 94 |
std::vfprintf(stderr, format, args); |
| 95 |
va_end(args); |
| 96 |
} |
| 97 |
#endif |
| 98 |
|
| 99 |
uint8_t readU8(librevenge::RVNGInputStream *input, bool /* bigEndian */) |
| 100 |
{ |
| 101 |
checkStream(input); |
| 102 |
|
| 103 |
unsigned long numBytesRead; |
| 104 |
uint8_t const *p = input->read(sizeof(uint8_t), numBytesRead); |
| 105 |
|
| 106 |
if (p && numBytesRead == sizeof(uint8_t)) |
| 107 |
return *(uint8_t const *)(p); |
| 108 |
throw EndOfStreamException(); |
| 109 |
} |
| 110 |
|
| 111 |
uint16_t readU16(librevenge::RVNGInputStream *input, bool bigEndian) |
| 112 |
{ |
| 113 |
checkStream(input); |
| 114 |
|
| 115 |
unsigned long numBytesRead; |
| 116 |
uint8_t const *p = input->read(sizeof(uint16_t), numBytesRead); |
| 117 |
|
| 118 |
if (p && numBytesRead == sizeof(uint16_t)) |
| 119 |
{ |
| 120 |
if (bigEndian) |
| 121 |
return static_cast<uint16_t>((uint16_t)p[1]|((uint16_t)p[0]<<8)); |
| 122 |
return static_cast<uint16_t>((uint16_t)p[0]|((uint16_t)p[1]<<8)); |
| 123 |
} |
| 124 |
throw EndOfStreamException(); |
| 125 |
} |
| 126 |
|
| 127 |
uint32_t readU32(librevenge::RVNGInputStream *input, bool bigEndian) |
| 128 |
{ |
| 129 |
checkStream(input); |
| 130 |
|
| 131 |
unsigned long numBytesRead; |
| 132 |
uint8_t const *p = input->read(sizeof(uint32_t), numBytesRead); |
| 133 |
|
| 134 |
if (p && numBytesRead == sizeof(uint32_t)) |
| 135 |
{ |
| 136 |
if (bigEndian) |
| 137 |
return (uint32_t)p[3]|((uint32_t)p[2]<<8)|((uint32_t)p[1]<<16)|((uint32_t)p[0]<<24); |
| 138 |
return (uint32_t)p[0]|((uint32_t)p[1]<<8)|((uint32_t)p[2]<<16)|((uint32_t)p[3]<<24); |
| 139 |
} |
| 140 |
throw EndOfStreamException(); |
| 141 |
} |
| 142 |
|
| 143 |
uint64_t readU64(librevenge::RVNGInputStream *input, bool bigEndian) |
| 144 |
{ |
| 145 |
checkStream(input); |
| 146 |
|
| 147 |
unsigned long numBytesRead; |
| 148 |
uint8_t const *p = input->read(sizeof(uint64_t), numBytesRead); |
| 149 |
|
| 150 |
if (p && numBytesRead == sizeof(uint64_t)) |
| 151 |
{ |
| 152 |
if (bigEndian) |
| 153 |
return (uint64_t)p[7]|((uint64_t)p[6]<<8)|((uint64_t)p[5]<<16)|((uint64_t)p[4]<<24)|((uint64_t)p[3]<<32)|((uint64_t)p[2]<<40)|((uint64_t)p[1]<<48)|((uint64_t)p[0]<<56); |
| 154 |
return (uint64_t)p[0]|((uint64_t)p[1]<<8)|((uint64_t)p[2]<<16)|((uint64_t)p[3]<<24)|((uint64_t)p[4]<<32)|((uint64_t)p[5]<<40)|((uint64_t)p[6]<<48)|((uint64_t)p[7]<<56); |
| 155 |
} |
| 156 |
throw EndOfStreamException(); |
| 157 |
} |
| 158 |
|
| 159 |
int16_t readS16(librevenge::RVNGInputStream *input, bool bigEndian) |
| 160 |
{ |
| 161 |
return int16_t(readU16(input, bigEndian)); |
| 162 |
} |
| 163 |
|
| 164 |
int32_t readS32(librevenge::RVNGInputStream *input, bool bigEndian) |
| 165 |
{ |
| 166 |
return int32_t(readU32(input, bigEndian)); |
| 167 |
} |
| 168 |
|
| 169 |
double readFloat16(librevenge::RVNGInputStream *input, bool bigEndian) |
| 170 |
{ |
| 171 |
return readU16(input, bigEndian) / double(0x10000); |
| 172 |
} |
| 173 |
|
| 174 |
double readFraction(librevenge::RVNGInputStream *input, bool bigEndian) |
| 175 |
{ |
| 176 |
int32_t num = readS32(input, bigEndian); |
| 177 |
return (num >> 16) + ((num & 0xffff) / double(0x10000)); |
| 178 |
} |
| 179 |
|
| 180 |
const unsigned char *readNBytes(librevenge::RVNGInputStream *const input, const unsigned long numBytes) |
| 181 |
{ |
| 182 |
checkStream(input); |
| 183 |
|
| 184 |
unsigned long readBytes = 0; |
| 185 |
const unsigned char *const s = input->read(numBytes, readBytes); |
| 186 |
|
| 187 |
if (numBytes != readBytes) |
| 188 |
throw EndOfStreamException(); |
| 189 |
|
| 190 |
return s; |
| 191 |
} |
| 192 |
|
| 193 |
string readCString(librevenge::RVNGInputStream *input) |
| 194 |
{ |
| 195 |
checkStream(input); |
| 196 |
|
| 197 |
string str; |
| 198 |
unsigned char c = readU8(input); |
| 199 |
while (0 != c) |
| 200 |
{ |
| 201 |
str.push_back(c); |
| 202 |
c = readU8(input); |
| 203 |
} |
| 204 |
|
| 205 |
return str; |
| 206 |
} |
| 207 |
|
| 208 |
string readPascalString(librevenge::RVNGInputStream *input) |
| 209 |
{ |
| 210 |
checkStream(input); |
| 211 |
|
| 212 |
const unsigned length = readU8(input); |
| 213 |
|
| 214 |
return readString(input, length); |
| 215 |
} |
| 216 |
|
| 217 |
std::string readString(librevenge::RVNGInputStream *input, const unsigned length) |
| 218 |
{ |
| 219 |
checkStream(input); |
| 220 |
|
| 221 |
string str; |
| 222 |
str.reserve(length); |
| 223 |
for (unsigned i = 0; length != i; ++i) |
| 224 |
str.push_back(readU8(input)); |
| 225 |
|
| 226 |
return str; |
| 227 |
} |
| 228 |
|
| 229 |
std::string readPlatformString(librevenge::RVNGInputStream *input, bool bigEndian) |
| 230 |
{ |
| 231 |
return bigEndian ? readPascalString(input) : readCString(input); |
| 232 |
} |
| 233 |
|
| 234 |
void skip(librevenge::RVNGInputStream *input, unsigned long numBytes) |
| 235 |
{ |
| 236 |
checkStream(input); |
| 237 |
|
| 238 |
seekRelative(input, static_cast<long>(numBytes)); |
| 239 |
} |
| 240 |
|
| 241 |
void seek(librevenge::RVNGInputStream *const input, const unsigned long pos) |
| 242 |
{ |
| 243 |
if (!input) |
| 244 |
throw EndOfStreamException(); |
| 245 |
|
| 246 |
if (0 != input->seek(static_cast<long>(pos), librevenge::RVNG_SEEK_SET)) |
| 247 |
throw SeekFailedException(); |
| 248 |
} |
| 249 |
|
| 250 |
void seekRelative(librevenge::RVNGInputStream *const input, const long pos) |
| 251 |
{ |
| 252 |
if (!input) |
| 253 |
throw EndOfStreamException(); |
| 254 |
|
| 255 |
if (0 != input->seek(pos, librevenge::RVNG_SEEK_CUR)) |
| 256 |
throw SeekFailedException(); |
| 257 |
} |
| 258 |
|
| 259 |
unsigned long getRemainingLength(librevenge::RVNGInputStream *const input) |
| 260 |
{ |
| 261 |
if (!input || input->tell() < 0) |
| 262 |
throw SeekFailedException(); |
| 263 |
|
| 264 |
const unsigned long begin = input->tell(); |
| 265 |
unsigned long end = begin; |
| 266 |
|
| 267 |
if (0 == input->seek(0, librevenge::RVNG_SEEK_END)) |
| 268 |
end = input->tell(); |
| 269 |
else |
| 270 |
{ |
| 271 |
// librevenge::RVNG_SEEK_END does not work. Use the harder way. |
| 272 |
while (!input->isEnd()) |
| 273 |
{ |
| 274 |
readU8(input); |
| 275 |
++end; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
seek(input, begin); |
| 280 |
|
| 281 |
return end - begin; |
| 282 |
} |
| 283 |
|
| 284 |
uint8_t readU8(const std::shared_ptr<librevenge::RVNGInputStream> input, bool) |
| 285 |
{ |
| 286 |
return readU8(input.get()); |
| 287 |
} |
| 288 |
|
| 289 |
uint16_t readU16(const std::shared_ptr<librevenge::RVNGInputStream> input, const bool bigEndian) |
| 290 |
{ |
| 291 |
return readU16(input.get(), bigEndian); |
| 292 |
} |
| 293 |
|
| 294 |
uint32_t readU32(const std::shared_ptr<librevenge::RVNGInputStream> input, const bool bigEndian) |
| 295 |
{ |
| 296 |
return readU32(input.get(), bigEndian); |
| 297 |
} |
| 298 |
|
| 299 |
uint64_t readU64(const std::shared_ptr<librevenge::RVNGInputStream> input, const bool bigEndian) |
| 300 |
{ |
| 301 |
return readU64(input.get(), bigEndian); |
| 302 |
} |
| 303 |
|
| 304 |
int16_t readS16(std::shared_ptr<librevenge::RVNGInputStream> input, bool bigEndian) |
| 305 |
{ |
| 306 |
return readS16(input.get(), bigEndian); |
| 307 |
} |
| 308 |
|
| 309 |
int32_t readS32(std::shared_ptr<librevenge::RVNGInputStream> input, bool bigEndian) |
| 310 |
{ |
| 311 |
return readS32(input.get(), bigEndian); |
| 312 |
} |
| 313 |
|
| 314 |
double readFloat16(std::shared_ptr<librevenge::RVNGInputStream> input, bool bigEndian) |
| 315 |
{ |
| 316 |
return readFloat16(input.get(), bigEndian); |
| 317 |
} |
| 318 |
|
| 319 |
double readFraction(std::shared_ptr<librevenge::RVNGInputStream> input, bool bigEndian) |
| 320 |
{ |
| 321 |
return readFraction(input.get(), bigEndian); |
| 322 |
} |
| 323 |
|
| 324 |
const unsigned char *readNBytes(const std::shared_ptr<librevenge::RVNGInputStream> input, const unsigned long numBytes) |
| 325 |
{ |
| 326 |
return readNBytes(input.get(), numBytes); |
| 327 |
} |
| 328 |
|
| 329 |
std::string readCString(const std::shared_ptr<librevenge::RVNGInputStream> input) |
| 330 |
{ |
| 331 |
return readCString(input.get()); |
| 332 |
} |
| 333 |
|
| 334 |
std::string readPascalString(const std::shared_ptr<librevenge::RVNGInputStream> input) |
| 335 |
{ |
| 336 |
return readPascalString(input.get()); |
| 337 |
} |
| 338 |
|
| 339 |
std::string readString(std::shared_ptr<librevenge::RVNGInputStream> input, const unsigned length) |
| 340 |
{ |
| 341 |
return readString(input.get(), length); |
| 342 |
} |
| 343 |
|
| 344 |
std::string readPlatformString(std::shared_ptr<librevenge::RVNGInputStream> input, bool bigEndian) |
| 345 |
{ |
| 346 |
return readPlatformString(input.get(), bigEndian); |
| 347 |
} |
| 348 |
|
| 349 |
void skip(const std::shared_ptr<librevenge::RVNGInputStream> input, const unsigned long numBytes) |
| 350 |
{ |
| 351 |
return skip(input.get(), numBytes); |
| 352 |
} |
| 353 |
|
| 354 |
void seek(const std::shared_ptr<librevenge::RVNGInputStream> input, const unsigned long pos) |
| 355 |
{ |
| 356 |
seek(input.get(), pos); |
| 357 |
} |
| 358 |
|
| 359 |
void seekRelative(const std::shared_ptr<librevenge::RVNGInputStream> input, const long pos) |
| 360 |
{ |
| 361 |
seekRelative(input.get(), pos); |
| 362 |
} |
| 363 |
|
| 364 |
unsigned long getRemainingLength(const std::shared_ptr<librevenge::RVNGInputStream> &input) |
| 365 |
{ |
| 366 |
return getRemainingLength(input.get()); |
| 367 |
} |
| 368 |
|
| 369 |
EndOfStreamException::EndOfStreamException() |
| 370 |
{ |
| 371 |
QXP_DEBUG_MSG(("Throwing EndOfStreamException\n")); |
| 372 |
} |
| 373 |
|
| 374 |
double deg2rad(double value) |
| 375 |
{ |
| 376 |
using namespace boost::math::double_constants; |
| 377 |
|
| 378 |
return normalizeDegAngle(value) * degree; |
| 379 |
} |
| 380 |
|
| 381 |
double normalizeRadAngle(double radAngle) |
| 382 |
{ |
| 383 |
using namespace boost::math::double_constants; |
| 384 |
radAngle = std::fmod(radAngle, two_pi); |
| 385 |
if (radAngle < 0) |
| 386 |
radAngle += two_pi; |
| 387 |
return radAngle; |
| 388 |
} |
| 389 |
|
| 390 |
double normalizeDegAngle(double degAngle) |
| 391 |
{ |
| 392 |
degAngle = std::fmod(degAngle, 360.0); |
| 393 |
if (degAngle < 0.0) |
| 394 |
degAngle += 360.0; |
| 395 |
return degAngle; |
| 396 |
} |
| 397 |
|
| 398 |
void appendCharacters(librevenge::RVNGString &text, const char *characters, const size_t size, |
| 399 |
const char *encoding) |
| 400 |
{ |
| 401 |
if (size == 0) |
| 402 |
{ |
| 403 |
QXP_DEBUG_MSG(("Attempt to append 0 characters!")); |
| 404 |
return; |
| 405 |
} |
| 406 |
|
| 407 |
UErrorCode status = U_ZERO_ERROR; |
| 408 |
std::unique_ptr<UConverter, void(*)(UConverter *)> conv(ucnv_open(encoding, &status), ucnv_close); |
| 409 |
if (U_SUCCESS(status)) |
| 410 |
{ |
| 411 |
// ICU documentation claims that character-by-character processing is faster "for small amounts of data" and "'normal' charsets" |
| 412 |
// (in any case, it is more convenient :) ) |
| 413 |
const char *src = &characters[0]; |
| 414 |
const char *srcLimit = (const char *)src + size; |
| 415 |
while (src < srcLimit) |
| 416 |
{ |
| 417 |
uint32_t ucs4Character = (uint32_t)ucnv_getNextUChar(conv.get(), &src, srcLimit, &status); |
| 418 |
if (U_SUCCESS(status)) |
| 419 |
{ |
| 420 |
_appendUCS4(text, ucs4Character); |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
} |
| 427 |
|
| 428 |
/* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |