objpas.pp 9.3 KB

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