objpas.pp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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(Const Name : ShortString) : AnsiString;
  45. Procedure ResetResourceTables;
  46. Function SetResourceString(Const Name : Shortstring; 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. PResourceStringRecord = ^TResourceStringRecord;
  158. TResourceStringRecord = Packed Record
  159. DefaultValue,
  160. CurrentValue : AnsiString;
  161. HashValue : longint;
  162. Name : ShortString;
  163. end;
  164. TResourceStringTable = Packed Record
  165. Count : longint;
  166. Resrec : Array[Word] of TResourceStringRecord;
  167. end;
  168. Var
  169. ResourceStringTable : TResourceStringTable; External Name 'RESOURCESTRINGLIST';
  170. function CalcStringHashValue(Const N : ShortString) : longint;
  171. Var hash,g,I : longint;
  172. begin
  173. hash:=0;
  174. For I:=1 to Length(N) do { 0 terminated }
  175. begin
  176. hash:=hash shl 4;
  177. inc(Hash,Ord(N[i]));
  178. g:=hash and ($f shl 28);
  179. if g<>0 then
  180. begin
  181. hash:=hash xor (g shr 24);
  182. hash:=hash xor g;
  183. end;
  184. end;
  185. If Hash=0 then
  186. CalcStringHashValue:=Not(0)
  187. else
  188. CalcStringHashValue:=Hash;
  189. end;
  190. Function FindIndex (Const Value : Shortstring) : Longint;
  191. Var
  192. I,Hash : longint;
  193. begin
  194. // Linear search, later we can implement binary search.
  195. Hash:=CalcStringHashValue(Value);
  196. Result:=-1;
  197. With ResourceStringTable do
  198. For I:=0 to Count-1 do
  199. If Hash=Resrec[I].HashValue then
  200. begin
  201. Result:=I;
  202. Break;
  203. end;
  204. If Result<>-1 then
  205. begin
  206. With ResourceStringTable do
  207. While (Result<=Count) do
  208. If Value=ResRec[Result].Name then
  209. exit
  210. else
  211. Inc(Result);
  212. Result:=-1;
  213. end;
  214. end;
  215. Function GetResourceString(Const Name : ShortString) : AnsiString;[Public,Alias : 'FPC_GETRESOURCESTRING'];
  216. Var I : longint;
  217. begin
  218. I:=FindIndex(Name);
  219. If I<>-1 then
  220. Result:=ResourceStringTable.ResRec[I].CurrentValue
  221. else
  222. Result:='';
  223. end;
  224. Function SetResourceString(Const Name : ShortString; Const Value : AnsiString) : Boolean;
  225. Var Hash : Longint;
  226. begin
  227. Hash:=FindIndex(Name);
  228. Result:=Hash<>-1;
  229. If Result then
  230. ResourceStringTable.ResRec[Hash].CurrentValue:=Value;
  231. end;
  232. Procedure ResetResourceTables;
  233. Var I : longint;
  234. begin
  235. With ResourceStringTable do
  236. For I:=0 to Count-1 do
  237. With ResRec[i] do
  238. CurrentValue:=DefaultValue;
  239. end;
  240. {$endif}
  241. Initialization
  242. {$IFDEF HasResourceStrings}
  243. ResetResourceTables;
  244. {$endif}
  245. InitMemoryManager;
  246. finalization
  247. ResetMemoryManager;
  248. end.
  249. {
  250. $Log$
  251. Revision 1.31 1999-08-15 21:02:56 michael
  252. + Changed resource string mechanism to use names.
  253. Revision 1.30 1999/08/15 18:56:13 michael
  254. + Delphi-style getmem and freemem
  255. Revision 1.29 1999/07/23 23:13:54 peter
  256. * array[cardinal] is buggy, use array[word]
  257. * small fix in getresourcestring
  258. Revision 1.28 1999/07/23 22:51:11 michael
  259. * Added HasResourceStrings check
  260. Revision 1.27 1999/07/22 20:30:13 michael
  261. + Implemented resource stuff
  262. Revision 1.26 1999/07/07 10:04:04 michael
  263. + Paramstr now returns cmdline args >255 chars in ansistring objpas.pp
  264. Revision 1.25 1999/07/06 22:44:22 florian
  265. + implemented a paramstr function which returns an ansistring, nevertheless
  266. it is limited to 255 chars because it maps to the system.paramstr, maybe
  267. we should use cmdline instead
  268. Revision 1.24 1999/05/17 21:52:43 florian
  269. * most of the Object Pascal stuff moved to the system unit
  270. Revision 1.23 1999/05/13 21:54:28 peter
  271. * objpas fixes
  272. Revision 1.22 1999/04/16 20:47:20 florian
  273. + tobject.messagestringtable function for Megido/GTK support
  274. added
  275. Revision 1.21 1999/02/23 14:04:36 pierre
  276. * call %edi => call *%edi
  277. Revision 1.20 1999/02/22 23:30:54 florian
  278. + TObject.Dispatch and TObject.DispatchStr added, working
  279. Revision 1.19 1998/12/24 10:12:03 michael
  280. Implemented AssignFile and CloseFile compatibility
  281. Revision 1.18 1998/10/12 12:42:58 florian
  282. * as operator runtime error can be now caught by an errorproc
  283. Revision 1.17 1998/10/05 12:32:53 peter
  284. + assert() support
  285. Revision 1.16 1998/10/03 15:07:16 florian
  286. + TObject.AfterConstruction and TObject.BeforeDestruction of Delphi 4
  287. Revision 1.15 1998/09/24 16:13:48 michael
  288. Changes in exception and open array handling
  289. Revision 1.14 1998/09/23 12:40:43 michael
  290. Fixed TVarRec again. Should be OK now
  291. Revision 1.13 1998/09/23 12:18:32 michael
  292. + added VType in TVArRec
  293. Revision 1.12 1998/09/23 10:00:47 peter
  294. * tvarrec should be 8 bytes
  295. Revision 1.11 1998/09/22 15:30:07 peter
  296. * array of const update
  297. Revision 1.9 1998/09/16 13:08:19 michael
  298. Added AbstractErrorHandler
  299. Revision 1.8 1998/09/06 21:27:31 florian
  300. + method tobject.classinfo added
  301. Revision 1.7 1998/09/04 08:49:06 peter
  302. * 0.99.5 doesn't compile a whole objpas anymore to overcome crashes
  303. Revision 1.6 1998/08/23 20:58:52 florian
  304. + rtti for objects and classes
  305. + TObject.GetClassName implemented
  306. Revision 1.5 1998/07/30 16:10:11 michael
  307. + Added support for ExceptProc+
  308. Revision 1.4 1998/07/29 15:44:33 michael
  309. included sysutils and math.pp as target. They compile now.
  310. Revision 1.3 1998/07/29 10:09:28 michael
  311. + put in exception support
  312. Revision 1.2 1998/03/25 23:40:24 florian
  313. + stuff from old objpash.inc and objpas.inc merged in
  314. }