2
0

objpas.pp 15 KB

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