objpas.pp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. 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. implementation
  56. {****************************************************************************
  57. Compatibility routines.
  58. ****************************************************************************}
  59. { Untyped file support }
  60. Procedure AssignFile(Var f:File;const Name:string);
  61. begin
  62. System.Assign (F,Name);
  63. end;
  64. Procedure AssignFile(Var f:File;p:pchar);
  65. begin
  66. System.Assign (F,P);
  67. end;
  68. Procedure AssignFile(Var f:File;c:char);
  69. begin
  70. System.Assign (F,C);
  71. end;
  72. Procedure CloseFile(Var f:File);
  73. begin
  74. { Catch Runtime error/Exception }
  75. {$I+}
  76. System.Close(f);
  77. {$I-}
  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. { Catch Runtime error/Exception }
  95. {$I+}
  96. System.Close(T);
  97. {$I-}
  98. end;
  99. { Typed file supoort }
  100. Procedure AssignFile(Var f:TypedFile;const Name:string);
  101. begin
  102. system.Assign(F,Name);
  103. end;
  104. Procedure AssignFile(Var f:TypedFile;p:pchar);
  105. begin
  106. system.Assign (F,p);
  107. end;
  108. Procedure AssignFile(Var f:TypedFile;c:char);
  109. begin
  110. system.Assign (F,C);
  111. end;
  112. Function ParamStr(Param : Integer) : Ansistring;
  113. Var Len : longint;
  114. begin
  115. if (Param>=0) and (Param<argc) then
  116. begin
  117. Len:=0;
  118. While Argv[Param][Len]<>#0 do
  119. Inc(len);
  120. SetLength(Result,Len);
  121. If Len>0 then
  122. Move(Argv[Param][0],Result[1],Len);
  123. end
  124. else
  125. paramstr:='';
  126. end;
  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 longint($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. Initialization
  260. ResetResourceTables;
  261. finalization
  262. end.
  263. {
  264. $Log$
  265. Revision 1.7 2001-08-19 21:02:02 florian
  266. * fixed and added a lot of stuff to get the Jedi DX( headers
  267. compiled
  268. Revision 1.6 2001/08/01 21:43:11 peter
  269. * generate error for closefile
  270. Revision 1.5 2000/12/16 15:58:18 jonas
  271. * removed warnings about possible range check errors
  272. Revision 1.4 2000/11/13 14:41:20 marco
  273. * Unix renamefest for defines
  274. Revision 1.3 2000/07/14 10:33:10 michael
  275. + Conditionals fixed
  276. Revision 1.2 2000/07/13 11:33:51 michael
  277. + removed logs
  278. }