Changeset 73979 in vbox for trunk/src/VBox/Runtime/common/misc
- Timestamp:
- Aug 30, 2018 1:58:20 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/misc/json.cpp
r73978 r73979 148 148 PFNRTJSONTOKENIZERREAD pfnRead; 149 149 /** Opaque user data. */ 150 void 150 void *pvUser; 151 151 /** Current offset into the input stream. */ 152 152 size_t offInput; 153 153 /** Number of valid bytes in the input buffer. */ 154 154 size_t cbBuf; 155 /* Current offset into the input buffer. */155 /** Current offset into the input buffer. */ 156 156 size_t offBuf; 157 157 /** Input cache buffer. */ … … 169 169 /** The tokenizer error state. */ 170 170 int rcTok; 171 /** Where to return extended error information.*/ 172 PRTERRINFO pErrInfo; 171 173 } RTJSONTOKENIZER; 172 174 /** Pointer to a JSON tokenizer. */ … … 253 255 254 256 /********************************************************************************************************************************* 255 * Global variables*257 * Internal Functions * 256 258 *********************************************************************************************************************************/ 257 258 static int rtJsonParseValue(PRTJSONTOKENIZER pTokenizer, PRTJSONTOKEN pToken, 259 PRTJSONVALINT *ppJsonVal, PRTERRINFO pErrInfo); 259 static int rtJsonParseValue(PRTJSONTOKENIZER pTokenizer, PRTJSONTOKEN pToken, PRTJSONVALINT *ppJsonVal); 260 260 261 261 262 /** … … 475 476 { 476 477 pToken->enmClass = RTJSONTOKENCLASS_INVALID; 477 rc = VERR_JSON_MALFORMED; 478 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "malformed literal '%.6s' (line %zu col %zu)", 479 &szLiteral[0], pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart); 478 480 } 479 481 … … 585 587 break; 586 588 default: 587 rc = VERR_JSON_MALFORMED; 589 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "bad escape sequence (line %zu col %zu)", 590 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart); 591 break; 588 592 } 589 593 } … … 704 708 { 705 709 pToken->enmClass = RTJSONTOKENCLASS_INVALID; 706 rc = VERR_JSON_MALFORMED; 710 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "bad token '%c' (line %zu col %zu)", 711 ch, pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart); 707 712 } 708 713 … … 720 725 * @param pfnRead Read callback for the input stream. 721 726 * @param pvUser Opaque user data to pass to the callback. 722 */ 723 static int rtJsonTokenizerInit(PRTJSONTOKENIZER pTokenizer, PFNRTJSONTOKENIZERREAD pfnRead, void *pvUser) 727 * @param pErrInfo Where to return extended error info. 728 */ 729 static int rtJsonTokenizerInit(PRTJSONTOKENIZER pTokenizer, PFNRTJSONTOKENIZERREAD pfnRead, void *pvUser, PRTERRINFO pErrInfo) 724 730 { 725 731 pTokenizer->pfnRead = pfnRead; … … 734 740 pTokenizer->pTokenNext = &pTokenizer->Token2; 735 741 pTokenizer->rcTok = VINF_SUCCESS; 742 pTokenizer->pErrInfo = pErrInfo; 736 743 737 744 RT_ZERO(pTokenizer->achBuf); … … 881 888 * @param pTokenizer The tokenizer to use. 882 889 * @param pJsonVal The JSON array value to fill in. 883 * @param pErrInfo Where to store extended error info. Optional. 884 */ 885 static int rtJsonParseArray(PRTJSONTOKENIZER pTokenizer, PRTJSONVALINT pJsonVal, PRTERRINFO pErrInfo) 890 */ 891 static int rtJsonParseArray(PRTJSONTOKENIZER pTokenizer, PRTJSONVALINT pJsonVal) 886 892 { 887 893 int rc = VINF_SUCCESS; … … 897 903 { 898 904 PRTJSONVALINT pVal = NULL; 899 rc = rtJsonParseValue(pTokenizer, pToken, &pVal , pErrInfo);905 rc = rtJsonParseValue(pTokenizer, pToken, &pVal); 900 906 if (RT_SUCCESS(rc)) 901 907 { … … 924 930 && !fSkippedSep 925 931 && pToken->enmClass != RTJSONTOKENCLASS_END_ARRAY) 926 rc = VERR_JSON_MALFORMED; 932 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "expected end of array (#1) (line %zu col %zu)", 933 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart); 927 934 } 928 935 … … 936 943 } 937 944 else 938 rc = VERR_JSON_MALFORMED; 945 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "expected end of array (#2) (line %zu col %zu)", 946 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart); 939 947 } 940 948 … … 955 963 * @param pTokenizer The tokenizer to use. 956 964 * @param pJsonVal The JSON object value to fill in. 957 * @param pErrInfo Where to store extended error info. Optional. 958 */ 959 static int rtJsonParseObject(PRTJSONTOKENIZER pTokenizer, PRTJSONVALINT pJsonVal, PRTERRINFO pErrInfo) 965 */ 966 static int rtJsonParseObject(PRTJSONTOKENIZER pTokenizer, PRTJSONVALINT pJsonVal) 960 967 { 961 968 int rc = VINF_SUCCESS; … … 978 985 rc = rtJsonTokenizerGetToken(pTokenizer, &pToken); 979 986 if (RT_SUCCESS(rc)) 980 rc = rtJsonParseValue(pTokenizer, pToken, &pVal , pErrInfo);987 rc = rtJsonParseValue(pTokenizer, pToken, &pVal); 981 988 if (RT_SUCCESS(rc)) 982 989 { … … 1012 1019 && !fSkippedSep 1013 1020 && pToken->enmClass != RTJSONTOKENCLASS_END_OBJECT) 1014 rc = VERR_JSON_MALFORMED; 1021 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "expected end of object (#1) (line %zu col %zu)", 1022 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart); 1015 1023 } 1016 1024 } 1017 1025 else 1018 rc = VERR_JSON_MALFORMED; 1026 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "expected name separator (line %zu col %zu)", 1027 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart); 1019 1028 } 1020 1029 … … 1029 1038 } 1030 1039 else 1031 rc = VERR_JSON_MALFORMED; 1040 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "expected end of object (#2) (line %zu col %zu)", 1041 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart); 1032 1042 } 1033 1043 … … 1053 1063 * @param pToken The token to parse. 1054 1064 * @param ppJsonVal Where to store the pointer to the JSON value on success. 1055 * @param pErrInfo Where to store extended error info. Optional. 1056 */ 1057 static int rtJsonParseValue(PRTJSONTOKENIZER pTokenizer, PRTJSONTOKEN pToken, 1058 PRTJSONVALINT *ppJsonVal, PRTERRINFO pErrInfo) 1065 */ 1066 static int rtJsonParseValue(PRTJSONTOKENIZER pTokenizer, PRTJSONTOKEN pToken, PRTJSONVALINT *ppJsonVal) 1059 1067 { 1060 1068 int rc = VINF_SUCCESS; … … 1067 1075 pVal = rtJsonValueCreate(RTJSONVALTYPE_ARRAY); 1068 1076 if (RT_LIKELY(pVal)) 1069 rc = rtJsonParseArray(pTokenizer, pVal , pErrInfo);1077 rc = rtJsonParseArray(pTokenizer, pVal); 1070 1078 break; 1071 1079 case RTJSONTOKENCLASS_BEGIN_OBJECT: … … 1073 1081 pVal = rtJsonValueCreate(RTJSONVALTYPE_OBJECT); 1074 1082 if (RT_LIKELY(pVal)) 1075 rc = rtJsonParseObject(pTokenizer, pVal , pErrInfo);1083 rc = rtJsonParseObject(pTokenizer, pVal); 1076 1084 break; 1077 1085 case RTJSONTOKENCLASS_STRING: … … 1099 1107 pVal = rtJsonValueCreate(RTJSONVALTYPE_TRUE); 1100 1108 break; 1109 1110 case RTJSONTOKENCLASS_INVALID: 1111 Assert(!pTokenizer->pErrInfo || RTErrInfoIsSet(pTokenizer->pErrInfo)); 1112 rc = VERR_JSON_MALFORMED; 1113 break; 1101 1114 case RTJSONTOKENCLASS_END_ARRAY: 1115 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "unexpected '}' (line %zu col %zu)", 1116 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart); 1117 break; 1102 1118 case RTJSONTOKENCLASS_END_OBJECT: 1119 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "unexpected ']' (line %zu col %zu)", 1120 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart); 1121 break; 1103 1122 case RTJSONTOKENCLASS_NAME_SEPARATOR: 1123 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "unexpected ':' (line %zu col %zu)", 1124 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart); 1125 break; 1104 1126 case RTJSONTOKENCLASS_VALUE_SEPARATOR: 1127 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "unexpected ',' (line %zu col %zu)", 1128 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart); 1129 break; 1105 1130 case RTJSONTOKENCLASS_EOS: 1131 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "expected end of object (#1) (line %zu col %zu)", 1132 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart); 1133 break; 1106 1134 default: 1107 /** @todo Error info */1108 rc = VERR_JSON_MALFORMED;1135 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "Unexpected token class %d (line %zu col %zu)", 1136 pToken->enmClass, pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart); 1109 1137 break; 1110 1138 } … … 1129 1157 * @param pTokenizer The tokenizer state. 1130 1158 * @param ppJsonVal Where to store the root JSON value on success. 1131 * @param pErrInfo Where to store extended error info. Optional. 1132 */ 1133 static int rtJsonParse(PRTJSONTOKENIZER pTokenizer, PRTJSONVALINT *ppJsonVal, 1134 PRTERRINFO pErrInfo) 1159 */ 1160 static int rtJsonParse(PRTJSONTOKENIZER pTokenizer, PRTJSONVALINT *ppJsonVal) 1135 1161 { 1136 1162 PRTJSONTOKEN pToken = NULL; 1137 1163 int rc = rtJsonTokenizerGetToken(pTokenizer, &pToken); 1138 1164 if (RT_SUCCESS(rc)) 1139 rc = rtJsonParseValue(pTokenizer, pToken, ppJsonVal , pErrInfo);1165 rc = rtJsonParseValue(pTokenizer, pToken, ppJsonVal); 1140 1166 1141 1167 return rc; … … 1198 1224 } 1199 1225 1200 RTDECL(int) RTJsonParseFromBuf(PRTJSONVAL phJsonVal, const uint8_t *pbBuf, size_t cbBuf, 1201 PRTERRINFO pErrInfo) 1226 RTDECL(int) RTJsonParseFromBuf(PRTJSONVAL phJsonVal, const uint8_t *pbBuf, size_t cbBuf, PRTERRINFO pErrInfo) 1202 1227 { 1203 1228 AssertPtrReturn(phJsonVal, VERR_INVALID_POINTER); … … 1210 1235 Args.u.pbBuf = pbBuf; 1211 1236 1212 int rc = rtJsonTokenizerInit(&Tokenizer, rtJsonTokenizerParseFromBuf, &Args );1237 int rc = rtJsonTokenizerInit(&Tokenizer, rtJsonTokenizerParseFromBuf, &Args, pErrInfo); 1213 1238 if (RT_SUCCESS(rc)) 1214 1239 { 1215 rc = rtJsonParse(&Tokenizer, phJsonVal , pErrInfo);1240 rc = rtJsonParse(&Tokenizer, phJsonVal); 1216 1241 rtJsonTokenizerDestroy(&Tokenizer); 1217 1242 } … … 1230 1255 * forces with RTJsonParseFromBuf. */ 1231 1256 RTJSONTOKENIZER Tokenizer; 1232 int rc = rtJsonTokenizerInit(&Tokenizer, rtJsonTokenizerParseFromString, (void *)pszStr );1257 int rc = rtJsonTokenizerInit(&Tokenizer, rtJsonTokenizerParseFromString, (void *)pszStr, pErrInfo); 1233 1258 if (RT_SUCCESS(rc)) 1234 1259 { 1235 rc = rtJsonParse(&Tokenizer, phJsonVal , pErrInfo);1260 rc = rtJsonParse(&Tokenizer, phJsonVal); 1236 1261 rtJsonTokenizerDestroy(&Tokenizer); 1237 1262 } … … 1254 1279 RTJSONTOKENIZER Tokenizer; 1255 1280 1256 rc = rtJsonTokenizerInit(&Tokenizer, rtJsonTokenizerParseFromFile, &Args );1281 rc = rtJsonTokenizerInit(&Tokenizer, rtJsonTokenizerParseFromFile, &Args, pErrInfo); 1257 1282 if (RT_SUCCESS(rc)) 1258 1283 { 1259 rc = rtJsonParse(&Tokenizer, phJsonVal , pErrInfo);1284 rc = rtJsonParse(&Tokenizer, phJsonVal); 1260 1285 rtJsonTokenizerDestroy(&Tokenizer); 1261 1286 }
Note:
See TracChangeset
for help on using the changeset viewer.