objpas.pp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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. {$ifdef CPU16}
  20. const
  21. MaxInt = MaxSmallint;
  22. type
  23. Integer = smallint;
  24. PInteger = ^Integer;
  25. {$else CPU16}
  26. const
  27. MaxInt = MaxLongint;
  28. type
  29. Integer = longint;
  30. PInteger = ^Integer;
  31. {$endif CPU16}
  32. { Ansistring are the default }
  33. PString = PAnsiString;
  34. { array types }
  35. {$ifdef CPU16}
  36. IntegerArray = array[0..(32768 div SizeOf(Integer))-2] of Integer;
  37. {$else CPU16}
  38. IntegerArray = array[0..$effffff] of Integer;
  39. {$endif CPU16}
  40. TIntegerArray = IntegerArray;
  41. PIntegerArray = ^IntegerArray;
  42. {$ifdef CPU16}
  43. PointerArray = array [0..(32768 div SizeOf(Pointer))-2] of Pointer;
  44. {$else CPU16}
  45. PointerArray = array [0..512*1024*1024-2] of Pointer;
  46. {$endif CPU16}
  47. TPointerArray = PointerArray;
  48. PPointerArray = ^PointerArray;
  49. TBoundArray = array of integer;
  50. {$if FPC_FULLVERSION >= 20701}
  51. { Generic support for enumerator interfaces. These are added here, because
  52. mode (Obj)FPC does currently not allow the overloading of types with
  53. generic types (this will need a modeswitch...) }
  54. { Note: In Delphi these two generic types inherit from the two interfaces
  55. above, but in FPC as well as in Delphi(!) this leads to problems,
  56. because of method hiding and method implementation. E.g.
  57. consider a class which enumerates integers one needs to implement
  58. a GetCurrent for TObject as well... }
  59. generic IEnumerator<T> = interface
  60. function GetCurrent: T;
  61. function MoveNext: Boolean;
  62. procedure Reset;
  63. property Current: T read GetCurrent;
  64. end;
  65. generic IEnumerable<T> = interface
  66. function GetEnumerator: specialize IEnumerator<T>;
  67. end;
  68. {$endif}
  69. {$ifdef FPC_HAS_FEATURE_CLASSES}
  70. Var
  71. ExceptionClass: TClass; { Exception base class (must actually be Exception, defined in sysutils ) }
  72. {$endif FPC_HAS_FEATURE_CLASSES}
  73. {****************************************************************************
  74. Compatibility routines.
  75. ****************************************************************************}
  76. {$ifdef FPC_HAS_FEATURE_FILEIO}
  77. { Untyped file support }
  78. Procedure AssignFile(out f:File;p:pchar);
  79. Procedure AssignFile(out f:File;c:char);
  80. Procedure AssignFile(out f:File;const Name:UnicodeString);
  81. Procedure AssignFile(out f:File;const Name:RawByteString);
  82. Procedure CloseFile(var f:File);
  83. {$endif FPC_HAS_FEATURE_FILEIO}
  84. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  85. { Text file support }
  86. Procedure AssignFile(out t:Text;p:pchar);
  87. Procedure AssignFile(out t:Text;c:char);
  88. Procedure AssignFile(out t:Text;const Name:UnicodeString);
  89. Procedure AssignFile(out t:Text;const Name:RawByteString);
  90. Procedure CloseFile(Var t:Text);
  91. {$endif FPC_HAS_FEATURE_TEXTIO}
  92. {$ifdef FPC_HAS_FEATURE_FILEIO}
  93. { Typed file supoort }
  94. Procedure AssignFile(out f:TypedFile;p:pchar);
  95. Procedure AssignFile(out f:TypedFile;c:char);
  96. Procedure AssignFile(out f:TypedFile;const Name:UnicodeString);
  97. Procedure AssignFile(out f:TypedFile;const Name:RawByteString);
  98. {$endif FPC_HAS_FEATURE_FILEIO}
  99. {$ifdef FPC_HAS_FEATURE_COMMANDARGS}
  100. { ParamStr should return also an ansistring }
  101. Function ParamStr(Param : Integer) : Ansistring;
  102. {$endif FPC_HAS_FEATURE_COMMANDARGS}
  103. {****************************************************************************
  104. Resource strings.
  105. ****************************************************************************}
  106. {$ifdef FPC_HAS_FEATURE_RESOURCES}
  107. type
  108. TResourceIterator = Function (Name,Value : AnsiString; Hash : Longint; arg:pointer) : AnsiString;
  109. Function Hash(S : AnsiString) : LongWord;
  110. Procedure ResetResourceTables;
  111. Procedure FinalizeResourceTables;
  112. Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
  113. Procedure SetUnitResourceStrings (const UnitName:string;SetFunction : TResourceIterator;arg:pointer);
  114. {$ifndef RESSTRSECTIONS}
  115. Function ResourceStringTableCount : Longint;
  116. Function ResourceStringCount(TableIndex : longint) : longint;
  117. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  118. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  119. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString;
  120. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  121. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  122. {$endif RESSTRSECTIONS}
  123. { Delphi compatibility }
  124. type
  125. PResStringRec=^AnsiString;
  126. TResStringRec=AnsiString;
  127. Function LoadResString(p:PResStringRec):AnsiString;
  128. {$endif FPC_HAS_FEATURE_RESOURCES}
  129. implementation
  130. {****************************************************************************
  131. Compatibility routines.
  132. ****************************************************************************}
  133. {$ifdef FPC_HAS_FEATURE_FILEIO}
  134. { Untyped file support }
  135. Procedure AssignFile(out f:File;p:pchar);
  136. begin
  137. System.Assign (F,p);
  138. end;
  139. Procedure AssignFile(out f:File;c:char);
  140. begin
  141. System.Assign (F,c);
  142. end;
  143. Procedure AssignFile(out f:File;const Name:RawBytestring);
  144. begin
  145. System.Assign (F,Name);
  146. end;
  147. Procedure AssignFile(out f:File;const Name:UnicodeString);
  148. begin
  149. System.Assign (F,Name);
  150. end;
  151. Procedure CloseFile(Var f:File); [IOCheck];
  152. begin
  153. { Catch Runtime error/Exception }
  154. System.Close(f);
  155. end;
  156. {$endif FPC_HAS_FEATURE_FILEIO}
  157. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  158. { Text file support }
  159. Procedure AssignFile(out t:Text;p:pchar);
  160. begin
  161. System.Assign (T,p);
  162. end;
  163. Procedure AssignFile(out t:Text;c:char);
  164. begin
  165. System.Assign (T,c);
  166. end;
  167. Procedure AssignFile(out t:Text;const Name:RawBytestring);
  168. begin
  169. System.Assign (T,Name);
  170. end;
  171. Procedure AssignFile(out t:Text;const Name:UnicodeString);
  172. begin
  173. System.Assign (T,Name);
  174. end;
  175. Procedure CloseFile(Var t:Text); [IOCheck];
  176. begin
  177. { Catch Runtime error/Exception }
  178. System.Close(T);
  179. end;
  180. {$endif FPC_HAS_FEATURE_TEXTIO}
  181. {$ifdef FPC_HAS_FEATURE_FILEIO}
  182. { Typed file support }
  183. Procedure AssignFile(out f:TypedFile;p:pchar);
  184. begin
  185. System.Assign (F,p);
  186. end;
  187. Procedure AssignFile(out f:TypedFile;c:char);
  188. begin
  189. System.Assign (F,c);
  190. end;
  191. Procedure AssignFile(out f:TypedFile;const Name:RawBytestring);
  192. begin
  193. System.Assign (F,Name);
  194. end;
  195. Procedure AssignFile(out f:TypedFile;const Name:UnicodeString);
  196. begin
  197. System.Assign (F,Name);
  198. end;
  199. {$endif FPC_HAS_FEATURE_FILEIO}
  200. {$ifdef FPC_HAS_FEATURE_COMMANDARGS}
  201. Function ParamStr(Param : Integer) : ansistring;
  202. begin
  203. {
  204. Paramstr(0) should return the name of the binary.
  205. Since this functionality is included in the system unit,
  206. we fetch it from there.
  207. Normally, pathnames are less than 255 chars anyway,
  208. so this will work correct in 99% of all cases.
  209. In time, the system unit should get a GetExeName call.
  210. }
  211. if (Param=0) then
  212. Result:=System.Paramstr(0)
  213. else if (Param>0) and (Param<argc) then
  214. Result:=Argv[Param]
  215. else
  216. Result:='';
  217. end;
  218. {$endif FPC_HAS_FEATURE_COMMANDARGS}
  219. {$ifdef FPC_HAS_FEATURE_RESOURCES}
  220. { ---------------------------------------------------------------------
  221. ResourceString support
  222. ---------------------------------------------------------------------}
  223. Function Hash(S : AnsiString) : LongWord;
  224. Var
  225. thehash,g,I : LongWord;
  226. begin
  227. thehash:=0;
  228. For I:=1 to Length(S) do { 0 terminated }
  229. begin
  230. thehash:=thehash shl 4;
  231. inc(theHash,Ord(S[i]));
  232. g:=thehash and LongWord($f shl 28);
  233. if g<>0 then
  234. begin
  235. thehash:=thehash xor (g shr 24);
  236. thehash:=thehash xor g;
  237. end;
  238. end;
  239. If theHash=0 then
  240. Hash:=$ffffffff
  241. else
  242. Hash:=TheHash;
  243. end;
  244. {$ifdef RESSTRSECTIONS}
  245. Type
  246. PResourceStringRecord = ^TResourceStringRecord;
  247. TResourceStringRecord = Packed Record
  248. Name,
  249. CurrentValue,
  250. DefaultValue : AnsiString;
  251. HashValue : LongWord;
  252. {$ifdef cpu64}
  253. Dummy : LongWord; // alignment
  254. {$endif cpu64}
  255. end;
  256. TResourceStringTableList = Packed Record
  257. Count : sizeint;
  258. Tables : Array[{$ifdef cpu16}Byte{$else cpu16}Word{$endif cpu16}] of record
  259. TableStart,
  260. TableEnd : PResourceStringRecord;
  261. end;
  262. end;
  263. { Support for string constants initialized with resourcestrings }
  264. {$ifdef FPC_HAS_RESSTRINITS}
  265. PResStrInitEntry = ^TResStrInitEntry;
  266. TResStrInitEntry = record
  267. Addr: PPointer;
  268. Data: PResourceStringRecord;
  269. end;
  270. TResStrInitTable = packed record
  271. Count: {$ifdef VER2_6}longint{$else}sizeint{$endif};
  272. Tables: packed array[1..{$ifdef cpu16}8191{$else cpu16}32767{$endif cpu16}] of PResStrInitEntry;
  273. end;
  274. var
  275. ResStrInitTable : TResStrInitTable; external name 'FPC_RESSTRINITTABLES';
  276. procedure UpdateResourceStringRefs;
  277. var
  278. i: integer;
  279. ptable: PResStrInitEntry;
  280. begin
  281. for i:=1 to ResStrInitTable.Count do
  282. begin
  283. ptable:=ResStrInitTable.Tables[i];
  284. while Assigned(ptable^.Addr) do
  285. begin
  286. AnsiString(ptable^.Addr^):=ptable^.Data^.CurrentValue;
  287. Inc(ptable);
  288. end;
  289. end;
  290. end;
  291. {$endif FPC_HAS_RESSTRINITS}
  292. Var
  293. ResourceStringTable : TResourceStringTableList; External Name 'FPC_RESOURCESTRINGTABLES';
  294. Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
  295. Var
  296. ResStr : PResourceStringRecord;
  297. i : integer;
  298. s : AnsiString;
  299. begin
  300. With ResourceStringTable do
  301. begin
  302. For i:=0 to Count-1 do
  303. begin
  304. ResStr:=Tables[I].TableStart;
  305. { Skip first entry (name of the Unit) }
  306. inc(ResStr);
  307. while ResStr<Tables[I].TableEnd do
  308. begin
  309. s:=SetFunction(ResStr^.Name,ResStr^.DefaultValue,Longint(ResStr^.HashValue),arg);
  310. if s<>'' then
  311. ResStr^.CurrentValue:=s;
  312. inc(ResStr);
  313. end;
  314. end;
  315. end;
  316. {$ifdef FPC_HAS_RESSTRINITS}
  317. UpdateResourceStringRefs;
  318. {$endif FPC_HAS_RESSTRINITS}
  319. end;
  320. Procedure SetUnitResourceStrings (const UnitName:string;SetFunction : TResourceIterator;arg:pointer);
  321. Var
  322. ResStr : PResourceStringRecord;
  323. i : integer;
  324. s,
  325. UpUnitName : AnsiString;
  326. begin
  327. With ResourceStringTable do
  328. begin
  329. UpUnitName:=UpCase(UnitName);
  330. For i:=0 to Count-1 do
  331. begin
  332. ResStr:=Tables[I].TableStart;
  333. { Check name of the Unit }
  334. if ResStr^.Name<>UpUnitName then
  335. continue;
  336. inc(ResStr);
  337. while ResStr<Tables[I].TableEnd do
  338. begin
  339. s:=SetFunction(ResStr^.Name,ResStr^.DefaultValue,Longint(ResStr^.HashValue),arg);
  340. if s<>'' then
  341. ResStr^.CurrentValue:=s;
  342. inc(ResStr);
  343. end;
  344. end;
  345. end;
  346. {$ifdef FPC_HAS_RESSTRINITS}
  347. { Resourcestrings of one unit may be referenced from other units,
  348. so updating everything is the only option. }
  349. UpdateResourceStringRefs;
  350. {$endif FPC_HAS_RESSTRINITS}
  351. end;
  352. Procedure ResetResourceTables;
  353. Var
  354. ResStr : PResourceStringRecord;
  355. i : integer;
  356. begin
  357. With ResourceStringTable do
  358. begin
  359. For i:=0 to Count-1 do
  360. begin
  361. ResStr:=Tables[I].TableStart;
  362. { Skip first entry (name of the Unit) }
  363. inc(ResStr);
  364. while ResStr<Tables[I].TableEnd do
  365. begin
  366. ResStr^.CurrentValue:=ResStr^.DefaultValue;
  367. inc(ResStr);
  368. end;
  369. end;
  370. end;
  371. end;
  372. Procedure FinalizeResourceTables;
  373. Var
  374. ResStr : PResourceStringRecord;
  375. i : integer;
  376. begin
  377. With ResourceStringTable do
  378. begin
  379. For i:=0 to Count-1 do
  380. begin
  381. ResStr:=Tables[I].TableStart;
  382. { Skip first entry (name of the Unit) }
  383. inc(ResStr);
  384. while ResStr<Tables[I].TableEnd do
  385. begin
  386. ResStr^.CurrentValue:='';
  387. inc(ResStr);
  388. end;
  389. end;
  390. end;
  391. end;
  392. {$else RESSTRSECTIONS}
  393. Type
  394. PResourceStringRecord = ^TResourceStringRecord;
  395. TResourceStringRecord = Packed Record
  396. DefaultValue,
  397. CurrentValue : AnsiString;
  398. HashValue : LongWord;
  399. Name : AnsiString;
  400. end;
  401. TResourceStringTable = Packed Record
  402. Count : longint;
  403. Resrec : Array[Word] of TResourceStringRecord;
  404. end;
  405. PResourceStringTable = ^TResourceStringTable;
  406. TResourceTableList = Packed Record
  407. Count : longint;
  408. Tables : Array[Word] of PResourceStringTable;
  409. end;
  410. Var
  411. ResourceStringTable : TResourceTablelist; External Name 'FPC_RESOURCESTRINGTABLES';
  412. Function GetResourceString(Const TheTable: TResourceStringTable;Index : longint) : AnsiString;[Public,Alias : 'FPC_GETRESOURCESTRING'];
  413. begin
  414. If (Index>=0) and (Index<TheTAble.Count) then
  415. Result:=TheTable.ResRec[Index].CurrentValue
  416. else
  417. Result:='';
  418. end;
  419. Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
  420. Var I,J : longint;
  421. begin
  422. With ResourceStringTable do
  423. For I:=0 to Count-1 do
  424. With Tables[I]^ do
  425. For J:=0 to Count-1 do
  426. With ResRec[J] do
  427. CurrentValue:=SetFunction(Name,DefaultValue,Longint(HashValue),arg);
  428. end;
  429. Procedure SetUnitResourceStrings (const UnitName:string;SetFunction : TResourceIterator;arg:pointer);
  430. begin
  431. SetResourceStrings (SetFunction,arg);
  432. end;
  433. Procedure ResetResourceTables;
  434. Var I,J : longint;
  435. begin
  436. With ResourceStringTable do
  437. For I:=0 to Count-1 do
  438. With Tables[I]^ do
  439. For J:=0 to Count-1 do
  440. With ResRec[J] do
  441. CurrentValue:=DefaultValue;
  442. end;
  443. Procedure FinalizeResourceTables;
  444. Var I,J : longint;
  445. begin
  446. With ResourceStringTable do
  447. For I:=0 to Count-1 do
  448. With Tables[I]^ do
  449. For J:=0 to Count-1 do
  450. With ResRec[J] do
  451. CurrentValue:='';
  452. end;
  453. Function ResourceStringTableCount : Longint;
  454. begin
  455. Result:=ResourceStringTable.Count;
  456. end;
  457. Function CheckTableIndex (Index: longint) : Boolean;
  458. begin
  459. Result:=(Index<ResourceStringTable.Count) and (Index>=0)
  460. end;
  461. Function CheckStringIndex (TableIndex,Index: longint) : Boolean;
  462. begin
  463. Result:=(TableIndex<ResourceStringTable.Count) and (TableIndex>=0) and
  464. (Index<ResourceStringTable.Tables[TableIndex]^.Count) and (Index>=0)
  465. end;
  466. Function ResourceStringCount(TableIndex : longint) : longint;
  467. begin
  468. If not CheckTableIndex(TableIndex) then
  469. Result:=-1
  470. else
  471. Result:=ResourceStringTable.Tables[TableIndex]^.Count;
  472. end;
  473. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  474. begin
  475. If not CheckStringIndex(Tableindex,StringIndex) then
  476. Result:=''
  477. else
  478. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].Name;
  479. end;
  480. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  481. begin
  482. If not CheckStringIndex(Tableindex,StringIndex) then
  483. Result:=0
  484. else
  485. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].HashValue;
  486. end;
  487. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString;
  488. begin
  489. If not CheckStringIndex(Tableindex,StringIndex) then
  490. Result:=''
  491. else
  492. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].DefaultValue;
  493. end;
  494. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  495. begin
  496. If not CheckStringIndex(Tableindex,StringIndex) then
  497. Result:=''
  498. else
  499. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue;
  500. end;
  501. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  502. begin
  503. Result:=CheckStringIndex(Tableindex,StringIndex);
  504. If Result then
  505. ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue:=Value;
  506. end;
  507. {$endif RESSTRSECTIONS}
  508. Function LoadResString(p:PResStringRec):AnsiString;
  509. begin
  510. Result:=p^;
  511. end;
  512. {$endif FPC_HAS_FEATURE_RESOURCES}
  513. {$ifdef FPC_HAS_FEATURE_RESOURCES}
  514. Initialization
  515. { ResetResourceTables;}
  516. finalization
  517. FinalizeResourceTables;
  518. {$endif FPC_HAS_FEATURE_RESOURCES}
  519. end.