bool CISpellDict::Load ( const char * szFilename ) { if ( !szFilename ) return false; m_dEntries.Reset (); m_dEntries.Reserve ( 131072 ); FILE * pFile = fopen ( szFilename, "rt" ); if ( !pFile ) return false; char szWordBuffer [MAX_STR_LENGTH]; while ( !feof ( pFile ) ) { char * szResult = fgets ( szWordBuffer, MAX_STR_LENGTH, pFile ); if ( !szResult ) break; int iPos = strlen ( szWordBuffer ) - 1; while ( iPos>=0 && isspace ( (unsigned char)szWordBuffer[iPos] ) ) szWordBuffer [iPos--] = '\0'; CISpellDictWord Word; char * szPosition = strchr ( szWordBuffer, '/' ); if ( !szPosition ) { szPosition = szWordBuffer; while ( *szPosition && !isspace ( (unsigned char)*szPosition ) ) ++szPosition; *szPosition = '\0'; Word.m_sWord = szWordBuffer; } else { *szPosition = '\0'; Word.m_sWord = szWordBuffer; ++szPosition; char * szFlags = szPosition; while ( *szPosition && !isspace ( (unsigned char)*szPosition ) ) ++szPosition; *szPosition = '\0'; Word.m_sFlags = szFlags; } m_dEntries.Add ( Word ); } fclose ( pFile ); return true; }
bool CISpellAffix::Load ( const char * szFilename ) { if ( !szFilename ) return false; m_dRules.Reset (); memset ( m_dCharset, 0, sizeof ( m_dCharset ) ); m_bFirstCaseConv = true; m_bUseLowerCaser = false; m_bUseDictConversion = false; m_LowerCaser.Reset (); FILE * pFile = fopen ( szFilename, "rt" ); if ( !pFile ) return false; bool bResult = false; AffixFormat_e eFormat = DetectFormat ( pFile ); if ( eFormat==AFFIX_FORMAT_UNKNOWN ) printf ( "Failed to detect affix file format\n" ); else { fseek ( pFile, SEEK_SET, 0 ); printf ( "Using %s affix file format\n", AffixFormatName[eFormat] ); switch ( eFormat ) { case AFFIX_FORMAT_MYSPELL: bResult = LoadMySpell ( pFile ); break; case AFFIX_FORMAT_ISPELL: bResult = LoadISpell ( pFile ); break; case AFFIX_FORMAT_UNKNOWN: break; } } fclose ( pFile ); bool bHaveCrossPrefix = false; for ( int i = 0; i < m_dRules.GetLength () && !bHaveCrossPrefix; i++ ) if ( m_dRules[i].IsPrefix() && m_dRules[i].IsCrossProduct() ) bHaveCrossPrefix = true; bool bHaveCrossSuffix = false; for ( int i = 0; i < m_dRules.GetLength () && !bHaveCrossSuffix; i++ ) if ( !m_dRules[i].IsPrefix() && m_dRules[i].IsCrossProduct() ) bHaveCrossSuffix = true; m_bCheckCrosses = bHaveCrossPrefix && bHaveCrossSuffix; return bResult; }
bool CSphConfigParser::TryToExec ( char * pBuffer, char * pEnd, const char * szFilename, CSphVector<char> & dResult ) { int dPipe[2] = { -1, -1 }; if ( pipe ( dPipe ) ) { snprintf ( m_sError, sizeof ( m_sError ), "pipe() failed (error=%s)", strerror(errno) ); return false; } pBuffer = trim ( pBuffer ); int iRead = dPipe [0]; int iWrite = dPipe [1]; signal ( SIGCHLD, sigchld ); int iChild = fork(); if ( iChild == 0 ) { close ( iRead ); close ( STDOUT_FILENO ); dup2 ( iWrite, STDOUT_FILENO ); char * pPtr = pBuffer; char * pArgs = NULL; while ( *pPtr ) { if ( sphIsSpace ( *pPtr ) ) { *pPtr = '\0'; pArgs = trim ( pPtr+1 ); break; } pPtr++; } if ( pArgs ) execl ( pBuffer, pBuffer, pArgs, szFilename, NULL ); else execl ( pBuffer, pBuffer, szFilename, NULL ); exit ( 1 ); } else if ( iChild == -1 ) { snprintf ( m_sError, sizeof ( m_sError ), "fork failed (error=%s)", strerror(errno) ); return false; } close ( iWrite ); int iBytesRead, iTotalRead = 0; const int BUFFER_SIZE = 65536; dResult.Reset (); do { dResult.Resize ( iTotalRead + BUFFER_SIZE ); iBytesRead = read ( iRead, (void*)&(dResult [iTotalRead]), BUFFER_SIZE ); iTotalRead += iBytesRead; } while ( iBytesRead > 0 ); int iStatus; wait ( &iStatus ); iStatus = (signed char) WEXITSTATUS (iStatus); if ( iStatus ) { snprintf ( m_sError, sizeof ( m_sError ), "error executing '%s'", pBuffer ); return false; } if ( iBytesRead < 0 ) { snprintf ( m_sError, sizeof ( m_sError ), "pipe read error (error=%s)", strerror(errno) ); return false; } dResult.Resize ( iTotalRead + 1 ); dResult [iTotalRead] = '\0'; return true; }