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