* Open input file, format it, and close the output. * * @param fileName_ The path and name of the file to be processed. */
| 469 | * @param fileName_ The path and name of the file to be processed. |
| 470 | */ |
| 471 | void ASConsole::formatFile(const string& fileName_) |
| 472 | { |
| 473 | stringstream in; |
| 474 | ostringstream out; |
| 475 | FileEncoding encoding = readFile(fileName_, in); |
| 476 | |
| 477 | // Unless a specific language mode has been set, set the language mode |
| 478 | // according to the file's suffix. |
| 479 | if (!formatter.getModeManuallySet()) |
| 480 | { |
| 481 | if (stringEndsWith(fileName_, string(".java"))) |
| 482 | formatter.setJavaStyle(); |
| 483 | else if (stringEndsWith(fileName_, string(".cs"))) |
| 484 | formatter.setSharpStyle(); |
| 485 | else |
| 486 | formatter.setCStyle(); |
| 487 | } |
| 488 | |
| 489 | // set line end format |
| 490 | string nextLine; // next output line |
| 491 | filesAreIdentical = true; // input and output files are identical |
| 492 | LineEndFormat lineEndFormat = formatter.getLineEndFormat(); |
| 493 | initializeOutputEOL(lineEndFormat); |
| 494 | // do this AFTER setting the file mode |
| 495 | ASStreamIterator<stringstream> streamIterator(&in); |
| 496 | formatter.init(&streamIterator); |
| 497 | |
| 498 | // format the file |
| 499 | while (formatter.hasMoreLines()) |
| 500 | { |
| 501 | nextLine = formatter.nextLine(); |
| 502 | out << nextLine; |
| 503 | linesOut++; |
| 504 | if (formatter.hasMoreLines()) |
| 505 | { |
| 506 | setOutputEOL(lineEndFormat, streamIterator.getOutputEOL()); |
| 507 | out << outputEOL; |
| 508 | } |
| 509 | else |
| 510 | { |
| 511 | streamIterator.saveLastInputLine(); // to compare the last input line |
| 512 | // this can happen if the file if missing a closing bracket and break-blocks is requested |
| 513 | if (formatter.getIsLineReady()) |
| 514 | { |
| 515 | setOutputEOL(lineEndFormat, streamIterator.getOutputEOL()); |
| 516 | out << outputEOL; |
| 517 | nextLine = formatter.nextLine(); |
| 518 | out << nextLine; |
| 519 | linesOut++; |
| 520 | streamIterator.saveLastInputLine(); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | if (filesAreIdentical) |
| 525 | { |
| 526 | if (streamIterator.checkForEmptyLine) |
| 527 | { |
| 528 | if (nextLine.find_first_not_of(" \t") != string::npos) |
nothing calls this directly
no test coverage detected