objpas.pp 9.9 KB

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