pastoc.pas 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. {
  2. This file is part of the Free Pascal run time library.
  3. A file in Amiga system run time library.
  4. Copyright (c) 2000-2003 by Nils Sjoholm
  5. member of the Amiga RTL development team.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {
  13. History:
  14. Added the define use_amiga_smartlink.
  15. 13 Jan 2003.
  16. [email protected] Nils Sjoholm
  17. }
  18. {$I useamigasmartlink.inc}
  19. {$ifdef use_amiga_smartlink}
  20. {$smartlink on}
  21. {$endif use_amiga_smartlink}
  22. unit PasToC;
  23. interface
  24. function Pas2C( s : String): PChar;
  25. implementation
  26. const
  27. MEMF_ANY = %000000000000000000000000; { * Any type of memory will do * }
  28. MEMF_PUBLIC = %000000000000000000000001;
  29. MEMF_CLEAR = %000000010000000000000000;
  30. Type
  31. ULONG = Longint;
  32. pRemember = ^tRemember;
  33. tRemember = record
  34. NextRemember : pRemember;
  35. RememberSize : ULONG;
  36. Memory : Pointer;
  37. end;
  38. var
  39. myrememberkey : pRemember;
  40. remember_exit : pointer;
  41. FUNCTION fpcAllocRemember(VAR rememberKey : pRemember; size : ULONG; flags : ULONG) : POINTER;
  42. BEGIN
  43. ASM
  44. MOVE.L A6,-(A7)
  45. MOVEA.L rememberKey,A0
  46. MOVE.L size,D0
  47. MOVE.L flags,D1
  48. MOVEA.L _IntuitionBase,A6
  49. JSR -396(A6)
  50. MOVEA.L (A7)+,A6
  51. MOVE.L D0,@RESULT
  52. END;
  53. END;
  54. PROCEDURE fpcFreeRemember(VAR rememberKey : pRemember; reallyForget : LONGINT);
  55. BEGIN
  56. ASM
  57. MOVE.L A6,-(A7)
  58. MOVEA.L rememberKey,A0
  59. MOVE.L reallyForget,D0
  60. MOVEA.L _IntuitionBase,A6
  61. JSR -408(A6)
  62. MOVEA.L (A7)+,A6
  63. END;
  64. END;
  65. Function StringPcharCopy(Dest: PChar; Source: String):PChar;
  66. var
  67. counter : byte;
  68. Begin
  69. counter := 0;
  70. { if empty pascal string }
  71. { then setup and exit now }
  72. if Source = '' then
  73. Begin
  74. Dest[0] := #0;
  75. StringPCharCopy := Dest;
  76. exit;
  77. end;
  78. for counter:=1 to length(Source) do
  79. begin
  80. Dest[counter-1] := Source[counter];
  81. end;
  82. { terminate the string }
  83. Dest[counter] := #0;
  84. StringPcharCopy:=Dest;
  85. end;
  86. function Pas2C(s : string): PChar;
  87. var
  88. themem : Pointer;
  89. begin
  90. themem := fpcAllocRemember(myrememberkey,length(s)+1, MEMF_CLEAR or MEMF_PUBLIC);
  91. if themem = nil then begin
  92. writeln('Can''t allocate memory for string');
  93. halt(20);
  94. end else begin
  95. StringPCharCopy(themem,s);
  96. Pas2C := themem;
  97. end;
  98. end;
  99. procedure ReleasePasToC;
  100. begin
  101. ExitProc := remember_exit;
  102. fpcFreeRemember(myrememberkey,1);
  103. end;
  104. begin
  105. myrememberkey := nil;
  106. remember_exit := ExitProc;
  107. ExitProc := @ReleasePasToC;
  108. end.