objpas.pp 12 KB

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