Einzelnen Beitrag anzeigen

Strict

Registriert seit: 24. Mär 2020
47 Beiträge
 
#1

Strings aus Pascal-Datei filtern und exportieren

  Alt 16. Apr 2020, 18:30
Ich begebe mich hier mit meinem Beispiel höchstwahrscheinlich auf dünnes Eis, was das korrekte Verwenden und Auswerten von Texten ist.
Ein paar Pascal-Dateien muss ich parsen und Stings rausfiltern und exportieren.

Mein erster Versuch.
[DELPHI]
Ich begebe mich mal daran einen ganz schlechten Parser zu bauen, der Strings rausfiltert und stelle den dann hier vor. Könnte aber etwas dauern.

Das scheitert bestimmt kläglich. Ob das mit RegEx einfacher ist, Strings rauszufiltern?

Delphi-Quellcode:
const
 BaseFile: string = 'Language.EN.pas';
var
 i, j, LineIndexAllocation, LineIndexEndOfAssignment, PosStart, PosEnd: Integer;
 LLines, LLinesExport: TStringList;
 s, LUnitName, LFileToParse, LFileExport, LLine, LComponentOrVariable, LLineContentFilterted, msgcommentline, msgstrline, msgid, msgstr: string;
 BAllocationFound, BEndOfAssignmentFound: Boolean;
 Arr: TArray<string>;

 procedure AddDataToPOFile(Str: string);
 begin
  Str := Trim(Str);

  if IsHeader or (Pos('''', Str) > 0) then
   begin
    msgcommentline := '#. unit: ' + LUnitName + ', ' + LComponentOrVariable;
    msgstrline := '# ' + BaseFile + ':' + (LineIndexAllocation + 1).ToString;
    msgid := Str;
    msgstr := Str;

    LLinesExport.Add(msgcommentline);
    LLinesExport.Add(msgstrline);
    LLinesExport.Add('msgid "' + msgid + '"');
    LLinesExport.Add('msgstr ""');
    LLinesExport.Add('');
   end;
 end;

begin
 LLines := TStringList.Create;
 try
  LFileToParse := Path + BaseFile;
  if FileExists(LFileToParse) then
   begin
    LFileExport := 'Lang-export.po';

    LLines.LoadFromFile(LFileToParse);
    if LLines.Count > 0 then
     begin
      LLinesExport := TStringList.Create;
      try
       LLinesExport.Add('msgid ""');
       LLinesExport.Add('msgstr ""');
       LLinesExport.Add('"POT-Creation-Date: \n"');
       LLinesExport.Add('"PO-Revision-Date: \n"');
       LLinesExport.Add('"Last-Translator: \n"');
       LLinesExport.Add('"MIME-Version: 1.0\n"');
       LLinesExport.Add('"Content-Type: text/plain; charset=iso-8859-1\n"');
       LLinesExport.Add('"Content-Transfer-Encoding: 8bit\n"');
       LLinesExport.Add('"Language: en\n"');
       LLinesExport.Add('"X-Generator: NAME\n"');
       LLinesExport.Add('');
       LLinesExport.Add('# BASE FILE for translating NAME into other languages');
       LLinesExport.Add('# Copyright (C) 1234 NAME');
       LLinesExport.Add('# This file is distributed under the same license as NAME');
       LLinesExport.Add('# NAME, 1234.');

       LLinesExport.Add('# header end');
       LLinesExport.Add('');

       BAllocationFound := False;
       BEndOfAssignmentFound := False;
       LineIndexAllocation := -1;
       LineIndexEndOfAssignment := -1;

       for i := 0 to LLines.Count - 1 do
        begin
         LLine := LLines.Strings[i].Trim;

         if LLine = 'then
          Continue;

         if LLine.StartsWith('unit ') then
          begin
           LUnitName := Copy(LLine, Pos(' ', LLine), Length(LLine));
           LUnitName := Trim(Copy(LUnitName, 1, Length(LUnitName) - 1));
           Continue;
          end;

         // ('German', 'English')
         if LLine.Contains('(''') and LLine.Contains(',') and LLine.Contains(''')') then
          begin
           LComponentOrVariable := Trim(Copy(LLine, 1, Pos(':', LLine) - 1));

           LLine := Copy(LLine, Pos('(', LLine) + 1, Length(LLine));
           if LLine.EndsWith(');') then
            LLine := Copy(LLine, 1, Length(LLine) - 2);
           Arr := LLine.Split([',']);

           LineIndexAllocation := i;
           for s in Arr do
            AddDataToPOFile(s);

           Continue;
          end;

         if LLine.Contains(':=') then
          begin
           LComponentOrVariable := Trim(Copy(LLine, 1, Pos(':=', LLine) - 1));
           BAllocationFound := True;
           LineIndexAllocation := i;
          end;

         if LLine.Contains(';') then
          begin
           BEndOfAssignmentFound := True;
           LineIndexEndOfAssignment := i;
          end;

         if BAllocationFound and BEndOfAssignmentFound then
          begin
           s := '';
           for j := LineIndexAllocation to LineIndexEndOfAssignment do
            s := s + ' ' + LLines.Strings[j].Trim;

           PosStart := Pos(':=', s) + 2;
           PosEnd := Pos(';', s);

           AddDataToPOFile(Copy(s, PosStart, PosEnd - PosStart));

           BAllocationFound := False;
           BEndOfAssignmentFound := False;
          end;
        end;

       if LLinesExport.Count > 0 then
        begin
         LLinesExport.SaveToFile(LFileExport);
        end;
      finally
       LLinesExport.Free;
      end;
     end;
   end;
 finally
  LLines.Free;
 end;
Ihr könnt zum Testen gerne diese Datei verwenden.
Delphi-Quellcode:
unit Language.EN;

interface

uses
 Winapi.Windows, System.SysUtils;

type
 TDummyParent = record
 private const
  Languages: array [0 .. 2] of string = ('German', 'English');
 public
  class var a, b, c, d: string;
  class procedure DummyProcedure; static;
 end;

implementation

class procedure TDummyParent.DummyProcedure;
begin
 a := 'string-a';
 b := 'string-b';

 c := sLineBreak + 'string-d';

 d :=
  '1 2 3 ' +
  '4 5 6 ' +
  '7 8 9';
end;
Wie kann man das optimieren, sodass es aber noch einfach bleibt und kein Langzeitprojekt wird?

Geändert von Strict (16. Apr 2020 um 21:09 Uhr)
  Mit Zitat antworten Zitat