objpas.pp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. { Old compilers search for these variables in objpas unit }
  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. Type
  45. TResourceIterator = Function (Name,Value : AnsiString; Hash : Longint) : AnsiString;
  46. Function Hash(S : AnsiString) : longint;
  47. Procedure ResetResourceTables;
  48. Procedure SetResourceStrings (SetFunction : TResourceIterator);
  49. Function ResourceStringTableCount : Longint;
  50. Function ResourceStringCount(TableIndex : longint) : longint;
  51. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  52. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  53. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString;
  54. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  55. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  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. { Catch Runtime error/Exception }
  76. {$I+}
  77. System.Close(f);
  78. {$I-}
  79. end;
  80. { Text file support }
  81. Procedure AssignFile(Var t:Text;const s:string);
  82. begin
  83. System.Assign (T,S);
  84. end;
  85. Procedure AssignFile(Var t:Text;p:pchar);
  86. begin
  87. System.Assign (T,P);
  88. end;
  89. Procedure AssignFile(Var t:Text;c:char);
  90. begin
  91. System.Assign (T,C);
  92. end;
  93. Procedure CloseFile(Var t:Text);
  94. begin
  95. { Catch Runtime error/Exception }
  96. {$I+}
  97. System.Close(T);
  98. {$I-}
  99. end;
  100. { Typed file supoort }
  101. Procedure AssignFile(Var f:TypedFile;const Name:string);
  102. begin
  103. system.Assign(F,Name);
  104. end;
  105. Procedure AssignFile(Var f:TypedFile;p:pchar);
  106. begin
  107. system.Assign (F,p);
  108. end;
  109. Procedure AssignFile(Var f:TypedFile;c:char);
  110. begin
  111. system.Assign (F,C);
  112. end;
  113. Function ParamStr(Param : Integer) : Ansistring;
  114. Var Len : longint;
  115. begin
  116. if (Param>=0) and (Param<argc) then
  117. begin
  118. Len:=0;
  119. While Argv[Param][Len]<>#0 do
  120. Inc(len);
  121. SetLength(Result,Len);
  122. If Len>0 then
  123. Move(Argv[Param][0],Result[1],Len);
  124. end
  125. else
  126. paramstr:='';
  127. end;
  128. { ---------------------------------------------------------------------
  129. ResourceString support
  130. ---------------------------------------------------------------------}
  131. Type
  132. PResourceStringRecord = ^TResourceStringRecord;
  133. TResourceStringRecord = Packed Record
  134. DefaultValue,
  135. CurrentValue : AnsiString;
  136. HashValue : longint;
  137. Name : AnsiString;
  138. end;
  139. TResourceStringTable = Packed Record
  140. Count : longint;
  141. Resrec : Array[Word] of TResourceStringRecord;
  142. end;
  143. PResourceStringTable = ^TResourceStringTable;
  144. TResourceTableList = Packed Record
  145. Count : longint;
  146. Tables : Array[Word] of PResourceStringTable;
  147. end;
  148. Var
  149. ResourceStringTable : TResourceTablelist; External Name 'FPC_RESOURCESTRINGTABLES';
  150. Function Hash(S : AnsiString) : longint;
  151. Var thehash,g,I : longint;
  152. begin
  153. thehash:=0;
  154. For I:=1 to Length(S) do { 0 terminated }
  155. begin
  156. thehash:=thehash shl 4;
  157. inc(theHash,Ord(S[i]));
  158. g:=thehash and longint($f shl 28);
  159. if g<>0 then
  160. begin
  161. thehash:=thehash xor (g shr 24);
  162. thehash:=thehash xor g;
  163. end;
  164. end;
  165. If theHash=0 then
  166. Hash:=Not(0)
  167. else
  168. Hash:=TheHash;
  169. end;
  170. Function GetResourceString(Const TheTable: TResourceStringTable;Index : longint) : AnsiString;[Public,Alias : 'FPC_GETRESOURCESTRING'];
  171. begin
  172. If (Index>=0) and (Index<TheTAble.Count) then
  173. Result:=TheTable.ResRec[Index].CurrentValue
  174. else
  175. Result:='';
  176. end;
  177. (*
  178. Function SetResourceString(Hash : Longint;Const Name : ShortString; Const Value : AnsiString) : Boolean;
  179. begin
  180. Hash:=FindIndex(Hash,Name);
  181. Result:=Hash<>-1;
  182. If Result then
  183. ResourceStringTable.ResRec[Hash].CurrentValue:=Value;
  184. end;
  185. *)
  186. Procedure SetResourceStrings (SetFunction : TResourceIterator);
  187. Var I,J : longint;
  188. begin
  189. With ResourceStringTable do
  190. For I:=0 to Count-1 do
  191. With Tables[I]^ do
  192. For J:=0 to Count-1 do
  193. With ResRec[J] do
  194. CurrentValue:=SetFunction(Name,DefaultValue,HashValue);
  195. end;
  196. Procedure ResetResourceTables;
  197. Var I,J : longint;
  198. begin
  199. With ResourceStringTable do
  200. For I:=0 to Count-1 do
  201. With Tables[I]^ do
  202. For J:=0 to Count-1 do
  203. With ResRec[J] do
  204. CurrentValue:=DefaultValue;
  205. end;
  206. Function ResourceStringTableCount : Longint;
  207. begin
  208. Result:=ResourceStringTable.Count;
  209. end;
  210. Function CheckTableIndex (Index: longint) : Boolean;
  211. begin
  212. Result:=(Index<ResourceStringTable.Count) and (Index>=0)
  213. end;
  214. Function CheckStringIndex (TableIndex,Index: longint) : Boolean;
  215. begin
  216. Result:=(TableIndex<ResourceStringTable.Count) and (TableIndex>=0) and
  217. (Index<ResourceStringTable.Tables[TableIndex]^.Count) and (Index>=0)
  218. end;
  219. Function ResourceStringCount(TableIndex : longint) : longint;
  220. begin
  221. If not CheckTableIndex(TableIndex) then
  222. Result:=-1
  223. else
  224. Result:=ResourceStringTable.Tables[TableIndex]^.Count;
  225. end;
  226. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  227. begin
  228. If not CheckStringIndex(Tableindex,StringIndex) then
  229. Result:=''
  230. else
  231. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].Name;
  232. end;
  233. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  234. begin
  235. If not CheckStringIndex(Tableindex,StringIndex) then
  236. Result:=0
  237. else
  238. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].HashValue;
  239. end;
  240. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString;
  241. begin
  242. If not CheckStringIndex(Tableindex,StringIndex) then
  243. Result:=''
  244. else
  245. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].DefaultValue;
  246. end;
  247. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  248. begin
  249. If not CheckStringIndex(Tableindex,StringIndex) then
  250. Result:=''
  251. else
  252. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue;
  253. end;
  254. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  255. begin
  256. Result:=CheckStringIndex(Tableindex,StringIndex);
  257. If Result then
  258. ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue:=Value;
  259. end;
  260. Initialization
  261. ResetResourceTables;
  262. finalization
  263. end.
  264. {
  265. $Log$
  266. Revision 1.6 2001-08-01 21:43:11 peter
  267. * generate error for closefile
  268. Revision 1.5 2000/12/16 15:58:18 jonas
  269. * removed warnings about possible range check errors
  270. Revision 1.4 2000/11/13 14:41:20 marco
  271. * Unix renamefest for defines
  272. Revision 1.3 2000/07/14 10:33:10 michael
  273. + Conditionals fixed
  274. Revision 1.2 2000/07/13 11:33:51 michael
  275. + removed logs
  276. }