objpas.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 SetResourceStrings (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(Var f:File;const Name:string);
  81. begin
  82. System.Assign (F,Name);
  83. end;
  84. Procedure AssignFile(Var f:File;p:pchar);
  85. begin
  86. System.Assign (F,P);
  87. end;
  88. Procedure AssignFile(Var f:File;c:char);
  89. begin
  90. System.Assign (F,C);
  91. end;
  92. Procedure CloseFile(Var f:File);
  93. begin
  94. { Catch Runtime error/Exception }
  95. {$I+}
  96. System.Close(f);
  97. {$I-}
  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);
  113. begin
  114. { Catch Runtime error/Exception }
  115. {$I+}
  116. System.Close(T);
  117. {$I-}
  118. end;
  119. { Typed file supoort }
  120. Procedure AssignFile(Var f:TypedFile;const Name:string);
  121. begin
  122. system.Assign(F,Name);
  123. end;
  124. Procedure AssignFile(Var f:TypedFile;p:pchar);
  125. begin
  126. system.Assign (F,p);
  127. end;
  128. Procedure AssignFile(Var f:TypedFile;c:char);
  129. begin
  130. system.Assign (F,C);
  131. end;
  132. Function ParamStr(Param : Integer) : Ansistring;
  133. Var Len : longint;
  134. begin
  135. {
  136. Paramstr(0) should return the name of the binary.
  137. Since this functionality is included in the system unit,
  138. we fetch it from there.
  139. Normally, pathnames are less than 255 chars anyway,
  140. so this will work correct in 99% of all cases.
  141. In time, the system unit should get a GetExeName call.
  142. }
  143. if (Param=0) then
  144. Result:=System.Paramstr(0)
  145. else if (Param>0) and (Param<argc) then
  146. begin
  147. Len:=0;
  148. While Argv[Param][Len]<>#0 do
  149. Inc(len);
  150. SetLength(Result,Len);
  151. If Len>0 then
  152. Move(Argv[Param][0],Result[1],Len);
  153. end
  154. else
  155. paramstr:='';
  156. end;
  157. { ---------------------------------------------------------------------
  158. ResourceString support
  159. ---------------------------------------------------------------------}
  160. Function Hash(S : AnsiString) : LongWord;
  161. Var
  162. thehash,g,I : LongWord;
  163. begin
  164. thehash:=0;
  165. For I:=1 to Length(S) do { 0 terminated }
  166. begin
  167. thehash:=thehash shl 4;
  168. inc(theHash,Ord(S[i]));
  169. g:=thehash and LongWord($f shl 28);
  170. if g<>0 then
  171. begin
  172. thehash:=thehash xor (g shr 24);
  173. thehash:=thehash xor g;
  174. end;
  175. end;
  176. If theHash=0 then
  177. Hash:=$ffffffff
  178. else
  179. Hash:=TheHash;
  180. end;
  181. {$ifdef RESSTRSECTIONS}
  182. Type
  183. PResourceStringRecord = ^TResourceStringRecord;
  184. TResourceStringRecord = Packed Record
  185. Name,
  186. CurrentValue,
  187. DefaultValue : AnsiString;
  188. HashValue : LongWord;
  189. {$ifdef cpu64}
  190. Dummy : LongWord; // alignment
  191. {$endif cpu64}
  192. end;
  193. TResourceStringTableList = Packed Record
  194. Count : ptrint;
  195. Tables : Array[Word] of record
  196. TableStart,
  197. TableEnd : PResourceStringRecord;
  198. end;
  199. end;
  200. Var
  201. ResourceStringTable : TResourceStringTableList; External Name 'FPC_RESOURCESTRINGTABLES';
  202. Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
  203. Var
  204. ResStr : PResourceStringRecord;
  205. i : Longint;
  206. begin
  207. With ResourceStringTable do
  208. begin
  209. For i:=0 to Count-1 do
  210. begin
  211. ResStr:=Tables[I].TableStart;
  212. while ResStr<Tables[I].TableEnd do
  213. begin
  214. ResStr^.CurrentValue:=SetFunction(ResStr^.Name,ResStr^.DefaultValue,ResStr^.HashValue,arg);
  215. inc(ResStr);
  216. end;
  217. end;
  218. end;
  219. end;
  220. Procedure ResetResourceTables;
  221. Var
  222. ResStr : PResourceStringRecord;
  223. i : Longint;
  224. begin
  225. With ResourceStringTable do
  226. begin
  227. For i:=0 to Count-1 do
  228. begin
  229. ResStr:=Tables[I].TableStart;
  230. while ResStr<Tables[I].TableEnd do
  231. begin
  232. ResStr^.CurrentValue:=ResStr^.DefaultValue;
  233. inc(ResStr);
  234. end;
  235. end;
  236. end;
  237. end;
  238. Procedure FinalizeResourceTables;
  239. Var
  240. ResStr : PResourceStringRecord;
  241. i : Longint;
  242. begin
  243. With ResourceStringTable do
  244. begin
  245. For i:=0 to Count-1 do
  246. begin
  247. ResStr:=Tables[I].TableStart;
  248. while ResStr<Tables[I].TableEnd do
  249. begin
  250. ResStr^.CurrentValue:='';
  251. inc(ResStr);
  252. end;
  253. end;
  254. end;
  255. end;
  256. {$else RESSTRSECTIONS}
  257. Type
  258. PResourceStringRecord = ^TResourceStringRecord;
  259. TResourceStringRecord = Packed Record
  260. DefaultValue,
  261. CurrentValue : AnsiString;
  262. HashValue : LongWord;
  263. Name : AnsiString;
  264. end;
  265. TResourceStringTable = Packed Record
  266. Count : longint;
  267. Resrec : Array[Word] of TResourceStringRecord;
  268. end;
  269. PResourceStringTable = ^TResourceStringTable;
  270. TResourceTableList = Packed Record
  271. Count : longint;
  272. Tables : Array[Word] of PResourceStringTable;
  273. end;
  274. Var
  275. ResourceStringTable : TResourceTablelist; External Name 'FPC_RESOURCESTRINGTABLES';
  276. Function GetResourceString(Const TheTable: TResourceStringTable;Index : longint) : AnsiString;[Public,Alias : 'FPC_GETRESOURCESTRING'];
  277. begin
  278. If (Index>=0) and (Index<TheTAble.Count) then
  279. Result:=TheTable.ResRec[Index].CurrentValue
  280. else
  281. Result:='';
  282. end;
  283. Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
  284. Var I,J : longint;
  285. begin
  286. With ResourceStringTable do
  287. For I:=0 to Count-1 do
  288. With Tables[I]^ do
  289. For J:=0 to Count-1 do
  290. With ResRec[J] do
  291. CurrentValue:=SetFunction(Name,DefaultValue,HashValue,arg);
  292. end;
  293. Procedure ResetResourceTables;
  294. Var I,J : longint;
  295. begin
  296. With ResourceStringTable do
  297. For I:=0 to Count-1 do
  298. With Tables[I]^ do
  299. For J:=0 to Count-1 do
  300. With ResRec[J] do
  301. CurrentValue:=DefaultValue;
  302. end;
  303. Procedure FinalizeResourceTables;
  304. Var I,J : longint;
  305. begin
  306. With ResourceStringTable do
  307. For I:=0 to Count-1 do
  308. With Tables[I]^ do
  309. For J:=0 to Count-1 do
  310. With ResRec[J] do
  311. CurrentValue:='';
  312. end;
  313. Function ResourceStringTableCount : Longint;
  314. begin
  315. Result:=ResourceStringTable.Count;
  316. end;
  317. Function CheckTableIndex (Index: longint) : Boolean;
  318. begin
  319. Result:=(Index<ResourceStringTable.Count) and (Index>=0)
  320. end;
  321. Function CheckStringIndex (TableIndex,Index: longint) : Boolean;
  322. begin
  323. Result:=(TableIndex<ResourceStringTable.Count) and (TableIndex>=0) and
  324. (Index<ResourceStringTable.Tables[TableIndex]^.Count) and (Index>=0)
  325. end;
  326. Function ResourceStringCount(TableIndex : longint) : longint;
  327. begin
  328. If not CheckTableIndex(TableIndex) then
  329. Result:=-1
  330. else
  331. Result:=ResourceStringTable.Tables[TableIndex]^.Count;
  332. end;
  333. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  334. begin
  335. If not CheckStringIndex(Tableindex,StringIndex) then
  336. Result:=''
  337. else
  338. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].Name;
  339. end;
  340. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  341. begin
  342. If not CheckStringIndex(Tableindex,StringIndex) then
  343. Result:=0
  344. else
  345. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].HashValue;
  346. end;
  347. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString;
  348. begin
  349. If not CheckStringIndex(Tableindex,StringIndex) then
  350. Result:=''
  351. else
  352. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].DefaultValue;
  353. end;
  354. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  355. begin
  356. If not CheckStringIndex(Tableindex,StringIndex) then
  357. Result:=''
  358. else
  359. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue;
  360. end;
  361. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  362. begin
  363. Result:=CheckStringIndex(Tableindex,StringIndex);
  364. If Result then
  365. ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue:=Value;
  366. end;
  367. {$endif RESSTRSECTIONS}
  368. Function LoadResString(p:PResStringRec):AnsiString;
  369. begin
  370. Result:=p^;
  371. end;
  372. Initialization
  373. ResetResourceTables;
  374. finalization
  375. FinalizeResourceTables;
  376. end.