OSDN Git Service

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