objpas.pp 14 KB

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