BinClone/CloneSearcherLib/CSAssemblyFileMgr.cpp
//
// Create a temporary assembly file based on the given content.
//
Create a temporary assembly file based on the given content.
Create a temporary assembly file based on the given content .
bool CCSAssemblyFileMgr::createAssemblyFile(LPCTSTR content, CString& newAssemblyFilePath)
{
    CString contentStr = content;
    if (contentStr.IsEmpty()) {
        tcout << _T("CCSAssemblyFileMgr: empty content. Failed to create assembly file.") << endl;
        ASSERT(false);
        return false;
    }
    static int tempFileID = 0;      
    CString tempFileIDstr, tempFolderPath;
    CBFStrHelper::intToStr(tempFileID++, tempFileIDstr);
    if (!CBFFileHelper::getTempFolderPath(tempFolderPath)) {
        tcout << _T("CCSAssemblyFileMgr: failed to get temporary folder path.") << endl;
        ASSERT(false);
        return false;
    }
    CString tempAssemblyFilePath = tempFolderPath + _T("\\_");
    tempAssemblyFilePath += CS_ASSMEBLYFILE_TMP_FILENAME;
    tempAssemblyFilePath += tempFileIDstr;
    tempAssemblyFilePath += _T(".");
    tempAssemblyFilePath += CS_ASSEMBLYFILE_EXT3;
    if (contentStr.Find(_T("proc near")) == -1 && contentStr.Find(_T("proc far")) == -1)
        contentStr = _T("sub_fake proc near\n") + contentStr + _T("\nsub_fake endp\n");
    try {
        CStdioFile newAssemblyFile;
        if (!newAssemblyFile.Open(tempAssemblyFilePath, CFile::modeCreate | CFile::modeWrite)) {
            tcout << _T("CCDAssemblyFileMgr: Failed to create new assembly file ") << tempAssemblyFilePath << endl;
            return false;
        }        
        newAssemblyFile.WriteString(contentStr);
        newAssemblyFile.Close();
    }
    catch (CFileException&) {
        tcout << _T("Failed to create new assembly file: ") << tempAssemblyFilePath << endl;
        ASSERT(false);
        CBFFileHelper::removeFile((LPCTSTR) tempAssemblyFilePath);
        return false;
    }
    newAssemblyFilePath = tempAssemblyFilePath;
    return true;
}
bool CCSAssemblyFileMgr :: createAssemblyFile ( LPCTSTR content , CString & newAssemblyFilePath ) { CString contentStr = content ; if ( contentStr . IsEmpty ( ) ) { tcout << _T ( " " ) << endl ; ASSERT ( false ) ; return false ; } static int tempFileID = 0 ; CString tempFileIDstr , tempFolderPath ; CBFStrHelper :: intToStr ( tempFileID ++ , tempFileIDstr ) ; if ( ! CBFFileHelper :: getTempFolderPath ( tempFolderPath ) ) { tcout << _T ( " " ) << endl ; ASSERT ( false ) ; return false ; } CString tempAssemblyFilePath = tempFolderPath + _T ( " \\ " ) ; tempAssemblyFilePath += CS_ASSMEBLYFILE_TMP_FILENAME ; tempAssemblyFilePath += tempFileIDstr ; tempAssemblyFilePath += _T ( " " ) ; tempAssemblyFilePath += CS_ASSEMBLYFILE_EXT3 ; if ( contentStr . Find ( _T ( " " ) ) == -1 && contentStr . Find ( _T ( " " ) ) == -1 ) contentStr = _T ( " \n " ) + contentStr + _T ( " \n \n " ) ; try { CStdioFile newAssemblyFile ; if ( ! newAssemblyFile . Open ( tempAssemblyFilePath , CFile :: modeCreate | CFile :: modeWrite ) ) { tcout << _T ( " " ) << tempAssemblyFilePath << endl ; return false ; } newAssemblyFile . WriteString ( contentStr ) ; newAssemblyFile . Close ( ) ; } catch ( CFileException & ) { tcout << _T ( " " ) << tempAssemblyFilePath << endl ; ASSERT ( false ) ; CBFFileHelper :: removeFile ( ( LPCTSTR ) tempAssemblyFilePath ) ; return false ; } newAssemblyFilePath = tempAssemblyFilePath ; return true ; }
Create a temporary assembly file based on the given content.
Create a temporary assembly file based on the given content .
// create a new filename     // ensure every file has a different name // make sure the content contains some at least one function. // create a new file