2
0

objpas.pp 10 KB

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