VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/rest/RTCRestOutputToString.cpp@ 73956

Last change on this file since 73956 was 73956, checked in by vboxsync, 7 years ago

IPRT/rest: Added RTJsonIteratorBeginArray and RTJsonIteratorBeginObject for more accurate JSON decoding. Early RTCRestArray implementation. bugref:9167

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.9 KB
Line 
1/* $Id: RTCRestOutputToString.cpp 73956 2018-08-29 15:09:34Z vboxsync $ */
2/** @file
3 * IPRT - C++ REST, RTCRestOutputToString 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.215389.xyz. 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#include <iprt/cpp/restbase.h>
32
33#include <iprt/err.h>
34#include <iprt/string.h>
35
36
37RTCRestOutputToString::RTCRestOutputToString(RTCString *a_pDst, bool a_fAppend /*= false*/)
38 : m_pDst(a_pDst)
39 , m_fOutOfMemory(false)
40{
41 if (!a_fAppend)
42 m_pDst->setNull();
43}
44
45
46RTCRestOutputToString::~RTCRestOutputToString()
47{
48 /* We don't own the string, so we don't delete it! */
49 m_pDst = NULL;
50}
51
52
53/**
54 * @callback_method_impl{FNRTSTROUTPUT}
55 */
56/*static*/ DECLCALLBACK(size_t)
57RTCRestOutputToString::strOutput(void *pvArg, const char *pachChars, size_t cbChars)
58{
59 RTCRestOutputToString *pThis = (RTCRestOutputToString *)pvArg;
60 if (cbChars)
61 {
62 RTCString *pDst = pThis->m_pDst;
63 if (pDst && !pThis->m_fOutOfMemory)
64 {
65 /*
66 * Make sure we've got sufficient space available before we append.
67 */
68 size_t cchCurrent = pDst->length();
69 size_t cbCapacity = pDst->capacity();
70 size_t cbNeeded = cchCurrent + cbChars + 1;
71 if (cbNeeded <= cbCapacity)
72 { /* likely */ }
73 else
74 {
75 /* Grow it. */
76 if (cbNeeded < _16M)
77 {
78 if (cbCapacity <= _1K)
79 cbCapacity = _1K;
80 else
81 cbCapacity = RT_ALIGN_Z(cbCapacity, _1K);
82 while (cbCapacity < cbNeeded)
83 cbCapacity <<= 1;
84 }
85 else
86 {
87 cbCapacity = RT_ALIGN_Z(cbCapacity, _2M);
88 while (cbCapacity < cbNeeded)
89 cbCapacity += _2M;
90 }
91 int rc = pDst->reserveNoThrow(cbCapacity);
92 if (RT_SUCCESS(rc))
93 {
94 rc = pDst->reserveNoThrow(cbNeeded);
95 if (RT_FAILURE(rc))
96 {
97 pThis->m_fOutOfMemory = true;
98 return cbChars;
99 }
100 }
101 }
102
103 /*
104 * Do the appending.
105 */
106 pDst->append(pachChars, cbChars);
107 }
108 }
109 return cbChars;
110}
111
112
113size_t RTCRestOutputToString::vprintf(const char *pszFormat, va_list va)
114{
115 return RTStrFormatV(strOutput, this, NULL, NULL, pszFormat, va);
116}
117
118
119RTCString *RTCRestOutputToString::finalize()
120{
121 RTCString *pRet;
122 if (!m_fOutOfMemory)
123 pRet = m_pDst;
124 else
125 pRet = NULL;
126 m_pDst = NULL;
127 return pRet;
128}
129
Note: See TracBrowser for help on using the repository browser.

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