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,unsigned value,FString & path)14 static bool MakeAutoName(const FString &name,
15 const FString &extension, unsigned value, FString &path)
16 {
17 FChar number[16];
18 ConvertUInt32ToString(value, number);
19 path = name;
20 path += number;
21 path += extension;
22 return NFile::NFind::DoesFileOrDirExist(path);
23 }
24
AutoRenamePath(FString & fullProcessedPath)25 bool AutoRenamePath(FString &fullProcessedPath)
26 {
27 FString path;
28 int dotPos = fullProcessedPath.ReverseFind(FTEXT('.'));
29
30 int slashPos = fullProcessedPath.ReverseFind(FTEXT('/'));
31 #ifdef _WIN32
32 int slash1Pos = fullProcessedPath.ReverseFind(FTEXT('\\'));
33 slashPos = MyMax(slashPos, slash1Pos);
34 #endif
35
36 FString name, extension;
37 if (dotPos > slashPos && dotPos > 0)
38 {
39 name.SetFrom(fullProcessedPath, dotPos);
40 extension = fullProcessedPath.Ptr(dotPos);
41 }
42 else
43 name = fullProcessedPath;
44 name += L'_';
45 unsigned left = 1, right = (1 << 30);
46 while (left != right)
47 {
48 unsigned mid = (left + right) / 2;
49 if (MakeAutoName(name, extension, mid, path))
50 left = mid + 1;
51 else
52 right = mid;
53 }
54 return !MakeAutoName(name, extension, right, fullProcessedPath);
55 }
56