histlist.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. { $Id$ }
  2. {********[ SOURCE FILE OF GRAPHICAL FREE VISION ]**********}
  3. { }
  4. { System independent GRAPHICAL clone of HISTLIST.PAS }
  5. { }
  6. { Interface Copyright (c) 1992 Borland International }
  7. { }
  8. { Copyright (c) 1996, 1997, 1998, 1999 by Leon de Boer }
  9. { [email protected] - primary e-mail address }
  10. { [email protected] - backup e-mail address }
  11. { }
  12. {****************[ THIS CODE IS FREEWARE ]*****************}
  13. { }
  14. { This sourcecode is released for the purpose to }
  15. { promote the pascal language on all platforms. You may }
  16. { redistribute it and/or modify with the following }
  17. { DISCLAIMER. }
  18. { }
  19. { This SOURCE CODE is distributed "AS IS" WITHOUT }
  20. { WARRANTIES AS TO PERFORMANCE OF MERCHANTABILITY OR }
  21. { ANY OTHER WARRANTIES WHETHER EXPRESSED OR IMPLIED. }
  22. { }
  23. {*****************[ SUPPORTED PLATFORMS ]******************}
  24. { 16 and 32 Bit compilers }
  25. { DOS - Turbo Pascal 7.0 + (16 Bit) }
  26. { DPMI - Turbo Pascal 7.0 + (16 Bit) }
  27. { - FPC 0.9912+ (GO32V2) (32 Bit) }
  28. { WINDOWS - Turbo Pascal 7.0 + (16 Bit) }
  29. { - Delphi 1.0+ (16 Bit) }
  30. { WIN95/NT - Delphi 2.0+ (32 Bit) }
  31. { - Virtual Pascal 2.0+ (32 Bit) }
  32. { - Speedsoft Sybil 2.0+ (32 Bit) }
  33. { - FPC 0.9912+ (32 Bit) }
  34. { OS2 - Virtual Pascal 1.0+ (32 Bit) }
  35. { }
  36. {******************[ REVISION HISTORY ]********************}
  37. { Version Date Fix }
  38. { ------- --------- --------------------------------- }
  39. { 1.00 11 Nov 96 First DOS/DPMI platform release. }
  40. { 1.10 13 Jul 97 Windows platform code added. }
  41. { 1.20 29 Aug 97 Platform.inc sort added. }
  42. { 1.30 13 Oct 97 Delphi 2 32 bit code added. }
  43. { 1.40 05 May 98 Virtual pascal 2.0 code added. }
  44. { 1.50 30 Sep 99 Complete recheck preformed }
  45. { 1.51 03 Nov 99 FPC windows support added }
  46. {**********************************************************}
  47. UNIT HistList;
  48. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  49. INTERFACE
  50. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  51. {====Include file to sort compiler platform out =====================}
  52. {$I Platform.inc}
  53. {====================================================================}
  54. {==== Compiler directives ===========================================}
  55. {$IFNDEF PPC_FPC}{ FPC doesn't support these switches }
  56. {$F-} { Short calls are okay }
  57. {$A+} { Word Align Data }
  58. {$B-} { Allow short circuit boolean evaluations }
  59. {$O+} { This unit may be overlaid }
  60. {$G+} { 286 Code optimization - if you're on an 8088 get a real computer }
  61. {$P-} { Normal string variables }
  62. {$N-} { No 80x87 code generation }
  63. {$E+} { Emulation is on }
  64. {$ENDIF}
  65. {$X+} { Extended syntax is ok }
  66. {$R-} { Disable range checking }
  67. {$S-} { Disable Stack Checking }
  68. {$I-} { Disable IO Checking }
  69. {$Q-} { Disable Overflow Checking }
  70. {$V-} { Turn off strict VAR strings }
  71. {====================================================================}
  72. USES FVCommon, Objects; { Standard GFV units }
  73. {***************************************************************************}
  74. { INTERFACE ROUTINES }
  75. {***************************************************************************}
  76. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  77. { HISTORY SYSTEM CONTROL ROUTINES }
  78. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  79. {-InitHistory--------------------------------------------------------
  80. Initializes the history system usually called from Application.Init
  81. 30Sep99 LdB
  82. ---------------------------------------------------------------------}
  83. PROCEDURE InitHistory;
  84. {-DoneHistory--------------------------------------------------------
  85. Destroys the history system usually called from Application.Done
  86. 30Sep99 LdB
  87. ---------------------------------------------------------------------}
  88. PROCEDURE DoneHistory;
  89. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  90. { HISTORY ITEM ROUTINES }
  91. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  92. {-HistoryCount-------------------------------------------------------
  93. Returns the number of strings in the history list with ID number Id.
  94. 30Sep99 LdB
  95. ---------------------------------------------------------------------}
  96. FUNCTION HistoryCount (Id: Byte): Word;
  97. {-HistoryStr---------------------------------------------------------
  98. Returns the Index'th string in the history list with ID number Id.
  99. 30Sep99 LdB
  100. ---------------------------------------------------------------------}
  101. FUNCTION HistoryStr (Id: Byte; Index: Sw_Integer): String;
  102. {-ClearHistory-------------------------------------------------------
  103. Removes all strings from all history lists.
  104. 30Sep99 LdB
  105. ---------------------------------------------------------------------}
  106. PROCEDURE ClearHistory;
  107. {-HistoryAdd---------------------------------------------------------
  108. Adds the string Str to the history list indicated by Id.
  109. 30Sep99 LdB
  110. ---------------------------------------------------------------------}
  111. PROCEDURE HistoryAdd (Id: Byte; Const Str: String);
  112. function HistoryRemove(Id: Byte; Index: Sw_Integer): boolean;
  113. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  114. { HISTORY STREAM STORAGE AND RETREIVAL ROUTINES }
  115. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  116. {-LoadHistory--------------------------------------------------------
  117. Reads the application's history block from the stream S by reading the
  118. size of the block, then the block itself. Sets HistoryUsed to the end
  119. of the block read. Use LoadHistory to restore a history block saved
  120. with StoreHistory
  121. 30Sep99 LdB
  122. ---------------------------------------------------------------------}
  123. PROCEDURE LoadHistory (Var S: TStream);
  124. {-StoreHistory--------------------------------------------------------
  125. Writes the currently used portion of the history block to the stream
  126. S, first writing the length of the block then the block itself. Use
  127. the LoadHistory procedure to restore the history block.
  128. 30Sep99 LdB
  129. ---------------------------------------------------------------------}
  130. PROCEDURE StoreHistory (Var S: TStream);
  131. {***************************************************************************}
  132. { INITIALIZED PUBLIC VARIABLES }
  133. {***************************************************************************}
  134. {---------------------------------------------------------------------------}
  135. { INITIALIZED DOS/DPMI/WIN/NT/OS2 VARIABLES }
  136. {---------------------------------------------------------------------------}
  137. CONST
  138. HistorySize: sw_integer = 64*1024; { Maximum history size }
  139. HistoryUsed: sw_integer = 0; { History used }
  140. HistoryBlock: Pointer = Nil; { Storage block }
  141. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  142. IMPLEMENTATION
  143. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  144. {***************************************************************************}
  145. { PRIVATE RECORD DEFINITIONS }
  146. {***************************************************************************}
  147. {---------------------------------------------------------------------------}
  148. { THistRec RECORD DEFINITION
  149. Zero 1 byte, start marker
  150. Id 1 byte, History id
  151. <shortstring> 1 byte length+string data, Contents
  152. }
  153. {***************************************************************************}
  154. { UNINITIALIZED PRIVATE VARIABLES }
  155. {***************************************************************************}
  156. {---------------------------------------------------------------------------}
  157. { UNINITIALIZED DOS/DPMI/WIN/NT/OS2 VARIABLES }
  158. {---------------------------------------------------------------------------}
  159. VAR
  160. CurId: Byte; { Current history id }
  161. CurString: PString; { Current string }
  162. {***************************************************************************}
  163. { PRIVATE UNIT ROUTINES }
  164. {***************************************************************************}
  165. {---------------------------------------------------------------------------}
  166. { StartId -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 30Sep99 LdB }
  167. {---------------------------------------------------------------------------}
  168. PROCEDURE StartId (Id: Byte);
  169. BEGIN
  170. CurId := Id; { Set current id }
  171. CurString := HistoryBlock; { Set current string }
  172. END;
  173. {---------------------------------------------------------------------------}
  174. { DeleteString -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 30Sep99 LdB }
  175. {---------------------------------------------------------------------------}
  176. PROCEDURE DeleteString;
  177. VAR Len: Sw_Integer; P, P2: PChar;
  178. BEGIN
  179. P := PChar(CurString); { Current string }
  180. P2 := PChar(CurString); { Current string }
  181. Len := PByte(P2)^+3; { Length of data }
  182. Dec(P, 2); { Correct position }
  183. Inc(P2, PByte(P2)^+1); { Next hist record }
  184. { Shuffle history }
  185. Move(P2^, P^, Pointer(HistoryBlock) + HistoryUsed - Pointer(P2) );
  186. Dec(HistoryUsed, Len); { Adjust history used }
  187. END;
  188. {---------------------------------------------------------------------------}
  189. { AdvanceStringPtr -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 30Sep99 LdB }
  190. {---------------------------------------------------------------------------}
  191. PROCEDURE AdvanceStringPtr;
  192. VAR P: PChar;
  193. BEGIN
  194. While (CurString <> Nil) Do Begin
  195. If (Pointer(CurString) >= Pointer(HistoryBlock) + HistoryUsed) Then Begin{ Last string check }
  196. CurString := Nil; { Clear current string }
  197. Exit; { Now exit }
  198. End;
  199. Inc(PChar(CurString), PByte(CurString)^+1); { Move to next string }
  200. If (Pointer(CurString) >= Pointer(HistoryBlock) + HistoryUsed) Then Begin{ Last string check }
  201. CurString := Nil; { Clear current string }
  202. Exit; { Now exit }
  203. End;
  204. P := PChar(CurString); { Transfer record ptr }
  205. Inc(PChar(CurString), 2); { Move to string }
  206. if (P^<>#0) then
  207. RunError(215);
  208. Inc(P);
  209. If (P^ = Chr(CurId)) Then Exit; { Found the string }
  210. End;
  211. END;
  212. {---------------------------------------------------------------------------}
  213. { InsertString -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 30Sep99 LdB }
  214. {---------------------------------------------------------------------------}
  215. PROCEDURE InsertString (Id: Byte; Const Str: String);
  216. VAR P, P1, P2: PChar;
  217. BEGIN
  218. while (HistoryUsed+Length(Str)+3>HistorySize) do
  219. begin
  220. P:=PChar(HistoryBlock);
  221. while Pointer(P)<Pointer(HistoryBlock)+HistorySize do
  222. begin
  223. if Pointer(P)+Length(PShortString(P+2)^)+6+Length(Str) >
  224. Pointer(HistoryBlock)+HistorySize then
  225. begin
  226. Dec(HistoryUsed,Length(PShortString(P+2)^)+3);
  227. FillChar(P^,Pointer(HistoryBlock)+HistorySize-Pointer(P),#0);
  228. break;
  229. end;
  230. Inc(P,Length(PShortString(P+2)^)+3);
  231. end;
  232. end;
  233. P1 := PChar(HistoryBlock)+1; { First history record }
  234. P2 := P1+Length(Str)+3; { History record after }
  235. Move(P1^, P2^, HistoryUsed - 1); { Shuffle history data }
  236. P1^:=#0; { Set marker byte }
  237. Inc(P1);
  238. P1^:=Chr(Id); { Set history id }
  239. Inc(P1);
  240. Move(Str[0], P1^, Length(Str)+1); { Set history string }
  241. Inc(HistoryUsed, Length(Str)+3); { Inc history used }
  242. END;
  243. {***************************************************************************}
  244. { INTERFACE ROUTINES }
  245. {***************************************************************************}
  246. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  247. { HISTORY SYSTEM CONTROL ROUTINES }
  248. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  249. {---------------------------------------------------------------------------}
  250. { InitHistory -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 30Sep99 LdB }
  251. {---------------------------------------------------------------------------}
  252. PROCEDURE InitHistory;
  253. BEGIN
  254. if HistorySize>0 then
  255. GetMem(HistoryBlock, HistorySize); { Allocate block }
  256. ClearHistory; { Clear the history }
  257. END;
  258. {---------------------------------------------------------------------------}
  259. { DoneHistory -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 30Sep99 LdB }
  260. {---------------------------------------------------------------------------}
  261. PROCEDURE DoneHistory;
  262. BEGIN
  263. If (HistoryBlock <> Nil) Then { History block valid }
  264. begin
  265. FreeMem(HistoryBlock); { Release history block }
  266. HistoryBlock:=nil;
  267. end;
  268. END;
  269. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  270. { HISTORY ITEM ROUTINES }
  271. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  272. {---------------------------------------------------------------------------}
  273. { HistoryCount -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 30Sep99 LdB }
  274. {---------------------------------------------------------------------------}
  275. FUNCTION HistoryCount(Id: Byte): Word;
  276. VAR Count: Word;
  277. BEGIN
  278. StartId(Id); { Set to first record }
  279. Count := 0; { Clear count }
  280. If (HistoryBlock <> Nil) Then Begin { History initalized }
  281. AdvanceStringPtr; { Move to first string }
  282. While (CurString <> Nil) Do Begin
  283. Inc(Count); { Add one to count }
  284. AdvanceStringPtr; { Move to next string }
  285. End;
  286. End;
  287. HistoryCount := Count; { Return history count }
  288. END;
  289. {---------------------------------------------------------------------------}
  290. { HistoryStr -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 30Sep99 LdB }
  291. {---------------------------------------------------------------------------}
  292. FUNCTION HistoryStr(Id: Byte; Index: Sw_Integer): String;
  293. VAR I: Sw_Integer;
  294. BEGIN
  295. StartId(Id); { Set to first record }
  296. If (HistoryBlock <> Nil) Then Begin { History initalized }
  297. For I := 0 To Index Do AdvanceStringPtr; { Find indexed string }
  298. If (CurString <> Nil) Then
  299. HistoryStr := CurString^ Else { Return string }
  300. HistoryStr := ''; { Index not found }
  301. End Else HistoryStr := ''; { History uninitialized }
  302. END;
  303. {---------------------------------------------------------------------------}
  304. { ClearHistory -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 30Sep99 LdB }
  305. {---------------------------------------------------------------------------}
  306. PROCEDURE ClearHistory;
  307. BEGIN
  308. If (HistoryBlock <> Nil) Then Begin { History initiated }
  309. PChar(HistoryBlock)^ := #0; { Clear first byte }
  310. HistoryUsed := 1; { Set position }
  311. End;
  312. END;
  313. {---------------------------------------------------------------------------}
  314. { HistoryAdd -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 30Sep99 LdB }
  315. {---------------------------------------------------------------------------}
  316. PROCEDURE HistoryAdd (Id: Byte; Const Str: String);
  317. BEGIN
  318. If (Str = '') Then Exit; { Empty string exit }
  319. If (HistoryBlock = Nil) Then Exit; { History uninitialized }
  320. StartId(Id); { Set current data }
  321. AdvanceStringPtr; { Find the string }
  322. While (CurString <> nil) Do Begin
  323. If (Str = CurString^) Then DeleteString; { Delete duplicates }
  324. AdvanceStringPtr; { Find next string }
  325. End;
  326. InsertString(Id, Str); { Add new history item }
  327. END;
  328. function HistoryRemove(Id: Byte; Index: Sw_Integer): boolean;
  329. var
  330. I: Sw_Integer;
  331. begin
  332. StartId(Id);
  333. for I := 0 to Index do
  334. AdvanceStringPtr; { Find the string }
  335. if CurString <> nil then
  336. begin
  337. DeleteString;
  338. HistoryRemove:=true;
  339. end
  340. else
  341. HistoryRemove:=false;
  342. end;
  343. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  344. { HISTORY STREAM STORAGE AND RETREIVAL ROUTINES }
  345. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  346. {---------------------------------------------------------------------------}
  347. { LoadHistory -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 30Sep99 LdB }
  348. {---------------------------------------------------------------------------}
  349. PROCEDURE LoadHistory (Var S: TStream);
  350. VAR Size: sw_integer;
  351. BEGIN
  352. S.Read(Size, sizeof(Size)); { Read history size }
  353. If (HistoryBlock <> Nil) Then Begin { History initialized }
  354. If (Size <= HistorySize) Then Begin
  355. S.Read(HistoryBlock^, Size); { Read the history }
  356. HistoryUsed := Size; { History used }
  357. End Else S.Seek(S.GetPos + Size); { Move stream position }
  358. End Else S.Seek(S.GetPos + Size); { Move stream position }
  359. END;
  360. {---------------------------------------------------------------------------}
  361. { StoreHistory -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 30Sep99 LdB }
  362. {---------------------------------------------------------------------------}
  363. PROCEDURE StoreHistory (Var S: TStream);
  364. VAR Size: sw_integer;
  365. BEGIN
  366. If (HistoryBlock = Nil) Then Size := 0 Else { No history data }
  367. Size := HistoryUsed; { Size of history data }
  368. S.Write(Size, sizeof(Size)); { Write history size }
  369. If (Size > 0) Then S.Write(HistoryBlock^, Size); { Write history data }
  370. END;
  371. END.
  372. {
  373. $Log$
  374. Revision 1.14 2004-12-26 13:26:52 peter
  375. * remove phistrec usage
  376. Revision 1.13 2004/12/22 15:45:34 peter
  377. * fixed overflow when histsize=0
  378. Revision 1.12 2004/11/06 17:08:48 peter
  379. * drawing of tview merged from old fv code
  380. }