objpas.pp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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(const s:ansistring);overload;
  79. Procedure RmDir(const s:ansistring);overload;
  80. Procedure ChDir(const 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. Procedure MkDir(const s:ansistring);
  199. begin
  200. mkdirpchar(pchar(s),length(s));
  201. end;
  202. Procedure RmDir(const s:ansistring);
  203. begin
  204. RmDirpchar(pchar(s),length(s));
  205. end;
  206. Procedure ChDir(const s:ansistring);
  207. begin
  208. ChDirpchar(pchar(s),length(s));
  209. end;
  210. {$endif FPC_HAS_FEATURE_FILEIO}
  211. {$ifdef FPC_HAS_FEATURE_RESOURCES}
  212. { ---------------------------------------------------------------------
  213. ResourceString support
  214. ---------------------------------------------------------------------}
  215. Function Hash(S : AnsiString) : LongWord;
  216. Var
  217. thehash,g,I : LongWord;
  218. begin
  219. thehash:=0;
  220. For I:=1 to Length(S) do { 0 terminated }
  221. begin
  222. thehash:=thehash shl 4;
  223. inc(theHash,Ord(S[i]));
  224. g:=thehash and LongWord($f shl 28);
  225. if g<>0 then
  226. begin
  227. thehash:=thehash xor (g shr 24);
  228. thehash:=thehash xor g;
  229. end;
  230. end;
  231. If theHash=0 then
  232. Hash:=$ffffffff
  233. else
  234. Hash:=TheHash;
  235. end;
  236. {$ifdef RESSTRSECTIONS}
  237. Type
  238. PResourceStringRecord = ^TResourceStringRecord;
  239. TResourceStringRecord = Packed Record
  240. Name,
  241. CurrentValue,
  242. DefaultValue : AnsiString;
  243. HashValue : LongWord;
  244. {$ifdef cpu64}
  245. Dummy : LongWord; // alignment
  246. {$endif cpu64}
  247. end;
  248. TResourceStringTableList = Packed Record
  249. Count : ptrint;
  250. Tables : Array[Word] of record
  251. TableStart,
  252. TableEnd : PResourceStringRecord;
  253. end;
  254. end;
  255. Var
  256. ResourceStringTable : TResourceStringTableList; External Name 'FPC_RESOURCESTRINGTABLES';
  257. Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
  258. Var
  259. ResStr : PResourceStringRecord;
  260. i : Longint;
  261. s : AnsiString;
  262. begin
  263. With ResourceStringTable do
  264. begin
  265. For i:=0 to Count-1 do
  266. begin
  267. ResStr:=Tables[I].TableStart;
  268. { Skip first entry (name of the Unit) }
  269. inc(ResStr);
  270. while ResStr<Tables[I].TableEnd do
  271. begin
  272. s:=SetFunction(ResStr^.Name,ResStr^.DefaultValue,ResStr^.HashValue,arg);
  273. if s<>'' then
  274. ResStr^.CurrentValue:=s;
  275. inc(ResStr);
  276. end;
  277. end;
  278. end;
  279. end;
  280. Procedure SetUnitResourceStrings (const UnitName:string;SetFunction : TResourceIterator;arg:pointer);
  281. Var
  282. ResStr : PResourceStringRecord;
  283. i : Longint;
  284. s,
  285. UpUnitName : AnsiString;
  286. begin
  287. With ResourceStringTable do
  288. begin
  289. UpUnitName:=UpCase(UnitName);
  290. For i:=0 to Count-1 do
  291. begin
  292. ResStr:=Tables[I].TableStart;
  293. { Check name of the Unit }
  294. if ResStr^.Name<>UpUnitName then
  295. continue;
  296. inc(ResStr);
  297. while ResStr<Tables[I].TableEnd do
  298. begin
  299. s:=SetFunction(ResStr^.Name,ResStr^.DefaultValue,ResStr^.HashValue,arg);
  300. if s<>'' then
  301. ResStr^.CurrentValue:=s;
  302. inc(ResStr);
  303. end;
  304. end;
  305. end;
  306. end;
  307. Procedure ResetResourceTables;
  308. Var
  309. ResStr : PResourceStringRecord;
  310. i : Longint;
  311. begin
  312. With ResourceStringTable do
  313. begin
  314. For i:=0 to Count-1 do
  315. begin
  316. ResStr:=Tables[I].TableStart;
  317. { Skip first entry (name of the Unit) }
  318. inc(ResStr);
  319. while ResStr<Tables[I].TableEnd do
  320. begin
  321. ResStr^.CurrentValue:=ResStr^.DefaultValue;
  322. inc(ResStr);
  323. end;
  324. end;
  325. end;
  326. end;
  327. Procedure FinalizeResourceTables;
  328. Var
  329. ResStr : PResourceStringRecord;
  330. i : Longint;
  331. begin
  332. With ResourceStringTable do
  333. begin
  334. For i:=0 to Count-1 do
  335. begin
  336. ResStr:=Tables[I].TableStart;
  337. { Skip first entry (name of the Unit) }
  338. inc(ResStr);
  339. while ResStr<Tables[I].TableEnd do
  340. begin
  341. ResStr^.CurrentValue:='';
  342. inc(ResStr);
  343. end;
  344. end;
  345. end;
  346. end;
  347. {$else RESSTRSECTIONS}
  348. Type
  349. PResourceStringRecord = ^TResourceStringRecord;
  350. TResourceStringRecord = Packed Record
  351. DefaultValue,
  352. CurrentValue : AnsiString;
  353. HashValue : LongWord;
  354. Name : AnsiString;
  355. end;
  356. TResourceStringTable = Packed Record
  357. Count : longint;
  358. Resrec : Array[Word] of TResourceStringRecord;
  359. end;
  360. PResourceStringTable = ^TResourceStringTable;
  361. TResourceTableList = Packed Record
  362. Count : longint;
  363. Tables : Array[Word] of PResourceStringTable;
  364. end;
  365. Var
  366. ResourceStringTable : TResourceTablelist; External Name 'FPC_RESOURCESTRINGTABLES';
  367. Function GetResourceString(Const TheTable: TResourceStringTable;Index : longint) : AnsiString;[Public,Alias : 'FPC_GETRESOURCESTRING'];
  368. begin
  369. If (Index>=0) and (Index<TheTAble.Count) then
  370. Result:=TheTable.ResRec[Index].CurrentValue
  371. else
  372. Result:='';
  373. end;
  374. Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
  375. Var I,J : longint;
  376. begin
  377. With ResourceStringTable do
  378. For I:=0 to Count-1 do
  379. With Tables[I]^ do
  380. For J:=0 to Count-1 do
  381. With ResRec[J] do
  382. CurrentValue:=SetFunction(Name,DefaultValue,HashValue,arg);
  383. end;
  384. Procedure SetUnitResourceStrings (const UnitName:string;SetFunction : TResourceIterator;arg:pointer);
  385. begin
  386. SetResourceStrings (SetFunction,arg);
  387. end;
  388. Procedure ResetResourceTables;
  389. Var I,J : longint;
  390. begin
  391. With ResourceStringTable do
  392. For I:=0 to Count-1 do
  393. With Tables[I]^ do
  394. For J:=0 to Count-1 do
  395. With ResRec[J] do
  396. CurrentValue:=DefaultValue;
  397. end;
  398. Procedure FinalizeResourceTables;
  399. Var I,J : longint;
  400. begin
  401. With ResourceStringTable do
  402. For I:=0 to Count-1 do
  403. With Tables[I]^ do
  404. For J:=0 to Count-1 do
  405. With ResRec[J] do
  406. CurrentValue:='';
  407. end;
  408. Function ResourceStringTableCount : Longint;
  409. begin
  410. Result:=ResourceStringTable.Count;
  411. end;
  412. Function CheckTableIndex (Index: longint) : Boolean;
  413. begin
  414. Result:=(Index<ResourceStringTable.Count) and (Index>=0)
  415. end;
  416. Function CheckStringIndex (TableIndex,Index: longint) : Boolean;
  417. begin
  418. Result:=(TableIndex<ResourceStringTable.Count) and (TableIndex>=0) and
  419. (Index<ResourceStringTable.Tables[TableIndex]^.Count) and (Index>=0)
  420. end;
  421. Function ResourceStringCount(TableIndex : longint) : longint;
  422. begin
  423. If not CheckTableIndex(TableIndex) then
  424. Result:=-1
  425. else
  426. Result:=ResourceStringTable.Tables[TableIndex]^.Count;
  427. end;
  428. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  429. begin
  430. If not CheckStringIndex(Tableindex,StringIndex) then
  431. Result:=''
  432. else
  433. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].Name;
  434. end;
  435. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  436. begin
  437. If not CheckStringIndex(Tableindex,StringIndex) then
  438. Result:=0
  439. else
  440. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].HashValue;
  441. end;
  442. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString;
  443. begin
  444. If not CheckStringIndex(Tableindex,StringIndex) then
  445. Result:=''
  446. else
  447. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].DefaultValue;
  448. end;
  449. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  450. begin
  451. If not CheckStringIndex(Tableindex,StringIndex) then
  452. Result:=''
  453. else
  454. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue;
  455. end;
  456. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  457. begin
  458. Result:=CheckStringIndex(Tableindex,StringIndex);
  459. If Result then
  460. ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue:=Value;
  461. end;
  462. {$endif RESSTRSECTIONS}
  463. Function LoadResString(p:PResStringRec):AnsiString;
  464. begin
  465. Result:=p^;
  466. end;
  467. {$endif FPC_HAS_FEATURE_RESOURCES}
  468. {$ifdef FPC_HAS_FEATURE_RESOURCES}
  469. Initialization
  470. { ResetResourceTables;}
  471. finalization
  472. FinalizeResourceTables;
  473. {$endif FPC_HAS_FEATURE_RESOURCES}
  474. end.