1 | /* $Id: RTCRestJsonPrimaryCursor.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - C++ REST, RTCRestJsonPrimaryCursor implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2018-2020 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/errcore.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | char *RTCRestJsonPrimaryCursor::getPath(RTCRestJsonCursor const &a_rCursor, char *pszDst, size_t cbDst) const RT_NOEXCEPT
|
---|
39 | {
|
---|
40 | AssertReturn(cbDst > 0, NULL);
|
---|
41 |
|
---|
42 | /*
|
---|
43 | * To avoid using recursion, we need to first do a pass to figure sizes
|
---|
44 | * and depth. To keep things simple, with the exception of the top name
|
---|
45 | * we only copy out full names.
|
---|
46 | */
|
---|
47 | /* Special case: Insufficient space for the top name. */
|
---|
48 | size_t const cchTopName = strlen(a_rCursor.m_pszName);
|
---|
49 | if (cchTopName >= cbDst)
|
---|
50 | {
|
---|
51 | memcpy(pszDst, a_rCursor.m_pszName, cbDst - 1);
|
---|
52 | pszDst[cbDst - 1] = '\0';
|
---|
53 | }
|
---|
54 | else
|
---|
55 | {
|
---|
56 | /* Determin how deep we should go and the resulting length. */
|
---|
57 | size_t iMaxDepth = 0;
|
---|
58 | size_t cchTotal = cchTopName;
|
---|
59 | for (RTCRestJsonCursor const *pCur = a_rCursor.m_pParent; pCur; pCur = pCur->m_pParent)
|
---|
60 | {
|
---|
61 | size_t const cchName = strlen(pCur->m_pszName);
|
---|
62 | size_t cchNewTotal = cchName + 1 + cchTotal;
|
---|
63 | if (cchNewTotal < cbDst)
|
---|
64 | cchTotal = cchNewTotal;
|
---|
65 | else
|
---|
66 | break;
|
---|
67 | iMaxDepth++;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /* Produce the string, in reverse. */
|
---|
71 | char *psz = &pszDst[cchTotal];
|
---|
72 | *psz = '\0';
|
---|
73 | psz -= cchTopName;
|
---|
74 | memcpy(psz, a_rCursor.m_pszName, cchTopName);
|
---|
75 | for (RTCRestJsonCursor const *pCur = a_rCursor.m_pParent; pCur && iMaxDepth > 0; pCur = pCur->m_pParent)
|
---|
76 | {
|
---|
77 | psz -= 1;
|
---|
78 | *psz = '.';
|
---|
79 |
|
---|
80 | size_t const cchName = strlen(pCur->m_pszName);
|
---|
81 | psz -= cchName;
|
---|
82 | memcpy(psz, pCur->m_pszName, cchName);
|
---|
83 |
|
---|
84 | iMaxDepth--;
|
---|
85 | }
|
---|
86 | Assert(psz == pszDst);
|
---|
87 | }
|
---|
88 | return pszDst;
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | int RTCRestJsonPrimaryCursor::addError(RTCRestJsonCursor const &a_rCursor, int a_rc, const char *a_pszFormat, ...) RT_NOEXCEPT
|
---|
93 | {
|
---|
94 | va_list va;
|
---|
95 | va_start(va, a_pszFormat);
|
---|
96 | char szPath[128];
|
---|
97 | a_rc = RTErrInfoAddF(m_pErrInfo, a_rc, "%s: %N\n", getPath(a_rCursor, szPath, sizeof(szPath)), a_pszFormat, &va);
|
---|
98 | va_end(va);
|
---|
99 | return a_rc;
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | int RTCRestJsonPrimaryCursor::unknownField(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT
|
---|
104 | {
|
---|
105 | char szPath[128];
|
---|
106 | return RTErrInfoAddF(m_pErrInfo, VWRN_NOT_FOUND, "%s: unknown field (type %s)\n",
|
---|
107 | getPath(a_rCursor, szPath, sizeof(szPath)),
|
---|
108 | RTJsonValueTypeName(RTJsonValueGetType(a_rCursor.m_hValue)));
|
---|
109 | }
|
---|
110 |
|
---|