objpas.pp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. {
  136. Paramstr(0) should return the name of the binary.
  137. Since this functionality is included in the system unit,
  138. we fetch it from there.
  139. Normally, pathnames are less than 255 chars anyway,
  140. so this will work correct in 99% of all cases.
  141. In time, the system unit should get a GetExeName call.
  142. }
  143. if (Param=0) then
  144. Result:=System.Paramstr(0)
  145. else if (Param>0) and (Param<argc) then
  146. begin
  147. Len:=0;
  148. While Argv[Param][Len]<>#0 do
  149. Inc(len);
  150. SetLength(Result,Len);
  151. If Len>0 then
  152. Move(Argv[Param][0],Result[1],Len);
  153. end
  154. else
  155. paramstr:='';
  156. end;
  157. { ---------------------------------------------------------------------
  158. ResourceString support
  159. ---------------------------------------------------------------------}
  160. Type
  161. PResourceStringRecord = ^TResourceStringRecord;
  162. TResourceStringRecord = Packed Record
  163. DefaultValue,
  164. CurrentValue : AnsiString;
  165. HashValue : longint;
  166. Name : AnsiString;
  167. end;
  168. TResourceStringTable = Packed Record
  169. Count : longint;
  170. Resrec : Array[Word] of TResourceStringRecord;
  171. end;
  172. PResourceStringTable = ^TResourceStringTable;
  173. TResourceTableList = Packed Record
  174. Count : longint;
  175. Tables : Array[Word] of PResourceStringTable;
  176. end;
  177. Var
  178. ResourceStringTable : TResourceTablelist; External Name 'FPC_RESOURCESTRINGTABLES';
  179. Function Hash(S : AnsiString) : longint;
  180. Var thehash,g,I : longint;
  181. begin
  182. thehash:=0;
  183. For I:=1 to Length(S) do { 0 terminated }
  184. begin
  185. thehash:=thehash shl 4;
  186. inc(theHash,Ord(S[i]));
  187. g:=thehash and longint($f shl 28);
  188. if g<>0 then
  189. begin
  190. thehash:=thehash xor (g shr 24);
  191. thehash:=thehash xor g;
  192. end;
  193. end;
  194. If theHash=0 then
  195. Hash:=Not(0)
  196. else
  197. Hash:=TheHash;
  198. end;
  199. Function GetResourceString(Const TheTable: TResourceStringTable;Index : longint) : AnsiString;[Public,Alias : 'FPC_GETRESOURCESTRING'];
  200. begin
  201. If (Index>=0) and (Index<TheTAble.Count) then
  202. Result:=TheTable.ResRec[Index].CurrentValue
  203. else
  204. Result:='';
  205. end;
  206. (*
  207. Function SetResourceString(Hash : Longint;Const Name : ShortString; Const Value : AnsiString) : Boolean;
  208. begin
  209. Hash:=FindIndex(Hash,Name);
  210. Result:=Hash<>-1;
  211. If Result then
  212. ResourceStringTable.ResRec[Hash].CurrentValue:=Value;
  213. end;
  214. *)
  215. Procedure SetResourceStrings (SetFunction : TResourceIterator);
  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:=SetFunction(Name,DefaultValue,HashValue);
  224. end;
  225. Procedure ResetResourceTables;
  226. Var I,J : longint;
  227. begin
  228. With ResourceStringTable do
  229. For I:=0 to Count-1 do
  230. With Tables[I]^ do
  231. For J:=0 to Count-1 do
  232. With ResRec[J] do
  233. CurrentValue:=DefaultValue;
  234. end;
  235. Function ResourceStringTableCount : Longint;
  236. begin
  237. Result:=ResourceStringTable.Count;
  238. end;
  239. Function CheckTableIndex (Index: longint) : Boolean;
  240. begin
  241. Result:=(Index<ResourceStringTable.Count) and (Index>=0)
  242. end;
  243. Function CheckStringIndex (TableIndex,Index: longint) : Boolean;
  244. begin
  245. Result:=(TableIndex<ResourceStringTable.Count) and (TableIndex>=0) and
  246. (Index<ResourceStringTable.Tables[TableIndex]^.Count) and (Index>=0)
  247. end;
  248. Function ResourceStringCount(TableIndex : longint) : longint;
  249. begin
  250. If not CheckTableIndex(TableIndex) then
  251. Result:=-1
  252. else
  253. Result:=ResourceStringTable.Tables[TableIndex]^.Count;
  254. end;
  255. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  256. begin
  257. If not CheckStringIndex(Tableindex,StringIndex) then
  258. Result:=''
  259. else
  260. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].Name;
  261. end;
  262. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  263. begin
  264. If not CheckStringIndex(Tableindex,StringIndex) then
  265. Result:=0
  266. else
  267. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].HashValue;
  268. end;
  269. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString;
  270. begin
  271. If not CheckStringIndex(Tableindex,StringIndex) then
  272. Result:=''
  273. else
  274. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].DefaultValue;
  275. end;
  276. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  277. begin
  278. If not CheckStringIndex(Tableindex,StringIndex) then
  279. Result:=''
  280. else
  281. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue;
  282. end;
  283. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  284. begin
  285. Result:=CheckStringIndex(Tableindex,StringIndex);
  286. If Result then
  287. ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue:=Value;
  288. end;
  289. Function LoadResString(p:PResStringRec):AnsiString;
  290. begin
  291. Result:=p^;
  292. end;
  293. Initialization
  294. ResetResourceTables;
  295. finalization
  296. end.
  297. {
  298. $Log$
  299. Revision 1.12 2003-05-29 08:43:52 michael
  300. + Paramstr(0) must return binary name
  301. Revision 1.11 2002/09/07 16:01:22 peter
  302. * old logs removed and tabs fixed
  303. Revision 1.1.2.3 2003/05/29 08:42:23 michael
  304. + Paramstr(0) must return binary name
  305. Revision 1.10 2002/01/25 17:41:25 peter
  306. * delphi compatible array types
  307. Revision 1.9 2002/01/06 21:59:13 peter
  308. * regenerated
  309. }