objpas.pp 11 KB

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