- Timestamp:
- Aug 27, 2018 7:38:55 PM (6 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/iprt/cpp/restbase.h
r73918 r73920 317 317 318 318 /** 319 * Convert from (header) string value. 320 * 321 * The default implementation of is a wrapper around deserializeFromJson(). 322 * 323 * @returns IPRT status code. 324 * @param a_rValue The string value string to parse. 325 * @param a_pszName Field name or similar. 326 * @param a_pErrInfo Where to return additional error info. Optional. 327 * @param a_fFlags kCollectionFormat_xxx. 328 */ 329 virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL, 330 uint32_t a_fFlags = kCollectionFormat_Unspecified); 331 332 /** 319 333 * Returns the object type name. 320 334 */ … … 345 359 virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_OVERRIDE; 346 360 virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = 0) const RT_OVERRIDE; 361 virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL, 362 uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE; 347 363 virtual const char *getType(void) RT_OVERRIDE; 348 364 … … 370 386 RTCRestInt64 &operator=(RTCRestInt64 const &a_rThat); 371 387 372 388 /* Overridden methods: */ 373 389 virtual void resetToDefault() RT_OVERRIDE; 374 390 virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_OVERRIDE; 375 391 virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_OVERRIDE; 376 392 virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = 0) const RT_OVERRIDE; 393 virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL, 394 uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE; 377 395 virtual const char *getType(void) RT_OVERRIDE; 378 396 … … 405 423 virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_OVERRIDE; 406 424 virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = 0) const RT_OVERRIDE; 425 virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL, 426 uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE; 407 427 virtual const char *getType(void) RT_OVERRIDE; 408 428 … … 435 455 virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_OVERRIDE; 436 456 virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = 0) const RT_OVERRIDE; 457 virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL, 458 uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE; 437 459 virtual const char *getType(void) RT_OVERRIDE; 438 460 … … 465 487 virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_OVERRIDE; 466 488 virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = 0) const RT_OVERRIDE; 489 virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL, 490 uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE; 467 491 virtual const char *getType(void) RT_OVERRIDE; 468 492 … … 495 519 virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_OVERRIDE; 496 520 virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = 0) const RT_OVERRIDE; 521 virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL, 522 uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE; 497 523 virtual const char *getType(void) RT_OVERRIDE; 498 524 }; … … 516 542 return VERR_NOT_IMPLEMENTED; 517 543 } 544 virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL, 545 uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE; 518 546 virtual const char *getType(void) RT_OVERRIDE; 519 547 }; … … 537 565 return VERR_NOT_IMPLEMENTED; 538 566 } 567 virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL, 568 uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE; 539 569 virtual const char *getType(void) RT_OVERRIDE; 540 570 }; … … 606 636 public: 607 637 /** Default constructor. */ 608 RTCRestClientResponseBase() 609 : m_rcStatus(VERR_WRONG_ORDER) 610 {} 611 638 RTCRestClientResponseBase(); 612 639 /** Destructor. */ 613 virtual ~RTCRestClientResponseBase() 614 {} 615 640 virtual ~RTCRestClientResponseBase(); 616 641 /** Copy constructor. */ 617 RTCRestClientResponseBase(RTCRestClientResponseBase const &a_rThat) 618 : m_rcStatus(a_rThat.m_rcStatus) 619 {} 620 642 RTCRestClientResponseBase(RTCRestClientResponseBase const &a_rThat); 621 643 /** Copy assignment operator. */ 622 RTCRestClientResponseBase &operator=(RTCRestClientResponseBase const &a_rThat) 623 { 624 m_rcStatus = a_rThat.m_rcStatus; 625 return *this; 626 } 644 RTCRestClientResponseBase &operator=(RTCRestClientResponseBase const &a_rThat); 627 645 628 646 /** … … 636 654 * @param a_pppvBody If a body callback handler is installed, set the value pointed to to NULL. 637 655 */ 638 virtual int receivePrepare(RTHTTP a_hHttp, void ***a_pppvHdr, void ***a_pppvBody) 639 { 640 RT_NOREF(a_hHttp, a_pppvHdr, a_pppvBody); 641 return VINF_SUCCESS; 642 } 656 virtual int receivePrepare(RTHTTP a_hHttp, void ***a_pppvHdr, void ***a_pppvBody); 643 657 644 658 /** … … 652 666 * @note Called before consumeHeaders() and consumeBody(). 653 667 */ 654 virtual void receiveComplete(int a_rcStatus, RTHTTP a_hHttp) 655 { 656 RT_NOREF_PV(a_hHttp); 657 m_rcStatus = a_rcStatus; 658 } 668 virtual void receiveComplete(int a_rcStatus, RTHTTP a_hHttp); 659 669 660 670 /** 661 671 * Callback that consumes HTTP header data from the server. 662 672 * 663 * @param a_p vDataBody data.673 * @param a_pchData Body data. 664 674 * @param a_cbData Amount of body data. 665 675 * 666 676 * @note Called after receiveComplete().. 667 677 */ 668 virtual void consumeHeaders(const char *a_pvData, size_t a_cbData) 669 { 670 RT_NOREF(a_pvData, a_cbData); 671 } 678 virtual void consumeHeaders(const char *a_pchData, size_t a_cbData); 672 679 673 680 /** 674 681 * Callback that consumes HTTP body data from the server. 675 682 * 676 * @param a_p vDataBody data.683 * @param a_pchData Body data. 677 684 * @param a_cbData Amount of body data. 678 685 * 679 686 * @note Called after consumeHeaders(). 680 687 */ 681 virtual void consumeBody(const char *a_pvData, size_t a_cbData) 682 { 683 RT_NOREF(a_pvData, a_cbData); 684 } 688 virtual void consumeBody(const char *a_pchData, size_t a_cbData); 685 689 686 690 /** … … 689 693 * @returns IPRT status code. 690 694 */ 691 virtual void receiveFinal() 692 { 693 } 695 virtual void receiveFinal(); 694 696 695 697 /** … … 699 701 int getStatus() { return m_rcStatus; } 700 702 701 pr ivate:703 protected: 702 704 /** Negative numbers are IPRT errors, positive are HTTP status codes. */ 703 705 int m_rcStatus; 706 707 /** 708 * Helper that extracts a header field. 709 * 710 * @retval VINF_SUCCESS 711 * @retval VERR_NOT_FOUND if not found. 712 * @retval VERR_NO_STR_MEMORY 713 * @param a_pszField The header field header name. 714 * @param a_cchField The length of the header field name. 715 * @param a_pchData The header blob to search. 716 * @param a_cbData The size of the header blob to search. 717 * @param a_pStrDst Where to store the header value on successs. 718 */ 719 int extractHeaderFromBlob(const char *a_pszField, size_t a_cchField, const char *a_pchData, size_t a_cbData, 720 RTCString *a_pStrDst); 704 721 }; 705 722 -
trunk/src/VBox/Runtime/Makefile.kmk
r73918 r73920 1655 1655 common/rest/RTCRestClientApiBase.cpp \ 1656 1656 common/rest/RTCRestClientRequestBase.cpp \ 1657 common/rest/RTCRestClientResponseBase.cpp \ 1657 1658 common/rest/RTCRestJsonPrimaryCursor.cpp \ 1658 1659 common/rest/RTCRestOutputToString.cpp -
trunk/src/VBox/Runtime/common/rest/rest-primary-object-types.cpp
r73914 r73920 35 35 36 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <errno.h> 37 39 38 40 … … 55 57 56 58 57 int RTCRestObjectBase::toString(RTCString *a_pDst, uint32_t a_fFlags /*= 0*/) const58 { 59 Assert(a_fFlags == 0); RT_NOREF(a_fFlags);59 int RTCRestObjectBase::toString(RTCString *a_pDst, uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) const 60 { 61 Assert(a_fFlags == kCollectionFormat_Unspecified); RT_NOREF(a_fFlags); 60 62 61 63 /* … … 76 78 77 79 80 int RTCRestObjectBase::fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo /*= NULL*/, 81 uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) 82 { 83 Assert(a_fFlags == kCollectionFormat_Unspecified); RT_NOREF(a_fFlags); 84 85 /* 86 * Just wrap the JSON serialization method. 87 */ 88 RTJSONVAL hValue = NIL_RTJSONVAL; 89 int rc = RTJsonParseFromString(&hValue, a_rValue.c_str(), a_pErrInfo); 90 if (RT_SUCCESS(rc)) 91 { 92 RTCRestJsonPrimaryCursor PrimaryCursor(hValue, a_pszName, a_pErrInfo); 93 rc = deserializeFromJson(PrimaryCursor.m_Cursor); 94 } 95 return rc; 96 } 97 78 98 79 99 /********************************************************************************************************************************* … … 156 176 157 177 158 int RTCRestBool::toString(RTCString *a_pDst, uint32_t a_fFlags /*= 0*/) const 159 { 160 Assert(a_fFlags == 0); RT_NOREF(a_fFlags); 161 162 /* Be a little careful here to avoid throwing anything. */ 163 int rc = a_pDst->reserveNoThrow(m_fValue ? sizeof("true") : sizeof("false")); 164 if (RT_SUCCESS(rc)) 165 { 166 if (m_fValue) 167 a_pDst->assign(RT_STR_TUPLE("true")); 168 else 169 a_pDst->assign(RT_STR_TUPLE("false")); 170 } 171 return rc; 178 int RTCRestBool::toString(RTCString *a_pDst, uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) const 179 { 180 Assert(a_fFlags == 0); RT_NOREF(a_fFlags); 181 182 if (m_fValue) 183 return a_pDst->assignNoThrow(RT_STR_TUPLE("true")); 184 return a_pDst->assignNoThrow(RT_STR_TUPLE("false")); 185 } 186 187 188 int RTCRestBool::fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo /*= NULL*/, 189 uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) 190 { 191 Assert(a_fFlags == 0); RT_NOREF(a_fFlags); 192 if (a_rValue.startsWithWord("true", RTCString::CaseInsensitive)) 193 m_fValue = true; 194 else if (a_rValue.startsWithWord("false", RTCString::CaseInsensitive)) 195 m_fValue = false; 196 else 197 return RTErrInfoSetF(a_pErrInfo, VERR_INVALID_PARAMETER, "%s: unable to parse '%s' as bool", a_pszName, a_rValue.c_str()); 198 return VINF_SUCCESS; 172 199 } 173 200 … … 255 282 256 283 257 int RTCRestInt64::toString(RTCString *a_pDst, uint32_t a_fFlags /*= 0*/) const 258 { 259 Assert(a_fFlags == 0); RT_NOREF(a_fFlags); 260 261 /* Be a little careful here to avoid throwing anything. */ 262 char szValue[64]; 263 size_t cchValue = RTStrPrintf(szValue, sizeof(szValue), "%RI64", m_iValue); 264 int rc = a_pDst->reserveNoThrow(cchValue + 1); 265 if (RT_SUCCESS(rc)) 266 a_pDst->assign(szValue, cchValue); 267 return rc; 284 int RTCRestInt64::toString(RTCString *a_pDst, uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) const 285 { 286 Assert(a_fFlags == 0); RT_NOREF(a_fFlags); 287 288 return a_pDst->printfNoThrow("%RI64", m_iValue); 289 } 290 291 292 int RTCRestInt64::fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo /*= NULL*/, 293 uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) 294 { 295 Assert(a_fFlags == 0); RT_NOREF(a_fFlags); 296 297 int rc = RTStrToInt64Full(RTStrStripL(a_rValue.c_str()), 10, &m_iValue); 298 if (rc == VINF_SUCCESS || rc == VERR_TRAILING_SPACES) 299 return VINF_SUCCESS; 300 return RTErrInfoSetF(a_pErrInfo, rc, "%s: error %Rrc parsing '%s' as int64_t", a_pszName, rc, a_rValue.c_str()); 268 301 } 269 302 … … 358 391 359 392 360 int RTCRestInt32::toString(RTCString *a_pDst, uint32_t a_fFlags /*= 0*/) const 361 { 362 Assert(a_fFlags == 0); RT_NOREF(a_fFlags); 363 364 /* Be a little careful here to avoid throwing anything. */ 365 char szValue[16]; 366 size_t cchValue = RTStrPrintf(szValue, sizeof(szValue), "%RI32", m_iValue); 367 int rc = a_pDst->reserveNoThrow(cchValue + 1); 368 if (RT_SUCCESS(rc)) 369 a_pDst->assign(szValue, cchValue); 370 return rc; 393 int RTCRestInt32::toString(RTCString *a_pDst, uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) const 394 { 395 Assert(a_fFlags == 0); RT_NOREF(a_fFlags); 396 397 return a_pDst->printfNoThrow("%RI32", m_iValue); 398 } 399 400 401 int RTCRestInt32::fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo /*= NULL*/, 402 uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) 403 { 404 Assert(a_fFlags == 0); RT_NOREF(a_fFlags); 405 406 int rc = RTStrToInt32Full(RTStrStripL(a_rValue.c_str()), 10, &m_iValue); 407 if (rc == VINF_SUCCESS || rc == VERR_TRAILING_SPACES) 408 return VINF_SUCCESS; 409 return RTErrInfoSetF(a_pErrInfo, rc, "%s: error %Rrc parsing '%s' as int32_t", a_pszName, rc, a_rValue.c_str()); 371 410 } 372 411 … … 461 500 462 501 463 int RTCRestInt16::toString(RTCString *a_pDst, uint32_t a_fFlags /*= 0*/) const 464 { 465 Assert(a_fFlags == 0); RT_NOREF(a_fFlags); 466 467 /* Be a little careful here to avoid throwing anything. */ 468 char szValue[8]; 469 size_t cchValue = RTStrPrintf(szValue, sizeof(szValue), "%RI16", m_iValue); 470 int rc = a_pDst->reserveNoThrow(cchValue + 1); 471 if (RT_SUCCESS(rc)) 472 a_pDst->assign(szValue, cchValue); 473 return rc; 502 int RTCRestInt16::toString(RTCString *a_pDst, uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) const 503 { 504 Assert(a_fFlags == 0); RT_NOREF(a_fFlags); 505 506 return a_pDst->printfNoThrow("%RI16", m_iValue); 507 } 508 509 510 int RTCRestInt16::fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo /*= NULL*/, 511 uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) 512 { 513 Assert(a_fFlags == 0); RT_NOREF(a_fFlags); 514 515 int rc = RTStrToInt16Full(RTStrStripL(a_rValue.c_str()), 10, &m_iValue); 516 if (rc == VINF_SUCCESS || rc == VERR_TRAILING_SPACES) 517 return VINF_SUCCESS; 518 return RTErrInfoSetF(a_pErrInfo, rc, "%s: error %Rrc parsing '%s' as int16_t", a_pszName, rc, a_rValue.c_str()); 474 519 } 475 520 … … 544 589 545 590 546 int RTCRestDouble::toString(RTCString *a_pDst, uint32_t a_fFlags /*= 0*/) const591 int RTCRestDouble::toString(RTCString *a_pDst, uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) const 547 592 { 548 593 Assert(a_fFlags == 0); RT_NOREF(a_fFlags); … … 558 603 size_t const cchValue = strlen(szValue); 559 604 560 int rc = a_pDst->reserveNoThrow(cchValue + 1); 561 if (RT_SUCCESS(rc)) 562 a_pDst->assign(szValue, cchValue); 563 return rc; 605 return a_pDst->assignNoThrow(szValue, cchValue); 606 } 607 608 609 int RTCRestDouble::fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo /*= NULL*/, 610 uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) 611 { 612 Assert(a_fFlags == 0); RT_NOREF(a_fFlags); 613 614 const char *pszValue = RTStrStripL(a_rValue.c_str()); 615 errno = 0; 616 char *pszNext = NULL; 617 m_rdValue = strtod(pszValue, &pszNext); 618 if (errno == 0) 619 return VINF_SUCCESS; 620 int rc = RTErrConvertFromErrno(errno); 621 return RTErrInfoSetF(a_pErrInfo, rc, "%s: error %Rrc parsing '%s' as double", a_pszName, rc, a_rValue.c_str()); 564 622 } 565 623 … … 632 690 const char *pszValue = RTJsonValueGetString(a_rCursor.m_hValue); 633 691 const size_t cchValue = strlen(pszValue); 634 int rc = reserveNoThrow(cchValue + 1);692 int rc = assignNoThrow(pszValue, cchValue); 635 693 if (RT_SUCCESS(rc)) 636 {637 assign(pszValue, cchValue);638 694 return VINF_SUCCESS; 639 }640 695 return a_rCursor.m_pPrimary->addError(a_rCursor, rc, "no memory for %zu char long string", cchValue); 641 696 } … … 652 707 653 708 654 int RTCRestString::toString(RTCString *a_pDst, uint32_t a_fFlags /*= 0*/) const655 { 656 Assert(a_fFlags == 0); RT_NOREF(a_fFlags); 657 658 /* Careful as always. */659 if (m_cch) 660 { 661 int rc = a_pDst->reserveNoThrow(m_cch + 1); 662 if (RT_SUCCESS(rc)) 663 { /* likely */ }664 else 665 return rc;666 }667 a_pDst->assign(*this); 668 return VINF_SUCCESS;709 int RTCRestString::toString(RTCString *a_pDst, uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) const 710 { 711 Assert(a_fFlags == 0); RT_NOREF(a_fFlags); 712 713 return a_pDst->assignNoThrow(*this); 714 } 715 716 717 int RTCRestString::fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo /*= NULL*/, 718 uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) 719 { 720 Assert(a_fFlags == 0); RT_NOREF(a_fFlags); 721 RT_NOREF(a_pszName); RT_NOREF(a_pErrInfo); 722 723 return assignNoThrow(a_rValue); 669 724 } 670 725
Note:
See TracChangeset
for help on using the changeset viewer.