michael@0: // FilePathAutoRename.cpp michael@0: michael@0: #include "StdAfx.h" michael@0: #include "FilePathAutoRename.h" michael@0: michael@0: #include "Common/Defs.h" michael@0: #include "Common/IntToString.h" michael@0: michael@0: #include "Windows/FileName.h" michael@0: #include "Windows/FileFind.h" michael@0: michael@0: using namespace NWindows; michael@0: michael@0: static bool MakeAutoName(const UString &name, michael@0: const UString &extension, int value, UString &path) michael@0: { michael@0: wchar_t number[32]; michael@0: ConvertUInt64ToString(value, number); michael@0: path = name; michael@0: path += number; michael@0: path += extension; michael@0: return NFile::NFind::DoesFileExist(path); michael@0: } michael@0: michael@0: bool AutoRenamePath(UString &fullProcessedPath) michael@0: { michael@0: UString path; michael@0: int dotPos = fullProcessedPath.ReverseFind(L'.'); michael@0: michael@0: int slashPos = fullProcessedPath.ReverseFind(L'/'); michael@0: #ifdef _WIN32 michael@0: int slash1Pos = fullProcessedPath.ReverseFind(L'\\'); michael@0: slashPos = MyMax(slashPos, slash1Pos); michael@0: #endif michael@0: michael@0: UString name, extension; michael@0: if (dotPos > slashPos && dotPos > 0) michael@0: { michael@0: name = fullProcessedPath.Left(dotPos); michael@0: extension = fullProcessedPath.Mid(dotPos); michael@0: } michael@0: else michael@0: name = fullProcessedPath; michael@0: name += L'_'; michael@0: int indexLeft = 1, indexRight = (1 << 30); michael@0: while (indexLeft != indexRight) michael@0: { michael@0: int indexMid = (indexLeft + indexRight) / 2; michael@0: if (MakeAutoName(name, extension, indexMid, path)) michael@0: indexLeft = indexMid + 1; michael@0: else michael@0: indexRight = indexMid; michael@0: } michael@0: if (MakeAutoName(name, extension, indexRight, fullProcessedPath)) michael@0: return false; michael@0: return true; michael@0: }