fixgtkcdecl.pp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. {$mode objfpc}
  2. {$H+}
  3. uses
  4. sysutils;
  5. Function PosIdx (Const Substr : AnsiString; Const Source : AnsiString;i:longint) : Longint;
  6. var
  7. S : String;
  8. begin
  9. PosIdx:=0;
  10. if Length(SubStr)=0 then
  11. exit;
  12. while (i <= length (Source) - length (substr)) do
  13. begin
  14. inc (i);
  15. S:=copy(Source,i,length(Substr));
  16. if S=SubStr then
  17. exit(i);
  18. end;
  19. end;
  20. function trimspace(const s:string):string;
  21. var
  22. i,j : longint;
  23. begin
  24. i:=length(s);
  25. while (i>0) and (s[i] in [#9,' ']) do
  26. dec(i);
  27. j:=1;
  28. while (j<i) and (s[j] in [#9,' ']) do
  29. inc(j);
  30. trimspace:=Copy(s,j,i-j+1);
  31. end;
  32. function trimbegin(const s:string):string;
  33. var
  34. i,j : longint;
  35. begin
  36. i:=length(s);
  37. j:=1;
  38. while (j<i) and (s[j] in [#9,' ']) do
  39. inc(j);
  40. trimbegin:=Copy(s,j,i-j+1);
  41. end;
  42. procedure Replace(var s:string;const s1,s2:string);
  43. var
  44. last,
  45. i : longint;
  46. begin
  47. last:=0;
  48. repeat
  49. i:=posidx(s1,uppercase(s),last);
  50. if (i>0) then
  51. begin
  52. Delete(s,i,length(s1));
  53. Insert(s2,s,i);
  54. last:=i+1;
  55. end;
  56. until (i=0);
  57. end;
  58. procedure Conv(const fn: string);
  59. var
  60. t,f : text;
  61. lasts,funcname,
  62. s,ups : string;
  63. k,i,j : integer;
  64. gotisfunc,
  65. impl : boolean;
  66. begin
  67. writeln('processing ',fn);
  68. assign(t,fn);
  69. assign(f,'fixgtk.tmp');
  70. reset(t);
  71. rewrite(f);
  72. funcname:='';
  73. gotisfunc:=false;
  74. impl:=false;
  75. while not eof(t) do
  76. begin
  77. readln(t,s);
  78. { Remove unit part }
  79. if s='{$ifndef gtk_include_files}' then
  80. begin
  81. while not eof(t) do
  82. begin
  83. readln(t,s);
  84. if Pos('{$ifdef read_interface}',s)>0 then
  85. begin
  86. writeln(f,'{****************************************************************************');
  87. writeln(f,' Interface');
  88. writeln(f,'****************************************************************************}');
  89. writeln(f,'');
  90. writeln(f,s);
  91. break;
  92. end;
  93. if Pos('{$ifdef read_implementation}',s)>0 then
  94. begin
  95. writeln(f,'{****************************************************************************');
  96. writeln(f,' Implementation');
  97. writeln(f,'****************************************************************************}');
  98. writeln(f,'');
  99. writeln(f,s);
  100. impl:=true;
  101. break;
  102. end;
  103. if Pos('$Log:',s)>0 then
  104. begin
  105. writeln(f,'{');
  106. writeln(f,s);
  107. break;
  108. end;
  109. end;
  110. continue;
  111. end;
  112. Replace(s,'PROCEDURE','procedure');
  113. Replace(s,'FUNCTION','function');
  114. Replace(s,'FUNCTION ','function ');
  115. Replace(s,'PPG','PPG');
  116. Replace(s,'PG','PG');
  117. Replace(s,'GCHAR','gchar');
  118. Replace(s,'GUCHAR','guchar');
  119. Replace(s,'GINT','gint');
  120. Replace(s,'GUINT','guint');
  121. Replace(s,'GBOOL','gbool');
  122. Replace(s,'GSHORT','gshort');
  123. Replace(s,'GUSHORT','gushort');
  124. Replace(s,'GLONG','glong');
  125. Replace(s,'GULONG','gulong');
  126. Replace(s,'GFLOAT','gfloat');
  127. Replace(s,'GDOUBLE','gdouble');
  128. Replace(s,'GPOINTER','gpointer');
  129. Replace(s,'GCONSTPOINTER','gconstpointer');
  130. ups:=UpperCase(s);
  131. if Pos('IMPLEMENTATION',ups)>0 then
  132. impl:=true;
  133. i:=Pos('PROCEDURE',ups);
  134. if i>0 then
  135. if Pos('_PROCEDURE',ups)>0 then
  136. i:=0;
  137. if i=0 then
  138. begin
  139. i:=Pos('FUNCTION',ups);
  140. if Pos('_FUNCTION',ups)>0 then
  141. i:=0;
  142. end;
  143. if i<>0 then
  144. begin
  145. { Remove Spaces }
  146. j:=PosIdx(' ',s,i);
  147. while (j>0) do
  148. begin
  149. Delete(s,j,1);
  150. i:=j-1;
  151. j:=PosIdx(' ',s,i);
  152. end;
  153. ups:=UpperCase(s);
  154. { Fix Cdecl }
  155. if (Pos('g_',s)<>0) or (Pos('TGtkType',s)<>0) or
  156. ((i>2) and (s[i-2] in [':','='])) then
  157. begin
  158. j:=Pos('CDECL;',ups);
  159. if j=0 then
  160. j:=Length(s)+1
  161. else
  162. begin
  163. k:=Pos('{$IFNDEF WIN32}CDECL;{$ENDIF}',ups);
  164. if k>0 then
  165. begin
  166. j:=k;
  167. k:=29;
  168. end
  169. else
  170. begin
  171. k:=Pos('{$IFDEF WIN32}STDCALL;{$ELSE}CDECL;{$ENDIF}',ups);
  172. if k>0 then
  173. begin
  174. j:=k;
  175. k:=43;
  176. end
  177. else
  178. k:=6;
  179. end;
  180. Delete(s,j,k);
  181. end;
  182. Insert('cdecl;',s,j);
  183. end;
  184. ups:=UpperCase(s);
  185. if (not gotisfunc) and (Pos('function GTK_IS_',s)>0) then
  186. gotisfunc:=true;
  187. if not gotisfunc then
  188. begin
  189. j:=Pos('_GET_TYPE:TGTKTYPE',ups);
  190. funcname:=Copy(ups,14,j-14);
  191. if (i=1) and (j>0) then
  192. begin
  193. writeln(f,'function GTK_'+funcname+'_TYPE'+Copy(s,j+9,Length(s)-(j+9)+1));
  194. if impl then
  195. begin
  196. writeln(f,'function GTK_IS_',funcname,'(obj:pointer):boolean;');
  197. writeln(f,'begin');
  198. writeln(f,' GTK_IS_',funcname,':=(obj<>nil) and GTK_IS_',funcname,'_CLASS(PGtkTypeObject(obj)^.klass);');
  199. writeln(f,'end;');
  200. writeln(f,'function GTK_IS_',funcname,'_CLASS(klass:pointer):boolean;');
  201. writeln(f,'begin');
  202. writeln(f,' GTK_IS_',funcname,'_CLASS:=(klass<>nil) and (PGtkTypeClass(klass)^.thetype=GTK_',funcname,'_TYPE);');
  203. writeln(f,'end;');
  204. end
  205. else
  206. begin
  207. writeln(f,'function GTK_IS_',funcname,'(obj:pointer):boolean;');
  208. writeln(f,'function GTK_IS_',funcname,'_CLASS(klass:pointer):boolean;');
  209. end;
  210. writeln(f,'');
  211. end;
  212. end;
  213. end
  214. else
  215. { No procedure/function }
  216. begin
  217. { Remove the GTK_IS_ type decls }
  218. if (Copy(s,1,9)=' GTK_IS_') and (Pos('=',s)>0) and (Pos(':=',s)=0) then
  219. begin
  220. lasts:=s;
  221. continue;
  222. end;
  223. end;
  224. { Align function with procedure }
  225. if Copy(s,1,8)='function' then
  226. Insert(' ',s,9);
  227. lasts:=s;
  228. writeln(f,s);
  229. end;
  230. close(f);
  231. close(t);
  232. erase(t);
  233. rename(f,fn);
  234. end;
  235. var
  236. i : integer;
  237. dir : tsearchrec;
  238. begin
  239. for i:=1to paramcount do
  240. begin
  241. if findfirst(paramstr(i),$20,dir)=0 then
  242. repeat
  243. Conv(dir.name);
  244. until findnext(dir)<>0;
  245. findclose(dir);
  246. end;
  247. end.