WKT ParserTop

This is a simple parser for WKT (Well Known Text) files used for geographic projection descriptions. WKT files have a tree-like structure, similar to XML files.

Main parsing routineTop

The main parsing routine is very simple and fast:

procedure TMDWktNode.Parse(Stream: TStream);
var
  ch: Char;
  val: string;
  opened: boolean;
  lastcomma: integer;
  commas: integer;
  child: TMDWktNode;
  quoteopen: boolean;
begin
  opened := false;
  quoteopen := false;
  commas := 0;
  lastcomma := Stream.Position;
  val := '';
  while Stream.Read(ch, 1) = 1 do begin
    case ch of
      DEL_OPEN:
        begin
          if opened then begin
            AddValue(val);
            val := '';
            if commas <> 0 then begin
              // New child
              if fValues.Count <> 0 then
                // This is a sub-node not a value
                fValues.Delete(fValues.Count-1);
              Stream.Seek(lastcomma, soFromBeginning);
              child := TMDWktNode.Create;
              Add(child);
              child.Parse(Stream);
            end;
          end else begin
            // Name, Value
            opened := true;
            fKey := val;
            val := '';
          end;
        end;
      SEPARATOR:
        begin
          lastcomma := Stream.Position;
          AddValue(val);
          val := '';
          Inc(commas);
        end;
      DEL_CLOSE:
        begin
          // End
          AddValue(val);
          val := '';
          Break;
        end;
      ' ', #9, #10, #13:
        Continue;
      QUOTE:
        begin
          quoteopen := not quoteopen;
          if quoteopen then begin
            val := ch;
            while (Stream.Read(ch, 1) = 1) do begin
              val := val + ch;
              if ch = QUOTE then begin
                quoteopen := false;
                Break;
              end;
            end;
          end;
        end;
      else
        val := val + ch;
    end;
  end;
end;

DownloadTop