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