OSDN Git Service

This commit was manufactured by cvs2svn to create branch 'Bb62'.
[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                 // \83t\83@\83C\83\8b\82Ì\91\8dÝ\82ð\8am\94F
124                 if FileExists(FilePath) then begin
125             ini := TMemIniFile.Create(FilePath);
126                         sections := TStringList.Create;
127                         keys := TStringList.Create;
128                         try
129                                 ini.ReadSections(sections);
130
131                                 for i :=0 to sections.Count - 1 do begin
132                                         keys.Clear;
133                                         ini.ReadSection(sections[i], keys);
134                                         for j := 0 to keys.Count - 1 do begin
135                                                 resWord := TResistWord.Create;
136                                                 resWord.SetCategory(sections[i]);
137                                                 resWord.SetKey(keys[j]);
138                                                 resWord.SetText(ini.ReadString(sections[i], keys[j], ''));
139                                                 FDictionary.AddObject(resWord.GetKey, resWord);
140                                         end;
141                                 end;
142                         finally
143                                 keys.Free;
144                                 sections.Free;
145                                 ini.Free;
146                         end;
147                         if FSorted Then begin
148                                 FDictionary.CustomSort(KeySort);
149                         end;
150                 end;
151
152         except
153                 FInit := False;
154         end;
155 end;
156 //! \8ew\92è\82³\82ê\82½\83p\83X\82Ì\83t\83@\83C\83\8b\82É\95Û\91\82·\82é
157 procedure TInputAssistDM.SaveToFile(FilePath: String);
158 var
159         ini : TMemIniFile;
160         i : Integer;
161         resWord : TResistWord;
162 begin
163         if FileExists(FilePath) then begin
164                 try
165                         SysUtils.DeleteFile(FilePath);
166                 except
167                 end;
168         end;
169
170         ini := TMemIniFile.Create(FilePath);
171         try
172                 for i :=0 to FDictionary.Count - 1 do begin
173                         resWord := TResistWord(FDictionary.Objects[i]);
174                         ini.WriteString(resWord.FCategory, resWord.FKey, resWord.FText);
175                 end;
176                 ini.UpdateFile;
177         finally
178                 ini.Free;
179         end;
180 end;
181 //! \83f\83X\83g\83\89\83N\83^
182 procedure TInputAssistDM.DataModuleDestroy(Sender: TObject);
183 var
184         i : Integer;
185 begin
186         if (FDictionary <> nil) then begin
187                 for i := FDictionary.Count - 1 downto 0 do begin
188                         TResistWord(FDictionary.Objects[i]).Free;
189                 end;
190                 FDictionary.Clear;
191                 FDictionary.Capacity := 0;
192                 FDictionary.Free;
193         end;
194 end;
195 //! \83R\83\93\83X\83g\83\89\83N\83^
196 procedure TInputAssistDM.DataModuleCreate(Sender: TObject);
197 begin
198         FDictionary := TStringList.Create;
199         FDictionary.Sorted := False;
200         FSorted := True;
201 end;
202 //! \93o\98^\92P\8cê\90\94\8eæ\93¾
203 function TInputAssistDM.ResistWordCount : Integer;
204 begin
205         Result := 0;
206         if (FDictionary <> nil) then begin
207                 Result := FDictionary.Count;
208         end;
209 end;
210 //! \93o\98^\92P\8cê\8eæ\93¾
211 function TInputAssistDM.GetResistWord(Index: Integer): TResistWord;
212 begin
213         Result := nil;
214         if (FDictionary <> nil) then begin
215                 if (Index >= 0) and (Index < FDictionary.Count) then begin
216                         Result := TResistWord(FDictionary.Objects[index]);
217                 end;
218         end;
219 end;
220 //! \93o\98^\92P\8cê\82Ì\8dí\8f\9c
221 procedure TInputAssistDM.DeleteResistWord(ResistWord: TResistWord);
222 var
223         i : Integer;
224 begin
225         if (FDictionary <> nil) then begin
226                 for i := 0 to FDictionary.Count - 1 do begin
227                         if (ResistWord = FDictionary.Objects[i]) then begin
228                                 TResistWord(FDictionary.Objects[i]).Free;
229                                 FDictionary.Delete(i);
230                                 break;
231                         end;
232                 end;
233                 if FSorted Then begin
234                         FDictionary.CustomSort(KeySort);
235                 end;
236         end;
237 end;
238 //! \93o\98^\92P\8cê\92Ç\89Á
239 function TInputAssistDM.Add(Key: String): TResistWord;
240 var
241         resWord : TResistWord;
242 begin
243         Result := nil;
244         if (FDictionary <> nil) then begin
245                 resWord := TResistWord.Create;
246                 resWord.SetKey(Key);
247                 resWord.SetCategory('\83J\83e\83S\83\8a');
248                 resWord.SetText('\92è\8c^\95¶');
249                 FDictionary.AddObject(Key, resWord);
250                 Result := resWord;
251                 if FSorted Then begin
252                         FDictionary.CustomSort(KeySort);
253                 end;
254         end;
255 end;
256 //! \93o\98^\92P\8cê\82Ì\83L\81[\95Ï\8dX
257 procedure TInputAssistDM.ChangeKey(ResistWord: TResistWord);
258 var
259         i : Integer;
260 begin
261         if (FDictionary <> nil) then begin
262                 for i := 0 to FDictionary.Count - 1 do begin
263                         if (ResistWord = FDictionary.Objects[i]) then begin
264                                 FDictionary.Strings[i] := ResistWord.GetKey;
265                                 break;
266                         end;
267                 end;
268                 if FSorted Then begin
269                         FDictionary.CustomSort(KeySort);
270                 end;
271
272         end;
273 end;
274 //! Key\82ð\8e\9d\82Â\93o\98^\82³\82ê\82Ä\82¢\82é\92P\8cê\82ð\8eæ\93¾
275 function TInputAssistDM.GetStartWithKeyResistWords(Key: String; var list: TStringList): Integer;
276 var
277         i : Integer;
278         resWord : TResistWord;
279
280 begin
281         Result := 0;
282         if (FDictionary <> nil) and (list <> nil) then begin
283                 Key := ZenToHan(Key);
284                 for i := 0 to FDictionary.Count - 1 do begin
285                         if (AnsiPos(Key, ZenToHan(FDictionary.Strings[i])) = 1) then begin
286                                 Inc(Result);
287                                 resWord := TResistWord(FDictionary.Objects[i]);
288                                 list.AddObject(resWord.GetKey + '(' +
289                                                                 resWord.GetCategory + ')', resWord);
290                         end else if (Result > 0) then begin
291                                 //\83\\81[\83g\82³\82ê\82Ä\82¢\82é\82©\82ç\81A\83q\83b\83g\82·\82ê\82Î\98A\91±\82·\82é\82Í\82¸
292                                 break;
293                         end;
294                 end;
295         end;
296 end;
297 //! Key\82ð\83J\83e\83S\83\8a\82É\8e\9d\82Â\93o\98^\82³\82ê\82Ä\82¢\82é\92P\8cê\82ð\8eæ\93¾
298 function TInputAssistDM.GetStartWithCategoryResistWords(Key: String; var list: TStringList): Integer;
299 var
300         i : Integer;
301         resWord : TResistWord;
302 begin
303         Result := 0;
304         if (FDictionary <> nil) and (list <> nil) then begin
305                 Key := ZenToHan(Key);
306                 for i := 0 to FDictionary.Count - 1 do begin
307                         resWord := TResistWord(FDictionary.Objects[i]);
308                         if (AnsiPos(Key, ZenToHan(resWord.GetCategory)) = 1) then begin
309                                 Inc(Result);
310                                 list.AddObject(resWord.GetKey + '(' +
311                                                                 resWord.GetCategory + ')', resWord);
312                         end;
313                 end;
314                 list.CustomSort(CategorySort);
315         end;
316 end;
317
318 //! \83\\81[\83g\82Ì\8fó\91Ô\82Ì\8eæ\93¾
319 function TInputAssistDM.GetSorted: Boolean;
320 begin
321         Result := FSorted;
322 end;
323 //! \83\\81[\83g\8fó\91Ô\82Ì\90Ý\92è
324 procedure TInputAssistDM.SetSorted(Value: Boolean);
325 begin
326         if (not FSorted) and (Value) then begin
327                 FDictionary.CustomSort(KeySort);
328         end;
329         FSorted := Value;
330 end;
331 //! Key\82Ì\83J\83e\83S\83\8a\82É\93o\98^\82³\82ê\82Ä\82¢\82é\92P\8cê\82ð\8eæ\93¾
332 function TInputAssistDM.GetCategoryResistWords(Key: String; var list: TStringList): Integer;
333 var
334         i : Integer;
335         resWord : TResistWord;
336 begin
337         Result := 0;
338         if (FDictionary <> nil) and (list <> nil) then begin
339                 for i := 0 to FDictionary.Count - 1 do begin
340                         resWord := TResistWord(FDictionary.Objects[i]);
341                         if (Key = resWord.GetCategory) then begin
342                                 Inc(Result);
343                                 list.AddObject(resWord.GetKey + '(' +
344                                                                 resWord.GetCategory + ')', resWord);
345                         end;
346                 end;
347                 list.CustomSort(CategorySort);
348         end;
349 end;
350
351 //! \93o\98^\8dÏ\82Ý\83L\81[\82Ì\91S\82Ä\82Ì\83J\83e\83S\83\8a\83\8a\83X\83g\8eæ\93¾
352 procedure TInputAssistDM.GetCategoryList(var list: TStringList);
353 var
354         i : Integer;
355 begin
356         if (FDictionary <> nil) and (list <> nil) then begin
357                 // \8fd\95¡\83`\83F\83b\83N\82ðTStringList\82Ì\8b@\94\\82Å\8ds\82¤
358                 list.Clear;
359                 list.Duplicates := dupIgnore;
360                 list.Sorted := true;
361                 list.BeginUpdate;
362                 for i := 0 to FDictionary.Count - 1 do begin
363                         list.Add(TResistWord(FDictionary.Objects[i]).GetCategory);
364                 end;
365                 list.EndUpdate;
366         end;
367 end;
368
369 //! 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
370 function CategorySort(List: TStringList; Index1, Index2: Integer): Integer;
371 var
372         resWord1 : TResistWord;
373         resWord2 : TResistWord;
374 begin
375         Result := 0;
376         try
377                 resWord1 := TResistWord(List.Objects[Index1]);
378                 resWord2 := TResistWord(List.Objects[Index2]);
379                 Result := CompareStr(ZenToHan(resWord1.GetCategory),
380                                                                  ZenToHan(resWord2.GetCategory));
381                 if (Result = 0) then begin
382                         Result := CompareStr(ZenToHan(resWord1.GetKey),
383                                                                          ZenToHan(resWord2.GetKey));
384                 end;
385         except
386         end;
387 end;
388 //! 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
389 function KeySort(List: TStringList; Index1, Index2: Integer): Integer;
390 var
391         resWord1 : TResistWord;
392         resWord2 : TResistWord;
393 begin
394         Result := 0;
395         try
396                 resWord1 := TResistWord(List.Objects[Index1]);
397                 resWord2 := TResistWord(List.Objects[Index2]);
398                 Result := CompareStr(ZenToHan(resWord1.FKey),
399                                                                  ZenToHan(resWord2.FKey));
400                 if (Result = 0) then begin
401                         Result := CompareStr(ZenToHan(resWord1.GetCategory),
402                                                                          ZenToHan(resWord2.GetCategory));
403                 end;
404         except
405         end;
406 end;
407 //! \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
408 function TInputAssistDM.IsDupulicate(Key: String; Category: String): Boolean;
409 var
410         i : Integer;
411 begin
412         // \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é
413         Result := False;
414         if (FDictionary <> nil) then begin
415                 for i := 0 to FDictionary.Count - 1 do begin
416                         if (Key = FDictionary.Strings[i]) and
417                                 (Category = TResistWord(FDictionary.Objects[i]).GetCategory)
418                         then begin
419                                 Result := True;
420                                 Break;
421                         end;
422                 end;
423         end;
424 end;
425
426 end.