1   /************************************************************************
  2           Copy Handler 1.x - program for copying data in Microsoft Windows
  3                                                    systems.
  4           Copyright (C) 2001-2004 Ixen Gerthannes (copyhandler@o2.pl)
  5  
  6           This program is free software; you can redistribute it and/or modify
  7           it under the terms of the GNU General Public License as published by
  8           the Free Software Foundation; either version 2 of the License, or
  9           (at your option) any later version.
  10  
  11           This program is distributed in the hope that it will be useful,
  12           but WITHOUT ANY WARRANTY; without even the implied warranty of
  13           MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14           GNU General Public License for more details.
  15  
  16           You should have received a copy of the GNU General Public License
  17           along with this program; if not, write to the Free Software
  18           Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19   *************************************************************************/
  20   #include "stdafx.h"
  21   #include "Copy Handler.h"
  22   #include "StringHelpers.h"
  23   #include "stdio.h"
  24  
  25   #ifdef _MFC_VER
  26   void ExpandFormatString(CString* pstrFmt, DWORD dwError)
  27   {
  28           // replace strings %errnum & %errdesc to something else
  29           TCHAR xx[_MAX_PATH];
  30           pstrFmt->Replace(_T("%errnum"), _itot(dwError, xx, 10));
  31           
  32           FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError, 0, xx, _MAX_PATH, NULL);
  33  
  34           while (xx[_tcslen(xx)-1] == _T('\n') || xx[_tcslen(xx)-1] == _T('\r'))
  35                   xx[_tcslen(xx)-1] = _T('\0');
  36  
  37           pstrFmt->Replace(_T("%errdesc"), xx);
  38   }
  39   #endif
  40  
  41   LPTSTR GetSizeString(double dData, LPTSTR pszBuffer)
  42   {
  43           if (dData < 0.0)
  44                   dData=0.0;
  45  
  46           if (dData < 1200.0)
  47                   _stprintf(pszBuffer, _T("%.2f %s"), dData, GetResManager()->LoadString(IDS_BYTE_STRING));
  48           else if (dData < 1228800.0)
  49                   _stprintf(pszBuffer, _T("%.2f %s"), static_cast<double>(dData)/1024.0, GetResManager()->LoadString(IDS_KBYTE_STRING));
  50           else if (dData < 1258291200.0)
  51                   _stprintf(pszBuffer, _T("%.2f %s"), static_cast<double>(dData)/1048576.0, GetResManager()->LoadString(IDS_MBYTE_STRING));
  52           else
  53                   _stprintf(pszBuffer, _T("%.2f %s"), static_cast<double>(dData)/1073741824.0, GetResManager()->LoadString(IDS_GBYTE_STRING));
  54  
  55           return pszBuffer;
  56   }
  57  
  58   LPTSTR GetSizeString(__int64 llData, LPTSTR pszBuffer, bool bStrict)
  59   {
  60           if (llData < 0)
  61                   llData=0;
  62  
  63           if (llData >= 1258291200 && (!bStrict || (llData % 1073741824) == 0))
  64                   _stprintf(pszBuffer, _T("%.2f %s"), (double)(llData/1073741824.0), GetResManager()->LoadString(IDS_GBYTE_STRING));
  65           else if (llData >= 1228800 && (!bStrict || (llData % 1048576) == 0))
  66                   _stprintf(pszBuffer, _T("%.2f %s"), (double)(llData/1048576.0), GetResManager()->LoadString(IDS_MBYTE_STRING));
  67           else if (llData >= 1200 && (!bStrict || (llData % 1024) == 0))
  68                   _stprintf(pszBuffer, _T("%.2f %s"), (double)(llData/1024.0), GetResManager()->LoadString(IDS_KBYTE_STRING));
  69           else
  70                   _stprintf(pszBuffer, _T("%I64u %s"), llData, GetResManager()->LoadString(IDS_BYTE_STRING));
  71  
  72           return pszBuffer;
  73   }