Einzelnen Beitrag anzeigen

Benutzerbild von KodeZwerg
KodeZwerg

Registriert seit: 1. Feb 2018
3.690 Beiträge
 
Delphi 11 Alexandria
 
#9

AW: Parsen von Source Code Files

  Alt 15. Mär 2022, 10:20
Habe mal auf die schnelle was getippst wie ich es machen würde. Vielleicht hilft es?!
Delphi-Quellcode:
program FileWriter;

{$APPTYPE CONSOLE}

{.$R *.res}

uses
  System.SysUtils, System.Classes, System.IOUtils;


function MyParse(const AInFile, AOutFile: string): Boolean;
var
  LData: TBytes;
  LOutput: TStringList;
  i: Int64;
  s: string;
begin
  Result := False;
  s := '';
  if FileExists(AInFile) then
    begin
      LOutput := TStringList.Create;
      try
        LData := TFile.ReadAllBytes(AInFile); // Lade Datei in den Speicher
        for i := Low(LData) to High(LData) do
          begin
            if ((LData[i] <> $0D) and (LData[i] <> $0A) and (LData[i] <> $3B) and (LData[i] <> $20)) then // Zeilenumbrüche, Leerzeichen und Semikolon rausfiltern
              s := s + Chr(LData[i]);
            if (LData[i] = $3B) then // Semikolon entdeckt, erstelle fertige Zeile
              begin
                s := s + Chr(LData[i]);
                LOutput.Add(s);
                s := '';
              end;
          end;
        if LOutput.Count > 0 then
          begin
            LOutput.SaveToFile(AOutFile);
            Result := True;
          end;
      finally
        LOutput.Free;
      end;
    end;
end;

var
  LPath: string;
begin
  try
    Writeln('Parser starting...');
    LPath := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0)));
    MyParse(LPath + 'TestFile.txt', LPath + 'TestOut.txt');
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

//edit

als beispiel InFile:
Delphi-Quellcode:
sky130_fd_sc_hd__a21oi_1 _25041_ ( .A1(_15472_)
.A2(_16059_)
.B1(_15410_)
.Y(_16230_)
);

 sky130_fd_sc_hd__a21o_1 _25042_ ( .A1(_15433_)
.A2(_15309_)
.B1(_15394_)
.X(_16231_)
);sky130_fd_sc_hd__nor3_1 _29993_ ( .A(_05384_), .B(_05386_), .C(_05388_), .Y(_05389_) );
  
sky130_fd_sc_hd__a211oi_1 _29994_ ( .A1(_05371_), .A2(_05377_), .B1(_05382_), .C1(_05389_), .Y(_05390_) );sky130_fd_sc_hd__nor3_1 _29993_ (.A(_05384_), .B(_05386_),.C(_05388_),.Y(_05389_) );
sky130_fd_sc_hd__a211oi_1 _29994_ (.A1(_05371_),.A2(_05377_),.B1(_05382_),.C1(_05389_),.Y(_05390_));

sky130_fd_sc_hd__a211oi_1 _29994_ ( _05371 , _05377_, _05382_, _05389_ , _05390_ );
kommt folgendes raus:
Delphi-Quellcode:
sky130_fd_sc_hd__a21oi_1_25041_(.A1(_15472_).A2(_16059_).B1(_15410_).Y(_16230_));
sky130_fd_sc_hd__a21o_1_25042_(.A1(_15433_).A2(_15309_).B1(_15394_).X(_16231_));
sky130_fd_sc_hd__nor3_1_29993_(.A(_05384_),.B(_05386_),.C(_05388_),.Y(_05389_));
sky130_fd_sc_hd__a211oi_1_29994_(.A1(_05371_),.A2(_05377_),.B1(_05382_),.C1(_05389_),.Y(_05390_));
sky130_fd_sc_hd__nor3_1_29993_(.A(_05384_),.B(_05386_),.C(_05388_),.Y(_05389_));
sky130_fd_sc_hd__a211oi_1_29994_(.A1(_05371_),.A2(_05377_),.B1(_05382_),.C1(_05389_),.Y(_05390_));
sky130_fd_sc_hd__a211oi_1_29994_(_05371,_05377_,_05382_,_05389_,_05390_);
Gruß vom KodeZwerg

Geändert von KodeZwerg (15. Mär 2022 um 11:45 Uhr)
  Mit Zitat antworten Zitat