objpas.pp 15 KB

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