| 175 | } |
| 176 | |
| 177 | KTX_error_code CommandInfo::printInfoJSON(std::istream& file, bool minified) { |
| 178 | const auto base_indent = minified ? 0 : +0; |
| 179 | const auto indent_width = minified ? 0 : 4; |
| 180 | const auto space = minified ? "" : " "; |
| 181 | const auto nl = minified ? "" : "\n"; |
| 182 | |
| 183 | std::ostringstream messagesOS; |
| 184 | PrintIndent pi{messagesOS, base_indent, indent_width}; |
| 185 | |
| 186 | bool first = true; |
| 187 | const auto validationResult = validateIOStream(file, fmtInFile(options.inputFilepath), false, false, [&](const ValidationReport& issue) { |
| 188 | if (!std::exchange(first, false)) { |
| 189 | pi(2, "}},{}", nl); |
| 190 | } |
| 191 | pi(2, "{{{}", nl); |
| 192 | pi(3, "\"id\":{}{},{}", space, issue.id, nl); |
| 193 | pi(3, "\"type\":{}\"{}\",{}", space, toString(issue.type), nl); |
| 194 | pi(3, "\"message\":{}\"{}\",{}", space, escape_json_copy(issue.message), nl); |
| 195 | pi(3, "\"details\":{}\"{}\"{}", space, escape_json_copy(issue.details), nl); |
| 196 | }); |
| 197 | |
| 198 | file.clear(); // Clear any unexpected EOF from validation |
| 199 | |
| 200 | // Workaround detection to decide if ktx will produce any output into the json |
| 201 | // to eliminate a trailing comma |
| 202 | file.seekg(0, std::ios_base::end); |
| 203 | const auto fileSize = file.tellg(); |
| 204 | bool fileIdentifierIsCorrect = false; |
| 205 | if (fileSize >= 12) { |
| 206 | static constexpr uint8_t ktx2_identifier_reference[12] = KTX2_IDENTIFIER_REF; |
| 207 | ktx_uint8_t identifier[12]; |
| 208 | file.seekg(0, std::ios_base::beg); |
| 209 | file.read(reinterpret_cast<char*>(identifier), 12); |
| 210 | fileIdentifierIsCorrect = std::memcmp(identifier, ktx2_identifier_reference, 12) == 0; |
| 211 | } |
| 212 | const auto ktxWillPrintOutput = fileIdentifierIsCorrect && fileSize >= KTX2_HEADER_SIZE; |
| 213 | |
| 214 | PrintIndent out{std::cout, base_indent, indent_width}; |
| 215 | out(0, "{{{}", nl); |
| 216 | out(1, "\"$schema\":{}\"https://schema.khronos.org/ktx/info_v0.json\",{}", space, nl); |
| 217 | out(1, "\"valid\":{}{},{}", space, validationResult == 0, nl); |
| 218 | if (!first) { |
| 219 | out(1, "\"messages\":{}[{}", space, nl); |
| 220 | fmt::print("{}", std::move(messagesOS).str()); |
| 221 | out(2, "}}{}", nl); |
| 222 | out(1, "]{}{}", ktxWillPrintOutput ? "," : "", nl); |
| 223 | } else { |
| 224 | out(1, "\"messages\":{}[]{}{}", space, ktxWillPrintOutput ? "," : "", nl); |
| 225 | } |
| 226 | |
| 227 | file.seekg(0, std::ios_base::beg); |
| 228 | if (!file) { |
| 229 | out(0, "}}{}", nl); |
| 230 | return validationResult == 0 ? KTX_FILE_SEEK_ERROR : KTX_SUCCESS; |
| 231 | } |
| 232 | |
| 233 | StreambufStream<std::streambuf*> ktx2Stream{file.rdbuf(), std::ios::in | std::ios::binary}; |
| 234 | const auto result = ktxPrintKTX2InfoJSONForStream(ktx2Stream.stream(), base_indent + 1, indent_width, minified); |
nothing calls this directly
no test coverage detected