xref: /third_party/lzma/CPP/7zip/Common/FilePathAutoRename.cpp (revision 370b324c)
  • Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
  • only in /third_party/lzma/CPP/7zip/Common/
1// FilePathAutoRename.cpp
2
3#include "StdAfx.h"
4
5#include "../../Windows/FileFind.h"
6
7#include "FilePathAutoRename.h"
8
9using namespace NWindows;
10
11static 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
20bool 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((unsigned)dotPos);
30    extension = path.Ptr((unsigned)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

Indexes created Thu Nov 07 10:32:03 CST 2024