-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallingtolib.pas
79 lines (78 loc) · 2.17 KB
/
callingtolib.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
uses system.runtime.interopservices;
begin
var fpcpath:=readstring('fpcpath(you can use the one included with lazarus): ');
var t,o:text;
var inpname:=readstring('Enter filename: ');
var inpnamefuncs:=inpname+'funcs';
assign(t,inpname+'.pas');
assign(o,inpnamefuncs+'.pas');
t.reset;
o.Rewrite;
o.Writeln(
'library '+inpnamefuncs+';'+NewLine+
' '+NewLine+
'{$mode objfpc}{$H+}'+NewLine+
' '+NewLine);
var funcnames:array of string:=new string[1];
var funcnamesindex:integer:=0;
while not t.Eof do
begin
var s:=t.ReadString;
if s.Contains('cdecl') or s.Contains('stdcall') then
begin
if funcnamesindex >= funcnames.Length then
begin
setlength(funcnames,funcnames.Length+1);
end;
var strarr:=s.Split(' ');
strarr:=strarr[1].Split('(');
funcnames[funcnamesindex]:=strarr[0];
funcnamesindex+=1;
o.Writeln(s);
s:=t.ReadString;
while not s.Contains('begin') do
begin
o.Writeln(s);
s:=t.ReadString;
end;
var begins:integer:=1;
var ends:integer:=0;
while not (begins=ends) do
begin
if s.Contains('end;') then
begin
ends+=1;
o.Writeln('end;');
if begins=ends then break;
end
else
o.Writeln(s);
s:=t.ReadString;
end;
end;
end;
o.Writeln('exports');
for var i:integer :=0 to funcnames.Length-1 do
begin
o.write(' '+funcnames[i]);
if i<funcnames.Length-1 then
begin
o.writeln(', ');
end
else o.writeln(';');
end;
o.Writeln;
o.Writeln('end.');
o.Flush;
exec(fpcpath,'-Sd '+inpnamefuncs+'.pas');
if not(system.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(system.Runtime.InteropServices.OSPlatform.Windows)) then
begin
if fileexists('lib'+inpnamefuncs+'.dylib') then
begin
renamefile('lib'+inpnamefuncs+'.dylib',inpnamefuncs+'.dll');
end else if fileexists(inpnamefuncs+'.so') then
begin
renamefile(inpnamefuncs+'.so',inpnamefuncs+'.dll');
end;
end;
end.