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