1 // FilePathAutoRename.cpp
2
3 #include "StdAfx.h"
4
5 #include "../../Windows/FileFind.h"
6
7 #include "FilePathAutoRename.h"
8
9 using namespace NWindows;
10
MakeAutoName(const FString & name,const FString & extension,UInt32 value,FString & path)11 static bool MakeAutoName(const FString &name,
12 const FString &extension, UInt32 value, FString &path)
13 {
14 path = name;
15 path.Add_UInt32(value);
16 path += extension;
17 return NFile::NFind::DoesFileOrDirExist(path);
18 }
19
AutoRenamePath(FString & path)20 bool AutoRenamePath(FString &path)
21 {
22 int dotPos = path.ReverseFind_Dot();
23 int slashPos = path.ReverseFind_PathSepar();
24
25 FString name = path;
26 FString extension;
27 if (dotPos > slashPos + 1)
28 {
29 name.DeleteFrom(dotPos);
30 extension = path.Ptr(dotPos);
31 }
32 name += '_';
33
34 FString temp;
35
36 UInt32 left = 1, right = ((UInt32)1 << 30);
37 while (left != right)
38 {
39 UInt32 mid = (left + right) / 2;
40 if (MakeAutoName(name, extension, mid, temp))
41 left = mid + 1;
42 else
43 right = mid;
44 }
45 return !MakeAutoName(name, extension, right, path);
46 }
47