// Scan directory. This code get's called on // the spawned thread. It uses recursion to traverse // sub-directories when necessary. BOOL ScanDirectory( LPCTSTR pszDir ) { ClsString strPath, str; ClsFindFile find; ClsStdioFile file; // Are we still running? if ( m_Thread.Wait() != ClsWorkerThread::wtRunning ) return FALSE; // Do we need to scan the sub-directories? if ( m_bRecur ) { // Assign path. strPath = pszDir; // Add a backslash if not yet present. AddBackslash( strPath ); // Add filter. strPath += _T( "*.*" ); // Start the scan. if ( find.FindFile( strPath )) { // Continue until we are done... do { // Still running? if ( m_Thread.Wait() != ClsWorkerThread::wtRunning ) { // Close handle and exit. find.Close(); return FALSE; } // Is this a directory? if ( ! find.IsDots() && find.IsDirectory()) { // Get the full path of the directory. find.GetFilePath( strPath ); // Scan this directory. Recursion hard at work... if ( ScanDirectory( strPath ) == FALSE ) { // Prolly a shutdown event during // recursion. Close handle and return FALSE. find.Close(); return FALSE; } } } while ( find.FindNextFile()); // Close the handle. find.Close(); } } // Get the index of the first delimted part of the string. int nIndex = m_StrType.GetDelimitedPart( _T( ';' ), 0, str ); // Valid? if ( nIndex > 0 ) { // Scan using all filters. do { // Assign path. strPath = pszDir; // Add a backslash if necessary. AddBackslash( strPath ); // Add the filter string. strPath += str; // Scan the directory. if ( find.FindFile( strPath )) { // Continue until we are done. do { // Still running? if ( m_Thread.Wait() != ClsWorkerThread::wtRunning ) { // Close handle and exit. find.Close(); return FALSE; } // We only show the correct files. if ( ! find.IsDots() && ! find.IsDirectory()) { // Get the file name. ClsString strName; if ( find.GetFilePath( strName )) { // Show the file we are processing. Simply do a PostMessage() // to the dialog which handles the GUI stuff. It will also delete // the ClsString we allocate here. ClsString *pStr = new ClsString( strName ); PostMessage( THREAD_PROCESSING, 0, ( LPARAM )pStr ); // Gracefully handle IO errors. try { // Read the file line-by-line... TCHAR szBuf[ 4096 ]; file.Open( strName, _T( "r" )); m_nFiles++; int nLine = 0; while ( file.GetS( szBuf, 4096 )) { // Still running? if ( m_Thread.Wait() != ClsWorkerThread::wtRunning ) { // Close the file and the // find-file handles and exit. file.Close(); find.Close(); return FALSE; } // Get the base pointer of the buffer. TCHAR *ptr = szBuf; int nIdx; // Increase line number. nLine++; // Find all occurences on this line. BOOL bAnyFound = FALSE; while (( _tcslen( ptr )) && (( nIdx = m_BoyerMoore.FindForward( ptr, ( int )_tcslen( ptr ))) >= 0 )) { // Are we at the first found entry? if ( bAnyFound == FALSE ) { // Strip white spaces from the end of the input buffer. while ( _istspace( szBuf[ _tcslen( szBuf ) - 1 ] )) szBuf[ _tcslen( szBuf ) - 1 ] = 0; // Increase lines-found counter. m_nLines++; } // Found a least one. bAnyFound = TRUE; // Convert the line for print and post it to the // main thread for the GUI stuff. ClsString *pStr = ConvertForPrint( szBuf ); PostMessage( THREAD_FOUND, nLine, ( LPARAM )pStr ); // Increase search pointer so we can search the rest of the line. ptr += nIdx + 1; } } // Close file. file.Close(); } catch( ClsException& ) { // Continue on IO errors... continue; } } } } while ( find.FindNextFile()); // Close the handle. find.Close(); } // Next filter... } while (( nIndex = m_StrType.GetDelimitedPart( _T( ';' ), nIndex, str )) > 0 ); } return TRUE; }
// Read keywords from a file. BOOL KeywordEdit::ReadKeywords( LPCTSTR pszFileName ) { // Get item position and data. int nPos = m_List.GetCurSel(); _ASSERT( nPos != LB_ERR ); LPKEYWORDS pKey = ( LPKEYWORDS )m_List.GetItemData( nPos ); ClsStdioFile file; // Valid node? if ( pKey != ( LPKEYWORDS )LB_ERR ) { try { // Open the file. file.Open( pszFileName, _T( "r" )); // Read the file... TCHAR szBuffer[ 1024 ], *pszPtr, *pszCopy; while (( pszPtr = file.GetS( szBuffer, 1024 )) != NULL ) { // Skip leading blanks. while ( _istspace( *pszPtr )) pszPtr++; // Also remove trailing // blanks. RemoveTrailingBlanks( pszPtr ); // Any text left? if ( _tcslen( pszPtr )) { // Create a copy of the // input string. if (( pszCopy = StringArrayCopy( pKey->lpaKeywords, pszPtr )) != NULL ) { // Add it to the array. if ( ::ArrayAdd( pKey->lpaKeywords, &pszCopy, 1 ) == FALSE ) { // Error... MessageBox( ClsString( MAKEINTRESOURCE( IDS_NO_MEMORY )), ClsGetApp()->GetAppTitle(), MB_ICONERROR | MB_OK ); break; } } else { // Error... MessageBox( ClsString( MAKEINTRESOURCE( IDS_NO_MEMORY )), ClsGetApp()->GetAppTitle(), MB_ICONERROR | MB_OK ); break; } } } // Close the input file. file.Close(); } catch( ClsException& ex ) { UNREFERENCED_PARAMETER( ex ); return FALSE; } } // Sort the array. ::ArraySort( pKey->lpaKeywords, ( COMPFUNC )CompareKeywords ); return TRUE; }