objpas.pp 12 KB

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