www.pudn.com > GOOGLE.rar > 2.pas, change:2002-04-09,size:2263b


 
fuction IsDelimiter(aCh : Char; const aDelims : string) : boolean; 
var 
  i : integer; 
begin 
  Result := true; 
  if (aCh > ' ') then  
    begin 
      for i := 1 to length(aDelims) do 
        if (aDellims[i] = aCh then Exit; 
      Result := false; 
    end; 
end; 
 
procedure AAParseWords(aText : TStream; const aDelims : string; aAction : TaaWordParseAction); 
const 
  BufSize = 16 * 1024; 
var 
  State   : (ScanWord , ScanOther); 
  Buffer  : PChar; 
  BufPos  : PChar; 
  BufLeft : integer; 
  Ch      : char; 
  StopNow : boolean; 
  CharQ   : TaaCharQueue; 
begin 
  //perpare for the memory allocations 
  Buffer := nil; 
  CharQ := nil; 
  try 
    //create a character queue with which to bulid words 
    CharQ := TaaCharQueue.Create; 
    //allocate a buffer to hold data from the stream 
    GetMem(Buffer , BufferSize); 
    //force the stream to be read first time through 
    BufLeft := 1; 
    BufPos := Buffer; 
    //we'll start in the scanning delimiter state 
    State := ScanOther; 
    StopNow := false; 
    //continue until there is no more data in the stream 
    while (not StopNow) and (BufLeft <> 0) do 
      begin 
        //advance the buffer variables (reading more data if needed) 
        dec(BufLeft); 
        if (BufLeft <> 0) then 
          inc(BufPos) 
        else 
          begin 
            BufLeft := aText.Read(Buffer^ , BufSize); 
            BufPos := Buffer; 
          end; 
        //get the next character 
        Ch := BufPos^; 
        //process the character according to the state 
        case State of 
          ScanWord : 
            begin 
              if IsDelimiter(Ch , aDelims) then  
                begin 
                  aAction(CharQ.AsString , StopNow); 
                  State := ScanOther; 
                end 
              else 
                CharQ.Append(Ch); 
            end; 
          ScanOther : 
            begin 
              if not IsDelimiter(Ch , aDelims) then 
                begin 
                  CharQ.Clear; 
                  CharQ.Append(Ch); 
                  State := ScanWord; 
                end; 
            end; 
        end; 
      end; 
  finally 
    if (Buffer <> nil) then 
      FreeMem(Buffer); 
    CharQ.Free; 
  end; 
end;