Text Spinner Wizard

Text Spinner

Text Spinner is a wizard application which allows the user to select a input file and declare an output file and have the application perform some task (Spin) on the input file and save it as the output file.

Using the arrow buttons in the footer of the application, you can move through the pages of the wizard

First, select the “Import File” to be processed.

screenshot

Next, select the “Output File” to write the results to.

screenshot1

Then, move to the progress page to “Spin!”

screenshot2

Click the “Spin” button when your ready……

The application uses a thread to do the spinning, leaving you the capability to interact with the program (e.g. click the cancel button to stop and reset)

screenshot3

TSpinThread = class(TThread)
private
 FImportFile: string;
 FOutputFile: String;
 FProgressBar: TProgressBar;
 FMax: Integer;
 procedure SetProgressBarRange;
 procedure IncrementProgressBar;
 procedure SpinLine(const ALine: String; var ANewLine: String);
protected
 procedure Execute; override;
public
 constructor Create(const AImportFile, AOutPutFile: String; AProgressBar: TProgressBar;  OnThreadTerminate: TNotifyEvent);
end;

When the thread’s Execute method is called, it create’s a StringList and Loads the Import File into it. It then Sets the progress bar’s max property based on the number of lines in the file. Then it steps through each line in the StringList and calls the SpinLine method on the line of text in the Stringlist. After it spins the line, the Line is returned back in the var parameter “NewLine” where it is then assigned back to the StringList to be written out as the Output file.

procedure TSpinThread.Execute;
var
 ImportList: TStringList;
 I: Integer;
 NewLine: String;
begin
 ImportList := TStringList.Create;
 try
 ImportList.LoadFromFile(FImportFile);
 FMax := ImportList.Count;
 Synchronize(SetProgressBarRange);
 for I := 1 to ImportList.Count – 1 do
 begin
  SpinLine(ImportList[I], NewLine);
  ImportList[I]:= NewLine;
  Synchronize(IncrementProgressBar);
  if Terminated then
  exit;
  Sleep(1); //you can remove this line if you like
 end;
  ImportList.SaveToFile(FOutputFile);
  finally
  ImportList.Free;
 end;
end;

The SpinLine method allows you to do anything you want to the line (parse it, modify it, etc). In the example below, I am just iterating through each character in the line

procedure TSpinThread.SpinLine(const ALine: String; var ANewLine: String);

var I: Integer;
begin

 for I := 1 to Length(ALine) do
 begin
 //*****************************
//do something to each character of  ALine here
 //*****************************
 ANewLine:= ALine;
 end;
end;

DOWNLOAD IT AND GIVE IT A SPIN!  <SMILE>

Download Source and Executable

Click here to take the Delphi By Example survey

Another example of use would be to extract all words from a domain name. Read in a file of domain names, and write out a file of domain names and all the words found in the domain names. If you compare the two examples, you will see that the only difference is that I removed one page of settings from the application, and change the source code in the “Spin Line” procedure.

Download Source and Executable

Click here to take the Delphi By Example survey


Freelance Jobs

Freelance Jobs                                                                                        MVP_Logo_200x100          facebook-button-1