objpas.pp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1998,99 by the Free Pascal development team
  5. This unit makes Free Pascal as much as possible Delphi compatible
  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. {$Mode ObjFpc}
  13. {$I-,S-}
  14. unit objpas;
  15. interface
  16. type
  17. { first, in object pascal, the types must be redefined }
  18. smallint = system.integer;
  19. integer = system.longint;
  20. { the compiler searches in the objpas unit for the tvarrec symbol }
  21. TVarRec = System.TVarRec;
  22. PVarRec = ^TVarRec;
  23. {****************************************************************************
  24. Compatibility routines.
  25. ****************************************************************************}
  26. { Untyped file support }
  27. Procedure AssignFile(Var f:File;const Name:string);
  28. Procedure AssignFile(Var f:File;p:pchar);
  29. Procedure AssignFile(Var f:File;c:char);
  30. Procedure CloseFile(Var f:File);
  31. { Text file support }
  32. Procedure AssignFile(Var t:Text;const s:string);
  33. Procedure AssignFile(Var t:Text;p:pchar);
  34. Procedure AssignFile(Var t:Text;c:char);
  35. Procedure CloseFile(Var t:Text);
  36. { Typed file supoort }
  37. Procedure AssignFile(Var f:TypedFile;const Name:string);
  38. Procedure AssignFile(Var f:TypedFile;p:pchar);
  39. Procedure AssignFile(Var f:TypedFile;c:char);
  40. { ParamStr should return also an ansistring }
  41. Function ParamStr(Param : Integer) : Ansistring;
  42. {$ifdef HasResourceStrings}
  43. { Resourcestring support }
  44. Function GetResourceString(Hash : Longint) : AnsiString;
  45. Procedure ResetResourceTables;
  46. Function SetResourceString(Hash : longint; Const Value : AnsiString) : Boolean;
  47. {$endif}
  48. Procedure Getmem(Var p:pointer;Size:Longint);
  49. Procedure Freemem(Var p:pointer;Size:Longint);
  50. Procedure Freemem(Var p:pointer);
  51. implementation
  52. {****************************************************************************
  53. Compatibility routines.
  54. ****************************************************************************}
  55. { Untyped file support }
  56. Procedure AssignFile(Var f:File;const Name:string);
  57. begin
  58. System.Assign (F,Name);
  59. end;
  60. Procedure AssignFile(Var f:File;p:pchar);
  61. begin
  62. System.Assign (F,P);
  63. end;
  64. Procedure AssignFile(Var f:File;c:char);
  65. begin
  66. System.Assign (F,C);
  67. end;
  68. Procedure CloseFile(Var f:File);
  69. begin
  70. System.Close(f);
  71. end;
  72. { Text file support }
  73. Procedure AssignFile(Var t:Text;const s:string);
  74. begin
  75. System.Assign (T,S);
  76. end;
  77. Procedure AssignFile(Var t:Text;p:pchar);
  78. begin
  79. System.Assign (T,P);
  80. end;
  81. Procedure AssignFile(Var t:Text;c:char);
  82. begin
  83. System.Assign (T,C);
  84. end;
  85. Procedure CloseFile(Var t:Text);
  86. begin
  87. Close(T);
  88. end;
  89. { Typed file supoort }
  90. Procedure AssignFile(Var f:TypedFile;const Name:string);
  91. begin
  92. system.Assign(F,Name);
  93. end;
  94. Procedure AssignFile(Var f:TypedFile;p:pchar);
  95. begin
  96. system.Assign (F,p);
  97. end;
  98. Procedure AssignFile(Var f:TypedFile;c:char);
  99. begin
  100. system.Assign (F,C);
  101. end;
  102. Function ParamStr(Param : Integer) : Ansistring;
  103. Var Len : longint;
  104. begin
  105. if (Param>=0) and (Param<argc) then
  106. begin
  107. Len:=0;
  108. While Argv[Param][Len]<>#0 do
  109. Inc(len);
  110. SetLength(Result,Len);
  111. If Len>0 then
  112. Move(Argv[Param][0],Result[1],Len);
  113. end
  114. else
  115. paramstr:='';
  116. end;
  117. { ---------------------------------------------------------------------
  118. Delphi-Style memory management
  119. ---------------------------------------------------------------------}
  120. Type PLongint = ^Longint;
  121. Procedure Getmem(Var p:pointer;Size:Longint);
  122. begin
  123. SysGetmem(P,Size+SizeOf(Longint));
  124. PLongint(P)^:=Size;
  125. Inc(P,SizeOf(Longint));
  126. end;
  127. Procedure DummyFreemem(Var p:pointer;Size:Longint);
  128. begin
  129. FreeMem(P);
  130. end;
  131. Procedure Freemem(Var p:pointer;Size:Longint);
  132. begin
  133. Freemem(P);
  134. end;
  135. Procedure Freemem(Var p:pointer);
  136. begin
  137. Dec(P,SizeOf(Longint));
  138. SysFreemem(P,Plongint(P)^);
  139. end;
  140. Var OldMM,NEWMM : TmemoryManager;
  141. Procedure InitMemoryManager;
  142. begin
  143. GetMemoryManager(OldMM);
  144. NewMM.FreeMem:=@DummyFreeMem;
  145. NewMM.GetMem:=@GetMem;
  146. SetMemoryManager(NewMM);
  147. end;
  148. Procedure ResetMemoryManager;
  149. begin
  150. SetMemoryManager(OldMM);
  151. end;
  152. {$IFDEF HasResourceStrings}
  153. { ---------------------------------------------------------------------
  154. ResourceString support
  155. ---------------------------------------------------------------------}
  156. Type
  157. TResourceStringRecord = Packed Record
  158. DefaultValue,
  159. CurrentValue : AnsiString;
  160. HashValue : longint;
  161. end;
  162. TResourceStringTable = Packed Record
  163. Count : longint;
  164. Resrec : Array[Word] of TResourceStringRecord;
  165. end;
  166. Var
  167. ResourceStringTable : TResourceStringTable; External Name 'RESOURCESTRINGLIST';
  168. Function FindHashIndex (Value : Longint) : Longint;
  169. Var
  170. I : longint;
  171. begin
  172. // Linear search, later we can implement binary search.
  173. With ResourceStringTable do
  174. For I:=0 to Count-1 do
  175. If Value=Resrec[I].HashValue then
  176. begin
  177. Result:=I;
  178. exit;
  179. end;
  180. Result:=-1;
  181. end;
  182. Function GetResourceString(Hash : Longint) : AnsiString;[Public,Alias : 'FPC_GETRESOURCESTRING'];
  183. begin
  184. Hash:=FindHashIndex(Hash);
  185. If Hash<>-1 then
  186. Result:=ResourceStringTable.ResRec[Hash].CurrentValue
  187. else
  188. Result:='';
  189. end;
  190. Function SetResourceString(Hash : longint; Const Value : AnsiString) : Boolean;
  191. begin
  192. Hash:=FindHashIndex(Hash);
  193. Result:=Hash<>-1;
  194. If Result then
  195. ResourceStringTable.ResRec[Hash].CurrentValue:=Value;
  196. end;
  197. Procedure ResetResourceTables;
  198. Var I : longint;
  199. begin
  200. With ResourceStringTable do
  201. For I:=0 to Count-1 do
  202. With ResRec[i] do
  203. CurrentValue:=DefaultValue;
  204. end;
  205. {$endif}
  206. Initialization
  207. {$IFDEF HasResourceStrings}
  208. ResetResourceTables;
  209. {$endif}
  210. InitMemoryManager;
  211. finalization
  212. ResetMemoryManager;
  213. end.
  214. {
  215. $Log$
  216. Revision 1.30 1999-08-15 18:56:13 michael
  217. + Delphi-style getmem and freemem
  218. Revision 1.29 1999/07/23 23:13:54 peter
  219. * array[cardinal] is buggy, use array[word]
  220. * small fix in getresourcestring
  221. Revision 1.28 1999/07/23 22:51:11 michael
  222. * Added HasResourceStrings check
  223. Revision 1.27 1999/07/22 20:30:13 michael
  224. + Implemented resource stuff
  225. Revision 1.26 1999/07/07 10:04:04 michael
  226. + Paramstr now returns cmdline args >255 chars in ansistring objpas.pp
  227. Revision 1.25 1999/07/06 22:44:22 florian
  228. + implemented a paramstr function which returns an ansistring, nevertheless
  229. it is limited to 255 chars because it maps to the system.paramstr, maybe
  230. we should use cmdline instead
  231. Revision 1.24 1999/05/17 21:52:43 florian
  232. * most of the Object Pascal stuff moved to the system unit
  233. Revision 1.23 1999/05/13 21:54:28 peter
  234. * objpas fixes
  235. Revision 1.22 1999/04/16 20:47:20 florian
  236. + tobject.messagestringtable function for Megido/GTK support
  237. added
  238. Revision 1.21 1999/02/23 14:04:36 pierre
  239. * call %edi => call *%edi
  240. Revision 1.20 1999/02/22 23:30:54 florian
  241. + TObject.Dispatch and TObject.DispatchStr added, working
  242. Revision 1.19 1998/12/24 10:12:03 michael
  243. Implemented AssignFile and CloseFile compatibility
  244. Revision 1.18 1998/10/12 12:42:58 florian
  245. * as operator runtime error can be now caught by an errorproc
  246. Revision 1.17 1998/10/05 12:32:53 peter
  247. + assert() support
  248. Revision 1.16 1998/10/03 15:07:16 florian
  249. + TObject.AfterConstruction and TObject.BeforeDestruction of Delphi 4
  250. Revision 1.15 1998/09/24 16:13:48 michael
  251. Changes in exception and open array handling
  252. Revision 1.14 1998/09/23 12:40:43 michael
  253. Fixed TVarRec again. Should be OK now
  254. Revision 1.13 1998/09/23 12:18:32 michael
  255. + added VType in TVArRec
  256. Revision 1.12 1998/09/23 10:00:47 peter
  257. * tvarrec should be 8 bytes
  258. Revision 1.11 1998/09/22 15:30:07 peter
  259. * array of const update
  260. Revision 1.9 1998/09/16 13:08:19 michael
  261. Added AbstractErrorHandler
  262. Revision 1.8 1998/09/06 21:27:31 florian
  263. + method tobject.classinfo added
  264. Revision 1.7 1998/09/04 08:49:06 peter
  265. * 0.99.5 doesn't compile a whole objpas anymore to overcome crashes
  266. Revision 1.6 1998/08/23 20:58:52 florian
  267. + rtti for objects and classes
  268. + TObject.GetClassName implemented
  269. Revision 1.5 1998/07/30 16:10:11 michael
  270. + Added support for ExceptProc+
  271. Revision 1.4 1998/07/29 15:44:33 michael
  272. included sysutils and math.pp as target. They compile now.
  273. Revision 1.3 1998/07/29 10:09:28 michael
  274. + put in exception support
  275. Revision 1.2 1998/03/25 23:40:24 florian
  276. + stuff from old objpash.inc and objpas.inc merged in
  277. }