| 261 | } |
| 262 | |
| 263 | int run(FormatOptions opts, const vector<string> &files) { |
| 264 | int ret_code = 0; |
| 265 | TextPrinter tp; |
| 266 | if (files.size() > 1 && !opts.contains(FormatOpt::Inplace)) { |
| 267 | tp << "Expected inplace mode in case of more than 1 file\n"; |
| 268 | } |
| 269 | for (const auto &file: files) { |
| 270 | tp << "input file=" << file << '\n'; |
| 271 | std::ifstream t(file.c_str()); |
| 272 | |
| 273 | string str(std::string((std::istreambuf_iterator<char>(t)), |
| 274 | std::istreambuf_iterator<char>()).c_str()); |
| 275 | |
| 276 | auto res = transform_syntax(file, str, opts); |
| 277 | if (res.ok) { |
| 278 | if (opts.contains(FormatOpt::Inplace)) { |
| 279 | std::ofstream out(file.c_str()); |
| 280 | out << res.ok.value().c_str(); |
| 281 | } else { |
| 282 | tp << res.ok.value().c_str(); |
| 283 | } |
| 284 | } else if (res.error) { |
| 285 | if (!opts.contains(FormatOpt::Inplace)) { |
| 286 | tp << res.error->content << '\n'; |
| 287 | } |
| 288 | tp << file << '\n' << res.error->what << '\n'; |
| 289 | ret_code = -1; |
| 290 | } else { |
| 291 | tp << "cant_compile=" << file << '\n'; |
| 292 | } |
| 293 | } |
| 294 | return ret_code; |
| 295 | } |
| 296 | } |