| 738 | } |
| 739 | |
| 740 | bool TOPPASToolVertex::renameOutput_() |
| 741 | { |
| 742 | // get all output names |
| 743 | QStringList files = this->getFileNames(); |
| 744 | |
| 745 | std::map<String, NameComponent> name_old_to_new; |
| 746 | std::map<String, int> name_new_count, name_new_idx; // count occurrence (for optional counter infix) |
| 747 | |
| 748 | // a first round to find which filenames are not unique (and require augmentation with a counter) |
| 749 | |
| 750 | for (const QString& file : files) |
| 751 | { |
| 752 | String new_prefix = FileHandler::stripExtension(file); |
| 753 | String new_suffix = FileTypes::typeToName(FileHandler::getTypeByContent(file)); // this might replace bla.fasta with bla.FASTA ... which is the same file on Windows |
| 754 | if (file.endsWith(new_suffix.toQString(), Qt::CaseInsensitive)) // --> use the native suffix (to avoid deleting the source file when renaming) |
| 755 | { |
| 756 | new_suffix = String(file).suffix(new_suffix.size()); |
| 757 | } |
| 758 | NameComponent nc(new_prefix, new_suffix); |
| 759 | name_old_to_new[file] = nc; |
| 760 | ++name_new_count[nc.toString()]; |
| 761 | } |
| 762 | // for all names which occur more than once, introduce a counter |
| 763 | for (const QString& file : files) |
| 764 | { |
| 765 | if (name_new_count[name_old_to_new[file].toString()] > 1) // candidate for counter |
| 766 | { |
| 767 | name_old_to_new[file].counter = ++name_new_idx[name_old_to_new[file].toString()]; // start at index 1 |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | |
| 772 | for (Size i = 0; i < output_files_.size(); ++i) |
| 773 | { |
| 774 | for (RoundPackageIt it = output_files_[i].begin(); |
| 775 | it != output_files_[i].end(); |
| 776 | ++it) |
| 777 | { |
| 778 | for (int fi = 0; fi < it->second.filenames.size(); ++fi) |
| 779 | { |
| 780 | // rename file and update record |
| 781 | String old_filename = QDir::toNativeSeparators(it->second.filenames[fi]); |
| 782 | String new_filename = QDir::toNativeSeparators(name_old_to_new[it->second.filenames[fi]].toString().toQString()); |
| 783 | if (QFileInfo(old_filename.toQString()).canonicalFilePath() == QFileInfo(new_filename.toQString()).canonicalFilePath()) |
| 784 | { // source and target are identical -- no action required |
| 785 | continue; |
| 786 | } |
| 787 | QFile file(old_filename.toQString()); |
| 788 | if (File::exists(new_filename)) |
| 789 | { // rename only works if the target file does not exist: delete it first |
| 790 | bool success = File::remove(new_filename); |
| 791 | if (!success) |
| 792 | { |
| 793 | OPENMS_LOG_ERROR << "Could not remove '" << new_filename << "'.\n"; |
| 794 | throw Exception::FileNotFound(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, new_filename); |
| 795 | } |
| 796 | } |
| 797 | bool success = file.rename(new_filename.toQString()); |