source: trunk/Forms/FormImport.pas

Last change on this file was 227, checked in by chronos, 44 hours ago
  • Modified: Do not reference global Core object if possible.
File size: 4.1 KB
Line 
1unit FormImport;
2
3interface
4
5uses
6 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
7 Acronym, FormEx;
8
9type
10
11 { TFormImport }
12
13 TFormImport = class(TFormEx)
14 ButtonLoadFromFile: TButton;
15 ButtonProcess: TButton;
16 ComboBoxDataFormat: TComboBox;
17 Label1: TLabel;
18 Memo1: TMemo;
19 OpenDialog1: TOpenDialog;
20 procedure ButtonProcessClick(Sender: TObject);
21 procedure ButtonLoadFromFileClick(Sender: TObject);
22 private
23 procedure ImportMediaWiki;
24 procedure ImportCSV;
25 public
26 AcronymDb: TAcronymDb;
27 end;
28
29
30implementation
31
32{$R *.lfm}
33
34resourcestring
35 SImportedNewAcronyms = 'Imported %d new acronyms.';
36
37{ TFormImport }
38
39procedure TFormImport.ButtonProcessClick(Sender: TObject);
40begin
41 if ComboBoxDataFormat.ItemIndex = 0 then ImportCSV;
42 if ComboBoxDataFormat.ItemIndex = 1 then ImportMediaWiki;
43end;
44
45procedure TFormImport.ButtonLoadFromFileClick(Sender: TObject);
46begin
47 if ComboBoxDataFormat.ItemIndex = 0 then OpenDialog1.DefaultExt := '.csv';
48 if ComboBoxDataFormat.ItemIndex = 1 then OpenDialog1.DefaultExt := '.txt';
49 if OpenDialog1.Execute then
50 Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
51end;
52
53procedure TFormImport.ImportMediaWiki;
54var
55 I: Integer;
56 Line: string;
57 AcronymName: string;
58 AcronymMeaning: string;
59 AddedCount: Integer;
60 Acronym: TAcronym;
61 Meaning: TAcronymMeaning;
62begin
63 AcronymName := '';
64 AddedCount := 0;
65 for I := 0 to Memo1.Lines.Count - 1 do begin
66 Line := Trim(Memo1.Lines[I]);
67 if Copy(Line, 1, 1) = ';' then begin
68 if Pos(':', Line) > 0 then begin
69 AcronymName := Trim(Copy(Line, 2, Pos(':', Line) - 2));
70 Line := Copy(Line, Pos(':', Line), Length(Line));
71 end else AcronymName := Trim(Copy(Line, 2, Length(Line)));
72 end;
73 if Copy(Line, 1, 1) = ':' then begin
74 AcronymMeaning := Trim(Copy(Line, 2, Length(Line)));
75 if (AcronymName <> '') and (AcronymMeaning <> '') then begin
76 Acronym := AcronymDb.Acronyms.SearchByName(AcronymName);
77 if not Assigned(Acronym) then begin
78 Acronym := TAcronym.Create;
79 Acronym.Name := AcronymName;
80 AcronymDb.Acronyms.Add(Acronym);
81 end;
82 Meaning := Acronym.Meanings.SearchByName(AcronymMeaning);
83 if not Assigned(Meaning) then begin
84 Meaning := TAcronymMeaning.Create;
85 Meaning.Name := AcronymMeaning;
86 Meaning.Acronym := Acronym;
87 Acronym.Meanings.Add(Meaning);
88 Inc(AddedCount)
89 end;
90 end;
91 end;
92 end;
93 if AddedCount > 0 then AcronymDb.Modified := True;
94 ShowMessage(Format(SImportedNewAcronyms, [AddedCount]));
95end;
96
97procedure TFormImport.ImportCSV;
98var
99 I: Integer;
100 Line: string;
101 Columns: TStringList;
102 Acronym: TAcronym;
103 Meaning: TAcronymMeaning;
104 AcronymName: string;
105 AcronymMeaning: string;
106 AcronymDescription: string;
107 AddedCount: Integer;
108begin
109 AddedCount := 0;
110 Columns := TStringList.Create;
111 Columns.StrictDelimiter := True;
112 for I := 0 to Memo1.Lines.Count - 1 do begin
113 Line := Trim(Memo1.Lines[I]);
114 Columns.DelimitedText := Line;
115 if Columns.Count > 0 then AcronymName := Trim(Columns[0])
116 else AcronymName := '';
117 if Columns.Count > 1 then AcronymMeaning := Trim(Columns[1])
118 else AcronymMeaning := '';
119 if Columns.Count > 2 then AcronymDescription := Trim(Columns[2])
120 else AcronymDescription := '';
121 if (AcronymName <> '') and (AcronymDescription <> '') then begin
122 Acronym := AcronymDb.Acronyms.SearchByName(AcronymName);
123 if not Assigned(Acronym) then begin
124 Acronym := TAcronym.Create;
125 Acronym.Name := AcronymName;
126 AcronymDb.Acronyms.Add(Acronym);
127 end;
128 Meaning := Acronym.Meanings.SearchByName(AcronymDescription);
129 if not Assigned(Meaning) then begin
130 Meaning := TAcronymMeaning.Create;
131 Meaning.Name := AcronymMeaning;
132 Meaning.Description := AcronymDescription;
133 Meaning.Acronym := Acronym;
134 Acronym.Meanings.Add(Meaning);
135 Inc(AddedCount)
136 end;
137 end;
138 end;
139 Columns.Free;
140 if AddedCount > 0 then AcronymDb.Modified := True;
141 ShowMessage(Format(SImportedNewAcronyms, [AddedCount]));
142end;
143
144end.
145
Note: See TracBrowser for help on using the repository browser.