BinClone/CloneSearcherLib/CSAssemblyFileMgr.cpp
//
// Parse the whole input folder, including subfolders
//
Parse the whole input folder, including subfolders
Parse the whole input folder including subfolders
bool CCSAssemblyFileMgr::parseFolder(LPCTSTR folderPath, const CCSParam& param, bool bFirstScan)
{   
    tcout << _T("Parsing folder: ") << folderPath << endl;
    CString folderPathStr = folderPath;
    m_nTotalFiles = m_nTotalFunctions = m_nTotalRegions = 0;
    WIN32_FIND_DATA fileInfo;
    HANDLE hFile = ::FindFirstFile(folderPathStr + _T("\\*.*"), &fileInfo);
    do {
        CString fileName = fileInfo.cFileName;
        if (fileName.CompareNoCase(_T(".")) == 0 || fileName.CompareNoCase(_T("..")) == 0)
            continue;
        if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {        
            CString subFolderPath = folderPathStr + _T("\\") + fileName;
            if (!parseFolder((LPCTSTR) subFolderPath, param, bFirstScan))
                return false;
        }
        else {
            CString ext = fileName.Mid(fileName.Find(_T(".")) + 1);
            if (ext.CompareNoCase(CS_ASSEMBLYFILE_EXT1) == 0 || ext.CompareNoCase(CS_ASSEMBLYFILE_EXT2) == 0 || ext.CompareNoCase(CS_ASSEMBLYFILE_EXT3) == 0) {
                CString assemblyFilePath = folderPathStr + _T("\\") + fileName;
                CCSAssemblyFile assemblyFile(assemblyFilePath, this);
                if (!assemblyFile.parseFile(param, !bFirstScan))
                    return false;                
                if (!assemblyFile.normalizeCode(param))
                    return false;
                if (!assemblyFile.extractFunctions(m_pDBMgr, param, !bFirstScan))
                    return false;
                if (!bFirstScan) {  
                    m_nTotalFunctions += assemblyFile.getNumFunctions();
					tcout << _T("Number of functions: ") << assemblyFile.getNumFunctions() << _T(" in ") << assemblyFilePath.GetString() << endl;
                }
                if (!assemblyFile.extractRegions(m_pDBMgr, param, false, false, !bFirstScan, bFirstScan, this))
                    return false;
                if (!bFirstScan) {  
                    m_nTotalRegions += assemblyFile.getNumRegions();
                    tcout << _T("Number of regions: ") << assemblyFile.getNumRegions() << _T(" in ") << assemblyFilePath.GetString() << endl;
                    ++m_nTotalFiles;
                }
            }
        }
    } while (::FindNextFile(hFile, &fileInfo));
	if (bFirstScan) {
        if (!m_pDBMgr->storeGlobalFeatures(m_globalFeatures, m_globalMedians ,param)) {
			tcout << _T("Error: failed to store features") << endl;
			return false;	
        }  
		if(!filterOutFeatures(m_globalMedians)) {
			tcout << _T("Error: failed to filtering features ") << endl;
			return false;	
		}
		if(!m_pDBMgr->storeFilteredFeatures(m_globalFeatures, m_mediansNZ, m_globalMedians, param)){
			tcout << _T("Error: failed to store filtered features ") << endl;
			return false;	
		}	
	}
    if(!bFirstScan) {  
        tcout << _T("Total number of files: ") << m_nTotalFiles << endl;
        tcout << _T("Total number of functions: ") << m_nTotalFunctions << endl;
        tcout << _T("Total number of regions: ") << m_nTotalRegions << endl;
    }
    return true;
}
bool CCSAssemblyFileMgr :: parseFolder ( LPCTSTR folderPath , const CCSParam & param , bool bFirstScan ) { tcout << _T ( " " ) << folderPath << endl ; CString folderPathStr = folderPath ; m_nTotalFiles = m_nTotalFunctions = m_nTotalRegions = 0 ; WIN32_FIND_DATA fileInfo ; HANDLE hFile = :: FindFirstFile ( folderPathStr + _T ( " \\ " ) , & fileInfo ) ; do { CString fileName = fileInfo . cFileName ; if ( fileName . CompareNoCase ( _T ( " " ) ) == 0 || fileName . CompareNoCase ( _T ( " " ) ) == 0 ) continue ; if ( fileInfo . dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) { CString subFolderPath = folderPathStr + _T ( " \\ " ) + fileName ; if ( ! parseFolder ( ( LPCTSTR ) subFolderPath , param , bFirstScan ) ) return false ; } else { CString ext = fileName . Mid ( fileName . Find ( _T ( " " ) ) + 1 ) ; if ( ext . CompareNoCase ( CS_ASSEMBLYFILE_EXT1 ) == 0 || ext . CompareNoCase ( CS_ASSEMBLYFILE_EXT2 ) == 0 || ext . CompareNoCase ( CS_ASSEMBLYFILE_EXT3 ) == 0 ) { CString assemblyFilePath = folderPathStr + _T ( " \\ " ) + fileName ; CCSAssemblyFile assemblyFile ( assemblyFilePath , this ) ; if ( ! assemblyFile . parseFile ( param , ! bFirstScan ) ) return false ; if ( ! assemblyFile . normalizeCode ( param ) ) return false ; if ( ! assemblyFile . extractFunctions ( m_pDBMgr , param , ! bFirstScan ) ) return false ; if ( ! bFirstScan ) { m_nTotalFunctions += assemblyFile . getNumFunctions ( ) ; tcout << _T ( " " ) << assemblyFile . getNumFunctions ( ) << _T ( " " ) << assemblyFilePath . GetString ( ) << endl ; } if ( ! assemblyFile . extractRegions ( m_pDBMgr , param , false , false , ! bFirstScan , bFirstScan , this ) ) return false ; if ( ! bFirstScan ) { m_nTotalRegions += assemblyFile . getNumRegions ( ) ; tcout << _T ( " " ) << assemblyFile . getNumRegions ( ) << _T ( " " ) << assemblyFilePath . GetString ( ) << endl ; ++ m_nTotalFiles ; } } } } while ( :: FindNextFile ( hFile , & fileInfo ) ) ; if ( bFirstScan ) { if ( ! m_pDBMgr -> storeGlobalFeatures ( m_globalFeatures , m_globalMedians , param ) ) { tcout << _T ( " " ) << endl ; return false ; } if ( ! filterOutFeatures ( m_globalMedians ) ) { tcout << _T ( " " ) << endl ; return false ; } if ( ! m_pDBMgr -> storeFilteredFeatures ( m_globalFeatures , m_mediansNZ , m_globalMedians , param ) ) { tcout << _T ( " " ) << endl ; return false ; } } if ( ! bFirstScan ) { tcout << _T ( " " ) << m_nTotalFiles << endl ; tcout << _T ( " " ) << m_nTotalFunctions << endl ; tcout << _T ( " " ) << m_nTotalRegions << endl ; } return true ; }
Parse the whole input folder, including subfolders
Parse the whole input folder including subfolders
// It is a directory. // It is a file. // Matched extension // parse the assembly file // normalize code // create functions in memory and store into DB. // create regions and store into DB. // assemblyFile will be deallocated. /* if (!m_pDBMgr->preInsertInexact2Comb(m_mediansNZ, m_globalMedians, param) ) { 
			//tcout << _T("Error: failed to pre-insert Inexact2Comb table") << endl;
			//return false;	
        }*/