objpas.pp 12 KB

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