objpas.pp 15 KB

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