OSDN Git Service

不要な変数を削除
[gikonavigoeson/gikonavi.git] / InputAssistDataModule.pas
1 unit InputAssistDataModule;
2
3 interface
4
5 uses
6   SysUtils, Classes;
7
8 type
9   TResistWord = class;
10
11   TInputAssistDM = class(TDataModule)
12     procedure DataModuleDestroy(Sender: TObject);
13     procedure DataModuleCreate(Sender: TObject);
14   private
15         { Private \90é\8c¾ }
16         FInit : Boolean;
17         FDictionary : TStringList;      ///< \93o\98^\92P\8cê\82Æ\92è\8c^\95\82Ì\8e«\8f\91
18         function GetSorted: Boolean;    ///< \83\\81[\83g\82Ì\8fó\91Ô\82Ì\8eæ\93¾
19         procedure SetSorted(Value: Boolean);    ///< \83\\81[\83g\8fó\91Ô\82Ì\90Ý\92è
20
21   public
22         { Public \90é\8c¾ }
23         property Sorted : Boolean read GetSorted write SetSorted;
24         procedure Init(FilePath: String);
25         procedure SaveToFile(FilePath: String);
26         function ResistWordCount : Integer;     ///<\93o\98^\92P\8cê\90\94\8eæ\93¾
27         function GetResistWord(Index: Integer): TResistWord;    ///< \93o\98^\92P\8cê\8eæ\93¾
28         procedure DeleteResistWord(ResistWord: TResistWord);  ///< \93o\98^\92P\8cê\82Ì\8dí\8f\9c
29         function Add(Key: String): TResistWord; ///< \93o\98^\92P\8cê\92Ç\89Á
30         procedure ChangeKey(ResistWord: TResistWord);  ///< \93o\98^\92P\8cê\82Ì\83L\81[\95Ï\8dX
31         //! Key\82ð\83L\81[\82É\8e\9d\82Â\93o\98^\82³\82ê\82Ä\82¢\82é\92P\8cê\82ð\8eæ\93¾
32         function GetStartWithKeyResistWords(Key: String; var list: TStringList): Integer;
33         //! Key\82ð\83J\83e\83S\83\8a\82É\8e\9d\82Â\93o\98^\82³\82ê\82Ä\82¢\82é\92P\8cê\82ð\8eæ\93¾
34         function GetStartWithCategoryResistWords(Key: String; var list: TStringList): Integer;
35         //! Key\82Ì\83J\83e\83S\83\8a\82É\93o\98^\82³\82ê\82Ä\82¢\82é\92P\8cê\82ð\8eæ\93¾
36         function GetCategoryResistWords(Key: String; var list: TStringList): Integer;
37         //! \93o\98^\8dÏ\82Ý\83L\81[\82Ì\91S\82Ä\82Ì\83J\83e\83S\83\8a\83\8a\83X\83g\8eæ\93¾
38         procedure GetCategoryList(var list: TStringList);
39
40   end;
41
42   TResistWord = class(TObject)
43   private
44         FKey : String;          ///< \95Ï\8a·\8e\9e\82Ì\83L\81[\82É\82È\82é
45         FCategory : String;     ///< \95ª\97Þ
46         FText : String;         ///< \92è\8c^\95
47   public
48         function GetKey: String;
49         procedure SetKey(Value: String);
50         function GetCategory: String;
51         procedure SetCategory(Value: String);
52         function GetText: String;
53         procedure SetText(Value: String);
54         property Key: String read FKey write FKey;
55         property Category: String read FCategory write FCategory;
56         property Text: String read GetText write SetText;
57   end;
58
59   function CategorySort(List: TStringList; Index1, Index2: Integer): Integer;
60 var
61   InputAssistDM: TInputAssistDM;
62
63 implementation
64
65 uses
66   MojuUtils, IniFiles;
67
68 {$R *.dfm}
69 //! FKey\82É\90Ý\92è\82³\82ê\82Ä\82¢\82é\92l\82ð\8eæ\93¾\82·\82é
70 function TResistWord.GetKey: String;
71 begin
72         //\83G\83X\83P\81[\83v\82µ\82Ä\82¢\82é=\82ð\95\9c\8c³\82·\82é
73         Result := MojuUtils.CustomStringReplace(FKey, '&#61;', '=');
74 end;
75 //! FKey\82É\92l\82ð\90Ý\92è\82·\82é
76 procedure TResistWord.SetKey(Value: String);
77 begin
78         //=\82Í\95Û\91\8e\9e\82É\8eg\82¤\82Ì\82Å\83G\83X\83P\81[\83v\82·\82é
79         FKey := MojuUtils.CustomStringReplace(Value, '=', '&#61;');
80 end;
81 //! FCategory\82É\90Ý\92è\82³\82ê\82Ä\82¢\82é\92l\82ð\8eæ\93¾\82·\82é
82 function TResistWord.GetCategory: String;
83 begin
84         //\83G\83X\83P\81[\83v\82µ\82Ä\82¢\82é=\82ð\95\9c\8c³\82·\82é
85         Result := MojuUtils.CustomStringReplace(FCategory, '&#61;', '=');
86 end;
87 //! FCategory\82É\92l\82ð\90Ý\92è\82·\82é
88 procedure TResistWord.SetCategory(Value: String);
89 begin
90         //=\82Í\95Û\91\8e\9e\82É\8eg\82¤\82Ì\82Å\83G\83X\83P\81[\83v\82·\82é
91         FCategory := MojuUtils.CustomStringReplace(Value, '=', '&#61;');
92 end;
93 //! FText\82É\90Ý\92è\82³\82ê\82Ä\82¢\82é\92l\82ð\8eæ\93¾\82·\82é
94 function TResistWord.GetText: String;
95 begin
96         //\83G\83X\83P\81[\83v\82µ\82Ä\82¢\82é=\82ð\95\9c\8c³\82·\82é
97         Result := MojuUtils.CustomStringReplace(FText, '&#61;', '=');
98         // #1\82É\82µ\82½\89ü\8ds\83R\81[\83h\82ð#13#10\82É\95\9c\8c³\82·\82é
99         Result := MojuUtils.CustomStringReplace(Result, #1, #13#10);
100 end;
101 procedure TResistWord.SetText(Value: String);
102 begin
103         //=\82Í\95Û\91\8e\9e\82É\8eg\82¤\82Ì\82Å\83G\83X\83P\81[\83v\82·\82é
104         FText := MojuUtils.CustomStringReplace(Value, '=', '&#61;');
105         //\89ü\8ds\83R\81[\83h\82ð#1\82É\82·\82é\81i1\8ds\82É\82·\82é\82½\82ß)
106         FText := MojuUtils.CustomStringReplace(FText, #13#10, #1);
107 end;
108 //! \83t\83@\83C\83\8b\82ð\93Ç\82Ý\8d\9e\82ñ\82Å\8f\89\8aú\89»\82·\82é
109 procedure TInputAssistDM.Init(FilePath: String);
110 var
111         ini : TMemIniFile;
112         sections: TStringList;
113         keys: TStringList;
114         i, j : Integer;
115         resWord : TResistWord;
116 begin
117         FInit := True;
118         try
119                 // ini\83t\83@\83C\83\8b\82ª\96³\82¯\82ê\82Î\81A\83f\83t\83H\83\8b\83g\82ð\83\8a\83l\81[\83\80\82·\82é
120                 if not FileExists(FilePath) then begin
121                         RenameFile(ChangeFileExt(FilePath, '.default'), FilePath);
122                 end;
123
124                 // \83t\83@\83C\83\8b\82Ì\91\8dÝ\82ð\8am\94F
125                 if FileExists(FilePath) then begin
126                         ini := TMemIniFile.Create(FilePath);
127                         sections := TStringList.Create;
128                         keys := TStringList.Create;
129                         try
130                                 ini.ReadSections(sections);
131
132                                 for i :=0 to sections.Count - 1 do begin
133                                         keys.Clear;
134                                         ini.ReadSection(sections[i], keys);
135                                         for j := 0 to keys.Count - 1 do begin
136                                                 resWord := TResistWord.Create;
137                                                 resWord.SetCategory(sections[i]);
138                                                 resWord.SetKey(keys[j]);
139                                                 resWord.SetText(ini.ReadString(sections[i], keys[j], ''));
140                                                 FDictionary.AddObject(resWord.GetKey, resWord);
141                                         end;
142                                 end;
143                         finally
144                                 keys.Free;
145                                 sections.Free;
146                                 ini.Free;
147                         end;
148                 end;
149
150         except
151                 FInit := False;
152         end;
153 end;
154 //! \8ew\92è\82³\82ê\82½\83p\83X\82Ì\83t\83@\83C\83\8b\82É\95Û\91\82·\82é
155 procedure TInputAssistDM.SaveToFile(FilePath: String);
156 var
157         ini : TMemIniFile;
158         i : Integer;
159         resWord : TResistWord;
160 begin
161         if FileExists(FilePath) then begin
162                 try
163                         DeleteFile(FilePath);
164                 except
165                 end;
166         end;
167         ini := TMemIniFile.Create(FilePath);
168         try
169                 for i :=0 to FDictionary.Count - 1 do begin
170                         resWord := TResistWord(FDictionary.Objects[i]);
171                         ini.WriteString(resWord.FCategory, resWord.FKey, resWord.FText);
172                 end;
173                 ini.UpdateFile;
174         finally
175                 ini.Free;
176         end;
177 end;
178 //! \83f\83X\83g\83\89\83N\83^
179 procedure TInputAssistDM.DataModuleDestroy(Sender: TObject);
180 var
181         i : Integer;
182 begin
183         if (FDictionary <> nil) then begin
184                 for i := FDictionary.Count - 1 downto 0 do begin
185                         TResistWord(FDictionary.Objects[i]).Free;
186                 end;
187                 FDictionary.Clear;
188                 FDictionary.Capacity := 0;
189                 FDictionary.Free;
190         end;
191 end;
192 //! \83R\83\93\83X\83g\83\89\83N\83^
193 procedure TInputAssistDM.DataModuleCreate(Sender: TObject);
194 begin
195         FDictionary := TStringList.Create;
196         FDictionary.Duplicates := dupAccept;
197         FDictionary.Sorted := True;
198 end;
199 //! \93o\98^\92P\8cê\90\94\8eæ\93¾
200 function TInputAssistDM.ResistWordCount : Integer;
201 begin
202         Result := 0;
203         if (FDictionary <> nil) then begin
204                 Result := FDictionary.Count;
205         end;
206 end;
207 //! \93o\98^\92P\8cê\8eæ\93¾
208 function TInputAssistDM.GetResistWord(Index: Integer): TResistWord;
209 begin
210         Result := nil;
211         if (FDictionary <> nil) then begin
212                 if (Index >= 0) and (Index < FDictionary.Count) then begin
213                         Result := TResistWord(FDictionary.Objects[index]);
214                 end;
215         end;
216 end;
217 //! \93o\98^\92P\8cê\82Ì\8dí\8f\9c
218 procedure TInputAssistDM.DeleteResistWord(ResistWord: TResistWord);
219 var
220         i : Integer;
221 begin
222         if (FDictionary <> nil) then begin
223                 for i := 0 to FDictionary.Count - 1 do begin
224                         if (ResistWord = FDictionary.Objects[i]) then begin
225                                 TResistWord(FDictionary.Objects[i]).Free;
226                                 FDictionary.Delete(i);
227                                 break;
228                         end;
229                 end;
230         end;
231 end;
232 //! \93o\98^\92P\8cê\92Ç\89Á
233 function TInputAssistDM.Add(Key: String): TResistWord;
234 var
235         resWord : TResistWord;
236 begin
237         Result := nil;
238         if (FDictionary <> nil) then begin
239                 resWord := TResistWord.Create;
240                 resWord.SetKey(Key);
241                 resWord.SetCategory('\83J\83e\83S\83\8a');
242                 resWord.SetText('\92è\8c^\95¶');
243                 FDictionary.AddObject(Key, resWord);
244                 Result := resWord;
245         end;
246 end;
247 //! \93o\98^\92P\8cê\82Ì\83L\81[\95Ï\8dX
248 procedure TInputAssistDM.ChangeKey(ResistWord: TResistWord);
249 var
250         i : Integer;
251 begin
252         if (FDictionary <> nil) then begin
253                 for i := 0 to FDictionary.Count - 1 do begin
254                         if (ResistWord = FDictionary.Objects[i]) then begin
255                                 FDictionary.Strings[i] := ResistWord.GetKey;
256                                 break;
257                         end;
258                 end;
259         end;
260 end;
261 //! Key\82ð\8e\9d\82Â\93o\98^\82³\82ê\82Ä\82¢\82é\92P\8cê\82ð\8eæ\93¾
262 function TInputAssistDM.GetStartWithKeyResistWords(Key: String; var list: TStringList): Integer;
263 var
264         i : Integer;
265         resWord : TResistWord;
266
267 begin
268         Result := 0;
269         if (FDictionary <> nil) and (list <> nil) then begin
270                 Key := ZenToHan(Key);
271                 for i := 0 to FDictionary.Count - 1 do begin
272                         if (AnsiPos(Key, ZenToHan(FDictionary.Strings[i])) = 1) then begin
273                                 Inc(Result);
274                                 resWord := TResistWord(FDictionary.Objects[i]);
275                                 list.AddObject(resWord.GetKey + '(' +
276                                                                 resWord.GetCategory + ')', resWord);
277                         end else if (Result > 0) then begin
278                                 //\83\\81[\83g\82³\82ê\82Ä\82¢\82é\82©\82ç\81A\83q\83b\83g\82·\82ê\82Î\98A\91±\82·\82é\82Í\82¸
279                                 break;
280                         end;
281                 end;
282         end;
283 end;
284 //! Key\82ð\83J\83e\83S\83\8a\82É\8e\9d\82Â\93o\98^\82³\82ê\82Ä\82¢\82é\92P\8cê\82ð\8eæ\93¾
285 function TInputAssistDM.GetStartWithCategoryResistWords(Key: String; var list: TStringList): Integer;
286 var
287         i : Integer;
288         resWord : TResistWord;
289 begin
290         Result := 0;
291         if (FDictionary <> nil) and (list <> nil) then begin
292                 Key := ZenToHan(Key);
293                 for i := 0 to FDictionary.Count - 1 do begin
294                         resWord := TResistWord(FDictionary.Objects[i]);
295                         if (AnsiPos(Key, ZenToHan(resWord.GetCategory)) = 1) then begin
296                                 Inc(Result);
297                                 list.AddObject(resWord.GetKey + '(' +
298                                                                 resWord.GetCategory + ')', resWord);
299                         end;
300                 end;
301                 list.CustomSort(CategorySort);
302         end;
303 end;
304
305 //! \83\\81[\83g\82Ì\8fó\91Ô\82Ì\8eæ\93¾
306 function TInputAssistDM.GetSorted: Boolean;
307 begin
308         Result := False;
309         if (FDictionary <> nil) then begin
310                 Result := FDictionary.Sorted;
311         end;
312 end;
313 //! \83\\81[\83g\8fó\91Ô\82Ì\90Ý\92è
314 procedure TInputAssistDM.SetSorted(Value: Boolean);
315 begin
316         if (FDictionary <> nil) then begin
317                 FDictionary.Sorted := Value;
318         end;
319 end;
320 //! Key\82Ì\83J\83e\83S\83\8a\82É\93o\98^\82³\82ê\82Ä\82¢\82é\92P\8cê\82ð\8eæ\93¾
321 function TInputAssistDM.GetCategoryResistWords(Key: String; var list: TStringList): Integer;
322 var
323         i : Integer;
324         resWord : TResistWord;
325 begin
326         Result := 0;
327         if (FDictionary <> nil) and (list <> nil) then begin
328                 for i := 0 to FDictionary.Count - 1 do begin
329                         resWord := TResistWord(FDictionary.Objects[i]);
330                         if (Key = resWord.GetCategory) then begin
331                                 Inc(Result);
332                                 list.AddObject(resWord.GetKey + '(' +
333                                                                 resWord.GetCategory + ')', resWord);
334                         end;
335                 end;
336                 list.CustomSort(CategorySort);
337         end;
338 end;
339
340 //! \93o\98^\8dÏ\82Ý\83L\81[\82Ì\91S\82Ä\82Ì\83J\83e\83S\83\8a\83\8a\83X\83g\8eæ\93¾
341 procedure TInputAssistDM.GetCategoryList(var list: TStringList);
342 var
343         i : Integer;
344 begin
345         if (FDictionary <> nil) and (list <> nil) then begin
346                 // \8fd\95¡\83`\83F\83b\83N\82ðTStringList\82Ì\8b@\94\\82Å\8ds\82¤
347                 list.Clear;
348                 list.Duplicates := dupIgnore;
349                 list.Sorted := true;
350                 list.BeginUpdate;
351                 for i := 0 to FDictionary.Count - 1 do begin
352                         list.Add(TResistWord(FDictionary.Objects[i]).GetCategory);
353                 end;
354                 list.EndUpdate;
355         end;
356 end;
357
358 //! Key\82ð\83J\83e\83S\83\8a\82É\8e\9d\82Â\93o\98^\92P\8cê\82ð\95Ô\82·\8e\9e\82Ì\83\\81[\83g\97p\94ä\8ar\83\81\83\\83b\83h
359 function CategorySort(List: TStringList; Index1, Index2: Integer): Integer;
360 var
361         resWord1 : TResistWord;
362         resWord2 : TResistWord;
363 begin
364         Result := 0;
365         try
366                 resWord1 := TResistWord(List.Objects[Index1]);
367                 resWord2 := TResistWord(List.Objects[Index2]);
368                 Result := AnsiCompareStr(resWord1.GetCategory, resWord2.GetCategory);
369                 if (Result = 0) then begin
370                         Result := AnsiCompareStr(resWord1.GetKey, resWord2.GetKey);
371                 end;
372         except
373         end;
374 end;
375
376 end.