VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/rest/RTCRestOutputPrettyToString.cpp@ 76452

Last change on this file since 76452 was 76452, checked in by vboxsync, 6 years ago

IPRT: Ran scm --fix-err-h. bugref:9344

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.8 KB
Line 
1/* $Id: RTCRestOutputPrettyToString.cpp 76452 2018-12-25 01:41:25Z vboxsync $ */
2/** @file
3 * IPRT - C++ REST, RTCRestOutputPrettyToString implementation.
4 */
5
6/*
7 * Copyright (C) 2018 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP RTLOGGROUP_REST
32#include <iprt/cpp/restoutput.h>
33
34#include <iprt/errcore.h>
35#include <iprt/string.h>
36
37
38RTCRestOutputPrettyToString::RTCRestOutputPrettyToString(RTCString *a_pDst, bool a_fAppend /*= false*/) RT_NOEXCEPT
39 : RTCRestOutputPrettyBase()
40 , m_pDst(a_pDst)
41 , m_fOutOfMemory(false)
42{
43 if (!a_fAppend)
44 m_pDst->setNull();
45}
46
47
48RTCRestOutputPrettyToString::~RTCRestOutputPrettyToString()
49{
50 /* We don't own the string, so we don't delete it! */
51 m_pDst = NULL;
52}
53
54
55size_t RTCRestOutputPrettyToString::output(const char *a_pchString, size_t a_cchToWrite) RT_NOEXCEPT
56{
57 if (a_cchToWrite)
58 {
59 RTCString *pDst = m_pDst;
60 if (pDst && !m_fOutOfMemory)
61 {
62 /*
63 * Make sure we've got sufficient space available before we append.
64 */
65 size_t cchCurrent = pDst->length();
66 size_t cbCapacity = pDst->capacity();
67 size_t cbNeeded = cchCurrent + a_cchToWrite + 1;
68 if (cbNeeded <= cbCapacity)
69 { /* likely */ }
70 else
71 {
72 /* Grow it. */
73 if (cbNeeded < _16M)
74 {
75 if (cbCapacity <= _1K)
76 cbCapacity = _1K;
77 else
78 cbCapacity = RT_ALIGN_Z(cbCapacity, _1K);
79 while (cbCapacity < cbNeeded)
80 cbCapacity <<= 1;
81 }
82 else
83 {
84 cbCapacity = RT_ALIGN_Z(cbCapacity, _2M);
85 while (cbCapacity < cbNeeded)
86 cbCapacity += _2M;
87 }
88 int rc = pDst->reserveNoThrow(cbCapacity);
89 if (RT_SUCCESS(rc))
90 {
91 rc = pDst->reserveNoThrow(cbNeeded);
92 if (RT_FAILURE(rc))
93 {
94 m_fOutOfMemory = true;
95 return a_cchToWrite;
96 }
97 }
98 }
99
100 /*
101 * Do the appending.
102 */
103 pDst->append(a_pchString, a_cchToWrite);
104 }
105 }
106 return a_cchToWrite;
107}
108
109
110RTCString *RTCRestOutputPrettyToString::finalize() RT_NOEXCEPT
111{
112 RTCString *pRet;
113 if (!m_fOutOfMemory)
114 pRet = m_pDst;
115 else
116 pRet = NULL;
117 m_pDst = NULL;
118 return pRet;
119}
120
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette