objpas.pp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. const
  20. MaxInt = MaxLongint;
  21. type
  22. Integer = longint;
  23. PInteger = ^Integer;
  24. { Ansistring are the default }
  25. PString = PAnsiString;
  26. { array types }
  27. {$ifdef CPU16}
  28. IntegerArray = array[0..$eff] of Integer;
  29. {$else CPU16}
  30. IntegerArray = array[0..$effffff] of Integer;
  31. {$endif CPU16}
  32. TIntegerArray = IntegerArray;
  33. PIntegerArray = ^IntegerArray;
  34. {$ifdef CPU16}
  35. PointerArray = array [0..16*1024-2] of Pointer;
  36. {$else CPU16}
  37. PointerArray = array [0..512*1024*1024-2] of Pointer;
  38. {$endif CPU16}
  39. TPointerArray = PointerArray;
  40. PPointerArray = ^PointerArray;
  41. TBoundArray = array of integer;
  42. {$ifdef FPC_HAS_FEATURE_CLASSES}
  43. Var
  44. ExceptionClass: TClass; { Exception base class (must actually be Exception, defined in sysutils ) }
  45. {$endif FPC_HAS_FEATURE_CLASSES}
  46. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  47. Var
  48. ExceptObjProc: Pointer; { Used to convert OS exceptions to exceptions in Delphi. Unused in FPC}
  49. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  50. {****************************************************************************
  51. Compatibility routines.
  52. ****************************************************************************}
  53. {$ifdef FPC_HAS_FEATURE_FILEIO}
  54. { Untyped file support }
  55. Procedure AssignFile(out f:File;const Name:string);
  56. Procedure AssignFile(out f:File;p:pchar);
  57. Procedure AssignFile(out f:File;c:char);
  58. Procedure CloseFile(var f:File);
  59. {$endif FPC_HAS_FEATURE_FILEIO}
  60. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  61. { Text file support }
  62. Procedure AssignFile(out t:Text;const s:string);
  63. Procedure AssignFile(out t:Text;p:pchar);
  64. Procedure AssignFile(out t:Text;c:char);
  65. Procedure CloseFile(Var t:Text);
  66. {$endif FPC_HAS_FEATURE_TEXTIO}
  67. {$ifdef FPC_HAS_FEATURE_FILEIO}
  68. { Typed file supoort }
  69. Procedure AssignFile(out f:TypedFile;const Name:string);
  70. Procedure AssignFile(out f:TypedFile;p:pchar);
  71. Procedure AssignFile(out f:TypedFile;c:char);
  72. {$endif FPC_HAS_FEATURE_FILEIO}
  73. {$ifdef FPC_HAS_FEATURE_COMMANDARGS}
  74. { ParamStr should return also an ansistring }
  75. Function ParamStr(Param : Integer) : Ansistring;
  76. {$endif FPC_HAS_FEATURE_COMMANDARGS}
  77. {$ifdef FPC_HAS_FEATURE_FILEIO}
  78. Procedure MkDir(s:ansistring);overload;
  79. Procedure RmDir(s:ansistring);overload;
  80. Procedure ChDir(s:ansistring);overload;
  81. {$endif FPC_HAS_FEATURE_FILEIO}
  82. {****************************************************************************
  83. Resource strings.
  84. ****************************************************************************}
  85. {$ifdef FPC_HAS_FEATURE_RESOURCES}
  86. type
  87. TResourceIterator = Function (Name,Value : AnsiString; Hash : Longint; arg:pointer) : AnsiString;
  88. Function Hash(S : AnsiString) : LongWord;
  89. Procedure ResetResourceTables;
  90. Procedure FinalizeResourceTables;
  91. Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
  92. Procedure SetUnitResourceStrings (const UnitName:string;SetFunction : TResourceIterator;arg:pointer);
  93. {$ifndef RESSTRSECTIONS}
  94. Function ResourceStringTableCount : Longint;
  95. Function ResourceStringCount(TableIndex : longint) : longint;
  96. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  97. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  98. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString;
  99. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  100. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  101. {$endif RESSTRSECTIONS}
  102. { Delphi compatibility }
  103. type
  104. PResStringRec=^AnsiString;
  105. TResStringRec=AnsiString;
  106. Function LoadResString(p:PResStringRec):AnsiString;
  107. {$endif FPC_HAS_FEATURE_RESOURCES}
  108. implementation
  109. {****************************************************************************
  110. Compatibility routines.
  111. ****************************************************************************}
  112. {$ifdef FPC_HAS_FEATURE_FILEIO}
  113. Procedure MkDirpchar(s: pchar;len:sizeuint);[IOCheck]; external name 'FPC_SYS_MKDIR';
  114. Procedure ChDirpchar(s: pchar;len:sizeuint);[IOCheck]; external name 'FPC_SYS_CHDIR';
  115. Procedure RmDirpchar(s: pchar;len:sizeuint);[IOCheck]; external name 'FPC_SYS_RMDIR';
  116. { Untyped file support }
  117. Procedure AssignFile(out f:File;const Name:string);
  118. begin
  119. System.Assign (F,Name);
  120. end;
  121. Procedure AssignFile(out f:File;p:pchar);
  122. begin
  123. System.Assign (F,P);
  124. end;
  125. Procedure AssignFile(out f:File;c:char);
  126. begin
  127. System.Assign (F,C);
  128. end;
  129. Procedure CloseFile(Var f:File); [IOCheck];
  130. begin
  131. { Catch Runtime error/Exception }
  132. System.Close(f);
  133. end;
  134. {$endif FPC_HAS_FEATURE_FILEIO}
  135. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  136. { Text file support }
  137. Procedure AssignFile(out t:Text;const s:string);
  138. begin
  139. System.Assign (T,S);
  140. end;
  141. Procedure AssignFile(out t:Text;p:pchar);
  142. begin
  143. System.Assign (T,P);
  144. end;
  145. Procedure AssignFile(out t:Text;c:char);
  146. begin
  147. System.Assign (T,C);
  148. end;
  149. Procedure CloseFile(Var t:Text); [IOCheck];
  150. begin
  151. { Catch Runtime error/Exception }
  152. System.Close(T);
  153. end;
  154. {$endif FPC_HAS_FEATURE_TEXTIO}
  155. {$ifdef FPC_HAS_FEATURE_FILEIO}
  156. { Typed file support }
  157. Procedure AssignFile(out f:TypedFile;const Name:string);
  158. begin
  159. system.Assign(F,Name);
  160. end;
  161. Procedure AssignFile(out f:TypedFile;p:pchar);
  162. begin
  163. system.Assign (F,p);
  164. end;
  165. Procedure AssignFile(out f:TypedFile;c:char);
  166. begin
  167. system.Assign (F,C);
  168. end;
  169. {$endif FPC_HAS_FEATURE_FILEIO}
  170. {$ifdef FPC_HAS_FEATURE_COMMANDARGS}
  171. Function ParamStr(Param : Integer) : Ansistring;
  172. Var Len : longint;
  173. begin
  174. {
  175. Paramstr(0) should return the name of the binary.
  176. Since this functionality is included in the system unit,
  177. we fetch it from there.
  178. Normally, pathnames are less than 255 chars anyway,
  179. so this will work correct in 99% of all cases.
  180. In time, the system unit should get a GetExeName call.
  181. }
  182. if (Param=0) then
  183. Result:=System.Paramstr(0)
  184. else if (Param>0) and (Param<argc) then
  185. begin
  186. Len:=0;
  187. While Argv[Param][Len]<>#0 do
  188. Inc(len);
  189. SetLength(Result,Len);
  190. If Len>0 then
  191. Move(Argv[Param][0],Result[1],Len);
  192. end
  193. else
  194. paramstr:='';
  195. end;
  196. {$endif FPC_HAS_FEATURE_COMMANDARGS}
  197. {$ifdef FPC_HAS_FEATURE_FILEIO}
  198. { xxDirPChar procedures can adjust directory separators in supplied string (at least
  199. Windows implementation does so). Therefore full copy of argument is needed,
  200. just passing by value isn't enough because it won't copy a string literal. }
  201. Procedure MkDir(s:ansistring);[IOCheck];
  202. begin
  203. UniqueString(s);
  204. mkdirpchar(pchar(s),length(s));
  205. end;
  206. Procedure RmDir(s:ansistring);[IOCheck];
  207. begin
  208. UniqueString(s);
  209. RmDirpchar(pchar(s),length(s));
  210. end;
  211. Procedure ChDir(s:ansistring);[IOCheck];
  212. begin
  213. UniqueString(s);
  214. ChDirpchar(pchar(s),length(s));
  215. end;
  216. {$endif FPC_HAS_FEATURE_FILEIO}
  217. {$ifdef FPC_HAS_FEATURE_RESOURCES}
  218. { ---------------------------------------------------------------------
  219. ResourceString support
  220. ---------------------------------------------------------------------}
  221. Function Hash(S : AnsiString) : LongWord;
  222. Var
  223. thehash,g,I : LongWord;
  224. begin
  225. thehash:=0;
  226. For I:=1 to Length(S) do { 0 terminated }
  227. begin
  228. thehash:=thehash shl 4;
  229. inc(theHash,Ord(S[i]));
  230. g:=thehash and LongWord($f shl 28);
  231. if g<>0 then
  232. begin
  233. thehash:=thehash xor (g shr 24);
  234. thehash:=thehash xor g;
  235. end;
  236. end;
  237. If theHash=0 then
  238. Hash:=$ffffffff
  239. else
  240. Hash:=TheHash;
  241. end;
  242. {$ifdef RESSTRSECTIONS}
  243. Type
  244. PResourceStringRecord = ^TResourceStringRecord;
  245. TResourceStringRecord = Packed Record
  246. Name,
  247. CurrentValue,
  248. DefaultValue : AnsiString;
  249. HashValue : LongWord;
  250. {$ifdef cpu64}
  251. Dummy : LongWord; // alignment
  252. {$endif cpu64}
  253. end;
  254. TResourceStringTableList = Packed Record
  255. Count : ptrint;
  256. Tables : Array[Word] of record
  257. TableStart,
  258. TableEnd : PResourceStringRecord;
  259. end;
  260. end;
  261. Var
  262. ResourceStringTable : TResourceStringTableList; External Name 'FPC_RESOURCESTRINGTABLES';
  263. Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
  264. Var
  265. ResStr : PResourceStringRecord;
  266. i : Longint;
  267. s : AnsiString;
  268. begin
  269. With ResourceStringTable do
  270. begin
  271. For i:=0 to Count-1 do
  272. begin
  273. ResStr:=Tables[I].TableStart;
  274. { Skip first entry (name of the Unit) }
  275. inc(ResStr);
  276. while ResStr<Tables[I].TableEnd do
  277. begin
  278. s:=SetFunction(ResStr^.Name,ResStr^.DefaultValue,Longint(ResStr^.HashValue),arg);
  279. if s<>'' then
  280. ResStr^.CurrentValue:=s;
  281. inc(ResStr);
  282. end;
  283. end;
  284. end;
  285. end;
  286. Procedure SetUnitResourceStrings (const UnitName:string;SetFunction : TResourceIterator;arg:pointer);
  287. Var
  288. ResStr : PResourceStringRecord;
  289. i : Longint;
  290. s,
  291. UpUnitName : AnsiString;
  292. begin
  293. With ResourceStringTable do
  294. begin
  295. UpUnitName:=UpCase(UnitName);
  296. For i:=0 to Count-1 do
  297. begin
  298. ResStr:=Tables[I].TableStart;
  299. { Check name of the Unit }
  300. if ResStr^.Name<>UpUnitName then
  301. continue;
  302. inc(ResStr);
  303. while ResStr<Tables[I].TableEnd do
  304. begin
  305. s:=SetFunction(ResStr^.Name,ResStr^.DefaultValue,Longint(ResStr^.HashValue),arg);
  306. if s<>'' then
  307. ResStr^.CurrentValue:=s;
  308. inc(ResStr);
  309. end;
  310. end;
  311. end;
  312. end;
  313. Procedure ResetResourceTables;
  314. Var
  315. ResStr : PResourceStringRecord;
  316. i : Longint;
  317. begin
  318. With ResourceStringTable do
  319. begin
  320. For i:=0 to Count-1 do
  321. begin
  322. ResStr:=Tables[I].TableStart;
  323. { Skip first entry (name of the Unit) }
  324. inc(ResStr);
  325. while ResStr<Tables[I].TableEnd do
  326. begin
  327. ResStr^.CurrentValue:=ResStr^.DefaultValue;
  328. inc(ResStr);
  329. end;
  330. end;
  331. end;
  332. end;
  333. Procedure FinalizeResourceTables;
  334. Var
  335. ResStr : PResourceStringRecord;
  336. i : Longint;
  337. begin
  338. With ResourceStringTable do
  339. begin
  340. For i:=0 to Count-1 do
  341. begin
  342. ResStr:=Tables[I].TableStart;
  343. { Skip first entry (name of the Unit) }
  344. inc(ResStr);
  345. while ResStr<Tables[I].TableEnd do
  346. begin
  347. ResStr^.CurrentValue:='';
  348. inc(ResStr);
  349. end;
  350. end;
  351. end;
  352. end;
  353. {$else RESSTRSECTIONS}
  354. Type
  355. PResourceStringRecord = ^TResourceStringRecord;
  356. TResourceStringRecord = Packed Record
  357. DefaultValue,
  358. CurrentValue : AnsiString;
  359. HashValue : LongWord;
  360. Name : AnsiString;
  361. end;
  362. TResourceStringTable = Packed Record
  363. Count : longint;
  364. Resrec : Array[Word] of TResourceStringRecord;
  365. end;
  366. PResourceStringTable = ^TResourceStringTable;
  367. TResourceTableList = Packed Record
  368. Count : longint;
  369. Tables : Array[Word] of PResourceStringTable;
  370. end;
  371. Var
  372. ResourceStringTable : TResourceTablelist; External Name 'FPC_RESOURCESTRINGTABLES';
  373. Function GetResourceString(Const TheTable: TResourceStringTable;Index : longint) : AnsiString;[Public,Alias : 'FPC_GETRESOURCESTRING'];
  374. begin
  375. If (Index>=0) and (Index<TheTAble.Count) then
  376. Result:=TheTable.ResRec[Index].CurrentValue
  377. else
  378. Result:='';
  379. end;
  380. Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
  381. Var I,J : longint;
  382. begin
  383. With ResourceStringTable do
  384. For I:=0 to Count-1 do
  385. With Tables[I]^ do
  386. For J:=0 to Count-1 do
  387. With ResRec[J] do
  388. CurrentValue:=SetFunction(Name,DefaultValue,Longint(HashValue),arg);
  389. end;
  390. Procedure SetUnitResourceStrings (const UnitName:string;SetFunction : TResourceIterator;arg:pointer);
  391. begin
  392. SetResourceStrings (SetFunction,arg);
  393. end;
  394. Procedure ResetResourceTables;
  395. Var I,J : longint;
  396. begin
  397. With ResourceStringTable do
  398. For I:=0 to Count-1 do
  399. With Tables[I]^ do
  400. For J:=0 to Count-1 do
  401. With ResRec[J] do
  402. CurrentValue:=DefaultValue;
  403. end;
  404. Procedure FinalizeResourceTables;
  405. Var I,J : longint;
  406. begin
  407. With ResourceStringTable do
  408. For I:=0 to Count-1 do
  409. With Tables[I]^ do
  410. For J:=0 to Count-1 do
  411. With ResRec[J] do
  412. CurrentValue:='';
  413. end;
  414. Function ResourceStringTableCount : Longint;
  415. begin
  416. Result:=ResourceStringTable.Count;
  417. end;
  418. Function CheckTableIndex (Index: longint) : Boolean;
  419. begin
  420. Result:=(Index<ResourceStringTable.Count) and (Index>=0)
  421. end;
  422. Function CheckStringIndex (TableIndex,Index: longint) : Boolean;
  423. begin
  424. Result:=(TableIndex<ResourceStringTable.Count) and (TableIndex>=0) and
  425. (Index<ResourceStringTable.Tables[TableIndex]^.Count) and (Index>=0)
  426. end;
  427. Function ResourceStringCount(TableIndex : longint) : longint;
  428. begin
  429. If not CheckTableIndex(TableIndex) then
  430. Result:=-1
  431. else
  432. Result:=ResourceStringTable.Tables[TableIndex]^.Count;
  433. end;
  434. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  435. begin
  436. If not CheckStringIndex(Tableindex,StringIndex) then
  437. Result:=''
  438. else
  439. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].Name;
  440. end;
  441. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  442. begin
  443. If not CheckStringIndex(Tableindex,StringIndex) then
  444. Result:=0
  445. else
  446. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].HashValue;
  447. end;
  448. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString;
  449. begin
  450. If not CheckStringIndex(Tableindex,StringIndex) then
  451. Result:=''
  452. else
  453. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].DefaultValue;
  454. end;
  455. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  456. begin
  457. If not CheckStringIndex(Tableindex,StringIndex) then
  458. Result:=''
  459. else
  460. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue;
  461. end;
  462. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  463. begin
  464. Result:=CheckStringIndex(Tableindex,StringIndex);
  465. If Result then
  466. ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue:=Value;
  467. end;
  468. {$endif RESSTRSECTIONS}
  469. Function LoadResString(p:PResStringRec):AnsiString;
  470. begin
  471. Result:=p^;
  472. end;
  473. {$endif FPC_HAS_FEATURE_RESOURCES}
  474. {$ifdef FPC_HAS_FEATURE_RESOURCES}
  475. Initialization
  476. { ResetResourceTables;}
  477. finalization
  478. FinalizeResourceTables;
  479. {$endif FPC_HAS_FEATURE_RESOURCES}
  480. end.