home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 June / Chip_2002-06_cd1.bin / zkuste / wincom / download / mltwcx.1.0.0.8-src.cab / mltwcx-src / ArchiveDescription.cpp next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  7.1 KB  |  255 lines

  1. #include "stdafx.h"
  2. #include "ArchiveDescription.h"
  3. #include "ConsoleOutput.h"
  4. #include "Config.h"
  5.  
  6. #define BUFFER_SIZE            4096
  7. //#define TYPE_KEY_SIZE        6
  8. //#define SEC_TYPE_MASK "Type%d"
  9.  
  10. #define ID                    "ID"
  11. #define IDPOS                "IDPos"
  12. #define ERRORLEVEL            "Errorlevel"
  13. #define IGNOREERRORS        "IgnoreErrors"
  14. #define DEBUG_KEY            "Debug"
  15. #define UNIXPATH_KEY        "UnixPath"
  16. #define SKIPDIRS_IN_FL_KEY    "SkipDirsInFileList"
  17. #define SKIPEMPTY_KEY        "SkipEmptyNames"
  18. #define FORCE_BATCH_UNPACK    "BatchUnpack"
  19. #define SEARCH_FOR_UGLY_DIRS "SearchForUglyDirs"
  20.  
  21. static char aKeyNames[][19]=
  22. {
  23.     "TypeName",
  24.     "Extension",
  25.     "Archiver",
  26.     "List",
  27.     "Start",
  28.     "End",
  29.     "Format0","Format1","Format2","Format3",
  30.     "Format4","Format5","Format6","Format7",
  31.     "Extract",
  32.   "ExtractWithPath",
  33.     "Test",
  34.     "Delete",
  35.     "Add",
  36.     "Move",
  37.     "InputString"
  38. };
  39.  
  40. CArchiveDescription::CArchiveDescription()
  41. {
  42.     ZeroMemory(m_alpStrings,sizeof(LPSTR)*STRINGS_COUNT);
  43.     m_iIDPos=0;
  44. //    m_iListErrorlevel=0;
  45.     m_bIgnoreErrors=0;
  46.     m_lpID=NULL;
  47.     m_iIDSize=0;    
  48.     m_iFormatHeight=0;
  49.     m_bDebug=0;
  50.     m_bUnixPath=FALSE;
  51.     m_bSkipDirsInFileList=false;
  52.     m_bSkipEmpty=TRUE;
  53.     m_bBatchUnpack=FALSE;
  54.   m_bSearchForUglyDirs = FALSE;
  55.  
  56.   m_bUserWarned = false;
  57. }
  58.  
  59. void CArchiveDescription::ReleaseContents()
  60. {
  61.     for(int i=0;i<STRINGS_COUNT;i++)
  62.         if(m_alpStrings[i])
  63.             delete[] m_alpStrings[i];
  64.  
  65.     ZeroMemory(m_alpStrings,sizeof(LPSTR)*STRINGS_COUNT);
  66.  
  67.     m_iIDPos=0;
  68. //    m_iListErrorlevel=0;
  69.     m_bIgnoreErrors=0;
  70.  
  71.     if(m_lpID)
  72.     {
  73.         delete[] m_lpID;
  74.         m_lpID=NULL;
  75.     }
  76.     m_iIDSize=0;
  77. }
  78.  
  79. LPSTR CArchiveDescription::GetString(char *szTypeKeyName,int iStrIdx, char *szIniPath)
  80. {
  81.     char buf[BUFFER_SIZE];
  82.     int iReaded=::GetPrivateProfileString(szTypeKeyName,aKeyNames[iStrIdx],"",buf,BUFFER_SIZE,szIniPath);
  83.     if(iReaded<=0)
  84.         return NULL;
  85.     m_alpStrings[iStrIdx]=new char[iReaded+2];
  86.     strcpy(m_alpStrings[iStrIdx],buf);
  87.     return m_alpStrings[iStrIdx];
  88. }
  89.  
  90. int HexVal(char c)
  91. {
  92.     if(isupper (c))
  93.         c = tolower (c);
  94.     if(isdigit (c))
  95.         return (c - '0');    /* '0'..'9' */
  96.     return (c - 'a' + 10);        /* 'a'..'f' */
  97. }
  98.  
  99. BOOL CArchiveDescription::Load(char *szSectionName,char *szIniPath)
  100. {
  101.     ReleaseContents();
  102.  
  103.     char buf[BUFFER_SIZE];
  104.     //char szTypeKeyName[TYPE_KEY_SIZE];
  105.     //wsprintf(szTypeKeyName,SEC_TYPE_MASK,iSectionNum);
  106.     //int iReaded=::GetPrivateProfileString(szSectionName,aKeyNames[TYPENAME_IDX],"",buf,BUFFER_SIZE,szIniPath);
  107.     //if(iReaded<=0)
  108.     //    return FALSE;
  109.     m_alpStrings[0]=new char[strlen(szSectionName)+1];
  110.     strcpy(m_alpStrings[0],szSectionName);
  111.  
  112.     for(int i=EXTENSION_IDX;i<STRINGS_COUNT;i++)
  113.         GetString(szSectionName,i,szIniPath);
  114.  
  115.     // multiple extensions support added ...
  116.     LPSTR lpExts=m_alpStrings[EXTENSION_IDX];
  117.     lpExts[lstrlen(lpExts)+1]=0;
  118.     while(lpExts=strchr(lpExts,','))
  119.         *lpExts++=0;
  120.  
  121.     int iReaded=::GetPrivateProfileString(szSectionName,ID,"",buf,BUFFER_SIZE,szIniPath);
  122.     if(iReaded>0)
  123.     {
  124.         m_iIDSize=(strlen(buf)+1)/3;
  125.         m_lpID=new BYTE[m_iIDSize];
  126.         for(i=0;i<m_iIDSize;i++)
  127.             m_lpID[i]=::HexVal((char)buf[i*3])*16 + ::HexVal((char)buf[i*3+1]);
  128.     }
  129.  
  130.     GetPrivateProfileString(szSectionName,IDPOS,"0",buf,BUFFER_SIZE,szIniPath);
  131.     m_bIDAtEnd=(buf[0] == '-');    
  132.     m_iIDPos=GetPrivateProfileInt(szSectionName,IDPOS,0,szIniPath);
  133. //    m_iListErrorlevel=GetPrivateProfileInt(szSectionName,ERRORLEVEL,0,szIniPath);
  134.     m_bIgnoreErrors=GetPrivateProfileInt(szSectionName,IGNOREERRORS,0,szIniPath);
  135.     m_bDebug=GetPrivateProfileInt(szSectionName,DEBUG_KEY,m_bDebug,szIniPath);
  136.     m_bUnixPath=GetPrivateProfileInt(szSectionName,UNIXPATH_KEY,m_bUnixPath,szIniPath);
  137.     m_bSkipDirsInFileList=GetPrivateProfileInt(szSectionName,SKIPDIRS_IN_FL_KEY,m_bSkipDirsInFileList,szIniPath);
  138.     m_bSkipEmpty=GetPrivateProfileInt(szSectionName,SKIPEMPTY_KEY,m_bSkipEmpty,szIniPath);
  139.     m_bBatchUnpack=GetPrivateProfileInt(szSectionName,FORCE_BATCH_UNPACK,m_bBatchUnpack,szIniPath);
  140.   m_bSearchForUglyDirs=GetPrivateProfileInt(szSectionName,SEARCH_FOR_UGLY_DIRS,m_bSearchForUglyDirs,szIniPath);
  141.     
  142.  
  143.     i=FORMAT_IDX;
  144.     while(m_alpStrings[i++])m_iFormatHeight++;
  145.  
  146.     if(m_bDebug)
  147.         wsprintf(m_szDebugOutputPath,"%smultiarc.%s.log",g_szMultiarcPath,String(TYPENAME_IDX));
  148.     else
  149.         m_szDebugOutputPath[0]=0;
  150.  
  151.     return TRUE;
  152. }
  153.  
  154. int CArchiveDescription::LoadArchiveDescriptions(CArchiveDescription **ppad, char *szIniName)
  155. {
  156.     char buffer[BUFFER_SIZE];
  157.     buffer[0]=0;
  158.     ::GetPrivateProfileSectionNames(buffer,BUFFER_SIZE,szIniName);
  159.  
  160.     char *p=buffer;
  161.     int l=0;
  162.     for(int cnt=0;l=strlen(p);cnt++,p+=l+1);
  163.     p=buffer;
  164.     
  165.     if(cnt)
  166.     {
  167.         *ppad=new CArchiveDescription[cnt];
  168.         for(int i=0;i<cnt;i++)
  169.         {
  170.             (*ppad)[i].Load(p,szIniName);
  171.             p+=strlen(p)+1;
  172.         }
  173.     }
  174.     return cnt;
  175. }
  176.  
  177. void CArchiveDescription::DebugOutput(LPCSTR szCommand, LPCSTR lpOutput, int errorlevel,LPCSTR lpCurDir)
  178. {
  179.     if(m_bDebug)
  180.     {
  181.         HANDLE hFile=CreateFile(m_szDebugOutputPath,GENERIC_WRITE,
  182.                                     FILE_SHARE_WRITE,0,OPEN_ALWAYS,
  183.                                     FILE_ATTRIBUTE_NORMAL,0);
  184.         if(hFile!=INVALID_HANDLE_VALUE)
  185.         {
  186.             SYSTEMTIME st;
  187.             GetLocalTime(&st);
  188.             char sz[1024];
  189.             char szBound[]="%s:%d/%d/%d %d:%d:%d\r\n\r\n";
  190.             DWORD dwWritten;
  191.             SetFilePointer(hFile,0,0,FILE_END);
  192.  
  193.             wsprintf(sz,szBound,"\r\n\r\n>>>> BEGIN",st.wDay,st.wMonth,st.wYear,st.wHour,st.wMinute,st.wSecond);
  194.  
  195.             WriteFile(hFile,sz,strlen(sz),&dwWritten,0);
  196.  
  197.             char szPrefix[]=">Command executed:";
  198.             WriteFile(hFile,szPrefix,strlen(szPrefix),&dwWritten,0);
  199.  
  200.             if(szCommand)
  201.             {
  202.                 WriteFile(hFile,szCommand,strlen(szCommand),&dwWritten,0);
  203.                 WriteFile(hFile,"\r\n\r\n",4,&dwWritten,0);
  204.             }
  205.             else
  206.             {
  207.                 strcpy(sz,"Warning!!! No command specified\r\n\r\n");
  208.                 WriteFile(hFile,sz,strlen(sz),&dwWritten,0);
  209.             }
  210.  
  211.             if(lpCurDir)
  212.             {
  213.                 char szCurDir[MAX_PATH*2];
  214.                 wsprintf(szCurDir,"Current directory : %s\r\n\r\n",lpCurDir);
  215.                 WriteFile(hFile,szCurDir,strlen(szCurDir),&dwWritten,0);
  216.             }
  217.  
  218.             char szPrefixOut[]="> === Archiver output begin ===\r\n\r\n";
  219.             WriteFile(hFile,szPrefixOut,strlen(szPrefixOut),&dwWritten,0);
  220.  
  221.             if(lpOutput)
  222.                 WriteFile(hFile,lpOutput,strlen(lpOutput),&dwWritten,0);
  223.             else
  224.             {
  225.                 strcpy(sz,"Warning!!! No archiver output was received\r\n\r\n");
  226.                 WriteFile(hFile,sz,strlen(sz),&dwWritten,0);
  227.             }
  228.  
  229.             char szPrefixEnd[]="\r\n> === Archiver output end ===\r\n\r\n";
  230.             WriteFile(hFile,szPrefixEnd,strlen(szPrefixEnd),&dwWritten,0);
  231.  
  232.             wsprintf(sz,"> Errorlevel returned: %d %s\r\n\r\n",errorlevel,(errorlevel==MAEL_CANT_CREATE_PROCESS)?"Warning!!! Process wasn't created.":"");
  233.             WriteFile(hFile,sz,strlen(sz),&dwWritten,0);
  234.  
  235.             GetLocalTime(&st);
  236.             wsprintf(sz,szBound,"<<<< END",st.wDay,st.wMonth,st.wYear,st.wHour,st.wMinute,st.wSecond);
  237.             WriteFile(hFile,sz,strlen(sz),&dwWritten,0);
  238.             CloseHandle(hFile);
  239.         }
  240.     }
  241. }
  242.  
  243. BOOL CArchiveDescription::BatchUnpack()
  244. {
  245.   if(m_bBatchUnpack && !m_alpStrings[EXTRACT_WITH_PATH_IDX])
  246.   {
  247.     MessageBox(GetFocus(),"You set ON \"Batch unpacking\" feature for this type of archives "
  248.                 "but didn't provide corresponding \"ExtractWithPath=\" command lone. Batch "
  249.                 "unpacking feature will be set OFF. Please, correct your settings","Warning",MB_ICONWARNING);
  250.  
  251.     m_bBatchUnpack = false;
  252.   }
  253.  
  254.   return m_bBatchUnpack;
  255. }