Changeset 3


Ignore:
Timestamp:
Nov 27, 2021, 11:14:09 AM (3 years ago)
Author:
chronos
Message:
  • Modified: More conversions.
Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore set to
      lib
      DirtyCppToPascal
      DirtyCppToPascal.res
      DirtyCppToPascal.lps
  • trunk/UFormMain.lfm

    r2 r3  
    1616    Anchors = [akTop, akLeft, akRight]
    1717    TabOrder = 0
    18     Text = '/home/chronos/Projekty/DirtyCppToPascal/CapitalAI'
     18    Text = '/home/chronos/Projekty/DirtyCppToPascal/trunk/CapitalAI'
    1919  end
    2020  object ButtonBrowse: TButton
  • trunk/UFormMain.pas

    r2 r3  
    3636    function SpaceAround(Text, What: string; Left: Boolean = True; Right: Boolean = True): string;
    3737    function Replace(Text: string; OldPattern, NewPattern: string): string;
     38    function ReplaceIdent(Text: string; OldPattern, NewPattern: string): string;
    3839    function ReplaceFunction(Text: string; ReturnType: string): string;
    3940    function ReplaceVariable(Text: string; VarType: string): string;
     
    182183    Files := TStringList.Create;
    183184    SearchFiles(Files, DirSrc, nil, nil);
     185
     186    for I := 0 to Files.Count - 1 do begin
     187      MemoLog.Lines.Add(Files[I]);
     188      FileNameSrc := Files[I];
     189      if DirectoryExists(FileNameSrc) then continue;
     190      if FileExists(FileNameSrc) then begin
     191        FileNameDst := DirDst + Copy(FileNameSrc, Length(DirSrc) + 1, MaxInt);
     192        ForceDirectories(ExtractFileDir(FileNameDst));
     193        FileExt := ExtractFileExt(FileNameSrc);
     194        if FileExt = '.cpp' then begin
     195          FileNameDst := ExtractFileNameWithoutExt(FileNameDst) + '.pas';
     196          DeleteFile(FileNameDst);
     197        end;
     198      end;
     199    end;
     200
    184201    for I := 0 to Files.Count - 1 do begin
    185202      MemoLog.Lines.Add(Files[I]);
     
    269286end;
    270287
     288function TFormMain.ReplaceIdent(Text: string; OldPattern, NewPattern: string
     289  ): string;
     290var
     291  I: Integer;
     292  TextBefore: string;
     293  TextAfter: string;
     294begin
     295  Result := '';
     296  while True do begin
     297    I := Pos(OldPattern, Text);
     298    if I > 0 then begin
     299      TextBefore := Copy(Text, 1, I - 1);
     300      TextAfter := Copy(Text, I + Length(OldPattern), Length(Text));
     301      if ((Length(TextBefore) >= 1) and IsAlphaNumeric(TextBefore[Length(TextBefore)]) or
     302       ((Length(Result) >= 1) and IsAlphaNumeric(Result[Length(Result)]) and
     303         (Copy(Result, Length(Result) - Length(NewPattern) + 1, Length(NewPattern)) <> NewPattern)) or
     304       ((Length(TextAfter) >= 1) and IsAlphaNumeric(TextAfter[1])))
     305        then begin
     306          Result := Result + TextBefore + OldPattern;
     307          Delete(Text, 1, Length(TextBefore) + Length(OldPattern));
     308          Continue;
     309        end;
     310      Result := Result + TextBefore + NewPattern;
     311      Delete(Text, 1, Length(TextBefore) + Length(OldPattern));
     312    end else Break;
     313  end;
     314  Result := Result + Text;
     315end;
     316
    271317function TFormMain.ReplaceFunction(Text: string; ReturnType: string): string;
    272318var
     
    421467  Line: string;
    422468const
    423   Types: array[0..8] of string = ('Integer', 'Boolean', 'Cardinal', 'Byte',
    424     'ShortInt',
    425     'Advances', 'TNewGameData', 'TServerCall', 'TClientCall');
     469  Types: array[0..11] of string = ('Integer', 'Boolean', 'Cardinal', 'Byte',
     470    'ShortInt', 'Pointer',
     471    'Advances', 'TNewGameData', 'TServerCall', 'TClientCall', 'MyEVOArea',
     472    'TileFunction');
    426473  PascalKeywords: array[0..1] of string = ('object', 'type');
    427474begin
     
    443490      Line := ReplaceSpecialSymbol(Line, '::', '.');
    444491      Line := Replace(Line, 'switch', 'case');
    445       Line := Replace(Line, 'int', 'Integer');
    446       Line := Replace(Line, 'bool', 'Boolean');
    447       Line := Replace(Line, 'unsigned long', 'Cardinal');
    448       Line := Replace(Line, 'unsigned short', 'ShortInt');
    449       Line := Replace(Line, 'unsigned char', 'Byte');
    450       Line := Replace(Line, 'unsigned Integer', 'Cardinal');
     492      Line := Replace(Line, 'unsigned long ', 'Cardinal ');
     493      Line := Replace(Line, 'unsigned short ', 'Byte ');
     494      Line := Replace(Line, 'unsigned char ', 'Byte ');
     495      Line := Replace(Line, 'unsigned int ', 'Cardinal ');
     496      Line := Replace(Line, 'int ', 'Integer ');
     497      Line := Replace(Line, 'short ', 'ShortInt ');
     498      Line := Replace(Line, 'long ', 'Integer ');
     499      Line := Replace(Line, 'bool ', 'Boolean ');
    451500      Line := Replace(Line, 'void *', 'Pointer ');
    452       Line := Replace(Line, 'std.ofstream', 'TStream');
     501      Line := Replace(Line, 'std.ofstream ', 'TStream ');
     502      Line := Replace(Line, 'std.ostream ', 'TStream ');
    453503      Line := ReplaceOperator(Line, '+=', ':=', '+');
    454504      Line := ReplaceOperator(Line, '-=', ':=', '-');
     
    473523      Line := Replace(Line, 'const ', '');
    474524      Line := Replace(Line, ' const', '');
     525      Line := Replace(Line, 'mutable ', '');
    475526      for J := 0 to Length(Types) - 1 do
    476527        Line := ReplaceFunction(Line, Types[J]);
     
    482533      Line := Replace(Line, '()', '');
    483534      for J := 0 to Length(PascalKeywords) - 1 do
    484         Line := Replace(Line, PascalKeywords[J], 'K' + PascalKeywords[J]);
     535        Line := ReplaceIdent(Line, PascalKeywords[J], 'K' + PascalKeywords[J]);
    485536      //Line := SpaceAround(Line, ':=');
    486537      //Line := SpaceAround(Line, '+');
Note: See TracChangeset for help on using the changeset viewer.