misc/libphysfs/lzma/CPP/7zip/Common/FilePathAutoRename.cpp
branchui-scaling
changeset 15283 c4fd2813b127
parent 13390 0135e64c6c66
parent 15279 7ab5cf405686
child 15663 d92eeb468dad
equal deleted inserted replaced
13390:0135e64c6c66 15283:c4fd2813b127
     1 // FilePathAutoRename.cpp
       
     2 
       
     3 #include "StdAfx.h"
       
     4 #include "FilePathAutoRename.h"
       
     5 
       
     6 #include "Common/Defs.h"
       
     7 #include "Common/IntToString.h"
       
     8 
       
     9 #include "Windows/FileName.h"
       
    10 #include "Windows/FileFind.h"
       
    11 
       
    12 using namespace NWindows;
       
    13 
       
    14 static bool MakeAutoName(const UString &name, 
       
    15     const UString &extension, int value, UString &path)
       
    16 {
       
    17   wchar_t number[32];
       
    18   ConvertUInt64ToString(value, number);
       
    19   path = name;
       
    20   path += number;
       
    21   path += extension;
       
    22   return NFile::NFind::DoesFileExist(path);
       
    23 }
       
    24 
       
    25 bool AutoRenamePath(UString &fullProcessedPath)
       
    26 {
       
    27   UString path;
       
    28   int dotPos = fullProcessedPath.ReverseFind(L'.');
       
    29 
       
    30   int slashPos = fullProcessedPath.ReverseFind(L'/');
       
    31   #ifdef _WIN32
       
    32   int slash1Pos = fullProcessedPath.ReverseFind(L'\\');
       
    33   slashPos = MyMax(slashPos, slash1Pos);
       
    34   #endif
       
    35 
       
    36   UString name, extension;
       
    37   if (dotPos > slashPos &&  dotPos > 0)
       
    38   {
       
    39     name = fullProcessedPath.Left(dotPos);
       
    40     extension = fullProcessedPath.Mid(dotPos);
       
    41   }
       
    42   else
       
    43     name = fullProcessedPath;
       
    44   name += L'_';
       
    45   int indexLeft = 1, indexRight = (1 << 30);
       
    46   while (indexLeft != indexRight)
       
    47   {
       
    48     int indexMid = (indexLeft + indexRight) / 2;
       
    49     if (MakeAutoName(name, extension, indexMid, path))
       
    50       indexLeft = indexMid + 1;
       
    51     else
       
    52       indexRight = indexMid;
       
    53   }
       
    54   if (MakeAutoName(name, extension, indexRight, fullProcessedPath))
       
    55     return false;
       
    56   return true;
       
    57 }