objpas.pp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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) : AnsiString;
  58. Function Hash(S : AnsiString) : longint;
  59. Procedure ResetResourceTables;
  60. Procedure SetResourceStrings (SetFunction : TResourceIterator);
  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);
  91. begin
  92. { Catch Runtime error/Exception }
  93. {$I+}
  94. System.Close(f);
  95. {$I-}
  96. end;
  97. { Text file support }
  98. Procedure AssignFile(Var t:Text;const s:string);
  99. begin
  100. System.Assign (T,S);
  101. end;
  102. Procedure AssignFile(Var t:Text;p:pchar);
  103. begin
  104. System.Assign (T,P);
  105. end;
  106. Procedure AssignFile(Var t:Text;c:char);
  107. begin
  108. System.Assign (T,C);
  109. end;
  110. Procedure CloseFile(Var t:Text);
  111. begin
  112. { Catch Runtime error/Exception }
  113. {$I+}
  114. System.Close(T);
  115. {$I-}
  116. end;
  117. { Typed file supoort }
  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. Type
  159. PResourceStringRecord = ^TResourceStringRecord;
  160. TResourceStringRecord = Packed Record
  161. DefaultValue,
  162. CurrentValue : AnsiString;
  163. HashValue : longint;
  164. Name : AnsiString;
  165. end;
  166. TResourceStringTable = Packed Record
  167. Count : longint;
  168. Resrec : Array[Word] of TResourceStringRecord;
  169. end;
  170. PResourceStringTable = ^TResourceStringTable;
  171. TResourceTableList = Packed Record
  172. Count : longint;
  173. Tables : Array[Word] of PResourceStringTable;
  174. end;
  175. Var
  176. ResourceStringTable : TResourceTablelist; External Name 'FPC_RESOURCESTRINGTABLES';
  177. Function Hash(S : AnsiString) : longint;
  178. Var thehash,g,I : longint;
  179. begin
  180. thehash:=0;
  181. For I:=1 to Length(S) do { 0 terminated }
  182. begin
  183. thehash:=thehash shl 4;
  184. inc(theHash,Ord(S[i]));
  185. g:=thehash and longint($f shl 28);
  186. if g<>0 then
  187. begin
  188. thehash:=thehash xor (g shr 24);
  189. thehash:=thehash xor g;
  190. end;
  191. end;
  192. If theHash=0 then
  193. Hash:=Not(0)
  194. else
  195. Hash:=TheHash;
  196. end;
  197. Function GetResourceString(Const TheTable: TResourceStringTable;Index : longint) : AnsiString;[Public,Alias : 'FPC_GETRESOURCESTRING'];
  198. begin
  199. If (Index>=0) and (Index<TheTAble.Count) then
  200. Result:=TheTable.ResRec[Index].CurrentValue
  201. else
  202. Result:='';
  203. end;
  204. (*
  205. Function SetResourceString(Hash : Longint;Const Name : ShortString; Const Value : AnsiString) : Boolean;
  206. begin
  207. Hash:=FindIndex(Hash,Name);
  208. Result:=Hash<>-1;
  209. If Result then
  210. ResourceStringTable.ResRec[Hash].CurrentValue:=Value;
  211. end;
  212. *)
  213. Procedure SetResourceStrings (SetFunction : TResourceIterator);
  214. Var I,J : longint;
  215. begin
  216. With ResourceStringTable do
  217. For I:=0 to Count-1 do
  218. With Tables[I]^ do
  219. For J:=0 to Count-1 do
  220. With ResRec[J] do
  221. CurrentValue:=SetFunction(Name,DefaultValue,HashValue);
  222. end;
  223. Procedure ResetResourceTables;
  224. Var I,J : longint;
  225. begin
  226. With ResourceStringTable do
  227. For I:=0 to Count-1 do
  228. With Tables[I]^ do
  229. For J:=0 to Count-1 do
  230. With ResRec[J] do
  231. CurrentValue:=DefaultValue;
  232. end;
  233. Procedure FinalizeResourceTables;
  234. Var I,J : longint;
  235. begin
  236. With ResourceStringTable do
  237. For I:=0 to Count-1 do
  238. With Tables[I]^ do
  239. For J:=0 to Count-1 do
  240. With ResRec[J] do
  241. CurrentValue:='';
  242. end;
  243. Function ResourceStringTableCount : Longint;
  244. begin
  245. Result:=ResourceStringTable.Count;
  246. end;
  247. Function CheckTableIndex (Index: longint) : Boolean;
  248. begin
  249. Result:=(Index<ResourceStringTable.Count) and (Index>=0)
  250. end;
  251. Function CheckStringIndex (TableIndex,Index: longint) : Boolean;
  252. begin
  253. Result:=(TableIndex<ResourceStringTable.Count) and (TableIndex>=0) and
  254. (Index<ResourceStringTable.Tables[TableIndex]^.Count) and (Index>=0)
  255. end;
  256. Function ResourceStringCount(TableIndex : longint) : longint;
  257. begin
  258. If not CheckTableIndex(TableIndex) then
  259. Result:=-1
  260. else
  261. Result:=ResourceStringTable.Tables[TableIndex]^.Count;
  262. end;
  263. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  264. begin
  265. If not CheckStringIndex(Tableindex,StringIndex) then
  266. Result:=''
  267. else
  268. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].Name;
  269. end;
  270. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  271. begin
  272. If not CheckStringIndex(Tableindex,StringIndex) then
  273. Result:=0
  274. else
  275. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].HashValue;
  276. end;
  277. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString;
  278. begin
  279. If not CheckStringIndex(Tableindex,StringIndex) then
  280. Result:=''
  281. else
  282. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].DefaultValue;
  283. end;
  284. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  285. begin
  286. If not CheckStringIndex(Tableindex,StringIndex) then
  287. Result:=''
  288. else
  289. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue;
  290. end;
  291. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  292. begin
  293. Result:=CheckStringIndex(Tableindex,StringIndex);
  294. If Result then
  295. ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue:=Value;
  296. end;
  297. Function LoadResString(p:PResStringRec):AnsiString;
  298. begin
  299. Result:=p^;
  300. end;
  301. Initialization
  302. ResetResourceTables;
  303. finalization
  304. FinalizeResourceTables;
  305. end.