objpas.pp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. Procedure SetUnitResourceStrings (const UnitName:string;SetFunction : TResourceIterator;arg:pointer);
  62. {$ifndef RESSTRSECTIONS}
  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. {$endif RESSTRSECTIONS}
  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); [IOCheck];
  94. begin
  95. { Catch Runtime error/Exception }
  96. System.Close(f);
  97. end;
  98. { Text file support }
  99. Procedure AssignFile(Var t:Text;const s:string);
  100. begin
  101. System.Assign (T,S);
  102. end;
  103. Procedure AssignFile(Var t:Text;p:pchar);
  104. begin
  105. System.Assign (T,P);
  106. end;
  107. Procedure AssignFile(Var t:Text;c:char);
  108. begin
  109. System.Assign (T,C);
  110. end;
  111. Procedure CloseFile(Var t:Text); [IOCheck];
  112. begin
  113. { Catch Runtime error/Exception }
  114. System.Close(T);
  115. end;
  116. { Typed file support }
  117. Procedure AssignFile(Var f:TypedFile;const Name:string);
  118. begin
  119. system.Assign(F,Name);
  120. end;
  121. Procedure AssignFile(Var f:TypedFile;p:pchar);
  122. begin
  123. system.Assign (F,p);
  124. end;
  125. Procedure AssignFile(Var f:TypedFile;c:char);
  126. begin
  127. system.Assign (F,C);
  128. end;
  129. Function ParamStr(Param : Integer) : Ansistring;
  130. Var Len : longint;
  131. begin
  132. {
  133. Paramstr(0) should return the name of the binary.
  134. Since this functionality is included in the system unit,
  135. we fetch it from there.
  136. Normally, pathnames are less than 255 chars anyway,
  137. so this will work correct in 99% of all cases.
  138. In time, the system unit should get a GetExeName call.
  139. }
  140. if (Param=0) then
  141. Result:=System.Paramstr(0)
  142. else if (Param>0) and (Param<argc) then
  143. begin
  144. Len:=0;
  145. While Argv[Param][Len]<>#0 do
  146. Inc(len);
  147. SetLength(Result,Len);
  148. If Len>0 then
  149. Move(Argv[Param][0],Result[1],Len);
  150. end
  151. else
  152. paramstr:='';
  153. end;
  154. { ---------------------------------------------------------------------
  155. ResourceString support
  156. ---------------------------------------------------------------------}
  157. Function Hash(S : AnsiString) : LongWord;
  158. Var
  159. thehash,g,I : LongWord;
  160. begin
  161. thehash:=0;
  162. For I:=1 to Length(S) do { 0 terminated }
  163. begin
  164. thehash:=thehash shl 4;
  165. inc(theHash,Ord(S[i]));
  166. g:=thehash and LongWord($f shl 28);
  167. if g<>0 then
  168. begin
  169. thehash:=thehash xor (g shr 24);
  170. thehash:=thehash xor g;
  171. end;
  172. end;
  173. If theHash=0 then
  174. Hash:=$ffffffff
  175. else
  176. Hash:=TheHash;
  177. end;
  178. {$ifdef RESSTRSECTIONS}
  179. Type
  180. PResourceStringRecord = ^TResourceStringRecord;
  181. TResourceStringRecord = Packed Record
  182. Name,
  183. CurrentValue,
  184. DefaultValue : AnsiString;
  185. HashValue : LongWord;
  186. {$ifdef cpu64}
  187. Dummy : LongWord; // alignment
  188. {$endif cpu64}
  189. end;
  190. TResourceStringTableList = Packed Record
  191. Count : ptrint;
  192. Tables : Array[Word] of record
  193. TableStart,
  194. TableEnd : PResourceStringRecord;
  195. end;
  196. end;
  197. Var
  198. ResourceStringTable : TResourceStringTableList; External Name 'FPC_RESOURCESTRINGTABLES';
  199. Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
  200. Var
  201. ResStr : PResourceStringRecord;
  202. i : Longint;
  203. s : AnsiString;
  204. begin
  205. With ResourceStringTable do
  206. begin
  207. For i:=0 to Count-1 do
  208. begin
  209. ResStr:=Tables[I].TableStart;
  210. { Skip first entry (name of the Unit) }
  211. inc(ResStr);
  212. while ResStr<Tables[I].TableEnd do
  213. begin
  214. s:=SetFunction(ResStr^.Name,ResStr^.DefaultValue,ResStr^.HashValue,arg);
  215. if s<>'' then
  216. ResStr^.CurrentValue:=s;
  217. inc(ResStr);
  218. end;
  219. end;
  220. end;
  221. end;
  222. Procedure SetUnitResourceStrings (const UnitName:string;SetFunction : TResourceIterator;arg:pointer);
  223. Var
  224. ResStr : PResourceStringRecord;
  225. i : Longint;
  226. s,
  227. UpUnitName : AnsiString;
  228. begin
  229. With ResourceStringTable do
  230. begin
  231. UpUnitName:=UpCase(UnitName);
  232. For i:=0 to Count-1 do
  233. begin
  234. ResStr:=Tables[I].TableStart;
  235. { Check name of the Unit }
  236. if ResStr^.Name<>UpUnitName then
  237. continue;
  238. inc(ResStr);
  239. while ResStr<Tables[I].TableEnd do
  240. begin
  241. s:=SetFunction(ResStr^.Name,ResStr^.DefaultValue,ResStr^.HashValue,arg);
  242. if s<>'' then
  243. ResStr^.CurrentValue:=s;
  244. inc(ResStr);
  245. end;
  246. end;
  247. end;
  248. end;
  249. Procedure ResetResourceTables;
  250. Var
  251. ResStr : PResourceStringRecord;
  252. i : Longint;
  253. begin
  254. With ResourceStringTable do
  255. begin
  256. For i:=0 to Count-1 do
  257. begin
  258. ResStr:=Tables[I].TableStart;
  259. { Skip first entry (name of the Unit) }
  260. inc(ResStr);
  261. while ResStr<Tables[I].TableEnd do
  262. begin
  263. ResStr^.CurrentValue:=ResStr^.DefaultValue;
  264. inc(ResStr);
  265. end;
  266. end;
  267. end;
  268. end;
  269. Procedure FinalizeResourceTables;
  270. Var
  271. ResStr : PResourceStringRecord;
  272. i : Longint;
  273. begin
  274. With ResourceStringTable do
  275. begin
  276. For i:=0 to Count-1 do
  277. begin
  278. ResStr:=Tables[I].TableStart;
  279. { Skip first entry (name of the Unit) }
  280. inc(ResStr);
  281. while ResStr<Tables[I].TableEnd do
  282. begin
  283. ResStr^.CurrentValue:='';
  284. inc(ResStr);
  285. end;
  286. end;
  287. end;
  288. end;
  289. {$else RESSTRSECTIONS}
  290. Type
  291. PResourceStringRecord = ^TResourceStringRecord;
  292. TResourceStringRecord = Packed Record
  293. DefaultValue,
  294. CurrentValue : AnsiString;
  295. HashValue : LongWord;
  296. Name : AnsiString;
  297. end;
  298. TResourceStringTable = Packed Record
  299. Count : longint;
  300. Resrec : Array[Word] of TResourceStringRecord;
  301. end;
  302. PResourceStringTable = ^TResourceStringTable;
  303. TResourceTableList = Packed Record
  304. Count : longint;
  305. Tables : Array[Word] of PResourceStringTable;
  306. end;
  307. Var
  308. ResourceStringTable : TResourceTablelist; External Name 'FPC_RESOURCESTRINGTABLES';
  309. Function GetResourceString(Const TheTable: TResourceStringTable;Index : longint) : AnsiString;[Public,Alias : 'FPC_GETRESOURCESTRING'];
  310. begin
  311. If (Index>=0) and (Index<TheTAble.Count) then
  312. Result:=TheTable.ResRec[Index].CurrentValue
  313. else
  314. Result:='';
  315. end;
  316. Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
  317. Var I,J : longint;
  318. begin
  319. With ResourceStringTable do
  320. For I:=0 to Count-1 do
  321. With Tables[I]^ do
  322. For J:=0 to Count-1 do
  323. With ResRec[J] do
  324. CurrentValue:=SetFunction(Name,DefaultValue,HashValue,arg);
  325. end;
  326. Procedure SetUnitResourceStrings (const UnitName:string;SetFunction : TResourceIterator;arg:pointer);
  327. begin
  328. SetResourceStrings (SetFunction,arg);
  329. end;
  330. Procedure ResetResourceTables;
  331. Var I,J : longint;
  332. begin
  333. With ResourceStringTable do
  334. For I:=0 to Count-1 do
  335. With Tables[I]^ do
  336. For J:=0 to Count-1 do
  337. With ResRec[J] do
  338. CurrentValue:=DefaultValue;
  339. end;
  340. Procedure FinalizeResourceTables;
  341. Var I,J : longint;
  342. begin
  343. With ResourceStringTable do
  344. For I:=0 to Count-1 do
  345. With Tables[I]^ do
  346. For J:=0 to Count-1 do
  347. With ResRec[J] do
  348. CurrentValue:='';
  349. end;
  350. Function ResourceStringTableCount : Longint;
  351. begin
  352. Result:=ResourceStringTable.Count;
  353. end;
  354. Function CheckTableIndex (Index: longint) : Boolean;
  355. begin
  356. Result:=(Index<ResourceStringTable.Count) and (Index>=0)
  357. end;
  358. Function CheckStringIndex (TableIndex,Index: longint) : Boolean;
  359. begin
  360. Result:=(TableIndex<ResourceStringTable.Count) and (TableIndex>=0) and
  361. (Index<ResourceStringTable.Tables[TableIndex]^.Count) and (Index>=0)
  362. end;
  363. Function ResourceStringCount(TableIndex : longint) : longint;
  364. begin
  365. If not CheckTableIndex(TableIndex) then
  366. Result:=-1
  367. else
  368. Result:=ResourceStringTable.Tables[TableIndex]^.Count;
  369. end;
  370. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  371. begin
  372. If not CheckStringIndex(Tableindex,StringIndex) then
  373. Result:=''
  374. else
  375. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].Name;
  376. end;
  377. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  378. begin
  379. If not CheckStringIndex(Tableindex,StringIndex) then
  380. Result:=0
  381. else
  382. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].HashValue;
  383. end;
  384. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString;
  385. begin
  386. If not CheckStringIndex(Tableindex,StringIndex) then
  387. Result:=''
  388. else
  389. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].DefaultValue;
  390. end;
  391. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  392. begin
  393. If not CheckStringIndex(Tableindex,StringIndex) then
  394. Result:=''
  395. else
  396. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue;
  397. end;
  398. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  399. begin
  400. Result:=CheckStringIndex(Tableindex,StringIndex);
  401. If Result then
  402. ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue:=Value;
  403. end;
  404. {$endif RESSTRSECTIONS}
  405. Function LoadResString(p:PResStringRec):AnsiString;
  406. begin
  407. Result:=p^;
  408. end;
  409. Initialization
  410. ResetResourceTables;
  411. finalization
  412. FinalizeResourceTables;
  413. end.