objpas.pp 13 KB

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