objpas.pp 15 KB

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