objpas.pp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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) : longint;
  59. Procedure ResetResourceTables;
  60. Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
  61. Function ResourceStringTableCount : Longint;
  62. Function ResourceStringCount(TableIndex : longint) : longint;
  63. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  64. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  65. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString;
  66. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  67. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  68. { Delphi compatibility }
  69. type
  70. PResStringRec=^AnsiString;
  71. TResStringRec=AnsiString;
  72. Function LoadResString(p:PResStringRec):AnsiString;
  73. implementation
  74. {****************************************************************************
  75. Compatibility routines.
  76. ****************************************************************************}
  77. { Untyped file support }
  78. Procedure AssignFile(Var f:File;const Name:string);
  79. begin
  80. System.Assign (F,Name);
  81. end;
  82. Procedure AssignFile(Var f:File;p:pchar);
  83. begin
  84. System.Assign (F,P);
  85. end;
  86. Procedure AssignFile(Var f:File;c:char);
  87. begin
  88. System.Assign (F,C);
  89. end;
  90. Procedure CloseFile(Var f:File); [IOCheck];
  91. begin
  92. { Catch Runtime error/Exception }
  93. System.Close(f);
  94. end;
  95. { Text file support }
  96. Procedure AssignFile(Var t:Text;const s:string);
  97. begin
  98. System.Assign (T,S);
  99. end;
  100. Procedure AssignFile(Var t:Text;p:pchar);
  101. begin
  102. System.Assign (T,P);
  103. end;
  104. Procedure AssignFile(Var t:Text;c:char);
  105. begin
  106. System.Assign (T,C);
  107. end;
  108. Procedure CloseFile(Var t:Text); [IOCheck];
  109. begin
  110. { Catch Runtime error/Exception }
  111. System.Close(T);
  112. end;
  113. { Typed file support }
  114. Procedure AssignFile(Var f:TypedFile;const Name:string);
  115. begin
  116. system.Assign(F,Name);
  117. end;
  118. Procedure AssignFile(Var f:TypedFile;p:pchar);
  119. begin
  120. system.Assign (F,p);
  121. end;
  122. Procedure AssignFile(Var f:TypedFile;c:char);
  123. begin
  124. system.Assign (F,C);
  125. end;
  126. Function ParamStr(Param : Integer) : Ansistring;
  127. Var Len : longint;
  128. begin
  129. {
  130. Paramstr(0) should return the name of the binary.
  131. Since this functionality is included in the system unit,
  132. we fetch it from there.
  133. Normally, pathnames are less than 255 chars anyway,
  134. so this will work correct in 99% of all cases.
  135. In time, the system unit should get a GetExeName call.
  136. }
  137. if (Param=0) then
  138. Result:=System.Paramstr(0)
  139. else if (Param>0) and (Param<argc) then
  140. begin
  141. Len:=0;
  142. While Argv[Param][Len]<>#0 do
  143. Inc(len);
  144. SetLength(Result,Len);
  145. If Len>0 then
  146. Move(Argv[Param][0],Result[1],Len);
  147. end
  148. else
  149. paramstr:='';
  150. end;
  151. { ---------------------------------------------------------------------
  152. ResourceString support
  153. ---------------------------------------------------------------------}
  154. Type
  155. PResourceStringRecord = ^TResourceStringRecord;
  156. TResourceStringRecord = Packed Record
  157. DefaultValue,
  158. CurrentValue : AnsiString;
  159. HashValue : longint;
  160. Name : AnsiString;
  161. end;
  162. TResourceStringTable = Packed Record
  163. Count : longint;
  164. Resrec : Array[Word] of TResourceStringRecord;
  165. end;
  166. PResourceStringTable = ^TResourceStringTable;
  167. TResourceTableList = Packed Record
  168. Count : longint;
  169. Tables : Array[Word] of PResourceStringTable;
  170. end;
  171. Var
  172. ResourceStringTable : TResourceTablelist; External Name 'FPC_RESOURCESTRINGTABLES';
  173. Function Hash(S : AnsiString) : longint;
  174. Var thehash,g,I : longint;
  175. begin
  176. thehash:=0;
  177. For I:=1 to Length(S) do { 0 terminated }
  178. begin
  179. thehash:=thehash shl 4;
  180. inc(theHash,Ord(S[i]));
  181. g:=thehash and longint($f shl 28);
  182. if g<>0 then
  183. begin
  184. thehash:=thehash xor (g shr 24);
  185. thehash:=thehash xor g;
  186. end;
  187. end;
  188. If theHash=0 then
  189. Hash:=Not(0)
  190. else
  191. Hash:=TheHash;
  192. end;
  193. Function GetResourceString(Const TheTable: TResourceStringTable;Index : longint) : AnsiString;[Public,Alias : 'FPC_GETRESOURCESTRING'];
  194. begin
  195. If (Index>=0) and (Index<TheTAble.Count) then
  196. Result:=TheTable.ResRec[Index].CurrentValue
  197. else
  198. Result:='';
  199. end;
  200. (*
  201. Function SetResourceString(Hash : Longint;Const Name : ShortString; Const Value : AnsiString) : Boolean;
  202. begin
  203. Hash:=FindIndex(Hash,Name);
  204. Result:=Hash<>-1;
  205. If Result then
  206. ResourceStringTable.ResRec[Hash].CurrentValue:=Value;
  207. end;
  208. *)
  209. Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
  210. Var I,J : longint;
  211. begin
  212. With ResourceStringTable do
  213. For I:=0 to Count-1 do
  214. With Tables[I]^ do
  215. For J:=0 to Count-1 do
  216. With ResRec[J] do
  217. CurrentValue:=SetFunction(Name,DefaultValue,HashValue,arg);
  218. end;
  219. Procedure ResetResourceTables;
  220. Var I,J : longint;
  221. begin
  222. With ResourceStringTable do
  223. For I:=0 to Count-1 do
  224. With Tables[I]^ do
  225. For J:=0 to Count-1 do
  226. With ResRec[J] do
  227. CurrentValue:=DefaultValue;
  228. end;
  229. Procedure FinalizeResourceTables;
  230. Var I,J : longint;
  231. begin
  232. With ResourceStringTable do
  233. For I:=0 to Count-1 do
  234. With Tables[I]^ do
  235. For J:=0 to Count-1 do
  236. With ResRec[J] do
  237. CurrentValue:='';
  238. end;
  239. Function ResourceStringTableCount : Longint;
  240. begin
  241. Result:=ResourceStringTable.Count;
  242. end;
  243. Function CheckTableIndex (Index: longint) : Boolean;
  244. begin
  245. Result:=(Index<ResourceStringTable.Count) and (Index>=0)
  246. end;
  247. Function CheckStringIndex (TableIndex,Index: longint) : Boolean;
  248. begin
  249. Result:=(TableIndex<ResourceStringTable.Count) and (TableIndex>=0) and
  250. (Index<ResourceStringTable.Tables[TableIndex]^.Count) and (Index>=0)
  251. end;
  252. Function ResourceStringCount(TableIndex : longint) : longint;
  253. begin
  254. If not CheckTableIndex(TableIndex) then
  255. Result:=-1
  256. else
  257. Result:=ResourceStringTable.Tables[TableIndex]^.Count;
  258. end;
  259. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  260. begin
  261. If not CheckStringIndex(Tableindex,StringIndex) then
  262. Result:=''
  263. else
  264. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].Name;
  265. end;
  266. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  267. begin
  268. If not CheckStringIndex(Tableindex,StringIndex) then
  269. Result:=0
  270. else
  271. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].HashValue;
  272. end;
  273. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString;
  274. begin
  275. If not CheckStringIndex(Tableindex,StringIndex) then
  276. Result:=''
  277. else
  278. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].DefaultValue;
  279. end;
  280. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  281. begin
  282. If not CheckStringIndex(Tableindex,StringIndex) then
  283. Result:=''
  284. else
  285. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue;
  286. end;
  287. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  288. begin
  289. Result:=CheckStringIndex(Tableindex,StringIndex);
  290. If Result then
  291. ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue:=Value;
  292. end;
  293. Function LoadResString(p:PResStringRec):AnsiString;
  294. begin
  295. Result:=p^;
  296. end;
  297. Initialization
  298. ResetResourceTables;
  299. finalization
  300. FinalizeResourceTables;
  301. end.