PageRenderTime 15ms CodeModel.GetById 12ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

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