| 162 | } |
| 163 | |
| 164 | bool cmQtAutoGenerator::FileWrite(std::string const& filename, |
| 165 | std::string const& content, |
| 166 | std::string* error) |
| 167 | { |
| 168 | // Make sure the parent directory exists |
| 169 | if (!cmQtAutoGenerator::MakeParentDirectory(filename)) { |
| 170 | if (error) { |
| 171 | *error = "Could not create parent directory."; |
| 172 | } |
| 173 | return false; |
| 174 | } |
| 175 | cmsys::ofstream ofs; |
| 176 | ofs.open(filename.c_str(), |
| 177 | (std::ios::out | std::ios::binary | std::ios::trunc)); |
| 178 | |
| 179 | // Use lambda to save destructor calls of ofs |
| 180 | return [&ofs, &content, error]() -> bool { |
| 181 | if (!ofs) { |
| 182 | if (error) { |
| 183 | *error = "Opening file for writing failed."; |
| 184 | } |
| 185 | return false; |
| 186 | } |
| 187 | ofs << content; |
| 188 | if (!ofs.good()) { |
| 189 | if (error) { |
| 190 | *error = "File writing failed."; |
| 191 | } |
| 192 | return false; |
| 193 | } |
| 194 | return true; |
| 195 | }(); |
| 196 | } |
| 197 | |
| 198 | bool cmQtAutoGenerator::FileDiffers(std::string const& filename, |
| 199 | std::string const& content) |