objpas.pp 12 KB

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