1 | /* $Id: RTCRestOutputPrettyBase.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - C++ REST, RTCRestOutputPrettyBase implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2018-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #define LOG_GROUP RTLOGGROUP_REST
|
---|
42 | #include <iprt/cpp/restoutput.h>
|
---|
43 |
|
---|
44 | #include <iprt/errcore.h>
|
---|
45 | #include <iprt/string.h>
|
---|
46 |
|
---|
47 |
|
---|
48 | RTCRestOutputPrettyBase::RTCRestOutputPrettyBase() RT_NOEXCEPT
|
---|
49 | : RTCRestOutputBase()
|
---|
50 | {
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 | RTCRestOutputPrettyBase::~RTCRestOutputPrettyBase()
|
---|
55 | {
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | uint32_t RTCRestOutputPrettyBase::beginArray() RT_NOEXCEPT
|
---|
60 | {
|
---|
61 | output(RT_STR_TUPLE("["));
|
---|
62 | uint32_t const uOldState = m_uState;
|
---|
63 | m_uState = (uOldState & 0xffff) + 1;
|
---|
64 | return uOldState;
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | void RTCRestOutputPrettyBase::endArray(uint32_t a_uOldState) RT_NOEXCEPT
|
---|
69 | {
|
---|
70 | m_uState = a_uOldState;
|
---|
71 | output(RT_STR_TUPLE("\n"));
|
---|
72 | outputIndentation();
|
---|
73 | output(RT_STR_TUPLE("]"));
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | uint32_t RTCRestOutputPrettyBase::beginObject() RT_NOEXCEPT
|
---|
78 | {
|
---|
79 | output(RT_STR_TUPLE("{"));
|
---|
80 | uint32_t const uOldState = m_uState;
|
---|
81 | m_uState = (uOldState & 0xffff) + 1;
|
---|
82 | return uOldState;
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | void RTCRestOutputPrettyBase::endObject(uint32_t a_uOldState) RT_NOEXCEPT
|
---|
87 | {
|
---|
88 | m_uState = a_uOldState;
|
---|
89 | output(RT_STR_TUPLE("\n"));
|
---|
90 | outputIndentation();
|
---|
91 | output(RT_STR_TUPLE("}"));
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | void RTCRestOutputPrettyBase::valueSeparator() RT_NOEXCEPT
|
---|
96 | {
|
---|
97 | if (m_uState & RT_BIT_32(31))
|
---|
98 | output(RT_STR_TUPLE(",\n"));
|
---|
99 | else
|
---|
100 | {
|
---|
101 | m_uState |= RT_BIT_32(31);
|
---|
102 | output(RT_STR_TUPLE("\n"));
|
---|
103 | }
|
---|
104 | outputIndentation();
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | void RTCRestOutputPrettyBase::valueSeparatorAndName(const char *a_pszName, size_t a_cchName) RT_NOEXCEPT
|
---|
109 | {
|
---|
110 | RT_NOREF(a_cchName);
|
---|
111 | if (m_uState & RT_BIT_32(31))
|
---|
112 | output(RT_STR_TUPLE(",\n"));
|
---|
113 | else
|
---|
114 | {
|
---|
115 | m_uState |= RT_BIT_32(31);
|
---|
116 | output(RT_STR_TUPLE("\n"));
|
---|
117 | }
|
---|
118 | outputIndentation();
|
---|
119 | printf("%RMjs: ", a_pszName);
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | void RTCRestOutputPrettyBase::outputIndentation() RT_NOEXCEPT
|
---|
124 | {
|
---|
125 | static char const s_szSpaces[] = " ";
|
---|
126 | size_t cchIndent = (m_uState & 0xffff) << 1;
|
---|
127 | while (cchIndent > 0)
|
---|
128 | {
|
---|
129 | size_t cbToWrite = RT_MIN(cchIndent, sizeof(s_szSpaces) - 1);
|
---|
130 | output(s_szSpaces, cbToWrite);
|
---|
131 | cchIndent -= cbToWrite;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|