objpas.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. This unit makes Free Pascal as much as possible Delphi compatible
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$Mode ObjFpc}
  12. {$I-}
  13. {$ifndef Unix}
  14. {$S-}
  15. {$endif}
  16. unit objpas;
  17. interface
  18. { first, in object pascal, the integer type must be redefined }
  19. const
  20. MaxInt = MaxLongint;
  21. type
  22. Integer = longint;
  23. PInteger = ^Integer;
  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. TBoundArray = array of integer;
  34. {****************************************************************************
  35. Compatibility routines.
  36. ****************************************************************************}
  37. { Untyped file support }
  38. Procedure AssignFile(Var f:File;const Name:string);
  39. Procedure AssignFile(Var f:File;p:pchar);
  40. Procedure AssignFile(Var f:File;c:char);
  41. Procedure CloseFile(Var f:File);
  42. { Text file support }
  43. Procedure AssignFile(Var t:Text;const s:string);
  44. Procedure AssignFile(Var t:Text;p:pchar);
  45. Procedure AssignFile(Var t:Text;c:char);
  46. Procedure CloseFile(Var t:Text);
  47. { Typed file supoort }
  48. Procedure AssignFile(Var f:TypedFile;const Name:string);
  49. Procedure AssignFile(Var f:TypedFile;p:pchar);
  50. Procedure AssignFile(Var f:TypedFile;c:char);
  51. { ParamStr should return also an ansistring }
  52. Function ParamStr(Param : Integer) : Ansistring;
  53. {****************************************************************************
  54. Resource strings.
  55. ****************************************************************************}
  56. type
  57. TResourceIterator = Function (Name,Value : AnsiString; Hash : Longint; arg:pointer) : AnsiString;
  58. Function Hash(S : AnsiString) : LongWord;
  59. Procedure ResetResourceTables;
  60. Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
  61. {$ifndef RESSTRSECTIONS}
  62. Function ResourceStringTableCount : Longint;
  63. Function ResourceStringCount(TableIndex : longint) : longint;
  64. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  65. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  66. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString;
  67. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  68. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  69. {$endif RESSTRSECTIONS}
  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. Function Hash(S : AnsiString) : LongWord;
  161. Var
  162. thehash,g,I : LongWord;
  163. begin
  164. thehash:=0;
  165. For I:=1 to Length(S) do { 0 terminated }
  166. begin
  167. thehash:=thehash shl 4;
  168. inc(theHash,Ord(S[i]));
  169. g:=thehash and LongWord($f shl 28);
  170. if g<>0 then
  171. begin
  172. thehash:=thehash xor (g shr 24);
  173. thehash:=thehash xor g;
  174. end;
  175. end;
  176. If theHash=0 then
  177. Hash:=$ffffffff
  178. else
  179. Hash:=TheHash;
  180. end;
  181. {$ifdef RESSTRSECTIONS}
  182. Type
  183. PResourceStringRecord = ^TResourceStringRecord;
  184. TResourceStringRecord = Packed Record
  185. Name,
  186. CurrentValue,
  187. DefaultValue : AnsiString;
  188. HashValue : LongWord;
  189. end;
  190. Var
  191. ResourceStrings : TResourceStringRecord; External Name 'FPC_RESOURCESTRINGS';
  192. Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
  193. Var
  194. ResStr : PResourceStringRecord;
  195. begin
  196. ResStr:=@ResourceStrings;
  197. while ResStr^.Name<>'' do
  198. begin
  199. ResStr^.CurrentValue:=SetFunction(ResStr^.Name,ResStr^.DefaultValue,ResStr^.HashValue,arg);
  200. inc(ResStr);
  201. end;
  202. end;
  203. Procedure ResetResourceTables;
  204. Var
  205. ResStr : PResourceStringRecord;
  206. begin
  207. ResStr:=@ResourceStrings;
  208. while ResStr^.Name<>'' do
  209. begin
  210. ResStr^.CurrentValue:=ResStr^.DefaultValue;
  211. inc(ResStr);
  212. end;
  213. end;
  214. Procedure FinalizeResourceTables;
  215. Var
  216. ResStr : PResourceStringRecord;
  217. begin
  218. ResStr:=@ResourceStrings;
  219. while ResStr^.Name<>'' do
  220. begin
  221. ResStr^.CurrentValue:='';
  222. inc(ResStr);
  223. end;
  224. end;
  225. {$else RESSTRSECTIONS}
  226. Type
  227. PResourceStringRecord = ^TResourceStringRecord;
  228. TResourceStringRecord = Packed Record
  229. DefaultValue,
  230. CurrentValue : AnsiString;
  231. HashValue : LongWord;
  232. Name : AnsiString;
  233. end;
  234. TResourceStringTable = Packed Record
  235. Count : longint;
  236. Resrec : Array[Word] of TResourceStringRecord;
  237. end;
  238. PResourceStringTable = ^TResourceStringTable;
  239. TResourceTableList = Packed Record
  240. Count : longint;
  241. Tables : Array[Word] of PResourceStringTable;
  242. end;
  243. Var
  244. ResourceStringTable : TResourceTablelist; External Name 'FPC_RESOURCESTRINGTABLES';
  245. Function GetResourceString(Const TheTable: TResourceStringTable;Index : longint) : AnsiString;[Public,Alias : 'FPC_GETRESOURCESTRING'];
  246. begin
  247. If (Index>=0) and (Index<TheTAble.Count) then
  248. Result:=TheTable.ResRec[Index].CurrentValue
  249. else
  250. Result:='';
  251. end;
  252. Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
  253. Var I,J : longint;
  254. begin
  255. With ResourceStringTable do
  256. For I:=0 to Count-1 do
  257. With Tables[I]^ do
  258. For J:=0 to Count-1 do
  259. With ResRec[J] do
  260. CurrentValue:=SetFunction(Name,DefaultValue,HashValue,arg);
  261. end;
  262. Procedure ResetResourceTables;
  263. Var I,J : longint;
  264. begin
  265. With ResourceStringTable do
  266. For I:=0 to Count-1 do
  267. With Tables[I]^ do
  268. For J:=0 to Count-1 do
  269. With ResRec[J] do
  270. CurrentValue:=DefaultValue;
  271. end;
  272. Procedure FinalizeResourceTables;
  273. Var I,J : longint;
  274. begin
  275. With ResourceStringTable do
  276. For I:=0 to Count-1 do
  277. With Tables[I]^ do
  278. For J:=0 to Count-1 do
  279. With ResRec[J] do
  280. CurrentValue:='';
  281. end;
  282. Function ResourceStringTableCount : Longint;
  283. begin
  284. Result:=ResourceStringTable.Count;
  285. end;
  286. Function CheckTableIndex (Index: longint) : Boolean;
  287. begin
  288. Result:=(Index<ResourceStringTable.Count) and (Index>=0)
  289. end;
  290. Function CheckStringIndex (TableIndex,Index: longint) : Boolean;
  291. begin
  292. Result:=(TableIndex<ResourceStringTable.Count) and (TableIndex>=0) and
  293. (Index<ResourceStringTable.Tables[TableIndex]^.Count) and (Index>=0)
  294. end;
  295. Function ResourceStringCount(TableIndex : longint) : longint;
  296. begin
  297. If not CheckTableIndex(TableIndex) then
  298. Result:=-1
  299. else
  300. Result:=ResourceStringTable.Tables[TableIndex]^.Count;
  301. end;
  302. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  303. begin
  304. If not CheckStringIndex(Tableindex,StringIndex) then
  305. Result:=''
  306. else
  307. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].Name;
  308. end;
  309. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  310. begin
  311. If not CheckStringIndex(Tableindex,StringIndex) then
  312. Result:=0
  313. else
  314. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].HashValue;
  315. end;
  316. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString;
  317. begin
  318. If not CheckStringIndex(Tableindex,StringIndex) then
  319. Result:=''
  320. else
  321. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].DefaultValue;
  322. end;
  323. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  324. begin
  325. If not CheckStringIndex(Tableindex,StringIndex) then
  326. Result:=''
  327. else
  328. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue;
  329. end;
  330. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  331. begin
  332. Result:=CheckStringIndex(Tableindex,StringIndex);
  333. If Result then
  334. ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue:=Value;
  335. end;
  336. {$endif RESSTRSECTIONS}
  337. Function LoadResString(p:PResStringRec):AnsiString;
  338. begin
  339. Result:=p^;
  340. end;
  341. Initialization
  342. ResetResourceTables;
  343. finalization
  344. FinalizeResourceTables;
  345. end.