VirtualBox

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

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

IPRT/rest: More request array and map setter methods. Fixed string defaults. Safer copying and resetToDefaults. bugref:9167

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