2008-03-25 08:24:19 +00:00
|
|
|
{
|
|
|
|
ObjCParserUtils.pas
|
|
|
|
Copyright (C) 2008 Dmitry 'Skalogryz' Boyarintsev
|
2008-04-07 14:06:35 +00:00
|
|
|
converting obj-c header to pascal (delphi compatible) unit
|
2008-03-25 08:24:19 +00:00
|
|
|
}
|
|
|
|
|
2008-04-07 14:06:35 +00:00
|
|
|
unit ObjCParserUtils;
|
2008-03-25 08:24:19 +00:00
|
|
|
|
|
|
|
interface
|
2008-04-07 14:06:35 +00:00
|
|
|
{$ifdef fpc}{$mode delphi}{$H+}{$endif}
|
2008-03-25 08:24:19 +00:00
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils, ObjCParserTypes;
|
2008-04-07 14:06:35 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
{ TConvertSettings }
|
|
|
|
//todo: hash table
|
|
|
|
TReplace = class(TObject)
|
|
|
|
Src : AnsiString;
|
|
|
|
Dst : AnsiString;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TReplaceItem = class(TObject)
|
|
|
|
ReplaceStr : AnsiString;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TReplaceList = class(TObject)
|
|
|
|
private
|
|
|
|
fItems : TStringList;
|
|
|
|
protected
|
|
|
|
function GetReplace(const ARepl: AnsiString): AnsiString;
|
|
|
|
procedure SetReplace(const ARepl, AValue: AnsiString);
|
|
|
|
|
|
|
|
function GetCaseSense: Boolean;
|
|
|
|
procedure SetCaseSense(AValue: Boolean);
|
|
|
|
public
|
|
|
|
constructor Create;
|
|
|
|
destructor Destroy; override;
|
|
|
|
property Replace[const s: AnsiString]: AnsiString read GetReplace write SetReplace; default;
|
|
|
|
property CaseSensetive: Boolean read GetCaseSense write SetCaseSense;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TConvertSettings = class(TObject)
|
|
|
|
public
|
|
|
|
IgnoreIncludes : TStringList;
|
|
|
|
DefineReplace : TReplaceList;
|
|
|
|
TypeDefReplace : TReplaceList; // replaces for C types
|
2008-04-08 09:22:54 +00:00
|
|
|
|
|
|
|
ConvertPrefix : TStringList;
|
2008-04-07 14:06:35 +00:00
|
|
|
constructor Create;
|
|
|
|
destructor Destroy; override;
|
|
|
|
end;
|
|
|
|
|
|
|
|
var
|
|
|
|
ConvertSettings : TConvertSettings;
|
|
|
|
|
2008-03-25 08:24:19 +00:00
|
|
|
procedure WriteOutIncludeFile(hdr: TObjCHeader; st: TStrings);
|
2008-04-07 14:06:35 +00:00
|
|
|
procedure WriteOutMainFramework(hdr: TObjCHeader; st: TStrings);
|
2008-03-25 08:24:19 +00:00
|
|
|
|
2008-04-01 06:29:04 +00:00
|
|
|
function ObjCToDelphiType(const objcType: AnsiString; isPointer: Boolean): AnsiString;
|
2008-03-25 08:24:19 +00:00
|
|
|
|
|
|
|
function StrFromFile(const FileName: AnsiString): AnsiString;
|
|
|
|
|
|
|
|
function IsMethodConstructor(cl: TClassDef; m: TClassMethodDef): Boolean;
|
|
|
|
function GetMethodStr(cl: TClassDef; m: TClassMethodDef; ForImplementation: Boolean): AnsiString;
|
|
|
|
function GetProcFuncHead(const FuncName, OfClass, Params, ResType: AnsiString; const FuncDest: AnsiString = ''): AnsiString;
|
|
|
|
function GetMethodParams(const m: TClassMethodDef): AnsiString;
|
|
|
|
function GetMethodResultType(const m: TClassMethodDef): AnsiString;
|
2008-04-01 06:29:04 +00:00
|
|
|
function IsPascalReserved(const s: AnsiString): Boolean;
|
2008-03-25 08:24:19 +00:00
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2008-04-01 06:29:04 +00:00
|
|
|
function IsPascalReserved(const s: AnsiString): Boolean;
|
|
|
|
var
|
|
|
|
ls : AnsiString;
|
|
|
|
begin
|
|
|
|
//todo: a hash table should be used?
|
|
|
|
Result := true;
|
|
|
|
if s = '' then Exit;
|
|
|
|
ls := AnsiLowerCase(s);
|
|
|
|
case ls[1] of
|
|
|
|
'a': Result := (ls = 'absolute') or (ls = 'abstract') or (ls = 'and') or (ls = 'array') or (ls = 'as') or (ls= 'asm') or (ls = 'assembler');
|
|
|
|
'b': Result := (ls = 'begin') or (ls = 'break');
|
|
|
|
'c': Result := (ls = 'cdecl') or (ls = 'class') or (ls = 'const') or (ls = 'constructor') or (ls = 'continue') or (ls = 'cppclass');
|
|
|
|
'd': Result := (ls = 'deprecated') or (ls = 'destructor') or (ls = 'div') or (ls = 'do') or (ls = 'downto');
|
|
|
|
'e': Result := (ls = 'else') or (ls = 'end') or (ls = 'except') or (ls = 'exit') or (ls = 'export') or (ls = 'exports') or (ls = 'external');
|
|
|
|
'f': Result := (ls = 'fail') or (ls = 'false') or (ls = 'far') or (ls = 'file') or (ls = 'finally') or (ls = 'for') or (ls = 'forward') or (ls = 'function');
|
|
|
|
'g': Result := (ls = 'goto');
|
|
|
|
'i':
|
|
|
|
Result := (ls = 'if') or (ls = 'implementation') or (ls = 'in') or (ls = 'index') or (ls = 'inherited') or (ls = 'initialization') or (ls = 'inline')
|
|
|
|
or (ls = 'interface') or (ls = 'interrupt') or (ls = 'is');
|
|
|
|
'l': Result := (ls = 'label') or (ls = 'library');
|
|
|
|
'm': Result := (ls = 'mod');
|
|
|
|
'n': Result := {(ls = 'name') or} (ls = 'near') or (ls = 'nil') or (ls = 'not');
|
|
|
|
'o': Result := (ls = 'object') or (ls = 'of') or (ls = 'on') or (ls = 'operator') or (ls = 'or') or (ls = 'otherwise');
|
|
|
|
'p':
|
|
|
|
Result := (ls = 'packed') or (ls = 'popstack') or (ls = 'private') or (ls = 'procedure') or (ls = 'program') or (ls = 'property')
|
|
|
|
or (ls = 'protected') or (ls = 'public');
|
|
|
|
'r': Result := (ls = 'raise') or (ls = 'record') or (ls = 'reintroduce') or (ls = 'repeat');
|
|
|
|
's': Result := (ls = 'self') or (ls = 'set') or (ls = 'shl') or (ls = 'shr') or (ls = 'stdcall') or (ls = 'string');
|
|
|
|
't': Result := (ls = 'then') or (ls = 'to') or (ls = 'true') or (ls = 'try') or (ls = 'type');
|
|
|
|
'u': Result := (ls = 'unimplemented') or (ls = 'unit') or (ls = 'until') or (ls = 'uses');
|
|
|
|
'v': Result := (ls = 'var') or (ls = 'virtual');
|
|
|
|
'w': Result := (ls = 'while') or (ls = 'with');
|
|
|
|
'x': Result := (ls = 'xor');
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2008-03-25 08:24:19 +00:00
|
|
|
function GetMethodResultType(const m: TClassMethodDef): AnsiString;
|
2008-04-01 06:29:04 +00:00
|
|
|
var
|
|
|
|
res : TObjCResultTypeDef;
|
2008-03-25 08:24:19 +00:00
|
|
|
begin
|
2008-04-01 06:29:04 +00:00
|
|
|
res := m.GetResultType;
|
|
|
|
if not Assigned(res) then Result := ''
|
|
|
|
else Result := ObjCToDelphiType(m.GetResultType._Name, m.GetResultType._IsPointer);
|
2008-03-25 08:24:19 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
function GetMethodParams(const m: TClassMethodDef): AnsiString;
|
|
|
|
var
|
|
|
|
i : Integer;
|
|
|
|
p : TObject;
|
|
|
|
vname : AnsiString;
|
|
|
|
vtype : AnsiString;
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
vname := '';
|
|
|
|
vtype := '';
|
|
|
|
for i := 0 to m.Items.Count - 1 do begin
|
|
|
|
p := TObject(m.Items[i]);
|
|
|
|
if p is TParamDescr then
|
|
|
|
vname := TParamDescr(p)._Descr
|
2008-04-01 06:29:04 +00:00
|
|
|
else if p is TObjCParameterDef then begin
|
|
|
|
if vname = '' then vname := TObjCParameterDef(p)._Name;
|
|
|
|
vtype := ObjCToDelphiType(TObjCParameterDef(p)._Res._Name, TObjCParameterDef(p)._Res._IsPointer);
|
2008-03-25 08:24:19 +00:00
|
|
|
if Result <> '' then Result := Result + '; ';
|
|
|
|
Result := Result + vname + ': ' + vtype;
|
|
|
|
vname := '';
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
function GetProcFuncHead(const FuncName, OfClass, Params, ResType, FuncDest: AnsiString): AnsiString;
|
|
|
|
begin
|
|
|
|
if FuncDest = '' then begin
|
|
|
|
if ResType = '' then Result := 'procedure '
|
|
|
|
else Result := 'function ';
|
|
|
|
end else
|
|
|
|
Result := FuncDest + ' ';
|
|
|
|
|
|
|
|
if OfClass <> '' then Result := Result + OfClass+'.';
|
|
|
|
Result := Result + FuncName;
|
|
|
|
if Params <> '' then
|
|
|
|
Result := Result + '('+Params+')';
|
2008-04-01 06:29:04 +00:00
|
|
|
if ResType <> '' then Result := Result+': '+ResType;
|
2008-03-25 08:24:19 +00:00
|
|
|
Result := Result + ';';
|
|
|
|
end;
|
|
|
|
|
|
|
|
function StrFromFile(const FileName: AnsiString): AnsiString;
|
|
|
|
var
|
|
|
|
fs : TFileStream;
|
|
|
|
begin
|
|
|
|
fs := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone);
|
|
|
|
try
|
|
|
|
SetLength(Result, fs.Size);
|
|
|
|
fs.Read(Result[1], fs.Size);
|
|
|
|
finally
|
|
|
|
fs.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2008-04-01 06:29:04 +00:00
|
|
|
function ObjCToDelphiType(const objcType: AnsiString; isPointer: Boolean): AnsiString;
|
2008-03-25 08:24:19 +00:00
|
|
|
var
|
|
|
|
l : AnsiString;
|
2008-04-07 14:06:35 +00:00
|
|
|
r : AnsiString;
|
2008-03-25 08:24:19 +00:00
|
|
|
begin
|
|
|
|
Result := objcType;
|
|
|
|
l := AnsiLowerCase(objcType);
|
|
|
|
if l = '' then Exit;
|
|
|
|
case l[1] of
|
|
|
|
'v':
|
2008-04-01 06:29:04 +00:00
|
|
|
if l = 'void' then begin
|
|
|
|
if not isPointer then Result := ''
|
|
|
|
else Result := 'Pointer';
|
|
|
|
end;
|
2008-03-25 08:24:19 +00:00
|
|
|
'i':
|
|
|
|
if l = 'id' then Result := 'objc.id'
|
|
|
|
else if l = 'int' then Result := 'Integer';
|
|
|
|
'b':
|
|
|
|
if l = 'bool' then Result := 'LongBool';
|
|
|
|
'l':
|
|
|
|
if l = 'long' then Result := 'Integer';
|
|
|
|
's':
|
|
|
|
if l = 'short' then Result := 'SmallInt';
|
|
|
|
'u':
|
|
|
|
if (l = 'unsigned long') or (l = 'usigned int') then
|
|
|
|
Result := 'LongWord'
|
|
|
|
else if (l = 'unsigned short') then
|
|
|
|
Result := 'Word';
|
|
|
|
'f':
|
|
|
|
if l = 'float' then Result := 'Single';
|
|
|
|
end;
|
2008-04-07 14:06:35 +00:00
|
|
|
if Result = objcType then begin
|
|
|
|
r := ConvertSettings.TypeDefReplace[objcType];
|
|
|
|
if r <> '' then Result := r;
|
|
|
|
end;
|
|
|
|
|
2008-03-25 08:24:19 +00:00
|
|
|
end;
|
|
|
|
|
2008-03-28 10:25:27 +00:00
|
|
|
|
2008-03-25 08:24:19 +00:00
|
|
|
function IsMethodConstructor(cl: TClassDef; m: TClassMethodDef): Boolean;
|
|
|
|
var
|
2008-04-01 06:29:04 +00:00
|
|
|
res : TObjCResultTypeDef;
|
2008-03-25 08:24:19 +00:00
|
|
|
l : AnsiString;
|
|
|
|
begin
|
|
|
|
Result := m._IsClassMethod;
|
|
|
|
if not Result then begin
|
|
|
|
//todo: C is case sensetive, so is it possible to have a initialing function name like
|
|
|
|
// 'InitWithSomething', rather than 'initWithSomething' (that is should be)???
|
|
|
|
//todo: to make sure, it's not a name,like 'Initialzation';
|
|
|
|
l := AnsiLowerCase(m._Name);
|
|
|
|
if Pos('init', l) = 1 then Result := true;
|
|
|
|
end;
|
|
|
|
if not Result then Exit;
|
|
|
|
|
|
|
|
res := m.GetResultType;
|
2008-04-01 06:29:04 +00:00
|
|
|
l := res._Name;
|
2008-03-25 08:24:19 +00:00
|
|
|
Result := (l = 'id') or (l = cl._ClassName);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function GetMethodStr(cl: TClassDef; m: TClassMethodDef; ForImplementation: Boolean): AnsiString;
|
|
|
|
var
|
2008-04-01 06:29:04 +00:00
|
|
|
// i : integer;
|
2008-03-25 08:24:19 +00:00
|
|
|
ft : AnsiString;
|
2008-04-01 06:29:04 +00:00
|
|
|
res : AnsiString;
|
2008-03-25 08:24:19 +00:00
|
|
|
begin
|
2008-04-01 06:29:04 +00:00
|
|
|
res := GetMethodResultType(m);
|
|
|
|
if IsMethodConstructor(cl, m) then begin
|
|
|
|
ft := 'constructor';
|
|
|
|
res := '';
|
|
|
|
end else
|
|
|
|
ft := '';
|
|
|
|
|
2008-03-25 08:24:19 +00:00
|
|
|
if ForImplementation
|
2008-04-01 06:29:04 +00:00
|
|
|
then Result := GetProcFuncHead(m._Name, cl._ClassName, GetMethodParams(m), res, ft)
|
|
|
|
else Result := GetProcFuncHead(m._Name, '', GetMethodParams(m), res, ft)
|
2008-03-25 08:24:19 +00:00
|
|
|
end;
|
|
|
|
|
2008-03-27 15:28:02 +00:00
|
|
|
// returns define pas file name form Objective C name, like
|
|
|
|
// NSApplication.h -> NSAPPLICATION_PAS_H
|
|
|
|
// SomePath/SomePath/SomeFileName.h -> SOMEFILENAME_PAS_H
|
2008-03-28 13:03:13 +00:00
|
|
|
function GetIfDefFileName(const FileName, DefExt: AnsiString): AnsiString;
|
2008-03-25 08:24:19 +00:00
|
|
|
var
|
|
|
|
i : integer;
|
2008-04-01 06:29:04 +00:00
|
|
|
// s : AnsiString;
|
2008-03-25 08:24:19 +00:00
|
|
|
begin
|
2008-03-27 15:28:02 +00:00
|
|
|
//todo: don't like it...
|
2008-03-25 08:24:19 +00:00
|
|
|
Result := Copy(FileName, 1, length(FileName) - length(ExtractFileExt(FileName)));
|
|
|
|
Result := AnsiUpperCase(Result);
|
|
|
|
for i := 1 to length(Result) do
|
|
|
|
if Result[i] = '.' then
|
|
|
|
Result[i] := '_';
|
2008-03-28 13:03:13 +00:00
|
|
|
Result := Result + '_PAS_'+DefExt;
|
2008-03-25 08:24:19 +00:00
|
|
|
end;
|
|
|
|
|
2008-03-27 15:28:02 +00:00
|
|
|
// returns include pas file name form Objective C name, like
|
|
|
|
// <AppKit/NSApplication.h> -> NSApplication.inc
|
|
|
|
// "SomePath/SomePath/SomeFileName.h> -> SomeFileName.h
|
|
|
|
function GetIncludeFile(const s: AnsiString): AnsiString;
|
|
|
|
var
|
|
|
|
i : Integer;
|
2008-04-08 10:45:08 +00:00
|
|
|
vs : AnsiString;
|
|
|
|
pth : AnsiString;
|
2008-03-27 15:28:02 +00:00
|
|
|
begin
|
2008-04-08 10:45:08 +00:00
|
|
|
//todo: still, i don't like it...
|
|
|
|
Result :='';
|
|
|
|
i := 1;
|
|
|
|
ScanWhile(s, i, [#32, #9]);
|
|
|
|
vs := Copy(s, i, length(s) - i + 1);
|
|
|
|
if vs = '' then Exit;
|
|
|
|
|
|
|
|
|
|
|
|
if (vs[1] = '<') or (vs[1] = '"') then vs := Copy(vs, 2, length(vs) - 1);
|
|
|
|
if vs = '' then Exit;
|
|
|
|
|
|
|
|
i := length(vs);
|
|
|
|
if (vs[i] = '>') or (vs[i] = '"') then vs := Copy(vs, 1, length(vs) - 1);
|
|
|
|
if vs = '' then Exit;
|
|
|
|
|
|
|
|
|
|
|
|
pth := vs;
|
|
|
|
while (pth <> '') and (length(pth)>1) do begin
|
|
|
|
if ConvertSettings.IgnoreIncludes.IndexOf(pth) >= 0 then
|
|
|
|
Exit; // file must be excluded;
|
|
|
|
pth := ExtractFilePath(ExcludeTrailingPathDelimiter(pth));
|
|
|
|
end;
|
|
|
|
|
|
|
|
Result := ExtractFileName(vs);
|
|
|
|
Result := Copy(Result, 1, length(Result) - length(ExtractFileExt(vs))) + '.inc';
|
|
|
|
|
|
|
|
(*
|
2008-03-27 15:28:02 +00:00
|
|
|
Result := '';
|
|
|
|
if s = '' then Exit;
|
2008-04-01 06:29:04 +00:00
|
|
|
// i := length(s);
|
|
|
|
{ if (s[i] = '"') or (s[i] = '>') then
|
|
|
|
dec(i);}
|
2008-03-27 15:28:02 +00:00
|
|
|
i := length(s) - 1;
|
|
|
|
// dummy, but it works =)
|
|
|
|
while (i > 0) and (s[i] in ['.', 'A'..'Z', 'a'..'z', '0'..'9']) do dec(i);
|
|
|
|
|
2008-04-08 10:45:08 +00:00
|
|
|
Result := Copy(s, i + 1, length(s) - i);*)
|
2008-03-27 15:28:02 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
// returns pascal style of precomiler "if defined" section
|
|
|
|
// exclusion is done for Cocoa known precompiler definion, for ex:
|
|
|
|
// MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3 -> MAC_OS_X_VERSION_10_3
|
|
|
|
// any other #ifdef excpresions would be passed "as is" even if are incorrect
|
|
|
|
// for pascal
|
2008-04-07 14:06:35 +00:00
|
|
|
function PrecompileIfDefToPascal(const prm: AnsiString; var isDef: Boolean): AnsiString;
|
2008-03-27 15:28:02 +00:00
|
|
|
var
|
|
|
|
i : Integer;
|
2008-04-07 14:06:35 +00:00
|
|
|
vs : AnsiString;
|
2008-03-27 15:28:02 +00:00
|
|
|
begin
|
2008-04-07 14:06:35 +00:00
|
|
|
i := 1;
|
|
|
|
ScanWhile(prm, i, [#32, #9]);
|
|
|
|
if prm[i] = '!' then begin
|
|
|
|
isDef := false;
|
|
|
|
inc(i);
|
|
|
|
ScanWhile(prm, i, [#32, #9]);
|
|
|
|
end else
|
|
|
|
isDef :=true;
|
|
|
|
vs := Copy(prm, i, length(prm) - i + 1);
|
|
|
|
|
2008-03-27 15:28:02 +00:00
|
|
|
// really slow... and... don't like this anyway!
|
2008-04-07 14:06:35 +00:00
|
|
|
vs := ConvertSettings.DefineReplace[vs];
|
|
|
|
if vs <> ''
|
|
|
|
then Result := vs
|
|
|
|
else Result := prm;
|
|
|
|
{ for i := 0 to ConvertSettings.DefineReplace.C
|
2008-03-27 15:28:02 +00:00
|
|
|
Result := prm;
|
|
|
|
i := Pos(VerExclude, prm);
|
|
|
|
if i > 0 then begin
|
|
|
|
i := i + length(VerExclude);
|
|
|
|
while (i <= length(Result)) and (Result[i] = ' ') do inc(i);
|
|
|
|
if i <= length(Result) then
|
|
|
|
Result := Copy(prm, i, length(Result) - i + 1);
|
2008-04-07 14:06:35 +00:00
|
|
|
end;}
|
2008-03-27 15:28:02 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
// converts TProcpmiler entity to pascal entity
|
|
|
|
// #import or #include -> {$Include Something.inc}
|
|
|
|
// #define SOMETHING -> {$define SOMETHING}
|
|
|
|
// #ifdef SOMETHING -> {$ifdef SOMETHING}
|
|
|
|
// etc...
|
|
|
|
function WriteOutPrecompToPascal(Prec: TPrecompiler): AnsiString;
|
|
|
|
var
|
|
|
|
dir : AnsiString;
|
2008-04-07 14:06:35 +00:00
|
|
|
prm : AnsiString;
|
|
|
|
isdef : Boolean;
|
|
|
|
const
|
|
|
|
isdefConst : array [Boolean] of AnsiString = ('ifndef', 'ifdef');
|
2008-03-27 15:28:02 +00:00
|
|
|
begin
|
|
|
|
dir := AnsiLowerCase(Prec._Directive);
|
2008-04-07 14:06:35 +00:00
|
|
|
if (dir = '#import') or (dir = '#include') then begin
|
|
|
|
|
|
|
|
prm := GetIncludeFile(Prec._Params);
|
2008-04-08 10:45:08 +00:00
|
|
|
if (prm <> '') and (prm <> ' .inc') and (ConvertSettings.IgnoreIncludes.IndexOf(prm) < 0) then
|
2008-04-07 14:06:35 +00:00
|
|
|
Result := Format('{$include %s}', [prm]);
|
|
|
|
|
|
|
|
end else if (dir = '#if') then begin
|
|
|
|
prm := PrecompileIfDefToPascal(Prec._Params, isdef);
|
|
|
|
Result := Format('{$%s %s}', [isdefConst[isdef], prm]);
|
|
|
|
end else if (dir = '#else') then
|
2008-03-27 15:28:02 +00:00
|
|
|
Result := '{$else}'
|
|
|
|
else if (dir = '#endif') then
|
|
|
|
Result := '{$endif}';
|
|
|
|
end;
|
|
|
|
|
2008-03-28 10:25:27 +00:00
|
|
|
procedure WriteOutCommentStr(const AComment, Prefix: AnsiString; Subs: TStrings);
|
|
|
|
var
|
|
|
|
i : Integer;
|
|
|
|
j : Integer;
|
|
|
|
k : Integer;
|
|
|
|
cmtln : AnsiString;
|
|
|
|
begin
|
|
|
|
i := 1;
|
|
|
|
while i <= length(AComment) do begin
|
|
|
|
// scan for multylined comments
|
|
|
|
cmtln := ScanTo(AComment, i, [#10, #13]);
|
|
|
|
if i < length(AComment) then begin
|
|
|
|
if (AComment[i] = #10) and (AComment[i+1] = #13) then inc(i)
|
|
|
|
else if (AComment[i] = #13) and (AComment[i+1] = #10) then inc(i);
|
|
|
|
end;
|
|
|
|
inc(i);
|
|
|
|
|
|
|
|
// break long comments into lines
|
|
|
|
j := 1;
|
|
|
|
while j <= length(cmtln) do begin
|
|
|
|
k := j;
|
|
|
|
inc(j, 80);
|
|
|
|
if j > length(cmtln) then j := length(cmtln);
|
|
|
|
ScanTo(cmtln, j, [#32, #10, #13, #9]);
|
|
|
|
subs.Add(Prefix + '// ' + Copy(cmtln, k, j - k));
|
|
|
|
inc(j);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure WriteOutIfComment(Items: TList; Index: Integer; const Prefix: AnsiString; Subs: TStrings);
|
|
|
|
var
|
|
|
|
j : integer;
|
|
|
|
begin
|
|
|
|
if (Index < 0) or (Index >= Items.Count) then Exit;
|
|
|
|
|
|
|
|
j := Index;
|
|
|
|
while (j >= 0) and (TObject(Items[j]) is TComment) do dec(j);
|
|
|
|
inc(j);
|
|
|
|
for j := j to index do
|
|
|
|
//if TObject(Items[Index]) is TComment then
|
|
|
|
WriteOutCommentStr( TComment(Items[j])._Comment, Prefix, Subs);
|
|
|
|
end;
|
|
|
|
|
2008-03-27 15:28:02 +00:00
|
|
|
// clears empty precompile statements, like
|
|
|
|
// {$ifdef SOMETHING}
|
|
|
|
// {$endif}
|
|
|
|
// and
|
|
|
|
// {$ifdef SOMETHING}
|
|
|
|
// {$else}
|
|
|
|
// {$endif}
|
|
|
|
// will be removed
|
|
|
|
procedure ClearEmptyPrecompile(subs: TStrings);
|
|
|
|
var
|
|
|
|
i : integer;
|
|
|
|
j : Integer;
|
|
|
|
begin
|
|
|
|
// don't like it either...
|
|
|
|
i := subs.Count - 1; if i < 0 then Exit;
|
|
|
|
j := i;
|
|
|
|
|
|
|
|
if Pos('{$endif', subs[i]) = 0 then Exit;
|
|
|
|
dec(i); if i < 0 then Exit;
|
|
|
|
|
|
|
|
if Pos('{$else', subs[i]) > 0 then
|
|
|
|
dec(i); if i < 0 then Exit;
|
|
|
|
|
|
|
|
if Pos('{$ifdef', subs[i]) > 0 then
|
|
|
|
for i := j downto i do
|
|
|
|
subs.Delete(i);
|
|
|
|
end;
|
|
|
|
|
2008-03-28 13:03:13 +00:00
|
|
|
procedure BeginSection(const SectionName: AnsiString; st: TStrings);
|
2008-03-25 08:24:19 +00:00
|
|
|
begin
|
2008-04-08 09:22:54 +00:00
|
|
|
st.Add('');
|
2008-03-25 08:24:19 +00:00
|
|
|
st.Add('{$ifdef '+SectionName+'}');
|
2008-03-28 13:03:13 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure BeginExcludeSection(const DefineName: AnsiString; st: TStrings);
|
|
|
|
begin
|
|
|
|
st.Add('{$ifndef '+DefineName+'}');
|
|
|
|
st.Add('{$define '+DefineName+'}');
|
2008-04-08 09:22:54 +00:00
|
|
|
st.Add('');
|
2008-03-25 08:24:19 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure EndSection(st: TStrings);
|
|
|
|
begin
|
|
|
|
st.Add('{$endif}');
|
|
|
|
end;
|
|
|
|
|
2008-03-27 15:28:02 +00:00
|
|
|
// todo: remove Prefix param...
|
2008-03-28 10:25:27 +00:00
|
|
|
procedure WriteOutIfDefPrecompiler(prec: TPrecompiler; const Prefix: AnsiString; subs: TStrings);
|
2008-03-27 15:28:02 +00:00
|
|
|
var
|
|
|
|
ppas : AnsiString;
|
|
|
|
isend : Boolean;
|
|
|
|
begin
|
|
|
|
ppas := WriteOutPrecompToPascal(prec);
|
|
|
|
isend := IsSubStr('{$endif', ppas, 1);
|
2008-04-07 14:06:35 +00:00
|
|
|
if isend or IsSubStr('{$ifndef', ppas, 1) or IsSubStr('{$ifdef', ppas, 1) or IsSubStr('{$else', ppas, 1) then
|
2008-03-27 15:28:02 +00:00
|
|
|
subs.Add(Prefix + ppas);
|
|
|
|
if isend then ClearEmptyPrecompile(subs);
|
|
|
|
end;
|
|
|
|
|
2008-04-08 09:22:54 +00:00
|
|
|
function GetClassConst(const ClassName, ConstName: AnsiString): AnsiString;
|
|
|
|
begin
|
|
|
|
Result := Format('Str%s_%s', [ClassName, ConstName]);
|
|
|
|
end;
|
|
|
|
|
2008-03-25 08:24:19 +00:00
|
|
|
procedure WriteOutClassToHeader(cl : TClassDef; subs: TStrings; conststr: TStrings);
|
|
|
|
var
|
|
|
|
i : Integer;
|
2008-04-01 06:29:04 +00:00
|
|
|
// j : Integer;
|
2008-03-25 08:24:19 +00:00
|
|
|
s : AnsiString;
|
|
|
|
ss : AnsiString;
|
|
|
|
mtd : TClassMethodDef;
|
2008-03-27 15:28:02 +00:00
|
|
|
obj : TObject;
|
2008-04-08 09:22:54 +00:00
|
|
|
cs : AnsiString;
|
2008-03-25 08:24:19 +00:00
|
|
|
begin
|
2008-04-08 09:22:54 +00:00
|
|
|
cs := GetClassConst(cl._ClassName, cl._ClassName);
|
|
|
|
if conststr.IndexOf(cs) < 0 then begin
|
|
|
|
conststr.Add(cs);
|
|
|
|
s := Format(' %s = ''%s'';', [cs, cl._ClassName]);
|
2008-03-25 08:24:19 +00:00
|
|
|
subs.Add(s);
|
2008-04-08 09:22:54 +00:00
|
|
|
end;
|
|
|
|
|
2008-03-27 15:28:02 +00:00
|
|
|
for i := 0 to cl.Items.Count - 1 do begin
|
|
|
|
obj := TObject(cl.Items[i]);
|
|
|
|
if obj is TClassMethodDef then begin
|
2008-03-25 08:24:19 +00:00
|
|
|
mtd := TClassMethodDef(cl.Items[i]);
|
2008-04-08 09:22:54 +00:00
|
|
|
|
|
|
|
cs := GetClassConst(cl._ClassName, mtd._Name);
|
|
|
|
if conststr.IndexOf(cs) < 0 then begin
|
|
|
|
conststr.Add(cs);
|
|
|
|
ss := Format(' %s = ''%s'';', [cs, mtd._Name]);
|
|
|
|
subs.add(ss);
|
|
|
|
end;
|
2008-03-27 15:28:02 +00:00
|
|
|
end else if obj is TPrecompiler then begin
|
2008-03-28 10:25:27 +00:00
|
|
|
WriteOutIfDefPrecompiler(TPrecompiler(obj), ' ', subs);
|
2008-03-27 15:28:02 +00:00
|
|
|
end;
|
|
|
|
end; {of for}
|
|
|
|
subs.Add('');
|
|
|
|
end;
|
|
|
|
|
2008-04-01 06:29:04 +00:00
|
|
|
procedure ParseDefine(const s: AnsiString; var DefWhat, DefTo: AnsiString);
|
|
|
|
var
|
|
|
|
i : Integer;
|
|
|
|
begin
|
|
|
|
i := 1;
|
|
|
|
ScanWhile(s, i, [#9, #32, #10, #13]);
|
|
|
|
if i < length(s) then begin
|
|
|
|
DefWhat := ScanTo(s, i, [#9, #32, #10, #13]);
|
|
|
|
ScanWhile(s, i, [#9, #32]);
|
|
|
|
DefTo := Copy(s, i, length(s) - i + 1);
|
|
|
|
end else
|
|
|
|
DefTo := '';
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure WriteOutPrecompDefine(const Prec: TPrecompiler; Prefix: AnsiString; st: TStrings);
|
|
|
|
var
|
|
|
|
a, b: AnsiString;
|
|
|
|
begin
|
|
|
|
if Prec._Directive = '#define' then begin
|
|
|
|
ParseDefine(Prec._Params, a, b);
|
|
|
|
if b <> ''
|
|
|
|
then st.Add(Prefix + Format('%s = %s;', [a, b]))
|
|
|
|
else st.Add(Prefix + Format('{$define %s}', [a]));
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2008-03-28 13:03:13 +00:00
|
|
|
procedure WriteOutPrecompInclude(Prec: TPrecompiler; st: TStrings);
|
2008-03-27 15:28:02 +00:00
|
|
|
var
|
|
|
|
dlph : AnsiString;
|
|
|
|
begin
|
|
|
|
dlph := WriteOutPrecompToPascal(Prec);
|
2008-04-07 14:06:35 +00:00
|
|
|
if IsSubStr('{$include', dlph, 1) then
|
|
|
|
st.Add(dlph);
|
2008-03-27 15:28:02 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
function GetPascalEnumValue(const Name, Param: AnsiString): AnsiString;
|
|
|
|
begin
|
|
|
|
Result := Name;
|
2008-03-28 10:25:27 +00:00
|
|
|
if Param <> '' then
|
|
|
|
Result := Result + ' = ' + Param
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function ReplaceStr(const sub, subrep, s: AnsiString): AnsiString;
|
|
|
|
var
|
|
|
|
i : Integer;
|
|
|
|
j : Integer;
|
|
|
|
begin
|
|
|
|
i := Pos(sub, s);
|
|
|
|
if i = 0 then begin
|
|
|
|
Result := s;
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
j := i + length(sub);
|
|
|
|
Result := Copy(s, 1, i - 1) + subrep + Copy(s, j, length(s) - j + 1);
|
2008-03-27 15:28:02 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
function GetPascalConstValue(const Vl: AnsiString): AnsiString;
|
|
|
|
begin
|
2008-03-28 10:25:27 +00:00
|
|
|
//todo: improve! check at h2pas
|
|
|
|
Result := ReplaceStr('<<', 'shl', vl);
|
|
|
|
Result := ReplaceStr('>>', 'shr', Result);
|
2008-04-07 14:06:35 +00:00
|
|
|
Result := ReplaceStr('||', 'or', Result);
|
|
|
|
Result := ReplaceStr('|', 'or', Result);
|
|
|
|
Result := ReplaceStr('&&', 'and', Result);
|
|
|
|
Result := ReplaceStr('&', 'and', Result);
|
2008-03-27 15:28:02 +00:00
|
|
|
end;
|
|
|
|
|
2008-03-28 10:25:27 +00:00
|
|
|
procedure WriteOutEnumValues(enm: TEnumTypeDef; const Prefix: AnsiString; st: TStrings);
|
2008-03-27 15:28:02 +00:00
|
|
|
var
|
|
|
|
vl : TEnumValue;
|
|
|
|
s : AnsiString;
|
|
|
|
i : Integer;
|
|
|
|
j : Integer;
|
|
|
|
begin
|
|
|
|
j := st.Count;
|
2008-03-28 10:25:27 +00:00
|
|
|
for i := 0 to enm.Items.Count - 1 do begin
|
2008-03-27 15:28:02 +00:00
|
|
|
if TObject(enm.Items[i]) is TEnumValue then begin
|
|
|
|
vl := TEnumValue(enm.Items[i]);
|
|
|
|
if st.Count > j then st[st.Count-1]:=st[st.Count-1]+', ';
|
|
|
|
s := GetPascalEnumValue(vl._Name, GetPascalConstValue(vl._Value));
|
|
|
|
s := Prefix + s;
|
|
|
|
st.Add(s);
|
|
|
|
end;
|
2008-03-28 10:25:27 +00:00
|
|
|
end;
|
2008-03-27 15:28:02 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
function Min(a, b: Integer): Integer;
|
|
|
|
begin
|
|
|
|
if a < b then Result := a
|
|
|
|
else Result := b;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure MatchFixes(const Name: AnsiString; var prefix, postfix: AnsiString);
|
|
|
|
var
|
|
|
|
i : integer;
|
|
|
|
ni, pi: integer;
|
2008-04-01 06:29:04 +00:00
|
|
|
// nc, pc: AnsiChar;
|
2008-03-27 15:28:02 +00:00
|
|
|
begin
|
|
|
|
for i := 1 to Min(length(Name), length(prefix)) do
|
|
|
|
if Name[i] <> prefix[i] then begin
|
|
|
|
prefix := Copy(prefix, 1, i - 1);
|
|
|
|
Break;
|
|
|
|
end;
|
|
|
|
|
|
|
|
ni := length(Name);
|
|
|
|
pi := length(postfix);
|
|
|
|
for i := 1 to Min(length(Name), length(postfix)) do begin
|
|
|
|
if Name[ni] <> postfix[pi] then begin // this cause a bug
|
|
|
|
postfix := Copy(Name, ni + 1, length(Name) - ni);
|
|
|
|
Break;
|
|
|
|
end;
|
|
|
|
dec(ni);
|
|
|
|
dec(pi);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function EvaluateEnumName(enm: TEnumTypeDef): AnsiString;
|
|
|
|
var
|
|
|
|
prefix : AnsiString;
|
|
|
|
postfix : AnsiSTring;
|
|
|
|
vl : TEnumValue;
|
|
|
|
known : integer;
|
|
|
|
i : Integer;
|
|
|
|
begin
|
|
|
|
known := 0;
|
2008-03-28 10:25:27 +00:00
|
|
|
Result := '';
|
2008-03-27 15:28:02 +00:00
|
|
|
for i := 0 to enm.Items.Count - 1 do begin
|
|
|
|
if TObject(enm.Items[i]) is TEnumValue then begin
|
|
|
|
vl := TEnumValue(enm.Items[i]);
|
|
|
|
if known = 0 then begin
|
|
|
|
prefix := vl._Name;
|
|
|
|
postfix := vl._Name;
|
|
|
|
end else
|
|
|
|
MatchFixes(vl._Name, prefix, postfix);
|
|
|
|
inc(known)
|
2008-03-25 08:24:19 +00:00
|
|
|
end;
|
2008-03-27 15:28:02 +00:00
|
|
|
end;
|
|
|
|
if (known <= 1) or (length(Result) < 3) then Result := 'todoEnumName' // if only one enumaration or none, name cannot be defined...
|
|
|
|
else Result := prefix + postfix;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure WriteOutEnumToHeader(enm: TEnumTypeDef; st: TStrings);
|
|
|
|
var
|
2008-04-07 14:06:35 +00:00
|
|
|
i : Integer;
|
|
|
|
// ent : TEnumValue;
|
|
|
|
obj : TObject;
|
|
|
|
pre : TEnumValue;
|
|
|
|
vl : TEnumValue;
|
|
|
|
vls : AnsiString;
|
|
|
|
vli : Integer;
|
2008-03-27 15:28:02 +00:00
|
|
|
begin
|
2008-04-07 14:06:35 +00:00
|
|
|
if enm._Name = '' then begin
|
|
|
|
// unnamed enums are written out as constants
|
|
|
|
pre := nil;
|
|
|
|
st.Add('const');
|
|
|
|
vli := 1;
|
|
|
|
for i := 0 to enm.Items.Count - 1 do begin
|
|
|
|
obj := TObject(enm.Items[i]);
|
|
|
|
if obj is TEnumValue then begin
|
|
|
|
vl := TEnumValue(obj);
|
|
|
|
if vl._Value = '' then begin
|
|
|
|
if not Assigned(pre) then begin
|
|
|
|
vls := '0';
|
|
|
|
pre := vl;
|
|
|
|
end else begin
|
|
|
|
vls := pre._Name + ' + ' + IntToStr(vli);
|
|
|
|
inc(vli);
|
|
|
|
end;
|
|
|
|
end else begin
|
|
|
|
vls := vl._Value;
|
|
|
|
vli := 1;
|
|
|
|
pre := vl;
|
|
|
|
end;
|
|
|
|
st.Add(Format(' %s = %s;', [vl._Name, GetPascalConstValue(vls)]));
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
st.Add('');
|
|
|
|
//st.Add('type');
|
|
|
|
end else begin
|
|
|
|
st.Add('type');
|
|
|
|
// named enums are written out as delphi enumerations
|
|
|
|
st.Add(Format(' %s = (', [enm._Name] ));
|
|
|
|
WriteOutEnumValues(enm, ' ', st );
|
|
|
|
st.Add(' );');
|
|
|
|
st.Add('');
|
|
|
|
end;
|
2008-03-25 08:24:19 +00:00
|
|
|
end;
|
|
|
|
|
2008-03-28 10:25:27 +00:00
|
|
|
procedure WriteOutTypeDefToHeader(typedef: TTypeNameDef; const Prefix: AnsiString; subs: TStrings);
|
2008-04-07 14:06:35 +00:00
|
|
|
var
|
|
|
|
vs : AnsiString;
|
|
|
|
tmp : AnsiString;
|
2008-03-28 10:25:27 +00:00
|
|
|
begin
|
2008-04-07 14:06:35 +00:00
|
|
|
vs := ConvertSettings.TypeDefReplace[typedef._Inherited];
|
|
|
|
if vs = '' then vs := typedef._Inherited;
|
|
|
|
if not Assigned(typedef._Type) or (typedef._Type is TTypeDef) then begin
|
|
|
|
subs.Add('type');
|
|
|
|
subs.Add(Prefix + Format('%s = %s;', [typedef._TypeName, vs]))
|
|
|
|
end else begin
|
|
|
|
if typedef._Type is TEnumTypeDef then begin
|
|
|
|
tmp := TEnumTypeDef(typedef._Type)._Name;
|
|
|
|
TEnumTypeDef(typedef._Type)._Name := typedef._TypeName;
|
|
|
|
WriteOutEnumToHeader(TEnumTypeDef(typedef._Type), subs);
|
|
|
|
TEnumTypeDef(typedef._Type)._Name := tmp;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
subs.Add('');
|
2008-03-28 10:25:27 +00:00
|
|
|
end;
|
|
|
|
|
2008-03-25 08:24:19 +00:00
|
|
|
procedure WriteOutHeaderSection(hdr: TObjCHeader; st: TStrings);
|
|
|
|
var
|
|
|
|
i : Integer;
|
|
|
|
cl : TClassDef;
|
|
|
|
subs : TStringList;
|
2008-04-01 06:29:04 +00:00
|
|
|
// s : AnsiString;
|
2008-03-25 08:24:19 +00:00
|
|
|
consts : TStringList;
|
2008-03-28 10:25:27 +00:00
|
|
|
const
|
|
|
|
SpacePrefix = ' ';
|
2008-03-25 08:24:19 +00:00
|
|
|
begin
|
2008-03-28 13:03:13 +00:00
|
|
|
BeginSection('HEADER', st);
|
|
|
|
BeginExcludeSection( GetIfDefFileName(hdr._FileName, 'H'), st);
|
2008-03-25 08:24:19 +00:00
|
|
|
subs := TStringList.Create;
|
|
|
|
consts := TStringList.Create;
|
2008-04-08 09:22:54 +00:00
|
|
|
|
2008-03-25 08:24:19 +00:00
|
|
|
try
|
|
|
|
for i := 0 to hdr.Items.Count - 1 do
|
2008-03-27 15:28:02 +00:00
|
|
|
if Assigned(hdr.Items[i]) then begin
|
2008-04-07 14:06:35 +00:00
|
|
|
|
2008-03-27 15:28:02 +00:00
|
|
|
if (TObject(hdr.Items[i]) is TClassDef) then begin
|
|
|
|
cl := TClassDef(hdr.Items[i]);
|
|
|
|
WriteOutClassToHeader(cl, subs, consts);
|
|
|
|
end else if (TObject(hdr.Items[i]) is TPrecompiler) then begin
|
2008-04-07 14:06:35 +00:00
|
|
|
WriteOutIfDefPrecompiler(TPrecompiler(hdr.Items[i]), SpacePrefix, st);
|
2008-03-28 13:03:13 +00:00
|
|
|
WriteOutPrecompInclude(TPrecompiler(hdr.Items[i]), st);
|
2008-04-01 06:29:04 +00:00
|
|
|
WriteOutPrecompDefine(TPrecompiler(hdr.Items[i]), ' ', subs);
|
2008-03-27 15:28:02 +00:00
|
|
|
end;
|
2008-03-25 08:24:19 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
if subs.Count > 0 then begin
|
|
|
|
st.Add('const');
|
|
|
|
st.AddStrings(subs);
|
|
|
|
subs.Clear;
|
|
|
|
end;
|
2008-04-07 14:06:35 +00:00
|
|
|
|
2008-03-27 15:28:02 +00:00
|
|
|
for i := 0 to hdr.Items.Count - 1 do
|
|
|
|
if Assigned(hdr.Items[i]) then begin
|
|
|
|
if (TObject(hdr.Items[i]) is TEnumTypeDef) then begin
|
2008-03-28 10:25:27 +00:00
|
|
|
WriteOutIfComment(hdr.Items, i - 1, SpacePrefix, subs);
|
2008-03-27 15:28:02 +00:00
|
|
|
WriteOutEnumToHeader(TEnumTypeDef(hdr.Items[i]), subs);
|
|
|
|
end else if (TObject(hdr.Items[i]) is TPrecompiler) then begin
|
2008-03-28 10:25:27 +00:00
|
|
|
WriteOutIfDefPrecompiler(TPrecompiler(hdr.Items[i]), SpacePrefix, st);
|
|
|
|
end else if (TObject(hdr.Items[i]) is TTypeNameDef) then begin
|
|
|
|
WriteOutTypeDefToHeader(TTypeNameDef(hdr.Items[i]), SpacePrefix, subs);
|
2008-04-07 14:06:35 +00:00
|
|
|
end else if (TObject(hdr.Items[i]) is TSkip) then
|
|
|
|
subs.Add('//'+ TSkip(hdr.Items[i])._Skip);
|
2008-03-27 15:28:02 +00:00
|
|
|
end; {of if}
|
2008-04-07 14:06:35 +00:00
|
|
|
|
2008-03-27 15:28:02 +00:00
|
|
|
if subs.Count > 0 then begin
|
2008-04-07 14:06:35 +00:00
|
|
|
//if subs[0] <> 'const' then st.Add('type');
|
2008-03-27 15:28:02 +00:00
|
|
|
st.AddStrings(subs);
|
|
|
|
subs.Clear;
|
|
|
|
end;
|
|
|
|
|
2008-03-25 08:24:19 +00:00
|
|
|
finally
|
2008-03-28 13:03:13 +00:00
|
|
|
EndSection(st);
|
2008-03-25 08:24:19 +00:00
|
|
|
EndSection(st);
|
2008-04-08 09:22:54 +00:00
|
|
|
subs.Add('');
|
2008-03-25 08:24:19 +00:00
|
|
|
subs.Free;
|
|
|
|
consts.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure WriteOutClassToClasses(cl: TClassDef; subs: TStrings);
|
|
|
|
var
|
2008-03-27 15:28:02 +00:00
|
|
|
i : Integer;
|
2008-04-01 06:29:04 +00:00
|
|
|
// cnt : Integer;
|
2008-03-25 08:24:19 +00:00
|
|
|
s : AnsiString;
|
|
|
|
j : Integer;
|
2008-03-27 15:28:02 +00:00
|
|
|
obj : TObject; // or TEntity
|
|
|
|
|
|
|
|
mtds : TStringList; // name of methods
|
2008-04-01 06:29:04 +00:00
|
|
|
// over : TStringList; // overloaded names
|
2008-03-27 15:28:02 +00:00
|
|
|
const
|
|
|
|
SpacePrefix = ' ';
|
2008-03-25 08:24:19 +00:00
|
|
|
begin
|
2008-04-08 09:22:54 +00:00
|
|
|
subs.Add('');
|
2008-03-25 08:24:19 +00:00
|
|
|
subs.Add(' { '+cl._ClassName +' }');
|
|
|
|
subs.Add('');
|
|
|
|
s := ' ' + cl._ClassName + ' = class';
|
|
|
|
if cl._SuperClass <> '' then begin
|
|
|
|
subs.Add(s + '('+cl._SuperClass+')');
|
|
|
|
subs.Add(' public');
|
|
|
|
subs.Add(' function getClass: objc.id; override;');
|
|
|
|
end else begin
|
|
|
|
subs.Add(s + '{from category '+ cl._Category +'}');
|
|
|
|
subs.Add(' public');
|
|
|
|
end;
|
2008-03-27 15:28:02 +00:00
|
|
|
|
|
|
|
mtds := TStringList.Create;
|
|
|
|
try
|
|
|
|
for j := 0 to cl.Items.Count - 1 do begin
|
|
|
|
obj := TObject(cl.Items[j]);
|
|
|
|
if obj is TClassMethodDef then begin
|
|
|
|
i := mtds.indexOf(TClassMethodDef(obj)._Name);
|
|
|
|
if i < 0 then
|
|
|
|
mtds.Add( TClassMethodDef(obj)._Name)
|
|
|
|
else
|
|
|
|
mtds.Objects[i] := TObject(Integer(mtds.Objects[i]) + 1);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
for j := 0 to cl.Items.Count - 1 do begin
|
|
|
|
obj := TObject(cl.Items[j]);
|
|
|
|
if obj is TClassMethodDef then begin
|
2008-03-28 10:25:27 +00:00
|
|
|
WriteOutIfComment(cl.Items, j - 1, ' ', subs);
|
2008-03-27 15:28:02 +00:00
|
|
|
s := GetMethodStr(cl, TClassMethodDef(cl.Items[j]), false);
|
|
|
|
i := mtds.IndexOf(TClassMethodDef(cl.Items[j])._Name);
|
|
|
|
if Integer(mtds.Objects[i]) > 0 then s := s + ' overload;';
|
|
|
|
subs.Add(SpacePrefix + s);
|
|
|
|
end else if obj is TPrecompiler then begin
|
2008-03-28 10:25:27 +00:00
|
|
|
WriteOutIfDefPrecompiler(TPrecompiler(obj), SpacePrefix, subs);
|
2008-03-27 15:28:02 +00:00
|
|
|
end;
|
2008-03-25 08:24:19 +00:00
|
|
|
end;
|
2008-03-27 15:28:02 +00:00
|
|
|
finally
|
|
|
|
mtds.Free;
|
|
|
|
end;
|
|
|
|
|
2008-03-25 08:24:19 +00:00
|
|
|
subs.Add(' end;');
|
|
|
|
subs.Add('');
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure WriteOutClassesSection(hdr: TObjCHeader; st: TStrings);
|
|
|
|
var
|
2008-03-27 15:28:02 +00:00
|
|
|
i : integer;
|
2008-04-01 06:29:04 +00:00
|
|
|
// cl : TClassDef;
|
|
|
|
// j : integer;
|
|
|
|
// s : AnsiString;
|
2008-03-25 08:24:19 +00:00
|
|
|
subs : TStringList;
|
|
|
|
begin
|
2008-03-28 13:03:13 +00:00
|
|
|
BeginSection('CLASSES', st);
|
2008-04-07 14:06:35 +00:00
|
|
|
//BeginSection(GetIfDefFileName(hdr._FileName, 'C'), st);
|
|
|
|
BeginExcludeSection( GetIfDefFileName(hdr._FileName, 'C'), st);
|
|
|
|
|
2008-03-25 08:24:19 +00:00
|
|
|
subs := TStringList.Create;
|
|
|
|
try
|
2008-03-28 13:03:13 +00:00
|
|
|
for i := 0 to hdr.Items.Count - 1 do
|
2008-04-01 06:29:04 +00:00
|
|
|
if Assigned(hdr.Items[i]) and (TObject(hdr.Items[i]) is TPrecompiler) then
|
2008-03-28 13:03:13 +00:00
|
|
|
WriteOutPrecompInclude(TPrecompiler(hdr.Items[i]), st);
|
|
|
|
|
2008-03-25 08:24:19 +00:00
|
|
|
for i := 0 to hdr.Items.Count - 1 do
|
2008-04-07 14:06:35 +00:00
|
|
|
if Assigned(hdr.Items[i]) then begin
|
|
|
|
if TObject(hdr.Items[i]) is TPrecompiler then
|
|
|
|
WriteOutIfDefPrecompiler(TPrecompiler(hdr.Items[i]), ' ', subs)
|
|
|
|
else if (TObject(hdr.Items[i]) is TClassDef) then begin
|
|
|
|
WriteOutIfComment(hdr.Items, i - 1, ' ', subs);
|
|
|
|
WriteOutClassToClasses(TClassDef(hdr.Items[i]), subs);
|
|
|
|
end;
|
2008-03-28 10:25:27 +00:00
|
|
|
end;
|
2008-03-25 08:24:19 +00:00
|
|
|
|
|
|
|
if subs.Count > 0 then begin
|
|
|
|
st.Add('type');
|
|
|
|
st.AddStrings(subs);
|
|
|
|
end;
|
|
|
|
|
|
|
|
finally
|
2008-04-07 14:06:35 +00:00
|
|
|
EndSection(st);
|
2008-03-25 08:24:19 +00:00
|
|
|
EndSection(st);
|
|
|
|
subs.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function isAnyParam(mtd: TClassMethodDef): boolean;
|
|
|
|
var
|
|
|
|
i : integer;
|
|
|
|
begin
|
|
|
|
Result := false;
|
|
|
|
for i := 0 to mtd.Items.Count - 1 do
|
2008-04-01 06:29:04 +00:00
|
|
|
if TObject(mtd.Items[i]) is TObjCParameterDef then begin
|
2008-03-25 08:24:19 +00:00
|
|
|
Result := true;
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
const
|
|
|
|
MtdPrefix = 'TMtd_';
|
|
|
|
MtdPostfix = '';
|
|
|
|
|
|
|
|
procedure ObjCMethodToProcType(mtd: TClassMethodDef; var typeName: AnsiString; subs: TStrings);
|
|
|
|
var
|
2008-04-01 06:29:04 +00:00
|
|
|
// i : integer;
|
2008-03-25 08:24:19 +00:00
|
|
|
s : AnsiString;
|
2008-04-07 14:06:35 +00:00
|
|
|
ms : AnsiString;
|
2008-03-25 08:24:19 +00:00
|
|
|
begin
|
|
|
|
typeName := MtdPrefix + mtd._Name + MtdPostFix;
|
|
|
|
subs.Add('type');
|
|
|
|
// function GetProcFuncHead(const FuncName, OfClass, Params, ResType, FuncDest: AnsiString): AnsiString;
|
2008-04-07 14:06:35 +00:00
|
|
|
ms := GetMethodParams(mtd);
|
|
|
|
if ms = '' then ms := 'param1: objc.id; param2: SEL'
|
|
|
|
else ms := 'param1: objc.id; param2: SEL' + ';' + ms;
|
|
|
|
s := Format(' %s = %s cdecl;',[typeName, GetProcFuncHead('', '', ms, GetMethodResultType(mtd), '' )]);
|
|
|
|
subs.Add(s);
|
2008-03-25 08:24:19 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
function GetParamsNames(mtd: TClassMethodDef): AnsiString;
|
|
|
|
var
|
|
|
|
i : Integer;
|
|
|
|
obj : TObject;
|
|
|
|
vname : AnsiString;
|
|
|
|
begin
|
|
|
|
vname := '';
|
|
|
|
Result := '';
|
|
|
|
for i := 0 to mtd.Items.Count - 1 do begin
|
|
|
|
obj := TObject(mtd.Items[i]);
|
|
|
|
if obj is TParamDescr then begin
|
|
|
|
if vName <> '' then Result := Result + vname + ', ';
|
|
|
|
vname := TParamDescr(obj)._Descr;
|
2008-04-01 06:29:04 +00:00
|
|
|
end else if obj is TObjCParameterDef then begin
|
|
|
|
if vname = '' then vname := TObjCParameterDef(obj)._Name;
|
2008-03-25 08:24:19 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
Result := Result + vname;
|
|
|
|
// Result := Copy(Result, 1, length(Result) - 2);
|
|
|
|
end;
|
|
|
|
|
2008-04-07 14:06:35 +00:00
|
|
|
|
|
|
|
// procedure writes out constructor entity to the implementation section
|
|
|
|
// with the followind structure
|
|
|
|
// assignes object's ClassID usinng GetClass method
|
|
|
|
// creates ObjC object calling objc_method Alloc
|
|
|
|
// adds procedure type and variable of objC init??? method, to wrap obj_SendMsg
|
|
|
|
// initialize ObjC object structure calling init??? method
|
|
|
|
|
|
|
|
procedure WriteOutConstructorMethod(mtd: TClassMethodDef; subs: TStrings);
|
2008-03-25 08:24:19 +00:00
|
|
|
var
|
2008-04-07 14:06:35 +00:00
|
|
|
typeName : AnsiString;
|
2008-03-25 08:24:19 +00:00
|
|
|
cl : TClassDef;
|
2008-04-07 14:06:35 +00:00
|
|
|
begin
|
|
|
|
cl := TClassDef(mtd.Owner);
|
|
|
|
ObjCMethodToProcType(mtd, typeName, subs);
|
|
|
|
subs.Add('var');
|
|
|
|
subs.Add(
|
|
|
|
Format(' vmethod: %s;', [typeName]));
|
|
|
|
subs.Add('begin');
|
|
|
|
subs.Add(' ClassID := getClass();');
|
|
|
|
subs.Add(' allocbuf := objc_msgSend(ClassID, sel_registerName(PChar(Str_alloc)), []);');
|
|
|
|
subs.Add(
|
|
|
|
Format(' vmethod := %s(@objc_msgSend);', [typeName]));
|
|
|
|
subs.Add(
|
|
|
|
Format(' Handle := vmethod(allocbuf, sel_registerName(PChar(Str%s_%s)), %s);', [cl._ClassName, mtd._Name, GetParamsNames(mtd)]));
|
|
|
|
subs.Add('end;');
|
|
|
|
end;
|
|
|
|
|
|
|
|
// writes out a method to implementation section
|
|
|
|
procedure WriteOutMethod(mtd: TClassMethodDef; subs: TStrings);
|
|
|
|
var
|
|
|
|
s : AnsiString;
|
|
|
|
typeName : AnsiString;
|
|
|
|
cl : TClassDef;
|
|
|
|
begin
|
|
|
|
cl := TClassDef(mtd.Owner);
|
|
|
|
s := Format('vmethod(Handle, sel_registerName(PChar(Str%s_%s)), %s)', [cl._ClassName, mtd._Name, GetParamsNames(mtd)]);
|
|
|
|
if ObjCToDelphiType(mtd.GetResultType._Name, mtd.GetResultType._IsPointer) <> '' then
|
|
|
|
s := 'Result := ' + s;
|
|
|
|
ObjCMethodToProcType(mtd, typeName, subs);
|
|
|
|
subs.Add('var');
|
|
|
|
subs.Add(
|
|
|
|
Format(' vmethod: %s;', [typeName]));
|
|
|
|
subs.Add('begin');
|
|
|
|
subs.Add(
|
|
|
|
Format(' vmethod := %s(@objc_msgSend);', [typeName]));
|
|
|
|
subs.Add(
|
|
|
|
Format(' %s;', [s]));
|
|
|
|
subs.Add('end;');
|
|
|
|
end;
|
|
|
|
|
|
|
|
// writes out a method to implementation section, that has no params
|
|
|
|
procedure WriteOutMethodNoParams(mtd: TClassMethodDef; subs: TStrings);
|
|
|
|
var
|
2008-03-25 08:24:19 +00:00
|
|
|
s : AnsiString;
|
2008-04-07 14:06:35 +00:00
|
|
|
res : AnsiString;
|
|
|
|
cl : TClassDef;
|
|
|
|
begin
|
|
|
|
cl := TClassDef(mtd.owner);
|
|
|
|
s := Format('objc_msgSend(Handle, sel_registerName(PChar(Str%s_%s)), [])', [cl._ClassName, mtd._Name]);
|
|
|
|
res := GetMethodResultType(mtd);
|
|
|
|
if res <> '' then begin
|
|
|
|
if res = 'objc.id' then s := 'Result := ' +s
|
|
|
|
else s := 'Result := '+res+'('+s+')'
|
|
|
|
end;
|
|
|
|
|
|
|
|
subs.Add('begin');
|
|
|
|
subs.Add(Format(' %s;', [s]));
|
|
|
|
subs.Add('end;');
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure WriteOutMethodToImplementation(mtd: TClassMethodDef; subs: TStrings);
|
|
|
|
var
|
|
|
|
cl : TClassDef;
|
2008-03-25 08:24:19 +00:00
|
|
|
typeName : AnsiString;
|
|
|
|
begin
|
2008-03-28 10:25:27 +00:00
|
|
|
typeName := '';
|
2008-03-25 08:24:19 +00:00
|
|
|
if not Assigned(mtd.Owner) or (not (TObject(mtd.Owner) is TClassDef)) then Exit; // method cannot be without owning class
|
|
|
|
cl := TClassDef(mtd.Owner);
|
|
|
|
|
2008-04-07 14:06:35 +00:00
|
|
|
subs.Add(GetMethodStr(cl, mtd, true));//writes out method header, like function NsType.NsName(params): Result
|
|
|
|
if IsMethodConstructor(cl, mtd) then
|
|
|
|
WriteOutConstructorMethod(mtd, subs)
|
|
|
|
else if not isAnyParam(mtd) then
|
|
|
|
WriteOutMethodNoParams(mtd, subs)
|
|
|
|
else
|
|
|
|
WriteOutMethod(mtd, subs);
|
2008-03-25 08:24:19 +00:00
|
|
|
subs.Add('');
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure WriteOutClassToImplementation(cl: TClassDef; subs: TStrings);
|
|
|
|
var
|
|
|
|
i : integer;
|
2008-03-27 15:28:02 +00:00
|
|
|
obj : TObject;
|
2008-03-25 08:24:19 +00:00
|
|
|
begin
|
|
|
|
subs.Add('{ '+cl._ClassName + ' }');
|
|
|
|
|
|
|
|
if cl._Category <> '' then begin
|
|
|
|
subs.Add(' //todo: classes of category');
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
|
|
|
|
subs.Add('');
|
|
|
|
subs.Add(GetProcFuncHead('getClass', cl._ClassName, '', 'objc.id'));
|
|
|
|
subs.Add('begin');
|
2008-04-07 14:06:35 +00:00
|
|
|
subs.Add(
|
|
|
|
Format(' Result := objc_getClass(Str%s_%s);', [cl._ClassName, cl._ClassName]));
|
|
|
|
subs.Add('end;');
|
2008-03-25 08:24:19 +00:00
|
|
|
subs.Add('');
|
|
|
|
|
2008-03-27 15:28:02 +00:00
|
|
|
for i := 0 to cl.Items.Count - 1 do begin
|
|
|
|
obj := TObject(cl.Items[i]);
|
|
|
|
if obj is TClassMethodDef then
|
|
|
|
WriteOutMethodToImplementation ( TClassMethodDef(cl.Items[i]), subs)
|
|
|
|
else if obj is TPrecompiler then
|
2008-03-28 10:25:27 +00:00
|
|
|
WriteOutIfDefPrecompiler( TPrecompiler(obj), '', subs);
|
2008-03-27 15:28:02 +00:00
|
|
|
end;
|
2008-03-25 08:24:19 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure WriteOutImplementationSection(hdr: TObjCHeader; st: TStrings);
|
|
|
|
var
|
|
|
|
i : Integer;
|
|
|
|
begin
|
2008-03-28 13:03:13 +00:00
|
|
|
BeginSection('IMPLEMENTATION', st);
|
2008-03-25 08:24:19 +00:00
|
|
|
try
|
|
|
|
for i := 0 to hdr.Items.Count - 1 do
|
2008-03-27 15:28:02 +00:00
|
|
|
if Assigned(hdr.Items[i]) then begin
|
|
|
|
if (TObject(hdr.Items[i]) is TClassDef) then
|
|
|
|
WriteOutClassToImplementation(TClassDef(hdr.Items[i]), st);
|
|
|
|
end;
|
2008-03-25 08:24:19 +00:00
|
|
|
finally
|
|
|
|
EndSection(st);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2008-03-28 10:25:27 +00:00
|
|
|
function AppleEnumType(items: TList; TypeDefIdx: Integer): Boolean;
|
|
|
|
var
|
|
|
|
EnumIdx : integer;
|
|
|
|
typedef : TTypeNameDef;
|
|
|
|
enumdef : TEnumTypeDef;
|
|
|
|
const
|
|
|
|
AppleInherit = 'NSUInteger';
|
|
|
|
begin
|
|
|
|
Result := false;
|
|
|
|
EnumIdx := TypeDefIdx - 1;
|
|
|
|
if (EnumIdx < 0) or (EnumIdx >= items.Count) then Exit;
|
2008-04-07 14:06:35 +00:00
|
|
|
|
2008-03-28 10:25:27 +00:00
|
|
|
if (TObject(items.Items[TypeDefIdx]) is TTypeNameDef) and
|
2008-04-07 14:06:35 +00:00
|
|
|
(TObject(items.Items[EnumIdx]) is TEnumTypeDef) then begin
|
2008-03-28 10:25:27 +00:00
|
|
|
typedef := TTypeNameDef(items.Items[TypeDefIdx]);
|
|
|
|
enumdef := TEnumTypeDef(items.Items[EnumIdx]);
|
|
|
|
end else
|
|
|
|
Exit;
|
|
|
|
|
2008-04-07 14:06:35 +00:00
|
|
|
if typedef._Inherited = AppleInherit then begin
|
|
|
|
enumdef._Name := typedef._TypeName;
|
|
|
|
Result := true;
|
|
|
|
end;
|
|
|
|
|
2008-03-28 10:25:27 +00:00
|
|
|
end;
|
|
|
|
|
2008-04-01 06:29:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
procedure FixAppleCategories(Items: TList; category: TClassDef);
|
|
|
|
var
|
|
|
|
i : Integer;
|
|
|
|
j : Integer;
|
|
|
|
cl : TClassdef;
|
|
|
|
begin
|
|
|
|
for i := 0 to Items.Count - 1 do
|
|
|
|
if TObject(Items[i]) is TClassDef then begin
|
|
|
|
cl := TClassDef(Items[i]);
|
2008-04-08 09:22:54 +00:00
|
|
|
if (cl._SuperClass <> '') and (cl._Category <> '') then
|
2008-04-01 06:29:04 +00:00
|
|
|
for j := 0 to category.Items.Count - 1 do begin
|
|
|
|
cl.Items.Add(category.Items[j]);
|
|
|
|
TEntity(category.Items[j]).owner := cl;
|
|
|
|
end; {of if}
|
|
|
|
end; {of if}
|
|
|
|
end;
|
|
|
|
|
2008-03-28 10:25:27 +00:00
|
|
|
procedure AppleHeaderFix(ent : TEntity);
|
|
|
|
var
|
|
|
|
i : Integer;
|
2008-04-01 06:29:04 +00:00
|
|
|
j : Integer;
|
2008-03-28 10:25:27 +00:00
|
|
|
obj : TEntity;
|
|
|
|
begin
|
2008-04-01 06:29:04 +00:00
|
|
|
// i := 0;
|
|
|
|
for i := 0 to ent.Items.Count - 1 do begin
|
2008-03-28 10:25:27 +00:00
|
|
|
obj := TEntity(ent.Items[i]);
|
2008-04-01 06:29:04 +00:00
|
|
|
if (obj is TTypeNameDef) and (AppleEnumType(ent.Items, i)) then begin
|
|
|
|
ent.Items[i] := nil;
|
|
|
|
obj.Free;
|
2008-04-08 09:22:54 +00:00
|
|
|
end else if (obj is TClassDef) and ((TClassDef(obj)._SuperClass = '') and (TClassDef(obj)._Category <> ''))then begin
|
2008-04-01 06:29:04 +00:00
|
|
|
FixAppleCategories(ent.Items, TClassDef(obj));
|
|
|
|
ent.Items[i] := nil;
|
|
|
|
obj.Free;
|
2008-04-08 09:22:54 +00:00
|
|
|
end else if (obj is TClassDef) and ((TClassDef(obj)._Category = '') and (TClassDef(obj)._ClassName = 'NSObject')) then begin
|
|
|
|
if TClassDef(obj)._SuperClass = '' then
|
|
|
|
TClassDef(obj)._SuperClass := 'TObject'
|
2008-04-01 06:29:04 +00:00
|
|
|
end else if (obj is TParamDescr) then begin
|
|
|
|
if IsPascalReserved(TParamDescr(obj)._Descr) then
|
|
|
|
TParamDescr(obj)._Descr := '_'+TParamDescr(obj)._Descr;
|
|
|
|
end else if (obj is TObjCParameterDef) then begin
|
|
|
|
if IsPascalReserved(TObjCParameterDef(obj)._Name) then
|
|
|
|
TObjCParameterDef(obj)._Name := '_' + TObjCParameterDef(obj)._Name;
|
|
|
|
end;
|
2008-03-28 10:25:27 +00:00
|
|
|
end;
|
2008-04-01 06:29:04 +00:00
|
|
|
|
|
|
|
j := 0;
|
|
|
|
for i := 0 to ent.Items.Count - 1 do
|
|
|
|
if Assigned(ent.Items[i]) then begin
|
|
|
|
ent.Items[j] := ent.Items[i];
|
|
|
|
inc(j);
|
|
|
|
end;
|
|
|
|
ent.Items.Count := j;
|
|
|
|
|
2008-03-28 10:25:27 +00:00
|
|
|
for i := 0 to ent.Items.Count - 1 do
|
|
|
|
AppleHeaderFix( TEntity(ent.Items[i]));
|
2008-04-08 09:22:54 +00:00
|
|
|
|
2008-03-28 10:25:27 +00:00
|
|
|
end;
|
|
|
|
|
2008-03-25 08:24:19 +00:00
|
|
|
procedure WriteOutIncludeFile(hdr: TObjCHeader; st: TStrings);
|
2008-03-28 10:25:27 +00:00
|
|
|
var
|
2008-04-01 06:29:04 +00:00
|
|
|
// i : integer;
|
2008-03-28 10:25:27 +00:00
|
|
|
cmt : TComment;
|
2008-03-25 08:24:19 +00:00
|
|
|
begin
|
2008-04-01 06:29:04 +00:00
|
|
|
try
|
2008-04-08 09:22:54 +00:00
|
|
|
st.AddStrings(ConvertSettings.ConvertPrefix);
|
|
|
|
|
2008-04-01 06:29:04 +00:00
|
|
|
if hdr.Items.Count <= 0 then Exit;
|
|
|
|
AppleHeaderFix(hdr);
|
|
|
|
|
|
|
|
// .inc header-comment is the first comment entity in .h file , if any
|
|
|
|
if TObject(hdr.Items[0]) is TComment then begin
|
|
|
|
cmt := TComment(hdr.Items[0]);
|
|
|
|
st.Add('(*' + cmt._Comment + '*)');
|
|
|
|
cmt.Free;
|
|
|
|
hdr.Items.Delete(0);
|
|
|
|
end;
|
2008-03-28 10:25:27 +00:00
|
|
|
|
2008-04-01 06:29:04 +00:00
|
|
|
WriteOutHeaderSection(hdr, st);
|
|
|
|
WriteOutClassesSection(hdr, st);
|
|
|
|
WriteOutImplementationSection(hdr, st);
|
|
|
|
except
|
2008-03-28 10:25:27 +00:00
|
|
|
end;
|
2008-03-25 08:24:19 +00:00
|
|
|
end;
|
|
|
|
|
2008-04-07 14:06:35 +00:00
|
|
|
procedure WriteOutMainFramework(hdr: TObjCHeader; st: TStrings);
|
|
|
|
//var
|
|
|
|
// i : integer;
|
|
|
|
// nm : AnsiString;
|
|
|
|
begin
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TConvertSettings }
|
|
|
|
|
|
|
|
constructor TConvertSettings.Create;
|
|
|
|
begin
|
|
|
|
IgnoreIncludes := TStringList.Create;
|
|
|
|
IgnoreIncludes.CaseSensitive := false;
|
|
|
|
DefineReplace := TReplaceList.Create;
|
|
|
|
TypeDefReplace := TReplaceList.Create; // replaces for default types
|
2008-04-08 09:22:54 +00:00
|
|
|
ConvertPrefix := TStringList.Create;
|
2008-04-07 14:06:35 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TConvertSettings.Destroy;
|
|
|
|
begin
|
|
|
|
IgnoreIncludes.Free;
|
|
|
|
TypeDefReplace.Free;
|
|
|
|
DefineReplace.Free;
|
2008-04-08 09:22:54 +00:00
|
|
|
ConvertPrefix.Free;
|
2008-04-07 14:06:35 +00:00
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure InitConvertSettings;
|
|
|
|
begin
|
|
|
|
with ConvertSettings.IgnoreIncludes do begin
|
|
|
|
// must not be $included, because they are used
|
2008-04-08 10:45:08 +00:00
|
|
|
// Add('Foundation/');
|
|
|
|
// Add('Foundation/NSObject.h');
|
|
|
|
// Add('NSObjCRuntime.h');
|
|
|
|
// Add('Foundation/NSObject.h');
|
|
|
|
// Add('Foundation/Foundation.h');
|
2008-04-07 14:06:35 +00:00
|
|
|
end;
|
2008-04-08 10:45:08 +00:00
|
|
|
|
2008-04-07 14:06:35 +00:00
|
|
|
with ConvertSettings do begin
|
|
|
|
DefineReplace['MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_2'] := 'MAC_OS_X_VERSION_10_2';
|
|
|
|
DefineReplace['MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3'] := 'MAC_OS_X_VERSION_10_3';
|
|
|
|
DefineReplace['MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4'] := 'MAC_OS_X_VERSION_10_4';
|
|
|
|
DefineReplace['MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5'] := 'MAC_OS_X_VERSION_10_5';
|
|
|
|
DefineReplace['__LP64__'] := 'LP64';
|
|
|
|
TypeDefReplace['uint32_t'] := 'LongWord';
|
|
|
|
TypeDefReplace['uint8_t'] := 'byte';
|
|
|
|
TypeDefReplace['NSUInteger'] := 'LongWord';
|
2008-04-08 09:22:54 +00:00
|
|
|
TypeDefReplace['NSInteger'] := 'Integer';
|
2008-04-07 14:06:35 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TReplaceList }
|
|
|
|
|
|
|
|
constructor TReplaceList.Create;
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
fItems := TStringList.Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TReplaceList.Destroy;
|
|
|
|
begin
|
|
|
|
fItems.Free;
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TReplaceList.GetCaseSense: Boolean;
|
|
|
|
begin
|
|
|
|
Result := fItems.CaseSensitive;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TReplaceList.SetCaseSense(AValue: Boolean);
|
|
|
|
begin
|
|
|
|
fITems.CaseSensitive := AValue;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TReplaceList.GetReplace(const ARepl: AnsiString): AnsiString;
|
|
|
|
var
|
|
|
|
i : integer;
|
|
|
|
begin
|
|
|
|
i := fItems.IndexOf(ARepl);
|
|
|
|
if i < 0 then Result := ''
|
|
|
|
else Result := TReplaceItem(fItems.Objects[i]).ReplaceStr;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TReplaceList.SetReplace(const ARepl, AValue: AnsiString);
|
|
|
|
var
|
|
|
|
i : integer;
|
|
|
|
it : TReplaceItem;
|
|
|
|
begin
|
|
|
|
i := fItems.IndexOf(ARepl);
|
|
|
|
if i < 0 then begin
|
|
|
|
it := TReplaceItem.Create;
|
|
|
|
it.ReplaceStr := AValue;
|
|
|
|
fItems.AddObject(Arepl, it);
|
|
|
|
end else
|
|
|
|
TReplaceItem(fItems.Objects[i]).ReplaceStr := AValue;
|
|
|
|
end;
|
|
|
|
|
|
|
|
initialization
|
|
|
|
ConvertSettings := TConvertSettings.Create;
|
|
|
|
InitConvertSettings;
|
|
|
|
|
|
|
|
finalization
|
|
|
|
ConvertSettings.Free;
|
|
|
|
|
2008-03-25 08:24:19 +00:00
|
|
|
end.
|