/src/CMake/Source/cmPlistParser.cxx
Line | Count | Source |
1 | | /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | | file LICENSE.rst or https://cmake.org/licensing for details. */ |
3 | | #include "cmPlistParser.h" |
4 | | |
5 | | #include <iterator> |
6 | | #include <sstream> |
7 | | #include <string> |
8 | | #include <utility> |
9 | | #include <vector> |
10 | | |
11 | | #include <cm3p/json/value.h> |
12 | | |
13 | | #include "cmJSONState.h" |
14 | | #include "cmSystemTools.h" |
15 | | #include "cmUVProcessChain.h" |
16 | | #include "cmUVStream.h" |
17 | | |
18 | | namespace { |
19 | | std::vector<std::string> PlistToJsonCommand(std::string const& filename) |
20 | 0 | { |
21 | 0 | static std::string const plutil = cmSystemTools::FindProgram("plutil"); |
22 | 0 | if (!plutil.empty()) { |
23 | 0 | return { plutil, "-convert", "json", "-o", "-", filename }; |
24 | 0 | } |
25 | | |
26 | 0 | static std::string const plistutil = cmSystemTools::FindProgram("plistutil"); |
27 | 0 | if (!plistutil.empty()) { |
28 | 0 | return { plistutil, "-f", "json", "-o", "-", "-i", filename }; |
29 | 0 | } |
30 | | |
31 | 0 | return {}; |
32 | 0 | } |
33 | | } |
34 | | |
35 | | cm::optional<Json::Value> cmParsePlist(std::string const& filename) |
36 | 0 | { |
37 | 0 | std::vector<std::string> command = PlistToJsonCommand(filename); |
38 | 0 | if (command.empty()) { |
39 | 0 | return cm::nullopt; |
40 | 0 | } |
41 | | |
42 | 0 | cmUVProcessChainBuilder builder; |
43 | 0 | builder.AddCommand(std::move(command)); |
44 | 0 | builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT); |
45 | |
|
46 | 0 | auto chain = builder.Start(); |
47 | 0 | chain.Wait(); |
48 | |
|
49 | 0 | auto const& status = chain.GetStatus(0); |
50 | 0 | if (status.ExitStatus != 0) { |
51 | 0 | return cm::nullopt; |
52 | 0 | } |
53 | | |
54 | | // Buffer plutil's output into a seekable stream. cmJSONState must be able |
55 | | // peek at a leading BOM, which the process pipe behind cmUVIStream can't |
56 | | // handle. |
57 | 0 | cmUVIStream outputStream(chain.OutputStream()); |
58 | 0 | std::string output{ std::istreambuf_iterator<char>(outputStream), |
59 | 0 | std::istreambuf_iterator<char>() }; |
60 | 0 | std::istringstream jsonStream(output); |
61 | |
|
62 | 0 | Json::Value value; |
63 | 0 | cmJSONState parseState(jsonStream, &value, cmJSONState::StrictMode::Relaxed); |
64 | 0 | if (!parseState.errors.empty()) { |
65 | 0 | return cm::nullopt; |
66 | 0 | } |
67 | 0 | return cm::optional<Json::Value>(value); |
68 | 0 | } |