/components/jcf2/Settings/SetWordList.pas

http://github.com/graemeg/lazarus · Pascal · 167 lines · 98 code · 37 blank · 32 comment · 5 complexity · bfd968465f99d6a68babb6dae77a2871 MD5 · raw file

  1. {(*}
  2. (*------------------------------------------------------------------------------
  3. Delphi Code formatter source code
  4. The Original Code is SetWordList.pas, released June 2003.
  5. The Initial Developer of the Original Code is Anthony Steele.
  6. Portions created by Anthony Steele are Copyright (C) 1999-2008 Anthony Steele.
  7. All Rights Reserved.
  8. Contributor(s): Anthony Steele.
  9. The contents of this file are subject to the Mozilla Public License Version 1.1
  10. (the "License"). you may not use this file except in compliance with the License.
  11. You may obtain a copy of the License at http://www.mozilla.org/NPL/
  12. Software distributed under the License is distributed on an "AS IS" basis,
  13. WITHOUT WARRANTY OF ANY KIND, either express or implied.
  14. See the License for the specific language governing rights and limitations
  15. under the License.
  16. Alternatively, the contents of this file may be used under the terms of
  17. the GNU General Public License Version 2 or later (the "GPL")
  18. See http://www.gnu.org/licenses/gpl.html
  19. ------------------------------------------------------------------------------*)
  20. {*)}
  21. unit SetWordList;
  22. {
  23. This is a base class that abstrects the SetAnyWordCaps class
  24. Stores a generic word list
  25. }
  26. {$I JcfGlobal.inc}
  27. interface
  28. uses
  29. { delphi }Classes,
  30. { local }JcfSetBase, SettingsStream;
  31. type
  32. TSetWordList = class(TSetBase)
  33. private
  34. fbEnabled: boolean;
  35. fcWords: TStringList;
  36. protected
  37. procedure AddDefaultWords; virtual;
  38. public
  39. constructor Create(const psSectionName: string);
  40. destructor Destroy; override;
  41. procedure Clear;
  42. procedure Add(const psWord: string);
  43. function HasWord(const psWord: string): boolean;
  44. function IndexOfWord(const psWord: string): integer;
  45. function CapitaliseWord(const psWord: string): string;
  46. procedure WriteToStream(const pcOut: TSettingsOutput); override;
  47. procedure ReadFromStream(const pcStream: TSettingsInput); override;
  48. property Enabled: boolean Read fbEnabled Write fbEnabled;
  49. property Words: TStringList Read fcWords;
  50. end;
  51. implementation
  52. uses
  53. { delphi }
  54. {$IFNDEF FPC}Windows,{$ENDIF} SysUtils;
  55. const
  56. REG_ENABLED = 'Enabled';
  57. REG_WORDS = 'Words';
  58. { TSetWordList }
  59. constructor TSetWordList.Create(const psSectionName: string);
  60. begin
  61. inherited Create;
  62. Assert(psSectionName <> '');
  63. SetSection(psSectionName);
  64. fcWords := TStringList.Create;
  65. fcWords.Sorted := True;
  66. fcWords.Duplicates := dupIgnore;
  67. end;
  68. destructor TSetWordList.Destroy;
  69. begin
  70. FreeAndNil(fcWords);
  71. inherited;
  72. end;
  73. procedure TSetWordList.ReadFromStream(const pcStream: TSettingsInput);
  74. begin
  75. Assert(pcStream <> nil);
  76. fcWords.Sorted := False;
  77. fbEnabled := pcStream.Read(REG_ENABLED, True);
  78. if not pcStream.Read(REG_WORDS, fcWords) then
  79. AddDefaultWords;
  80. fcWords.Sort;
  81. fcWords.Sorted := True;
  82. end;
  83. procedure TSetWordList.WriteToStream(const pcOut: TSettingsOutput);
  84. begin
  85. Assert(pcOut <> nil);
  86. pcOut.Write(REG_ENABLED, fbEnabled);
  87. pcOut.Write(REG_WORDS, fcWords);
  88. end;
  89. function TSetWordList.HasWord(const psWord: string): boolean;
  90. begin
  91. if psWord = '' then
  92. Result := False
  93. else
  94. Result := (IndexOfWord(psWord) > -1);
  95. end;
  96. function TSetWordList.IndexOfWord(const psWord: string): integer;
  97. begin
  98. if psWord = '' then
  99. Result := -1
  100. else
  101. Result := fcWords.IndexOf(psWord);
  102. end;
  103. function TSetWordList.CapitaliseWord(const psWord: string): string;
  104. var
  105. liLoop: integer;
  106. begin
  107. Result := psWord;
  108. for liLoop := 0 to fcWords.Count - 1 do
  109. begin
  110. if AnsiCompareText(psWord, fcWords.Strings[liLoop]) = 0 then
  111. Result := fcWords.Strings[liLoop]; // this sets it to the right case
  112. end;
  113. end;
  114. procedure TSetWordList.AddDefaultWords;
  115. begin
  116. // do nothing, here for overide
  117. end;
  118. procedure TSetWordList.Add(const psWord: string);
  119. begin
  120. fcWords.Add(psWord);
  121. end;
  122. procedure TSetWordList.Clear;
  123. begin
  124. fcWords.Clear;
  125. end;
  126. end.