Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- { Started writing at 2010.06.16 by Kulverstukas || last update: 2010.06.17 }
- unit dictotrim;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes,SysUtils,FileUtil,LResources,Forms,Controls,Graphics,Dialogs,StdCtrls,
- Menus,ComCtrls, greetz, howto;
- type
- { TForm1 }
- TForm1 = class(TForm)
- SaveDialog1:TSaveDialog;
- trim:TButton;
- OpenDialog1:TOpenDialog;
- select:TButton;
- filepath:TEdit;
- ListBox1:TListBox;
- MainMenu1:TMainMenu;
- MenuItem1:TMenuItem;
- MenuItem2:TMenuItem;
- MenuItem3:TMenuItem;
- StatusBar1:TStatusBar;
- procedure MenuItem2Click(Sender:TObject);
- procedure MenuItem3Click(Sender:TObject);
- procedure selectClick(Sender:TObject);
- procedure trimClick(Sender:TObject);
- private
- { private declarations }
- public
- { public declarations }
- end;
- var
- Form1: TForm1;
- OriginalFile, TrimmedFile : string;
- OriginalText, TrimmedText : text;
- TottalWords, TrimmedWords : integer;
- implementation
- { TForm1 }
- //=====================================================
- function Trimm(line : string) : string;
- var i, i1, i2 : integer;
- begin
- Result := ''; // null the variable so it dosn't contain any data
- i := Pos('/',line); // determine exact position of forwardslash to count down from
- if i = 0 then // if no forwardslash found then ...
- //=======
- begin
- i2 := Length(line); // deteremine the length of this word
- Result := Copy(line,1,i2); // copy the word. "line" - the word; 1 - position from where to start copying; position2 - number at which to stop. In our case, the last letter of the word.
- TottalWords := TottalWords + 1; // count normal words
- Form1.StatusBar1.Panels[0].Text := 'Normal words: ' + IntToStr(TottalWords);
- Form1.ListBox1.Items.Add('Normal word: ' + Result);
- Form1.ListBox1.TopIndex := -1 + Form1.ListBox1.Items.Count;
- end
- //=======
- else
- begin
- i1 := i; // set i1 as end point
- Repeat
- Dec(i1); // repeat decreasing by 1 bit
- until (i1 < 1); // until i1 is less then 0
- Inc(i1); // increase by 1 bit if it's less then 0
- //=======
- Result := Copy(line,i1,i-1); // copy everything
- TrimmedWords := TrimmedWords + 1; // count trimmed words
- Form1.StatusBar1.Panels[1].Text := 'Trimmed words: ' + IntToStr(TrimmedWords);
- Form1.ListBox1.Items.Add('Trimmed word: ' + Result);
- Form1.ListBox1.TopIndex := -1 + Form1.ListBox1.Items.Count;
- end;
- //=======
- end;
- //=====================================================
- procedure StartToTrim;
- var line, word : string;
- begin
- //========
- OriginalFile := Form1.filepath.Text;
- TrimmedFile := Form1.SaveDialog1.FileName;
- Assign(OriginalText,OriginalFile);
- Reset(OriginalText);
- Assign(TrimmedText,TrimmedFile);
- Rewrite(TrimmedText);
- //========
- Form1.ListBox1.Items.Clear; // clear all items
- Repeat
- ReadLn(OriginalText, line); // read line to "line" variable
- Form1.Update;
- word := Trimm(line); // pass the line variable to "Trimm" funcion
- WriteLn(TrimmedText, word); // write the word to a file
- until EoF(OriginalText); // trim shit until end of file is reached
- //========
- Close(OriginalText);
- Close(TrimmedText);
- //========
- end;
- //=====================================================
- procedure TForm1.selectClick(Sender:TObject);
- begin
- //========
- Form1.OpenDialog1.InitialDir := GetCurrentDir; // open the folder where this app was launched
- Form1.OpenDialog1.Filter := 'Any file (*.*)|*.*|*.dic files|*.dic|Text files (*.txt)|*.txt';
- Form1.OpenDialog1.FilterIndex := 2;
- Form1.OpenDialog1.Options := [ofFileMustExist]; // file mus exist
- Form1.OpenDialog1.Title := 'Select a dictionary';
- //========
- if OpenDialog1.Execute then // if clicked on the button then show this open dialog
- begin
- Form1.filepath.Text := OpenDialog1.FileName; // put full path and filename in the edit box
- end;
- //========
- end;
- //=====================================================
- procedure TForm1.trimClick(Sender:TObject);
- begin
- if FileExists(Form1.filepath.Text) = True then
- begin
- Form1.SaveDialog1.InitialDir := GetCurrentDir;
- Form1.SaveDialog1.Filter := 'Any file (*.*)|*.*';
- Form1.SaveDialog1.Title := 'Save trimmed Dictionary';
- if Form1.SaveDialog1.Execute then
- begin
- Form1.trim.Enabled := False;
- Form1.Caption := 'DictoTrim - Trimming...';
- StartToTrim;
- Form1.trim.Enabled := True;
- Form1.Caption := 'DictoTrim';
- end;
- end
- else
- begin
- ShowMessage('File doesn''t exist!');
- end;
- end;
- //=====================================================
- procedure TForm1.MenuItem3Click(Sender:TObject);
- begin
- Form3.ShowModal;
- end;
- //=====================================================
- procedure TForm1.MenuItem2Click(Sender:TObject);
- begin
- Form2.ShowModal;
- end;
- //=====================================================
- initialization
- {$I dictotrim.lrs}
- end.
Add Comment
Please, Sign In to add comment