TStringTree object

Freeware 2002 by Kjell Hasthi
email: kjehas@frisurf.no (if outdated goto: www.tradesystemlab.com)

TStringTree is a class for handling a tree-structured stringlist.

TStringTree was born out of frustration of actually using a stringTree in the the same way as stringTrees are represented in TTreeView, all the housekeeping of nodes and child objects with potential "hang errors" during creating/destroying processes.

There must be a simpler way for handling a tree of strings! And it was. TStringTree does not create new objects at runtime.

TStringTree is very similar to directory structures so I decided to use the familiar terms of "directories" and "files" instead of nodes and child nodes.


Installation

StringTree.pas is source code.
TStringTree.htm lists all available functions.
pDemo.exe is the demo program.

If you are not using Delphi 3 and your Delphi compiler will not accept the resource files, do the following to reconstruct the demo program:

1. Create a new application
2. Merge "newdemo.pas" with "unit1.pas"
3. Add two Memo components to the form (Memo1 and Memo2)
4. Add two Edit components to the form (Edit1 and Edit2). Edit1 will be used for the path argument, Edit2 for the file argument
5. Add the follwing buttons to the form
	btnShowDirTree
	btnCreateDir
	btnCreateFile
	btnGetDirTree
	btnGetDirList
	btnGetFileList
	btnGetAllFiles
	btnFindFile
	btnDeleteDir
	btnDeleteFiles
	btnLoadFromFile
	btnSaveToFile
6. Make sure all event handlers are connected to the corresponding procedures in source file.


Basic use

When you have created a stringTree add strings to it by using CreateDir and CreateFile calls:

stringList1.CreateDir( 'aaa\B\C') 
- will create a root string (=directory) 'aaa', a subdirectory 'B', and a sub-subdirectory 'C'.

stringList1.CreateFile( 'aaa\B\C', 'file1') 
- will add a string (=file) to the directory 'aaa\B\C'.

stringList1.SaveToFile( 'C:\Data.ini')
- will save stringTree data to file 'C:\Data.ini'.

stringList1.LoadFromFile( 'C:\Data.ini')
- will load stringTree data from file 'C:\Data.ini'.


Advanced use

Let say you need a dictionary representing several "classes" of strings:
- delimiter
- object
- attribute
- symbolic value
- procedure

It is easy to maintain this dictionary with TStringTree.
If you use this call for adding delimiters:
CreateFile( 'delimiter', <string>)
then you can retrieve class of <string> by using the FindFile function. FindFile will return path to <string>, which will be 'delimiter' in this case.



More about use (from the demo program):


procedure TForm1.FormCreate(Sender: TObject);
begin
     StringTree1 := TStringTree.Create;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
     StringTree1.Free;
end;

procedure TForm1.btnGetDirTreeClick(Sender: TObject);
var
   stringList: TStringList;
begin
     if StringTree1.DirExists( Edit1.Text) then begin
        stringList:= TStringList.Create;
        if StringTree1.GetDirTree( Edit1.Text, stringList) = strOK then begin
           Memo1.Lines.Assign( stringList);
        end;
        stringList.Free;
     end;
end;

procedure TForm1.btnGetFileListClick(Sender: TObject);
var
   stringList: TStringList;
   stringTreeError: TStringTreeError;
begin
     stringList := TStringList.Create;
     Memo1.Lines.Clear;
     stringTreeError := StringTree1.GetFileList( Edit1.Text, stringList);
     if stringTreeError = strOK then
           Memo1.Lines.Assign( stringList)
     else ShowMessage( StringTree1.ErrorToStr( stringTreeError));
     stringList.Free;
end;

procedure TForm1.btnFindFileClick(Sender: TObject);
var
   stringList: TStringList;
begin
     stringList := TStringList.Create;
     if StringTree1.FindFile( Edit2.Text, stringList) = strOK then begin
           Memo1.Lines.Assign( stringList);
     end;
     stringList.Free;
end;

procedure TForm1.btnDeleteDirClick(Sender: TObject);
begin
     StringTree1.DeleteDir( Edit1.Text);
end;

procedure TForm1.btnGetDirListClick(Sender: TObject);
var
   stringList: TStringList;
begin
     stringList := TStringList.Create;
     StringTree1.GetDirList( Edit1.Text, stringList);
     Memo1.Lines.Assign( stringList);
     stringList.Free;
end;

procedure TForm1.btnCreateDirClick(Sender: TObject);
begin
     StringTree1.CreateDir( Edit1.Text);
end;

procedure TForm1.btnShowDirTreeClick(Sender: TObject);
var
   stringList: TStringList;
begin
     stringList := TStringList.Create;
     StringTree1.GetDirTree( '', stringList);
     Memo1.Lines.Assign( stringList);
     stringList.Free;
end;

procedure TForm1.btnCreateFileClick(Sender: TObject);
begin
     StringTree1.CreateFile( Edit1.Text, Edit2.Text);
end;

procedure TForm1.btnLoadFromFileClick(Sender: TObject);
begin
     StringTree1.LoadFromFile( ExtractFilePath( Application.ExeName)
         + 'Data.ini');
     Memo2.Lines.LoadFromFile( ExtractFilePath( Application.ExeName)
         + 'Data.ini');
end;


procedure TForm1.btnSaveToFileClick(Sender: TObject);
begin
     if StringTree1.SaveToFile( ExtractFilePath( Application.ExeName)
         + 'Data.ini') <> strOK then
         ShowMessage( 'Save failed');
end;

procedure TForm1.btnGetAllFilesClick(Sender: TObject);
var
   stringList: TStringList;
begin
     stringList := TStringList.Create;
     StringTree1.GetAllFiles( Edit1.Text, stringList);
     Memo1.Lines.Assign( stringList);
     stringList.Free;
end;


procedure TForm1.btnDeleteFilesClick(Sender: TObject);
var
   stringTreeError: TStringTreeError;
begin
     stringTreeError := StringTree1.DeleteFiles( Edit1.Text);
     if stringTreeError <> strOK then
        ShowMessage( StringTree1.ErrorToStr( stringTreeError));
end;