objpas.pp 13 KB

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