other-licenses/7zstub/src/7zip/Common/FilePathAutoRename.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/other-licenses/7zstub/src/7zip/Common/FilePathAutoRename.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,57 @@
     1.4 +// FilePathAutoRename.cpp
     1.5 +
     1.6 +#include "StdAfx.h"
     1.7 +#include "FilePathAutoRename.h"
     1.8 +
     1.9 +#include "Common/Defs.h"
    1.10 +#include "Common/IntToString.h"
    1.11 +
    1.12 +#include "Windows/FileName.h"
    1.13 +#include "Windows/FileFind.h"
    1.14 +
    1.15 +using namespace NWindows;
    1.16 +
    1.17 +static bool MakeAutoName(const UString &name, 
    1.18 +    const UString &extension, int value, UString &path)
    1.19 +{
    1.20 +  wchar_t number[32];
    1.21 +  ConvertUInt64ToString(value, number);
    1.22 +  path = name;
    1.23 +  path += number;
    1.24 +  path += extension;
    1.25 +  return NFile::NFind::DoesFileExist(path);
    1.26 +}
    1.27 +
    1.28 +bool AutoRenamePath(UString &fullProcessedPath)
    1.29 +{
    1.30 +  UString path;
    1.31 +  int dotPos = fullProcessedPath.ReverseFind(L'.');
    1.32 +
    1.33 +  int slashPos = fullProcessedPath.ReverseFind(L'/');
    1.34 +  #ifdef _WIN32
    1.35 +  int slash1Pos = fullProcessedPath.ReverseFind(L'\\');
    1.36 +  slashPos = MyMax(slashPos, slash1Pos);
    1.37 +  #endif
    1.38 +
    1.39 +  UString name, extension;
    1.40 +  if (dotPos > slashPos &&  dotPos > 0)
    1.41 +  {
    1.42 +    name = fullProcessedPath.Left(dotPos);
    1.43 +    extension = fullProcessedPath.Mid(dotPos);
    1.44 +  }
    1.45 +  else
    1.46 +    name = fullProcessedPath;
    1.47 +  name += L'_';
    1.48 +  int indexLeft = 1, indexRight = (1 << 30);
    1.49 +  while (indexLeft != indexRight)
    1.50 +  {
    1.51 +    int indexMid = (indexLeft + indexRight) / 2;
    1.52 +    if (MakeAutoName(name, extension, indexMid, path))
    1.53 +      indexLeft = indexMid + 1;
    1.54 +    else
    1.55 +      indexRight = indexMid;
    1.56 +  }
    1.57 +  if (MakeAutoName(name, extension, indexRight, fullProcessedPath))
    1.58 +    return false;
    1.59 +  return true;
    1.60 +}

mercurial