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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
|
/*++
Copyright (C) 2018 3MF Consortium
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Abstract:
NMR_StringUtils.cpp implements a few string helper functions that handle strings
correctly and Exception-safe
--*/
#include "Common/NMR_StringUtils.h"
#include "Common/NMR_Exception.h"
#include <climits>
#include <sstream>
#include <cmath>
#include <string.h>
#include <vector>
namespace NMR {
// Lookup table to convert UTF8 bytes to sequence length
const nfByte UTF8DecodeTable[256] = {
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,
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,
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,
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,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
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,
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,0,0
};
// Masks to decode highest UTF8 sequence byte
const nfByte UTF8DecodeMask[7] = {0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01};
nfInt32 fnStringToInt32(_In_z_ const nfChar * pszValue)
{
__NMRASSERT(pszValue);
nfInt32 nResult = 0;
// Convert to integer and make a input and range check!
nfChar * pEndPtr;
nResult = strtol(pszValue, &pEndPtr, 10);
// Check if any conversion happened
if ((pEndPtr == pszValue) || (!pEndPtr))
throw CNMRException(NMR_ERROR_EMPTYSTRINGTOINTCONVERSION);
if ((*pEndPtr != '\0') && (*pEndPtr != ' '))
throw CNMRException(NMR_ERROR_INVALIDSTRINGTOINTCONVERSION);
if ((nResult == LONG_MAX) || (nResult == LONG_MIN))
throw CNMRException(NMR_ERROR_STRINGTOINTCONVERSIONOUTOFRANGE);
return nResult;
}
nfUint32 fnStringToUint32(_In_z_ const nfChar * pszValue)
{
__NMRASSERT(pszValue);
nfUint32 nResult = 0;
// Convert to integer and make a input and range check!
nfChar * pEndPtr;
nResult = strtoul(pszValue, &pEndPtr, 10);
// Check if any conversion happened
if ((pEndPtr == pszValue) || (!pEndPtr))
throw CNMRException(NMR_ERROR_EMPTYSTRINGTOINTCONVERSION);
if ((*pEndPtr != '\0') && (*pEndPtr != ' '))
throw CNMRException(NMR_ERROR_INVALIDSTRINGTOINTCONVERSION);
if (nResult == ULONG_MAX)
throw CNMRException(NMR_ERROR_STRINGTOINTCONVERSIONOUTOFRANGE);
return nResult;
}
nfFloat fnStringToFloat(_In_z_ const nfChar * pszValue)
{
return (nfFloat)fnStringToDouble(pszValue);
}
nfDouble fnStringToDouble(_In_z_ const nfChar * pszValue)
{
__NMRASSERT(pwszValue);
nfDouble dResult = 0.0;
// Convert to double and make a input and range check!
nfChar * pEndPtr;
dResult = strtod(pszValue, &pEndPtr);
// Check if any conversion happened
if ((pEndPtr == pszValue) || (!pEndPtr))
throw CNMRException(NMR_ERROR_EMPTYSTRINGTODOUBLECONVERSION);
if ((*pEndPtr != '\0') && (*pEndPtr != ' '))
throw CNMRException(NMR_ERROR_INVALIDSTRINGTODOUBLECONVERSION);
if ((dResult == HUGE_VAL) || (dResult == -HUGE_VAL))
throw CNMRException(NMR_ERROR_STRINGTODOUBLECONVERSIONOUTOFRANGE);
return dResult;
}
std::string fnInt32ToString(_In_ nfInt32 nValue)
{
std::stringstream sStream;
sStream << nValue;
return sStream.str();
}
std::string fnUint32ToString(_In_ nfUint32 nValue)
{
std::stringstream sStream;
sStream << nValue;
return sStream.str();
}
std::string fnFloatToString(_In_ nfFloat fValue, _In_ nfUint32 precision)
{
std::stringstream sStream;
sStream << fValue;
return sStream.str();
}
nfChar fnColorDigitToHex(_In_ nfByte digit)
{
if (digit < 10)
return (nfChar)(digit + 48);
if (digit < 16)
return (nfChar)(digit + 55);
return L' ';
}
std::string fnColorToString(_In_ nfColor cColor)
{
nfChar pBuffer[16];
pBuffer[0] = '#';
pBuffer[2] = fnColorDigitToHex(cColor & 0xf); // R
pBuffer[1] = fnColorDigitToHex((cColor >> 4) & 0xf); // R
pBuffer[4] = fnColorDigitToHex((cColor >> 8) & 0xf); // G
pBuffer[3] = fnColorDigitToHex((cColor >> 12) & 0xf); // G
pBuffer[6] = fnColorDigitToHex((cColor >> 16) & 0xf); // B
pBuffer[5] = fnColorDigitToHex((cColor >> 20) & 0xf); // B
pBuffer[8] = fnColorDigitToHex((cColor >> 24) & 0xf); // A
pBuffer[7] = fnColorDigitToHex((cColor >> 28) & 0xf); // A
pBuffer[9] = 0;
return std::string(pBuffer);
}
std::string fnDoubleToString(_In_ nfFloat dValue, _In_ nfUint32 precision)
{
std::stringstream sStream;
sStream << dValue;
return sStream.str();
}
nfBool fnStringToSRGBColor(_In_z_ const nfChar * pszValue, _Out_ nfColor & cResult)
{
cResult = 0;
if (!pszValue)
return false;
nfUint32 nRed = 255;
nfUint32 nGreen = 255;
nfUint32 nBlue = 255;
nfUint32 nAlpha = 255;
std::string sString(pszValue);
if (sString.length() == 7) {
if (sString[0] != '#')
return false;
nRed = fnHexStringToUint32(sString.substr(1, 2).c_str());
nGreen = fnHexStringToUint32(sString.substr(3, 2).c_str());
nBlue = fnHexStringToUint32(sString.substr(5, 2).c_str());
cResult = nRed | (nGreen << 8) | (nBlue << 16) | (nAlpha << 24);
return true;
}
if (sString.length() == 9) {
if (sString[0] != '#')
return false;
nRed = fnHexStringToUint32(sString.substr(1, 2).c_str());
nGreen = fnHexStringToUint32(sString.substr(3, 2).c_str());
nBlue = fnHexStringToUint32(sString.substr(5, 2).c_str());
nAlpha = fnHexStringToUint32(sString.substr(7, 2).c_str());
cResult = nRed | (nGreen << 8) | (nBlue << 16) | (nAlpha << 24);
return true;
}
return false;
}
nfUint32 fnHexStringToUint32(_In_z_ const nfChar * pszValue)
{
if (!pszValue)
return 0;
nfChar * p;
nfUint32 nResult = strtoul(pszValue, &p, 16);
if (*p != 0)
throw CNMRException(NMR_ERROR_INVALIDHEXVALUE);
if (nResult == ULONG_MAX)
throw CNMRException(NMR_ERROR_RANGEERROR);
return nResult;
}
nfInt32 fnStringToInt32Comma(_In_z_ const nfChar * pszValue)
{
__NMRASSERT(pwszValue);
nfInt32 nResult = 0;
// Convert to integer and make a input and range check!
nfChar * pEndPtr;
nResult = strtol(pszValue, &pEndPtr, 10);
// Check if any conversion happened
if ((pEndPtr == pszValue) || (!pEndPtr))
throw CNMRException(NMR_ERROR_EMPTYSTRINGTOINTCONVERSION);
if ((*pEndPtr != '\0') && (*pEndPtr != ' ') && (*pEndPtr != ','))
throw CNMRException(NMR_ERROR_INVALIDSTRINGTOINTCONVERSION);
if ((nResult == LONG_MAX) || (nResult == LONG_MIN))
throw CNMRException(NMR_ERROR_STRINGTOINTCONVERSIONOUTOFRANGE);
return nResult;
}
void fnStringToCommaSeparatedIntegerTriplet(_In_z_ const nfChar * pszValue, _Out_ nfInt32 & nValue1, _Out_ nfInt32 & nValue2, _Out_ nfInt32 & nValue3)
{
const nfChar * pszCommaValue1 = strchr(pszValue, ',');
if (pszCommaValue1 != nullptr) {
if (*pszCommaValue1 == 0)
throw CNMRException(NMR_ERROR_INVALIDINTEGERTRIPLET);
pszCommaValue1++;
const nfChar * pszCommaValue2 = strchr(pszCommaValue1, ',');
if (pszCommaValue2 != nullptr) {
if (*pszCommaValue2 == 0)
throw CNMRException(NMR_ERROR_INVALIDINTEGERTRIPLET);
pszCommaValue2++;
nValue1 = fnStringToInt32Comma(pszValue);
nValue2 = fnStringToInt32Comma(pszCommaValue1);
nValue3 = fnStringToInt32Comma(pszCommaValue2);
}
else
throw CNMRException(NMR_ERROR_INVALIDINTEGERTRIPLET);
}
else
throw CNMRException(NMR_ERROR_INVALIDINTEGERTRIPLET);
}
void fnWStringToBufferSafe(_In_ const std::wstring sString, _Out_opt_ nfWChar * pwszBuffer, nfUint32 cbBufferSize, _Out_opt_ nfUint32 * pcbNeededChars)
{
__NMRASSERT(pwszBuffer);
// Check for possible integer overflows
size_t cbLength = sString.length();
if (cbLength > NMR_MAXSTRINGBUFFERSIZE)
throw CNMRException(NMR_ERROR_INVALIDBUFFERSIZE);
// return used buffer size
nfUint32 cbNeededChars = ((nfUint32)cbLength);
if (pcbNeededChars)
*pcbNeededChars = cbNeededChars;
// copy string
if (pwszBuffer) {
if (cbNeededChars >= cbBufferSize)
throw CNMRException(NMR_ERROR_INSUFFICIENTBUFFERSIZE);
#ifndef __GNUC__
wcscpy_s(pwszBuffer, cbBufferSize, sString.c_str());
#else
wcscpy(pwszBuffer, sString.c_str());
#endif
}
}
void fnStringToBufferSafe(_In_ const std::string sString, _Out_opt_ nfChar * pszBuffer, nfUint32 cbBufferSize, _Out_opt_ nfUint32 * pcbNeededChars)
{
__NMRASSERT(pszBuffer);
// Check for possible integer overflows
size_t cbLength = sString.length();
if (cbLength > NMR_MAXSTRINGBUFFERSIZE)
throw CNMRException(NMR_ERROR_INVALIDBUFFERSIZE);
// return used buffer size
nfUint32 cbNeededChars = ((nfUint32)cbLength);
if (pcbNeededChars)
*pcbNeededChars = cbNeededChars;
// copy string
if (pszBuffer) {
if (cbNeededChars >= cbBufferSize)
throw CNMRException(NMR_ERROR_INSUFFICIENTBUFFERSIZE);
#ifndef __GNUC__
strcpy_s(pszBuffer, cbBufferSize, sString.c_str());
#else
strcpy(pszBuffer, sString.c_str());
#endif
}
}
// UTF conversion functions
nfBool fnUTF16CharIsSurrogate(_In_ nfWChar cChar)
{
nfUint32 nSignature = (cChar & 0xfc00);
return (nSignature == 0xd800) || (nSignature == 0xdc00);
}
nfBool fnUTF16CharIsHighSurrogate(_In_ nfWChar cChar)
{
nfUint32 nSignature = (cChar & 0xfc00);
return (nSignature == 0xd800);
}
nfBool fnUTF16CharIsLowSurrogate(_In_ nfWChar cChar)
{
nfUint32 nSignature = (cChar & 0xfc00);
return (nSignature == 0xdc00);
}
nfUint32 fnUTF16toCharacterID(_In_ nfUint16 nHighSurrogate, _In_ nfUint16 nLowSurrogate)
{
if ((fnUTF16CharIsHighSurrogate(nLowSurrogate)) && (fnUTF16CharIsLowSurrogate(nHighSurrogate))) {
std::swap(nLowSurrogate, nHighSurrogate); // UTF16LE
}
if ((!fnUTF16CharIsHighSurrogate(nHighSurrogate)) || (!fnUTF16CharIsLowSurrogate(nLowSurrogate)))
throw CNMRException(NMR_ERROR_COULDNOTCONVERTTOUTF8);
nfUint32 nCode = (((nfUint32)(nHighSurrogate & 0x3ff)) << 10 | ((nfUint32)(nLowSurrogate & 0x3ff)));
return nCode + 0x10000;
}
void fnCharacterIDToUTF16(_In_ nfUint32 nCharacterID, _Out_ nfUint16 & nHighSurrogate, _Out_ nfUint16 & nLowSurrogate)
{
if ((nCharacterID < 0x10000) || (nCharacterID > 0x10FFFF))
throw CNMRException(NMR_ERROR_COULDNOTCONVERTTOUTF16);
nCharacterID -= 0x10000;
nHighSurrogate = (nCharacterID >> 10) | 0xd800;
nLowSurrogate = (nCharacterID & 0x3ff) | 0xdc00;
}
std::string fnUTF16toUTF8(_In_ const std::wstring sString)
{
// Check Input Sanity
size_t nLength = sString.length();
if (nLength == 0)
return "";
if (nLength > NMR_MAXSTRINGBUFFERSIZE)
throw CNMRException(NMR_ERROR_INVALIDBUFFERSIZE);
// Reserve UTF8 Buffer
nfUint32 nBufferSize = (nfUint32)nLength * 4 + 1;
std::vector<nfChar> Buffer;
Buffer.resize(nBufferSize);
// nfInt32 nResult;
// Alternative: Convert via Win API
// nResult = WideCharToMultiByte(CP_UTF8, 0, sString.c_str(), (nfUint32)nLength, &Buffer[0], nBufferSize, nullptr, nullptr);
// if (nResult == 0)
// throw CNMRException(NMR_ERROR_COULDNOTCONVERTTOUTF8);
const nfWChar * pChar = sString.c_str();
nfChar * pOutput = &Buffer[0];
while (*pChar) {
nfWChar cChar = *pChar;
nfUint32 nCharacter;
pChar++;
if (fnUTF16CharIsSurrogate(cChar)) {
nfWChar cLowChar = *pChar;
if (cLowChar == 0)
throw CNMRException(NMR_ERROR_COULDNOTCONVERTTOUTF8);
pChar++;
nCharacter = fnUTF16toCharacterID(cChar, cLowChar);
}
else {
nCharacter = cChar;
}
if (nCharacter < 0x80) {
// One Byte Encoding
*pOutput = nCharacter;
pOutput++;
}
else if (nCharacter < 0x800) {
// Two Byte Encoding
*pOutput = (nCharacter >> 6) | 0xC0;
pOutput++;
*pOutput = (nCharacter & 0x3f) | 0x80;
pOutput++;
}
else if (nCharacter < 0x10000) {
// Three Byte Encoding
*pOutput = (nCharacter >> 12) | 0xE0;
pOutput++;
*pOutput = ((nCharacter >> 6) & 0x3f) | 0x80;
pOutput++;
*pOutput = (nCharacter & 0x3f) | 0x80;
pOutput++;
}
else if (nCharacter < 0x200000) {
// Four Byte Encoding
*pOutput = (nCharacter >> 18) | 0xF0;
pOutput++;
*pOutput = ((nCharacter >> 12) & 0x3f) | 0x80;
pOutput++;
*pOutput = ((nCharacter >> 6) & 0x3f) | 0x80;
pOutput++;
*pOutput = (nCharacter & 0x3f) | 0x80;
pOutput++;
}
else if (nCharacter < 0x4000000) {
// Five Byte Encoding
*pOutput = (nCharacter >> 24) | 0xF8;
pOutput++;
*pOutput = ((nCharacter >> 18) & 0x3f) | 0x80;
pOutput++;
*pOutput = ((nCharacter >> 12) & 0x3f) | 0x80;
pOutput++;
*pOutput = ((nCharacter >> 6) & 0x3f) | 0x80;
pOutput++;
*pOutput = (nCharacter & 0x3f) | 0x80;
pOutput++;
}
else {
// Six Byte Encoding
*pOutput = (nCharacter >> 30) | 0xFC;
pOutput++;
*pOutput = ((nCharacter >> 24) & 0x3f) | 0x80;
pOutput++;
*pOutput = ((nCharacter >> 18) & 0x3f) | 0x80;
pOutput++;
*pOutput = ((nCharacter >> 12) & 0x3f) | 0x80;
pOutput++;
*pOutput = ((nCharacter >> 6) & 0x3f) | 0x80;
pOutput++;
*pOutput = (nCharacter & 0x3f) | 0x80;
pOutput++;
}
}
// write end byte
*pOutput = 0;
return std::string(&Buffer[0]);
}
std::wstring fnUTF8toUTF16(_In_ const std::string sString)
{
// Check Input Sanity
size_t nLength = sString.length();
if (nLength == 0)
return L"";
if (nLength > NMR_MAXSTRINGBUFFERSIZE)
throw CNMRException(NMR_ERROR_INVALIDBUFFERSIZE);
// Reserve UTF8 Buffer
nfUint32 nBufferSize = (nfUint32)nLength;
std::vector<nfWChar> Buffer;
Buffer.resize(nBufferSize * 2 + 2);
// Alternative: Convert via Win API
// nfInt32 nResult;
//nResult = MultiByteToWideChar(CP_UTF8, 0, sString.c_str(), (nfUint32)nLength, &Buffer[0], nBufferSize);
//if (nResult == 0)
//throw CNMRException(NMR_ERROR_COULDNOTCONVERTTOUTF16);
const nfChar * pChar = sString.c_str();
nfWChar * pOutput = &Buffer[0];
while (*pChar) {
nfByte cChar = (nfByte) *pChar;
nfUint32 nLength = UTF8DecodeTable[(nfUint32)cChar];
pChar++;
if (nLength == 0)
throw CNMRException(NMR_ERROR_COULDNOTCONVERTTOUTF16);
__NMRASSERT(nLength <= 6);
// Check for BOM (0xEF,0xBB,0xBF), this also checks for #0 characters at the end,
// so it does not read over the string end!
nfBool bIsBOM = false;
if (cChar == 0xef) {
if (*((const nfByte*) pChar) == 0xbb) {
if (*((const nfByte*) (pChar + 1)) == 0xbf) {
bIsBOM = true;
}
}
};
if (!bIsBOM) {
nfUint32 nCode = cChar & UTF8DecodeMask[nLength];
while (nLength > 1) {
cChar = *pChar;
if ((cChar & 0xc0) != 0x80)
throw CNMRException(NMR_ERROR_COULDNOTCONVERTTOUTF16);
pChar++;
// Map UTF8 sequence to code
nCode = (nCode << 6) | (cChar & 0x3f);
nLength--;
}
// Map Code to UTF16
if ((nCode < 0xd800) || ((nCode >= 0xe000) && (nCode <= 0xffff))) {
*pOutput = nCode;
pOutput++;
}
else {
nfUint16 nHighSurrogate, nLowSurrogate;
fnCharacterIDToUTF16(nCode, nHighSurrogate, nLowSurrogate);
*pOutput = nHighSurrogate;
pOutput++;
*pOutput = nLowSurrogate;
pOutput++;
}
}
else {
// If we find a UTF8 bom, we just ignore it.
__NMRASSERT(nLength == 3);
pChar += 2;
}
}
// write end byte
*pOutput = 0;
return std::wstring(&Buffer[0]);
}
nfUint32 fnBufferedUTF8toUTF16(_In_ const nfChar * pszInBuffer, _Out_ nfWChar * pszwOutBuffer, _In_ nfUint32 cbBufferSize, _Out_ nfUint32 * pnLastChar, _Out_ nfUint32 * pcbNeededCharacters)
{
if (pszInBuffer == nullptr)
throw CNMRException(NMR_ERROR_INVALIDPARAM);
if (pszwOutBuffer == nullptr)
throw CNMRException(NMR_ERROR_INVALIDPARAM);
if ((pnLastChar == nullptr) || (pcbNeededCharacters == nullptr))
throw CNMRException(NMR_ERROR_INVALIDPARAM);
if (cbBufferSize > NMR_MAXSTRINGBUFFERSIZE)
throw CNMRException(NMR_ERROR_INVALIDBUFFERSIZE);
// Set default values
nfUint32 cbOutCount = 0;
*pnLastChar = 0;
*pcbNeededCharacters = 0;
// Set iterating pointers
const nfChar * pInChar = pszInBuffer;
nfWChar * pOutChar = pszwOutBuffer;
// Iterate through input
nfInt32 cbCount = (nfInt32) cbBufferSize;
while (cbCount > 0) {
nfByte cChar = *pInChar;
// Check Multibyte Length Character
nfUint32 nLength = UTF8DecodeTable[(nfUint32)cChar];
if (nLength == 0)
throw CNMRException(NMR_ERROR_COULDNOTCONVERTTOUTF16);
__NMRASSERT(nLength <= 6);
// If we do not have enough Bytes left for the multibyte character, return needed count.
if (((nfInt32) nLength) > cbCount) {
*pcbNeededCharacters = nLength - cbCount;
return cbOutCount;
}
// Set multibyte character to next char
*pnLastChar += nLength;
// Read multibyte character byte by byte.
pInChar++;
cbCount--;
// Check for BOM (0xEF,0xBB,0xBF), this also checks for #0 characters at the end,
// so it does not read over the string end!
nfBool bIsBOM = false;
if (cChar == 0xef) {
if (*((const nfByte *)pInChar) == 0xbb) {
if (*((const nfByte *) (pInChar + 1)) == 0xbf) {
bIsBOM = true;
}
}
};
if (!bIsBOM) {
// create utf16 code
nfUint32 nCode = cChar & UTF8DecodeMask[nLength];
while (nLength > 1) {
cChar = *pInChar;
if ((cChar & 0xc0) != 0x80)
throw CNMRException(NMR_ERROR_COULDNOTCONVERTTOUTF16);
pInChar++;
cbCount--;
// Map UTF8 sequence to code
nCode = (nCode << 6) | (cChar & 0x3f);
nLength--;
}
// Map Code to UTF16
if ((nCode < 0xd800) || ((nCode >= 0xe000) && (nCode <= 0xffff))) {
*pOutChar = nCode;
pOutChar++;
cbOutCount++;
}
else {
nfUint16 nHighSurrogate, nLowSurrogate;
fnCharacterIDToUTF16(nCode, nHighSurrogate, nLowSurrogate);
*pOutChar = nHighSurrogate;
pOutChar++;
*pOutChar = nLowSurrogate;
pOutChar++;
cbOutCount += 2;
}
}
else {
__NMRASSERT(nLength == 3);
pInChar += 2;
cbCount -= 2;
}
}
// everything has been processed.
return cbOutCount;
}
nfBool fnStartsWithPathDelimiter(_In_ const std::string sPath)
{
const nfChar * pChar = sPath.c_str();
return ((*pChar == '/') || (*pChar == '\\'));
}
std::string fnRemoveLeadingPathDelimiter(_In_ const std::string sPath)
{
const nfChar * pChar = sPath.c_str();
while ((*pChar == '/') || (*pChar == '\\'))
pChar++;
return std::string(pChar);
}
std::string fnIncludeLeadingPathDelimiter(_In_ const std::string sPath)
{
if (sPath.length() == 0) {
return "/";
}
const nfChar * pChar = sPath.c_str();
if ((*pChar == '/') || (*pChar == '\\'))
return sPath;
std::string sPrefix = "/";
return sPrefix + sPath;
}
std::string fnExtractFileName(_In_ const std::string sFullPath)
{
const nfChar * pChar = sFullPath.c_str();
const nfChar * pLastDelimiter = nullptr;
while (*pChar != 0) {
if ((*pChar == '/') || (*pChar == '\\'))
pLastDelimiter = pChar;
pChar++;
}
if (pLastDelimiter != nullptr) {
// Leave away delimiter
pLastDelimiter++;
return std::string(pLastDelimiter);
}
else {
// We have no directory given
return sFullPath;
}
}
std::string fnExtractFileDir(_In_ const std::string sFullPath)
{
const nfChar * pChar = sFullPath.c_str();
const nfChar * pLastDelimiter = nullptr;
while (*pChar != 0) {
if ((*pChar == '/') || (*pChar == '\\'))
pLastDelimiter = pChar;
pChar++;
}
if (pLastDelimiter != nullptr) {
// Leave away delimiter
pLastDelimiter++;
return sFullPath.substr(0, pLastDelimiter - sFullPath.c_str());
}
else {
// We have no directory given
return std::string("");
}
}
std::vector<double> fnVctDouble_fromString(_In_ const std::string sString)
{
std::vector<double> vctValues;
const nfChar * pszString = sString.c_str();
const nfChar * pCurrent = pszString;
nfBool bFinished = false;
while (!bFinished) {
// Find next space
const nfChar * pBegin = pCurrent;
while ((*pCurrent != ' ') && (*pCurrent))
pCurrent++;
// If we have not found a space, convert value to double
if (pBegin != pCurrent) {
vctValues.push_back(fnStringToFloat(pBegin));
}
// If we are finished, break, otherwise skip space!
if (!*pCurrent)
bFinished = true;
else
pCurrent++;
}
return vctValues;
}
}
|