* Parse a string in the format "s/SEARCH/REPLACE/". */
| 281 | * Parse a string in the format "s/SEARCH/REPLACE/". |
| 282 | */ |
| 283 | static bool |
| 284 | parseSubstOpt(Replacements &replacements, const char *opt) |
| 285 | { |
| 286 | char separator; |
| 287 | |
| 288 | if (*opt++ != 's') { |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | separator = *opt++; |
| 293 | |
| 294 | // Parse the search pattern |
| 295 | const char *search_begin = opt; |
| 296 | while (*opt != separator) { |
| 297 | if (*opt == 0) { |
| 298 | return false; |
| 299 | } |
| 300 | ++opt; |
| 301 | } |
| 302 | const char *search_end = opt++; |
| 303 | |
| 304 | // Parse the replace pattern |
| 305 | const char *replace_begin = opt; |
| 306 | while (*opt != separator) { |
| 307 | if (*opt == 0) { |
| 308 | return false; |
| 309 | } |
| 310 | ++opt; |
| 311 | } |
| 312 | const char *replace_end = opt++; |
| 313 | |
| 314 | if (*opt != 0) { |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | std::string search(search_begin, search_end); |
| 319 | std::string replace(replace_begin, replace_end); |
| 320 | |
| 321 | // If search or replace strings are taken from a file, read the file |
| 322 | std::string file_subst = "@file("; |
| 323 | |
| 324 | for (int i = 0; i < 2; i++) { |
| 325 | std::string *str = i ? &search : &replace; |
| 326 | |
| 327 | if (!str->compare(0, file_subst.length(), file_subst)) { |
| 328 | if ((*str)[str->length()-1] != ')') { |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | std::string fname = str->substr(file_subst.length()); |
| 333 | fname[fname.length()-1] = 0; |
| 334 | FILE *f = fopen(fname.c_str(), "rt"); |
| 335 | if (!f) { |
| 336 | std::cerr << "error: cannot open file " << fname << "\n"; |
| 337 | return false; |
| 338 | } |
| 339 | char buf[1024]; |
| 340 | (*str) = ""; |