objpas.pp 10 KB

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