objpas.pp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1998,99 by the Free Pascal development team
  5. This unit makes Free Pascal as much as possible Delphi compatible
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$Mode ObjFpc}
  13. {$I-}
  14. {$ifndef linux}
  15. {$S-}
  16. {$endif}
  17. unit objpas;
  18. interface
  19. const
  20. MaxInt = MaxLongint;
  21. type
  22. { first, in object pascal, the types must be redefined }
  23. smallint = system.integer;
  24. integer = system.longint;
  25. { Old compilers search for these variables in objpas unit }
  26. {$ifndef SYSTEMTVARREC}
  27. pvarrec = system.pvarrec;
  28. tvarrec = system.tvarrec;
  29. {$endif}
  30. {****************************************************************************
  31. Compatibility routines.
  32. ****************************************************************************}
  33. { Untyped file support }
  34. Procedure AssignFile(Var f:File;const Name:string);
  35. Procedure AssignFile(Var f:File;p:pchar);
  36. Procedure AssignFile(Var f:File;c:char);
  37. Procedure CloseFile(Var f:File);
  38. { Text file support }
  39. Procedure AssignFile(Var t:Text;const s:string);
  40. Procedure AssignFile(Var t:Text;p:pchar);
  41. Procedure AssignFile(Var t:Text;c:char);
  42. Procedure CloseFile(Var t:Text);
  43. { Typed file supoort }
  44. Procedure AssignFile(Var f:TypedFile;const Name:string);
  45. Procedure AssignFile(Var f:TypedFile;p:pchar);
  46. Procedure AssignFile(Var f:TypedFile;c:char);
  47. { ParamStr should return also an ansistring }
  48. Function ParamStr(Param : Integer) : Ansistring;
  49. {$ifdef HasResourceStrings}
  50. Type
  51. TResourceIterator = Function (Name,Value : AnsiString; Hash : Longint) : AnsiString;
  52. Function Hash(S : AnsiString) : longint;
  53. Procedure ResetResourceTables;
  54. Procedure SetResourceStrings (SetFunction : TResourceIterator);
  55. Function ResourceStringTableCount : Longint;
  56. Function ResourceStringCount(TableIndex : longint) : longint;
  57. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  58. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  59. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString;
  60. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  61. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  62. {$endif}
  63. implementation
  64. {****************************************************************************
  65. Compatibility routines.
  66. ****************************************************************************}
  67. { Untyped file support }
  68. Procedure AssignFile(Var f:File;const Name:string);
  69. begin
  70. System.Assign (F,Name);
  71. end;
  72. Procedure AssignFile(Var f:File;p:pchar);
  73. begin
  74. System.Assign (F,P);
  75. end;
  76. Procedure AssignFile(Var f:File;c:char);
  77. begin
  78. System.Assign (F,C);
  79. end;
  80. Procedure CloseFile(Var f:File);
  81. begin
  82. System.Close(f);
  83. end;
  84. { Text file support }
  85. Procedure AssignFile(Var t:Text;const s:string);
  86. begin
  87. System.Assign (T,S);
  88. end;
  89. Procedure AssignFile(Var t:Text;p:pchar);
  90. begin
  91. System.Assign (T,P);
  92. end;
  93. Procedure AssignFile(Var t:Text;c:char);
  94. begin
  95. System.Assign (T,C);
  96. end;
  97. Procedure CloseFile(Var t:Text);
  98. begin
  99. Close(T);
  100. end;
  101. { Typed file supoort }
  102. Procedure AssignFile(Var f:TypedFile;const Name:string);
  103. begin
  104. system.Assign(F,Name);
  105. end;
  106. Procedure AssignFile(Var f:TypedFile;p:pchar);
  107. begin
  108. system.Assign (F,p);
  109. end;
  110. Procedure AssignFile(Var f:TypedFile;c:char);
  111. begin
  112. system.Assign (F,C);
  113. end;
  114. Function ParamStr(Param : Integer) : Ansistring;
  115. Var Len : longint;
  116. begin
  117. if (Param>=0) and (Param<argc) then
  118. begin
  119. Len:=0;
  120. While Argv[Param][Len]<>#0 do
  121. Inc(len);
  122. SetLength(Result,Len);
  123. If Len>0 then
  124. Move(Argv[Param][0],Result[1],Len);
  125. end
  126. else
  127. paramstr:='';
  128. end;
  129. {$IFDEF HasResourceStrings}
  130. { ---------------------------------------------------------------------
  131. ResourceString support
  132. ---------------------------------------------------------------------}
  133. Type
  134. PResourceStringRecord = ^TResourceStringRecord;
  135. TResourceStringRecord = Packed Record
  136. DefaultValue,
  137. CurrentValue : AnsiString;
  138. HashValue : longint;
  139. Name : AnsiString;
  140. end;
  141. TResourceStringTable = Packed Record
  142. Count : longint;
  143. Resrec : Array[Word] of TResourceStringRecord;
  144. end;
  145. PResourceStringTable = ^TResourceStringTable;
  146. TResourceTableList = Packed Record
  147. Count : longint;
  148. Tables : Array[Word] of PResourceStringTable;
  149. end;
  150. Var
  151. ResourceStringTable : TResourceTablelist; External Name 'FPC_RESOURCESTRINGTABLES';
  152. Function Hash(S : AnsiString) : longint;
  153. Var thehash,g,I : longint;
  154. begin
  155. thehash:=0;
  156. For I:=1 to Length(S) do { 0 terminated }
  157. begin
  158. thehash:=thehash shl 4;
  159. inc(theHash,Ord(S[i]));
  160. g:=thehash and ($f shl 28);
  161. if g<>0 then
  162. begin
  163. thehash:=thehash xor (g shr 24);
  164. thehash:=thehash xor g;
  165. end;
  166. end;
  167. If theHash=0 then
  168. Hash:=Not(0)
  169. else
  170. Hash:=TheHash;
  171. end;
  172. Function GetResourceString(Const TheTable: TResourceStringTable;Index : longint) : AnsiString;[Public,Alias : 'FPC_GETRESOURCESTRING'];
  173. begin
  174. If (Index>=0) and (Index<TheTAble.Count) then
  175. Result:=TheTable.ResRec[Index].CurrentValue
  176. else
  177. Result:='';
  178. end;
  179. (*
  180. Function SetResourceString(Hash : Longint;Const Name : ShortString; Const Value : AnsiString) : Boolean;
  181. begin
  182. Hash:=FindIndex(Hash,Name);
  183. Result:=Hash<>-1;
  184. If Result then
  185. ResourceStringTable.ResRec[Hash].CurrentValue:=Value;
  186. end;
  187. *)
  188. Procedure SetResourceStrings (SetFunction : TResourceIterator);
  189. Var I,J : longint;
  190. begin
  191. With ResourceStringTable do
  192. For I:=0 to Count-1 do
  193. With Tables[I]^ do
  194. For J:=0 to Count-1 do
  195. With ResRec[J] do
  196. CurrentValue:=SetFunction(Name,DefaultValue,HashValue);
  197. end;
  198. Procedure ResetResourceTables;
  199. Var I,J : longint;
  200. begin
  201. With ResourceStringTable do
  202. For I:=0 to Count-1 do
  203. With Tables[I]^ do
  204. For J:=0 to Count-1 do
  205. With ResRec[J] do
  206. CurrentValue:=DefaultValue;
  207. end;
  208. Function ResourceStringTableCount : Longint;
  209. begin
  210. Result:=ResourceStringTable.Count;
  211. end;
  212. Function CheckTableIndex (Index: longint) : Boolean;
  213. begin
  214. Result:=(Index<ResourceStringTable.Count) and (Index>=0)
  215. end;
  216. Function CheckStringIndex (TableIndex,Index: longint) : Boolean;
  217. begin
  218. Result:=(TableIndex<ResourceStringTable.Count) and (TableIndex>=0) and
  219. (Index<ResourceStringTable.Tables[TableIndex]^.Count) and (Index>=0)
  220. end;
  221. Function ResourceStringCount(TableIndex : longint) : longint;
  222. begin
  223. If not CheckTableIndex(TableIndex) then
  224. Result:=-1
  225. else
  226. Result:=ResourceStringTable.Tables[TableIndex]^.Count;
  227. end;
  228. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  229. begin
  230. If not CheckStringIndex(Tableindex,StringIndex) then
  231. Result:=''
  232. else
  233. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].Name;
  234. end;
  235. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  236. begin
  237. If not CheckStringIndex(Tableindex,StringIndex) then
  238. Result:=0
  239. else
  240. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].HashValue;
  241. end;
  242. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString;
  243. begin
  244. If not CheckStringIndex(Tableindex,StringIndex) then
  245. Result:=''
  246. else
  247. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].DefaultValue;
  248. end;
  249. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  250. begin
  251. If not CheckStringIndex(Tableindex,StringIndex) then
  252. Result:=''
  253. else
  254. result:=ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue;
  255. end;
  256. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  257. begin
  258. Result:=CheckStringIndex(Tableindex,StringIndex);
  259. If Result then
  260. ResourceStringTable.Tables[TableIndex]^.ResRec[StringIndex].CurrentValue:=Value;
  261. end;
  262. {$endif}
  263. Initialization
  264. {$IFDEF HasResourceStrings}
  265. ResetResourceTables;
  266. {$endif}
  267. finalization
  268. end.
  269. {
  270. $Log$
  271. Revision 1.44 1999-11-06 14:41:30 peter
  272. * truncated log
  273. Revision 1.43 1999/10/30 17:39:05 peter
  274. * memorymanager expanded with allocmem/reallocmem
  275. Revision 1.42 1999/10/03 19:41:30 peter
  276. * moved tvarrec to systemunit
  277. Revision 1.41 1999/09/28 21:13:33 florian
  278. * fixed bug 626, objpas must redefine maxint!
  279. Revision 1.40 1999/09/17 17:14:12 peter
  280. + new heap manager supporting delphi freemem(pointer)
  281. Revision 1.39 1999/08/28 13:03:23 michael
  282. + Added Hash function to interface
  283. Revision 1.38 1999/08/27 15:54:15 michael
  284. + Added many resourcestring methods
  285. Revision 1.37 1999/08/25 16:41:08 peter
  286. * resources are working again
  287. Revision 1.36 1999/08/24 22:42:56 michael
  288. * changed resourcestrings to new mechanism
  289. Revision 1.35 1999/08/24 12:02:29 michael
  290. + Changed external var for resourcestrings
  291. Revision 1.34 1999/08/20 10:50:55 michael
  292. + Fixed memory leak
  293. Revision 1.33 1999/08/19 19:52:26 michael
  294. * Fixed freemem bug; reported by Sebastian Guenther
  295. Revision 1.32 1999/08/15 21:28:57 michael
  296. + Pass hash also for speed reasons.
  297. Revision 1.31 1999/08/15 21:02:56 michael
  298. + Changed resource string mechanism to use names.
  299. Revision 1.30 1999/08/15 18:56:13 michael
  300. + Delphi-style getmem and freemem
  301. Revision 1.29 1999/07/23 23:13:54 peter
  302. * array[cardinal] is buggy, use array[word]
  303. * small fix in getresourcestring
  304. Revision 1.28 1999/07/23 22:51:11 michael
  305. * Added HasResourceStrings check
  306. Revision 1.27 1999/07/22 20:30:13 michael
  307. + Implemented resource stuff
  308. Revision 1.26 1999/07/07 10:04:04 michael
  309. + Paramstr now returns cmdline args >255 chars in ansistring objpas.pp
  310. Revision 1.25 1999/07/06 22:44:22 florian
  311. + implemented a paramstr function which returns an ansistring, nevertheless
  312. it is limited to 255 chars because it maps to the system.paramstr, maybe
  313. we should use cmdline instead
  314. Revision 1.24 1999/05/17 21:52:43 florian
  315. * most of the Object Pascal stuff moved to the system unit
  316. }