lcontrolstack.pp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. { Control stack
  2. CopyRight (C) 2004-2008 Ales Katona
  3. This library is Free software; you can rediStribute it and/or modify it
  4. under the terms of the GNU Library General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or (at your
  6. option) any later version.
  7. This program is diStributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; withOut even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  10. for more details.
  11. You should have received a Copy of the GNU Library General Public License
  12. along with This library; if not, Write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. This license has been modified. See File LICENSE for more inFormation.
  15. Should you find these sources withOut a LICENSE File, please contact
  16. me at [email protected]
  17. }
  18. unit lControlStack;
  19. {$mode objfpc}
  20. interface
  21. const
  22. TL_CSLENGTH = 3;
  23. type
  24. TLOnFull = procedure of object;
  25. TLControlStack = class
  26. private
  27. FItems: array of Char;
  28. FIndex: Byte;
  29. FOnFull: TLOnFull;
  30. function GetFull: Boolean;
  31. function GetItem(const i: Byte): Char;
  32. procedure SetItem(const i: Byte; const Value: Char);
  33. public
  34. constructor Create;
  35. procedure Clear;
  36. procedure Push(const Value: Char);
  37. property ItemIndex: Byte read FIndex;
  38. property Items[i: Byte]: Char read GetItem write SetItem; default;
  39. property Full: Boolean read GetFull;
  40. property OnFull: TLOnFull read FOnFull write FOnFull;
  41. end;
  42. implementation
  43. uses
  44. lTelnet;
  45. constructor TLControlStack.Create;
  46. begin
  47. FOnFull:=nil;
  48. FIndex:=0;
  49. SetLength(FItems, TL_CSLENGTH);
  50. end;
  51. function TLControlStack.GetFull: Boolean;
  52. begin
  53. Result:=False;
  54. if FIndex >= TL_CSLENGTH then
  55. Result:=True;
  56. end;
  57. function TLControlStack.GetItem(const i: Byte): Char;
  58. begin
  59. Result:=TS_NOP;
  60. if i < TL_CSLENGTH then
  61. Result:=FItems[i];
  62. end;
  63. procedure TLControlStack.SetItem(const i: Byte; const Value: Char);
  64. begin
  65. if i < TL_CSLENGTH then
  66. FItems[i]:=Value;
  67. end;
  68. procedure TLControlStack.Clear;
  69. begin
  70. FIndex:=0;
  71. end;
  72. procedure TLControlStack.Push(const Value: Char);
  73. begin
  74. if FIndex < TL_CSLENGTH then begin
  75. FItems[FIndex]:=Value;
  76. Inc(FIndex);
  77. if Full and Assigned(FOnFull) then
  78. FOnFull;
  79. end;
  80. end;
  81. end.