| 171 | } |
| 172 | |
| 173 | bool cmFileAPI::ReadJsonFile(std::string const& file, Json::Value& value, |
| 174 | std::string& error) |
| 175 | { |
| 176 | std::vector<char> content; |
| 177 | |
| 178 | cmsys::ifstream fin; |
| 179 | if (!cmSystemTools::FileIsDirectory(file)) { |
| 180 | fin.open(file.c_str(), std::ios::binary); |
| 181 | } |
| 182 | auto finEnd = fin.rdbuf()->pubseekoff(0, std::ios::end); |
| 183 | if (finEnd > 0) { |
| 184 | size_t finSize = finEnd; |
| 185 | try { |
| 186 | // Allocate a buffer to read the whole file. |
| 187 | content.resize(finSize); |
| 188 | |
| 189 | // Now read the file from the beginning. |
| 190 | fin.seekg(0, std::ios::beg); |
| 191 | fin.read(content.data(), finSize); |
| 192 | } catch (...) { |
| 193 | fin.setstate(std::ios::failbit); |
| 194 | } |
| 195 | } |
| 196 | fin.close(); |
| 197 | if (!fin) { |
| 198 | value = Json::Value(); |
| 199 | error = "failed to read from file"; |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | // Parse our buffer as json. |
| 204 | if (!this->JsonReader->parse(content.data(), content.data() + content.size(), |
| 205 | &value, &error)) { |
| 206 | value = Json::Value(); |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | std::string cmFileAPI::WriteJsonFile( |
| 214 | Json::Value const& value, std::string const& prefix, |