objpas.pp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. 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. { ---------------------------------------------------------------------
  123. ResourceString support
  124. ---------------------------------------------------------------------}
  125. Type
  126. PResourceStringRecord = ^TResourceStringRecord;
  127. TResourceStringRecord = Packed Record
  128. DefaultValue,
  129. CurrentValue : AnsiString;
  130. HashValue : longint;
  131. Name : AnsiString;
  132. end;
  133. TResourceStringTable = Packed Record
  134. Count : longint;
  135. Resrec : Array[Word] of TResourceStringRecord;
  136. end;
  137. PResourceStringTable = ^TResourceStringTable;
  138. TResourceTableList = Packed Record
  139. Count : longint;
  140. Tables : Array[Word] of PResourceStringTable;
  141. end;
  142. Var
  143. ResourceStringTable : TResourceTablelist; External Name 'FPC_RESOURCESTRINGTABLES';
  144. Function Hash(S : AnsiString) : longint;
  145. Var thehash,g,I : longint;
  146. begin
  147. thehash:=0;
  148. For I:=1 to Length(S) do { 0 terminated }
  149. begin
  150. thehash:=thehash shl 4;
  151. inc(theHash,Ord(S[i]));
  152. g:=thehash and longint($f shl 28);
  153. if g<>0 then
  154. begin
  155. thehash:=thehash xor (g shr 24);
  156. thehash:=thehash xor g;
  157. end;
  158. end;
  159. If theHash=0 then
  160. Hash:=Not(0)
  161. else
  162. Hash:=TheHash;
  163. end;
  164. Function GetResourceString(Const TheTable: TResourceStringTable;Index : longint) : AnsiString;[Public,Alias : 'FPC_GETRESOURCESTRING'];
  165. begin
  166. If (Index>=0) and (Index<TheTAble.Count) then
  167. Result:=TheTable.ResRec[Index].CurrentValue
  168. else
  169. Result:='';
  170. end;
  171. (*
  172. Function SetResourceString(Hash : Longint;Const Name : ShortString; Const Value : AnsiString) : Boolean;
  173. begin
  174. Hash:=FindIndex(Hash,Name);
  175. Result:=Hash<>-1;
  176. If Result then
  177. ResourceStringTable.ResRec[Hash].CurrentValue:=Value;
  178. end;
  179. *)
  180. Procedure SetResourceStrings (SetFunction : TResourceIterator);
  181. Var I,J : longint;
  182. begin
  183. With ResourceStringTable do
  184. For I:=0 to Count-1 do
  185. With Tables[I]^ do
  186. For J:=0 to Count-1 do
  187. With ResRec[J] do
  188. CurrentValue:=SetFunction(Name,DefaultValue,HashValue);
  189. end;
  190. Procedure ResetResourceTables;
  191. Var I,J : longint;
  192. begin
  193. With ResourceStringTable do
  194. For I:=0 to Count-1 do
  195. With Tables[I]^ do
  196. For J:=0 to Count-1 do
  197. With ResRec[J] do
  198. CurrentValue:=DefaultValue;
  199. end;
  200. Function ResourceStringTableCount : Longint;
  201. begin
  202. Result:=ResourceStringTable.Count;
  203. end;
  204. Function CheckTableIndex (Index: longint) : Boolean;
  205. begin
  206. Result:=(Index<ResourceStringTable.Count) and (Index>=0)
  207. end;
  208. Function CheckStringIndex (TableIndex,Index: longint) : Boolean;
  209. begin
  210. Result:=(TableIndex<ResourceStringTable.Count) and (TableIndex>=0) and
  211. (Index<ResourceStringTable.Tables[TableIndex]^.Count) and (Index>=0)
  212. end;
  213. Function ResourceStringCount(TableIndex : longint) : longint;
  214. begin
  215. If not CheckTableIndex(TableIndex) then
  216. Result:=-1
  217. else
  218. Result:=ResourceStringTable.Tables[TableIndex]^.Count;
  219. end;
  220. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  221. begin
  222. If not CheckStringIndex(Tableindex,StringIndex) then
  223. Result:=''
  224. else
  225. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].Name;
  226. end;
  227. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  228. begin
  229. If not CheckStringIndex(Tableindex,StringIndex) then
  230. Result:=0
  231. else
  232. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].HashValue;
  233. end;
  234. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString;
  235. begin
  236. If not CheckStringIndex(Tableindex,StringIndex) then
  237. Result:=''
  238. else
  239. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].DefaultValue;
  240. end;
  241. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  242. begin
  243. If not CheckStringIndex(Tableindex,StringIndex) then
  244. Result:=''
  245. else
  246. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue;
  247. end;
  248. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  249. begin
  250. Result:=CheckStringIndex(Tableindex,StringIndex);
  251. If Result then
  252. ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue:=Value;
  253. end;
  254. Initialization
  255. ResetResourceTables;
  256. finalization
  257. end.
  258. {
  259. $Log$
  260. Revision 1.5 2000-12-16 15:58:18 jonas
  261. * removed warnings about possible range check errors
  262. Revision 1.4 2000/11/13 14:41:20 marco
  263. * Unix renamefest for defines
  264. Revision 1.3 2000/07/14 10:33:10 michael
  265. + Conditionals fixed
  266. Revision 1.2 2000/07/13 11:33:51 michael
  267. + removed logs
  268. }