Kulverstukas

DictoTrim

Jun 17th, 2010
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.18 KB | None | 0 0
  1. { Started writing at 2010.06.16 by Kulverstukas || last update: 2010.06.17 }
  2.  
  3. unit dictotrim;
  4.  
  5. {$mode objfpc}{$H+}
  6.  
  7. interface
  8.  
  9. uses
  10.   Classes,SysUtils,FileUtil,LResources,Forms,Controls,Graphics,Dialogs,StdCtrls,
  11.   Menus,ComCtrls, greetz, howto;
  12.  
  13. type
  14.  
  15.   { TForm1 }
  16.  
  17.   TForm1 = class(TForm)
  18.     SaveDialog1:TSaveDialog;
  19.     trim:TButton;
  20.     OpenDialog1:TOpenDialog;
  21.     select:TButton;
  22.     filepath:TEdit;
  23.     ListBox1:TListBox;
  24.     MainMenu1:TMainMenu;
  25.     MenuItem1:TMenuItem;
  26.     MenuItem2:TMenuItem;
  27.     MenuItem3:TMenuItem;
  28.     StatusBar1:TStatusBar;
  29.     procedure MenuItem2Click(Sender:TObject);
  30.     procedure MenuItem3Click(Sender:TObject);
  31.     procedure selectClick(Sender:TObject);
  32.     procedure trimClick(Sender:TObject);
  33.   private
  34.     { private declarations }
  35.   public
  36.     { public declarations }
  37.   end;
  38.  
  39. var
  40.   Form1: TForm1;
  41.     OriginalFile, TrimmedFile : string;
  42.     OriginalText, TrimmedText : text;
  43.     TottalWords, TrimmedWords : integer;
  44.  
  45. implementation
  46.  
  47. { TForm1 }
  48. //=====================================================
  49. function Trimm(line : string) : string;
  50.  var i, i1, i2 : integer;
  51. begin
  52.    Result := '';                  // null the variable so it dosn't contain any data
  53.     i := Pos('/',line);           // determine exact position of forwardslash to count down from
  54.      if i = 0 then                // if no forwardslash found then ...
  55.     //=======
  56.       begin
  57.        i2 := Length(line);        // deteremine the length of this word
  58.        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.
  59.        TottalWords := TottalWords + 1;  // count normal words
  60.        Form1.StatusBar1.Panels[0].Text := 'Normal words: ' + IntToStr(TottalWords);
  61.        Form1.ListBox1.Items.Add('Normal word: ' + Result);
  62.        Form1.ListBox1.TopIndex := -1 + Form1.ListBox1.Items.Count;
  63.       end
  64.     //=======
  65.      else
  66.       begin
  67.        i1 := i;          // set i1 as end point
  68.         Repeat
  69.          Dec(i1);        // repeat decreasing by 1 bit
  70.         until (i1 < 1);  // until i1 is less then 0
  71.          Inc(i1);        // increase by 1 bit if it's less then 0
  72.     //=======
  73.      Result := Copy(line,i1,i-1);      // copy everything
  74.      TrimmedWords := TrimmedWords + 1; // count trimmed words
  75.      Form1.StatusBar1.Panels[1].Text := 'Trimmed words: ' + IntToStr(TrimmedWords);
  76.      Form1.ListBox1.Items.Add('Trimmed word: ' + Result);
  77.      Form1.ListBox1.TopIndex := -1 + Form1.ListBox1.Items.Count;
  78.       end;
  79.     //=======
  80. end;
  81. //=====================================================
  82. procedure StartToTrim;
  83.  var line, word : string;
  84. begin
  85.  //========
  86.  OriginalFile := Form1.filepath.Text;
  87.  TrimmedFile  := Form1.SaveDialog1.FileName;
  88.   Assign(OriginalText,OriginalFile);
  89.   Reset(OriginalText);
  90.   Assign(TrimmedText,TrimmedFile);
  91.   Rewrite(TrimmedText);
  92.  //========
  93.  Form1.ListBox1.Items.Clear;                  // clear all items
  94.    Repeat
  95.     ReadLn(OriginalText, line);                 // read line to "line" variable
  96.     Form1.Update;
  97.     word := Trimm(line);                       // pass the line variable to "Trimm" funcion
  98.     WriteLn(TrimmedText, word);                // write the word to a file
  99.    until EoF(OriginalText);                   // trim shit until end of file is reached
  100.  //========
  101.   Close(OriginalText);
  102.   Close(TrimmedText);
  103.  //========
  104. end;
  105. //=====================================================
  106. procedure TForm1.selectClick(Sender:TObject);
  107. begin
  108.  //========
  109.    Form1.OpenDialog1.InitialDir := GetCurrentDir;  // open the folder where this app was launched
  110.    Form1.OpenDialog1.Filter := 'Any file (*.*)|*.*|*.dic files|*.dic|Text files (*.txt)|*.txt';
  111.    Form1.OpenDialog1.FilterIndex := 2;
  112.    Form1.OpenDialog1.Options := [ofFileMustExist]; // file mus exist
  113.    Form1.OpenDialog1.Title := 'Select a dictionary';
  114.  //========
  115.     if OpenDialog1.Execute then   // if clicked on the button then show this open dialog
  116.      begin
  117.       Form1.filepath.Text := OpenDialog1.FileName;  // put full path and filename in the edit box
  118.      end;
  119.   //========
  120. end;
  121. //=====================================================
  122. procedure TForm1.trimClick(Sender:TObject);
  123. begin
  124.   if FileExists(Form1.filepath.Text) = True then
  125.    begin
  126.     Form1.SaveDialog1.InitialDir := GetCurrentDir;
  127.     Form1.SaveDialog1.Filter := 'Any file (*.*)|*.*';
  128.     Form1.SaveDialog1.Title := 'Save trimmed Dictionary';
  129.      if Form1.SaveDialog1.Execute then
  130.       begin
  131.        Form1.trim.Enabled := False;
  132.        Form1.Caption := 'DictoTrim - Trimming...';
  133.         StartToTrim;
  134.        Form1.trim.Enabled := True;
  135.        Form1.Caption := 'DictoTrim';
  136.       end;
  137.    end
  138.   else
  139.    begin
  140.     ShowMessage('File doesn''t exist!');
  141.    end;
  142. end;
  143. //=====================================================
  144. procedure TForm1.MenuItem3Click(Sender:TObject);
  145. begin
  146.  Form3.ShowModal;
  147. end;
  148. //=====================================================
  149. procedure TForm1.MenuItem2Click(Sender:TObject);
  150. begin
  151.  Form2.ShowModal;
  152. end;
  153. //=====================================================
  154. initialization
  155.   {$I dictotrim.lrs}
  156.  
  157. end.
Add Comment
Please, Sign In to add comment