objpas.pp 15 KB

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