home *** CD-ROM | disk | FTP | other *** search
- #include "stdafx.h"
- #include "ArchiveDescription.h"
- #include "ConsoleOutput.h"
- #include "Config.h"
-
- #define BUFFER_SIZE 4096
- //#define TYPE_KEY_SIZE 6
- //#define SEC_TYPE_MASK "Type%d"
-
- #define ID "ID"
- #define IDPOS "IDPos"
- #define ERRORLEVEL "Errorlevel"
- #define IGNOREERRORS "IgnoreErrors"
- #define DEBUG_KEY "Debug"
- #define UNIXPATH_KEY "UnixPath"
- #define SKIPDIRS_IN_FL_KEY "SkipDirsInFileList"
- #define SKIPEMPTY_KEY "SkipEmptyNames"
- #define FORCE_BATCH_UNPACK "BatchUnpack"
- #define SEARCH_FOR_UGLY_DIRS "SearchForUglyDirs"
-
- static char aKeyNames[][19]=
- {
- "TypeName",
- "Extension",
- "Archiver",
- "List",
- "Start",
- "End",
- "Format0","Format1","Format2","Format3",
- "Format4","Format5","Format6","Format7",
- "Extract",
- "ExtractWithPath",
- "Test",
- "Delete",
- "Add",
- "Move",
- "InputString"
- };
-
- CArchiveDescription::CArchiveDescription()
- {
- ZeroMemory(m_alpStrings,sizeof(LPSTR)*STRINGS_COUNT);
- m_iIDPos=0;
- // m_iListErrorlevel=0;
- m_bIgnoreErrors=0;
- m_lpID=NULL;
- m_iIDSize=0;
- m_iFormatHeight=0;
- m_bDebug=0;
- m_bUnixPath=FALSE;
- m_bSkipDirsInFileList=false;
- m_bSkipEmpty=TRUE;
- m_bBatchUnpack=FALSE;
- m_bSearchForUglyDirs = FALSE;
-
- m_bUserWarned = false;
- }
-
- void CArchiveDescription::ReleaseContents()
- {
- for(int i=0;i<STRINGS_COUNT;i++)
- if(m_alpStrings[i])
- delete[] m_alpStrings[i];
-
- ZeroMemory(m_alpStrings,sizeof(LPSTR)*STRINGS_COUNT);
-
- m_iIDPos=0;
- // m_iListErrorlevel=0;
- m_bIgnoreErrors=0;
-
- if(m_lpID)
- {
- delete[] m_lpID;
- m_lpID=NULL;
- }
- m_iIDSize=0;
- }
-
- LPSTR CArchiveDescription::GetString(char *szTypeKeyName,int iStrIdx, char *szIniPath)
- {
- char buf[BUFFER_SIZE];
- int iReaded=::GetPrivateProfileString(szTypeKeyName,aKeyNames[iStrIdx],"",buf,BUFFER_SIZE,szIniPath);
- if(iReaded<=0)
- return NULL;
- m_alpStrings[iStrIdx]=new char[iReaded+2];
- strcpy(m_alpStrings[iStrIdx],buf);
- return m_alpStrings[iStrIdx];
- }
-
- int HexVal(char c)
- {
- if(isupper (c))
- c = tolower (c);
- if(isdigit (c))
- return (c - '0'); /* '0'..'9' */
- return (c - 'a' + 10); /* 'a'..'f' */
- }
-
- BOOL CArchiveDescription::Load(char *szSectionName,char *szIniPath)
- {
- ReleaseContents();
-
- char buf[BUFFER_SIZE];
- //char szTypeKeyName[TYPE_KEY_SIZE];
- //wsprintf(szTypeKeyName,SEC_TYPE_MASK,iSectionNum);
- //int iReaded=::GetPrivateProfileString(szSectionName,aKeyNames[TYPENAME_IDX],"",buf,BUFFER_SIZE,szIniPath);
- //if(iReaded<=0)
- // return FALSE;
- m_alpStrings[0]=new char[strlen(szSectionName)+1];
- strcpy(m_alpStrings[0],szSectionName);
-
- for(int i=EXTENSION_IDX;i<STRINGS_COUNT;i++)
- GetString(szSectionName,i,szIniPath);
-
- // multiple extensions support added ...
- LPSTR lpExts=m_alpStrings[EXTENSION_IDX];
- lpExts[lstrlen(lpExts)+1]=0;
- while(lpExts=strchr(lpExts,','))
- *lpExts++=0;
-
- int iReaded=::GetPrivateProfileString(szSectionName,ID,"",buf,BUFFER_SIZE,szIniPath);
- if(iReaded>0)
- {
- m_iIDSize=(strlen(buf)+1)/3;
- m_lpID=new BYTE[m_iIDSize];
- for(i=0;i<m_iIDSize;i++)
- m_lpID[i]=::HexVal((char)buf[i*3])*16 + ::HexVal((char)buf[i*3+1]);
- }
-
- GetPrivateProfileString(szSectionName,IDPOS,"0",buf,BUFFER_SIZE,szIniPath);
- m_bIDAtEnd=(buf[0] == '-');
- m_iIDPos=GetPrivateProfileInt(szSectionName,IDPOS,0,szIniPath);
- // m_iListErrorlevel=GetPrivateProfileInt(szSectionName,ERRORLEVEL,0,szIniPath);
- m_bIgnoreErrors=GetPrivateProfileInt(szSectionName,IGNOREERRORS,0,szIniPath);
- m_bDebug=GetPrivateProfileInt(szSectionName,DEBUG_KEY,m_bDebug,szIniPath);
- m_bUnixPath=GetPrivateProfileInt(szSectionName,UNIXPATH_KEY,m_bUnixPath,szIniPath);
- m_bSkipDirsInFileList=GetPrivateProfileInt(szSectionName,SKIPDIRS_IN_FL_KEY,m_bSkipDirsInFileList,szIniPath);
- m_bSkipEmpty=GetPrivateProfileInt(szSectionName,SKIPEMPTY_KEY,m_bSkipEmpty,szIniPath);
- m_bBatchUnpack=GetPrivateProfileInt(szSectionName,FORCE_BATCH_UNPACK,m_bBatchUnpack,szIniPath);
- m_bSearchForUglyDirs=GetPrivateProfileInt(szSectionName,SEARCH_FOR_UGLY_DIRS,m_bSearchForUglyDirs,szIniPath);
-
-
- i=FORMAT_IDX;
- while(m_alpStrings[i++])m_iFormatHeight++;
-
- if(m_bDebug)
- wsprintf(m_szDebugOutputPath,"%smultiarc.%s.log",g_szMultiarcPath,String(TYPENAME_IDX));
- else
- m_szDebugOutputPath[0]=0;
-
- return TRUE;
- }
-
- int CArchiveDescription::LoadArchiveDescriptions(CArchiveDescription **ppad, char *szIniName)
- {
- char buffer[BUFFER_SIZE];
- buffer[0]=0;
- ::GetPrivateProfileSectionNames(buffer,BUFFER_SIZE,szIniName);
-
- char *p=buffer;
- int l=0;
- for(int cnt=0;l=strlen(p);cnt++,p+=l+1);
- p=buffer;
-
- if(cnt)
- {
- *ppad=new CArchiveDescription[cnt];
- for(int i=0;i<cnt;i++)
- {
- (*ppad)[i].Load(p,szIniName);
- p+=strlen(p)+1;
- }
- }
- return cnt;
- }
-
- void CArchiveDescription::DebugOutput(LPCSTR szCommand, LPCSTR lpOutput, int errorlevel,LPCSTR lpCurDir)
- {
- if(m_bDebug)
- {
- HANDLE hFile=CreateFile(m_szDebugOutputPath,GENERIC_WRITE,
- FILE_SHARE_WRITE,0,OPEN_ALWAYS,
- FILE_ATTRIBUTE_NORMAL,0);
- if(hFile!=INVALID_HANDLE_VALUE)
- {
- SYSTEMTIME st;
- GetLocalTime(&st);
- char sz[1024];
- char szBound[]="%s:%d/%d/%d %d:%d:%d\r\n\r\n";
- DWORD dwWritten;
- SetFilePointer(hFile,0,0,FILE_END);
-
- wsprintf(sz,szBound,"\r\n\r\n>>>> BEGIN",st.wDay,st.wMonth,st.wYear,st.wHour,st.wMinute,st.wSecond);
-
- WriteFile(hFile,sz,strlen(sz),&dwWritten,0);
-
- char szPrefix[]=">Command executed:";
- WriteFile(hFile,szPrefix,strlen(szPrefix),&dwWritten,0);
-
- if(szCommand)
- {
- WriteFile(hFile,szCommand,strlen(szCommand),&dwWritten,0);
- WriteFile(hFile,"\r\n\r\n",4,&dwWritten,0);
- }
- else
- {
- strcpy(sz,"Warning!!! No command specified\r\n\r\n");
- WriteFile(hFile,sz,strlen(sz),&dwWritten,0);
- }
-
- if(lpCurDir)
- {
- char szCurDir[MAX_PATH*2];
- wsprintf(szCurDir,"Current directory : %s\r\n\r\n",lpCurDir);
- WriteFile(hFile,szCurDir,strlen(szCurDir),&dwWritten,0);
- }
-
- char szPrefixOut[]="> === Archiver output begin ===\r\n\r\n";
- WriteFile(hFile,szPrefixOut,strlen(szPrefixOut),&dwWritten,0);
-
- if(lpOutput)
- WriteFile(hFile,lpOutput,strlen(lpOutput),&dwWritten,0);
- else
- {
- strcpy(sz,"Warning!!! No archiver output was received\r\n\r\n");
- WriteFile(hFile,sz,strlen(sz),&dwWritten,0);
- }
-
- char szPrefixEnd[]="\r\n> === Archiver output end ===\r\n\r\n";
- WriteFile(hFile,szPrefixEnd,strlen(szPrefixEnd),&dwWritten,0);
-
- wsprintf(sz,"> Errorlevel returned: %d %s\r\n\r\n",errorlevel,(errorlevel==MAEL_CANT_CREATE_PROCESS)?"Warning!!! Process wasn't created.":"");
- WriteFile(hFile,sz,strlen(sz),&dwWritten,0);
-
- GetLocalTime(&st);
- wsprintf(sz,szBound,"<<<< END",st.wDay,st.wMonth,st.wYear,st.wHour,st.wMinute,st.wSecond);
- WriteFile(hFile,sz,strlen(sz),&dwWritten,0);
- CloseHandle(hFile);
- }
- }
- }
-
- BOOL CArchiveDescription::BatchUnpack()
- {
- if(m_bBatchUnpack && !m_alpStrings[EXTRACT_WITH_PATH_IDX])
- {
- MessageBox(GetFocus(),"You set ON \"Batch unpacking\" feature for this type of archives "
- "but didn't provide corresponding \"ExtractWithPath=\" command lone. Batch "
- "unpacking feature will be set OFF. Please, correct your settings","Warning",MB_ICONWARNING);
-
- m_bBatchUnpack = false;
- }
-
- return m_bBatchUnpack;
- }