objpas.pp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. Type
  44. TResourceIterator = Function (Name,Value : AnsiString; Hash : Longint) : AnsiString;
  45. Function Hash(S : AnsiString) : longint;
  46. Procedure ResetResourceTables;
  47. Procedure SetResourceStrings (SetFunction : TResourceIterator);
  48. Function ResourceStringTableCount : Longint;
  49. Function ResourceStringCount(TableIndex : longint) : longint;
  50. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  51. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  52. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString;
  53. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  54. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  55. {$endif}
  56. Procedure Getmem(Var p:pointer;Size:Longint);
  57. Procedure Freemem(Var p:pointer;Size:Longint);
  58. Procedure Freemem(Var p:pointer);
  59. implementation
  60. {****************************************************************************
  61. Compatibility routines.
  62. ****************************************************************************}
  63. { Untyped file support }
  64. Procedure AssignFile(Var f:File;const Name:string);
  65. begin
  66. System.Assign (F,Name);
  67. end;
  68. Procedure AssignFile(Var f:File;p:pchar);
  69. begin
  70. System.Assign (F,P);
  71. end;
  72. Procedure AssignFile(Var f:File;c:char);
  73. begin
  74. System.Assign (F,C);
  75. end;
  76. Procedure CloseFile(Var f:File);
  77. begin
  78. System.Close(f);
  79. end;
  80. { Text file support }
  81. Procedure AssignFile(Var t:Text;const s:string);
  82. begin
  83. System.Assign (T,S);
  84. end;
  85. Procedure AssignFile(Var t:Text;p:pchar);
  86. begin
  87. System.Assign (T,P);
  88. end;
  89. Procedure AssignFile(Var t:Text;c:char);
  90. begin
  91. System.Assign (T,C);
  92. end;
  93. Procedure CloseFile(Var t:Text);
  94. begin
  95. Close(T);
  96. end;
  97. { Typed file supoort }
  98. Procedure AssignFile(Var f:TypedFile;const Name:string);
  99. begin
  100. system.Assign(F,Name);
  101. end;
  102. Procedure AssignFile(Var f:TypedFile;p:pchar);
  103. begin
  104. system.Assign (F,p);
  105. end;
  106. Procedure AssignFile(Var f:TypedFile;c:char);
  107. begin
  108. system.Assign (F,C);
  109. end;
  110. Function ParamStr(Param : Integer) : Ansistring;
  111. Var Len : longint;
  112. begin
  113. if (Param>=0) and (Param<argc) then
  114. begin
  115. Len:=0;
  116. While Argv[Param][Len]<>#0 do
  117. Inc(len);
  118. SetLength(Result,Len);
  119. If Len>0 then
  120. Move(Argv[Param][0],Result[1],Len);
  121. end
  122. else
  123. paramstr:='';
  124. end;
  125. { ---------------------------------------------------------------------
  126. Delphi-Style memory management
  127. ---------------------------------------------------------------------}
  128. Type PLongint = ^Longint;
  129. Procedure Getmem(Var p:pointer;Size:Longint);
  130. begin
  131. Inc(Size,SizeOf(Longint));
  132. SysGetmem(P,Size);
  133. PLongint(P)^:=Size;
  134. Inc(P,SizeOf(Longint));
  135. end;
  136. Procedure DummyFreemem(Var p:pointer;Size:Longint);
  137. begin
  138. FreeMem(P);
  139. end;
  140. Procedure Freemem(Var p:pointer;Size:Longint);
  141. begin
  142. Freemem(P);
  143. end;
  144. Procedure Freemem(Var p:pointer);
  145. begin
  146. If P<>Nil then
  147. begin
  148. Dec(P,SizeOf(Longint));
  149. SysFreemem(P,Plongint(P)^);
  150. end;
  151. end;
  152. Var OldMM,NEWMM : TmemoryManager;
  153. Procedure InitMemoryManager;
  154. begin
  155. GetMemoryManager(OldMM);
  156. NewMM.FreeMem:=@DummyFreeMem;
  157. NewMM.GetMem:=@GetMem;
  158. SetMemoryManager(NewMM);
  159. end;
  160. Procedure ResetMemoryManager;
  161. begin
  162. SetMemoryManager(OldMM);
  163. end;
  164. {$IFDEF HasResourceStrings}
  165. { ---------------------------------------------------------------------
  166. ResourceString support
  167. ---------------------------------------------------------------------}
  168. Type
  169. PResourceStringRecord = ^TResourceStringRecord;
  170. TResourceStringRecord = Packed Record
  171. DefaultValue,
  172. CurrentValue : AnsiString;
  173. HashValue : longint;
  174. Name : AnsiString;
  175. end;
  176. TResourceStringTable = Packed Record
  177. Count : longint;
  178. Resrec : Array[Word] of TResourceStringRecord;
  179. end;
  180. PResourceStringTable = ^TResourceStringTable;
  181. TResourceTableList = Packed Record
  182. Count : longint;
  183. Tables : Array[Word] of PResourceStringTable;
  184. end;
  185. Var
  186. ResourceStringTable : TResourceTablelist; External Name 'FPC_RESOURCESTRINGTABLES';
  187. Function Hash(S : AnsiString) : longint;
  188. Var thehash,g,I : longint;
  189. begin
  190. thehash:=0;
  191. For I:=1 to Length(S) do { 0 terminated }
  192. begin
  193. thehash:=thehash shl 4;
  194. inc(theHash,Ord(S[i]));
  195. g:=thehash and ($f shl 28);
  196. if g<>0 then
  197. begin
  198. thehash:=thehash xor (g shr 24);
  199. thehash:=thehash xor g;
  200. end;
  201. end;
  202. If theHash=0 then
  203. Hash:=Not(0)
  204. else
  205. Hash:=TheHash;
  206. end;
  207. Function GetResourceString(Const TheTable: TResourceStringTable;Index : longint) : AnsiString;[Public,Alias : 'FPC_GETRESOURCESTRING'];
  208. begin
  209. If (Index>=0) and (Index<TheTAble.Count) then
  210. Result:=TheTable.ResRec[Index].CurrentValue
  211. else
  212. Result:='';
  213. end;
  214. (*
  215. Function SetResourceString(Hash : Longint;Const Name : ShortString; Const Value : AnsiString) : Boolean;
  216. begin
  217. Hash:=FindIndex(Hash,Name);
  218. Result:=Hash<>-1;
  219. If Result then
  220. ResourceStringTable.ResRec[Hash].CurrentValue:=Value;
  221. end;
  222. *)
  223. Procedure SetResourceStrings (SetFunction : TResourceIterator);
  224. Var I,J : longint;
  225. begin
  226. With ResourceStringTable do
  227. For I:=0 to Count-1 do
  228. With Tables[I]^ do
  229. For J:=0 to Count-1 do
  230. With ResRec[J] do
  231. CurrentValue:=SetFunction(Name,DefaultValue,HashValue);
  232. end;
  233. Procedure ResetResourceTables;
  234. Var I,J : longint;
  235. begin
  236. With ResourceStringTable do
  237. For I:=0 to Count-1 do
  238. With Tables[I]^ do
  239. For J:=0 to Count-1 do
  240. With ResRec[J] do
  241. CurrentValue:=DefaultValue;
  242. end;
  243. Function ResourceStringTableCount : Longint;
  244. begin
  245. Result:=ResourceStringTable.Count;
  246. end;
  247. Function CheckTableIndex (Index: longint) : Boolean;
  248. begin
  249. Result:=(Index<ResourceStringTable.Count) and (Index>=0)
  250. end;
  251. Function CheckStringIndex (TableIndex,Index: longint) : Boolean;
  252. begin
  253. Result:=(TableIndex<ResourceStringTable.Count) and (TableIndex>=0) and
  254. (Index<ResourceStringTable.Tables[TableIndex]^.Count) and (Index>=0)
  255. end;
  256. Function ResourceStringCount(TableIndex : longint) : longint;
  257. begin
  258. If not CheckTableIndex(TableIndex) then
  259. Result:=-1
  260. else
  261. Result:=ResourceStringTable.Tables[TableIndex]^.Count;
  262. end;
  263. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  264. begin
  265. If not CheckStringIndex(Tableindex,StringIndex) then
  266. Result:=''
  267. else
  268. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].Name;
  269. end;
  270. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  271. begin
  272. If not CheckStringIndex(Tableindex,StringIndex) then
  273. Result:=0
  274. else
  275. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].HashValue;
  276. end;
  277. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString;
  278. begin
  279. If not CheckStringIndex(Tableindex,StringIndex) then
  280. Result:=''
  281. else
  282. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].DefaultValue;
  283. end;
  284. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  285. begin
  286. If not CheckStringIndex(Tableindex,StringIndex) then
  287. Result:=''
  288. else
  289. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue;
  290. end;
  291. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  292. begin
  293. Result:=CheckStringIndex(Tableindex,StringIndex);
  294. If Result then
  295. ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue:=Value;
  296. end;
  297. {$endif}
  298. Initialization
  299. {$IFDEF HasResourceStrings}
  300. ResetResourceTables;
  301. {$endif}
  302. InitMemoryManager;
  303. finalization
  304. ResetMemoryManager;
  305. end.
  306. {
  307. $Log$
  308. Revision 1.39 1999-08-28 13:03:23 michael
  309. + Added Hash function to interface
  310. Revision 1.38 1999/08/27 15:54:15 michael
  311. + Added many resourcestring methods
  312. Revision 1.37 1999/08/25 16:41:08 peter
  313. * resources are working again
  314. Revision 1.36 1999/08/24 22:42:56 michael
  315. * changed resourcestrings to new mechanism
  316. Revision 1.35 1999/08/24 12:02:29 michael
  317. + Changed external var for resourcestrings
  318. Revision 1.34 1999/08/20 10:50:55 michael
  319. + Fixed memory leak
  320. Revision 1.33 1999/08/19 19:52:26 michael
  321. * Fixed freemem bug; reported by Sebastian Guenther
  322. Revision 1.32 1999/08/15 21:28:57 michael
  323. + Pass hash also for speed reasons.
  324. Revision 1.31 1999/08/15 21:02:56 michael
  325. + Changed resource string mechanism to use names.
  326. Revision 1.30 1999/08/15 18:56:13 michael
  327. + Delphi-style getmem and freemem
  328. Revision 1.29 1999/07/23 23:13:54 peter
  329. * array[cardinal] is buggy, use array[word]
  330. * small fix in getresourcestring
  331. Revision 1.28 1999/07/23 22:51:11 michael
  332. * Added HasResourceStrings check
  333. Revision 1.27 1999/07/22 20:30:13 michael
  334. + Implemented resource stuff
  335. Revision 1.26 1999/07/07 10:04:04 michael
  336. + Paramstr now returns cmdline args >255 chars in ansistring objpas.pp
  337. Revision 1.25 1999/07/06 22:44:22 florian
  338. + implemented a paramstr function which returns an ansistring, nevertheless
  339. it is limited to 255 chars because it maps to the system.paramstr, maybe
  340. we should use cmdline instead
  341. Revision 1.24 1999/05/17 21:52:43 florian
  342. * most of the Object Pascal stuff moved to the system unit
  343. Revision 1.23 1999/05/13 21:54:28 peter
  344. * objpas fixes
  345. Revision 1.22 1999/04/16 20:47:20 florian
  346. + tobject.messagestringtable function for Megido/GTK support
  347. added
  348. Revision 1.21 1999/02/23 14:04:36 pierre
  349. * call %edi => call *%edi
  350. Revision 1.20 1999/02/22 23:30:54 florian
  351. + TObject.Dispatch and TObject.DispatchStr added, working
  352. Revision 1.19 1998/12/24 10:12:03 michael
  353. Implemented AssignFile and CloseFile compatibility
  354. Revision 1.18 1998/10/12 12:42:58 florian
  355. * as operator runtime error can be now caught by an errorproc
  356. Revision 1.17 1998/10/05 12:32:53 peter
  357. + assert() support
  358. Revision 1.16 1998/10/03 15:07:16 florian
  359. + TObject.AfterConstruction and TObject.BeforeDestruction of Delphi 4
  360. Revision 1.15 1998/09/24 16:13:48 michael
  361. Changes in exception and open array handling
  362. Revision 1.14 1998/09/23 12:40:43 michael
  363. Fixed TVarRec again. Should be OK now
  364. Revision 1.13 1998/09/23 12:18:32 michael
  365. + added VType in TVArRec
  366. Revision 1.12 1998/09/23 10:00:47 peter
  367. * tvarrec should be 8 bytes
  368. Revision 1.11 1998/09/22 15:30:07 peter
  369. * array of const update
  370. Revision 1.9 1998/09/16 13:08:19 michael
  371. Added AbstractErrorHandler
  372. Revision 1.8 1998/09/06 21:27:31 florian
  373. + method tobject.classinfo added
  374. Revision 1.7 1998/09/04 08:49:06 peter
  375. * 0.99.5 doesn't compile a whole objpas anymore to overcome crashes
  376. Revision 1.6 1998/08/23 20:58:52 florian
  377. + rtti for objects and classes
  378. + TObject.GetClassName implemented
  379. Revision 1.5 1998/07/30 16:10:11 michael
  380. + Added support for ExceptProc+
  381. Revision 1.4 1998/07/29 15:44:33 michael
  382. included sysutils and math.pp as target. They compile now.
  383. Revision 1.3 1998/07/29 10:09:28 michael
  384. + put in exception support
  385. Revision 1.2 1998/03/25 23:40:24 florian
  386. + stuff from old objpash.inc and objpas.inc merged in
  387. }