objpas.pp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 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-}
  14. {$ifndef Unix}
  15. {$S-}
  16. {$endif}
  17. unit objpas;
  18. interface
  19. { first, in object pascal, the integer type must be redefined }
  20. const
  21. MaxInt = MaxLongint;
  22. type
  23. integer = longint;
  24. {****************************************************************************
  25. Compatibility routines.
  26. ****************************************************************************}
  27. { Untyped file support }
  28. Procedure AssignFile(Var f:File;const Name:string);
  29. Procedure AssignFile(Var f:File;p:pchar);
  30. Procedure AssignFile(Var f:File;c:char);
  31. Procedure CloseFile(Var f:File);
  32. { Text file support }
  33. Procedure AssignFile(Var t:Text;const s:string);
  34. Procedure AssignFile(Var t:Text;p:pchar);
  35. Procedure AssignFile(Var t:Text;c:char);
  36. Procedure CloseFile(Var t:Text);
  37. { Typed file supoort }
  38. Procedure AssignFile(Var f:TypedFile;const Name:string);
  39. Procedure AssignFile(Var f:TypedFile;p:pchar);
  40. Procedure AssignFile(Var f:TypedFile;c:char);
  41. { ParamStr should return also an ansistring }
  42. Function ParamStr(Param : Integer) : Ansistring;
  43. {****************************************************************************
  44. Resource strings.
  45. ****************************************************************************}
  46. type
  47. TResourceIterator = Function (Name,Value : AnsiString; Hash : Longint) : AnsiString;
  48. Function Hash(S : AnsiString) : longint;
  49. Procedure ResetResourceTables;
  50. Procedure SetResourceStrings (SetFunction : TResourceIterator);
  51. Function ResourceStringTableCount : Longint;
  52. Function ResourceStringCount(TableIndex : longint) : longint;
  53. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  54. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  55. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString;
  56. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  57. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  58. { Delphi compatibility }
  59. type
  60. PResStringRec=^AnsiString;
  61. TResStringRec=AnsiString;
  62. Function LoadResString(p:PResStringRec):AnsiString;
  63. implementation
  64. {****************************************************************************
  65. Compatibility routines.
  66. ****************************************************************************}
  67. { Untyped file support }
  68. Procedure AssignFile(Var f:File;const Name:string);
  69. begin
  70. System.Assign (F,Name);
  71. end;
  72. Procedure AssignFile(Var f:File;p:pchar);
  73. begin
  74. System.Assign (F,P);
  75. end;
  76. Procedure AssignFile(Var f:File;c:char);
  77. begin
  78. System.Assign (F,C);
  79. end;
  80. Procedure CloseFile(Var f:File);
  81. begin
  82. { Catch Runtime error/Exception }
  83. {$I+}
  84. System.Close(f);
  85. {$I-}
  86. end;
  87. { Text file support }
  88. Procedure AssignFile(Var t:Text;const s:string);
  89. begin
  90. System.Assign (T,S);
  91. end;
  92. Procedure AssignFile(Var t:Text;p:pchar);
  93. begin
  94. System.Assign (T,P);
  95. end;
  96. Procedure AssignFile(Var t:Text;c:char);
  97. begin
  98. System.Assign (T,C);
  99. end;
  100. Procedure CloseFile(Var t:Text);
  101. begin
  102. { Catch Runtime error/Exception }
  103. {$I+}
  104. System.Close(T);
  105. {$I-}
  106. end;
  107. { Typed file supoort }
  108. Procedure AssignFile(Var f:TypedFile;const Name:string);
  109. begin
  110. system.Assign(F,Name);
  111. end;
  112. Procedure AssignFile(Var f:TypedFile;p:pchar);
  113. begin
  114. system.Assign (F,p);
  115. end;
  116. Procedure AssignFile(Var f:TypedFile;c:char);
  117. begin
  118. system.Assign (F,C);
  119. end;
  120. Function ParamStr(Param : Integer) : Ansistring;
  121. Var Len : longint;
  122. begin
  123. if (Param>=0) and (Param<argc) then
  124. begin
  125. Len:=0;
  126. While Argv[Param][Len]<>#0 do
  127. Inc(len);
  128. SetLength(Result,Len);
  129. If Len>0 then
  130. Move(Argv[Param][0],Result[1],Len);
  131. end
  132. else
  133. paramstr:='';
  134. end;
  135. { ---------------------------------------------------------------------
  136. ResourceString support
  137. ---------------------------------------------------------------------}
  138. Type
  139. PResourceStringRecord = ^TResourceStringRecord;
  140. TResourceStringRecord = Packed Record
  141. DefaultValue,
  142. CurrentValue : AnsiString;
  143. HashValue : longint;
  144. Name : AnsiString;
  145. end;
  146. TResourceStringTable = Packed Record
  147. Count : longint;
  148. Resrec : Array[Word] of TResourceStringRecord;
  149. end;
  150. PResourceStringTable = ^TResourceStringTable;
  151. TResourceTableList = Packed Record
  152. Count : longint;
  153. Tables : Array[Word] of PResourceStringTable;
  154. end;
  155. Var
  156. ResourceStringTable : TResourceTablelist; External Name 'FPC_RESOURCESTRINGTABLES';
  157. Function Hash(S : AnsiString) : longint;
  158. Var thehash,g,I : longint;
  159. begin
  160. thehash:=0;
  161. For I:=1 to Length(S) do { 0 terminated }
  162. begin
  163. thehash:=thehash shl 4;
  164. inc(theHash,Ord(S[i]));
  165. g:=thehash and longint($f shl 28);
  166. if g<>0 then
  167. begin
  168. thehash:=thehash xor (g shr 24);
  169. thehash:=thehash xor g;
  170. end;
  171. end;
  172. If theHash=0 then
  173. Hash:=Not(0)
  174. else
  175. Hash:=TheHash;
  176. end;
  177. Function GetResourceString(Const TheTable: TResourceStringTable;Index : longint) : AnsiString;[Public,Alias : 'FPC_GETRESOURCESTRING'];
  178. begin
  179. If (Index>=0) and (Index<TheTAble.Count) then
  180. Result:=TheTable.ResRec[Index].CurrentValue
  181. else
  182. Result:='';
  183. end;
  184. (*
  185. Function SetResourceString(Hash : Longint;Const Name : ShortString; Const Value : AnsiString) : Boolean;
  186. begin
  187. Hash:=FindIndex(Hash,Name);
  188. Result:=Hash<>-1;
  189. If Result then
  190. ResourceStringTable.ResRec[Hash].CurrentValue:=Value;
  191. end;
  192. *)
  193. Procedure SetResourceStrings (SetFunction : TResourceIterator);
  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:=SetFunction(Name,DefaultValue,HashValue);
  202. end;
  203. Procedure ResetResourceTables;
  204. Var I,J : longint;
  205. begin
  206. With ResourceStringTable do
  207. For I:=0 to Count-1 do
  208. With Tables[I]^ do
  209. For J:=0 to Count-1 do
  210. With ResRec[J] do
  211. CurrentValue:=DefaultValue;
  212. end;
  213. Function ResourceStringTableCount : Longint;
  214. begin
  215. Result:=ResourceStringTable.Count;
  216. end;
  217. Function CheckTableIndex (Index: longint) : Boolean;
  218. begin
  219. Result:=(Index<ResourceStringTable.Count) and (Index>=0)
  220. end;
  221. Function CheckStringIndex (TableIndex,Index: longint) : Boolean;
  222. begin
  223. Result:=(TableIndex<ResourceStringTable.Count) and (TableIndex>=0) and
  224. (Index<ResourceStringTable.Tables[TableIndex]^.Count) and (Index>=0)
  225. end;
  226. Function ResourceStringCount(TableIndex : longint) : longint;
  227. begin
  228. If not CheckTableIndex(TableIndex) then
  229. Result:=-1
  230. else
  231. Result:=ResourceStringTable.Tables[TableIndex]^.Count;
  232. end;
  233. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  234. begin
  235. If not CheckStringIndex(Tableindex,StringIndex) then
  236. Result:=''
  237. else
  238. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].Name;
  239. end;
  240. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  241. begin
  242. If not CheckStringIndex(Tableindex,StringIndex) then
  243. Result:=0
  244. else
  245. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].HashValue;
  246. end;
  247. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString;
  248. begin
  249. If not CheckStringIndex(Tableindex,StringIndex) then
  250. Result:=''
  251. else
  252. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].DefaultValue;
  253. end;
  254. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  255. begin
  256. If not CheckStringIndex(Tableindex,StringIndex) then
  257. Result:=''
  258. else
  259. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue;
  260. end;
  261. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  262. begin
  263. Result:=CheckStringIndex(Tableindex,StringIndex);
  264. If Result then
  265. ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue:=Value;
  266. end;
  267. Function LoadResString(p:PResStringRec):AnsiString;
  268. begin
  269. Result:=p^;
  270. end;
  271. Initialization
  272. ResetResourceTables;
  273. finalization
  274. end.
  275. {
  276. $Log$
  277. Revision 1.8 2001-10-22 21:19:33 peter
  278. * LoadResString, PResStringRec, TResStringRec compatibility added
  279. Revision 1.7 2001/08/19 21:02:02 florian
  280. * fixed and added a lot of stuff to get the Jedi DX( headers
  281. compiled
  282. Revision 1.6 2001/08/01 21:43:11 peter
  283. * generate error for closefile
  284. Revision 1.5 2000/12/16 15:58:18 jonas
  285. * removed warnings about possible range check errors
  286. Revision 1.4 2000/11/13 14:41:20 marco
  287. * Unix renamefest for defines
  288. Revision 1.3 2000/07/14 10:33:10 michael
  289. + Conditionals fixed
  290. Revision 1.2 2000/07/13 11:33:51 michael
  291. + removed logs
  292. }