Page 1 of 1

Creating a treeview out of directories listed in a text file

Posted: Thu Sep 29, 2005 5:05 am
by GeertVc
Hi,

Recently, I needed a way to represent directories -which were given in a text file- in a tree format.

The text file contained the following info (partly given):

Code: Select all

.
.
.
U:\chandec\DE_MGR\sub\avplf\comp\avpacx
U:\chandec\DE_MGR\sub\avplf\comp\avpacxop
U:\chandec\DE_MGR\sub\avplf\comp\avpamx
U:\chandec\DE_MGR\sub\avplf\comp\avpamxop
U:\chandec\DE_MGR\sub\avplf\comp\avpaoutdrv
U:\chandec\DE_MGR\sub\avplf\prod\avpapd
U:\chandec\DE_MGR\sub\avplf\comp\avpapdclbs
U:\chandec\DE_MGR\sub\avplf\comp\avpasw
U:\chandec\DE_MGR\sub\avplf\prod\avpcapc
U:\chandec\DE_MGR\sub\ceinfra\comp\ceisbspeth
U:\chandec\DE_MGR\sub\ceinfra\comp\ceiscon
U:\chandec\DE_MGR\sub\ceinfra\comp\ceisconnect
U:\chandec\DE_MGR\sub\ceinfra\comp\ceisdef
U:\chandec\DE_MGR\sub\ceinfra\comp\ceiserr
U:\chandec\DE_MGR\sub\osinfra\comp\istmr
U:\chandec\DE_MGR\sub\osinfra\comp\isvip
U:\chandec\DE_MGR\sub\osinfra\comp\iswdog
U:\chandec\DE_MGR\sub\avplf\comp\jagasw
U:\chandec\DE_MGR\sub\avplf\comp\jagaswfd
U:\chandec\DE_MGR\sub\avplf\comp\jagaudio
U:\chandec\DE_MGR\sub\avplf\comp\jagaudiofd
U:\chandec\DE_MGR\sub\avplf\comp\jagprop
U:\chandec\DE_MGR\sub\avplf\prod\jagsaw
U:\chandec\DE_MGR\sub\avplf\prod\jagsawfd
U:\chandec\DE_MGR\sub\avplf\comp\jagscl1
U:\chandec\DE_MGR\sub\avplf\comp\jagscl2
U:\chandec\DE_MGR\sub\avplf\prod\jagsndac
U:\chandec\DE_MGR\sub\avplf\prod\jagsndmut
U:\chandec\DE_MGR\sub\avplf\prod\jagsndreg
.
.
.
As you can see, the lines are randomly given, so no order (yet).

This is how I tackled the problem: the backslashes are my sync points.
  1. Read the text file into a wxArrayString.
  2. Sort the array
  3. Get all c files on a line-by-line base from the path given by the text file and put them in a wxArrayString too.
  4. Apply the following function on each and every line of the wxArrayString containing a list of complete paths, inclusive the c files located in those paths:

    Code: Select all

    //Populates the tree on a line-by-line base.  
    //Line is given by scanning the array containing all Components lines.
    void QacDlg::PopulateTree( const wxString& inputstring, const wxTreeItemId& parent )
    {
        int EndPos = inputstring.Find( "\\" );
        wxString NewSubString;
        wxString NewCtrlString;
    
        if ( -1 != EndPos )
        {
            //Found bslash, otherwise EndPos would be -1.
    
            //1. Take subpart out of the incoming string
            NewSubString = inputstring.Mid( 0, EndPos );
    
            //2. Prepare remainder string for next recursive access of the function
            NewCtrlString = inputstring.Mid( EndPos + 1 );
    
            if ( trectlComponentOverview->ItemHasChildren( parent ) )
            {
                //So, there's at least one more child...
                //Get the label of the child.
                //Therefore, first get the ID of the LAST child.  This is the last item of the NEXT level
                wxTreeItemId CurrentChild = trectlComponentOverview->GetLastChild( parent );
                wxString ChildLabel;
    
                if ( CurrentChild.IsOk() )
                {
                    ChildLabel = trectlComponentOverview->GetItemText( CurrentChild );
                }
                else
                {
                    wxMessageBox( _T( "Not possible to retrieve child label" )
                                , _T( "Error - Child label retrieving" )
                                , ( wxOK | wxICON_ERROR )
                                , this
                                );
                }
    
                if ( 0 == ChildLabel.Cmp( NewSubString ) )
                {
                    PopulateTree( NewCtrlString, CurrentChild );
                }
                else
                {
                    wxTreeItemId NewChild = trectlComponentOverview->AppendItem( parent, NewSubString );
                    PopulateTree( NewCtrlString, NewChild );
                }
            }
            else
            {
                //Add the next child to the tree and if successful, re-enter the function,
                //since we have to check furter to see if there's still a bslash present.
                wxTreeItemId Child = trectlComponentOverview->AppendItem( parent, NewSubString );
    
                if ( Child.IsOk() )
                {
                    PopulateTree( NewCtrlString, Child );
                }
                else
                {
                    wxMessageBox( _T( "Not possible to create a new child" )
                                , _T( "Error - Child creation" )
                                , ( wxOK | wxICON_ERROR )
                                , this
                                );
                }
            }
        }
        else
        {
            //Here we add the c file to the tree (as a leave)
            //Since we sorted the list, we're save by just appending the c-file to the last tree child
            wxTreeItemId Child = trectlComponentOverview->AppendItem( parent, inputstring );
    
            if ( !Child.IsOk() )
            {
                wxMessageBox( _T( "Adding next child failed!" )
                            , _T( "Error - Adding child" )
                            , ( wxOK | wxICON_ERROR )
                            , this
                            );
            }
        }
    }
Best rgds,

--Geert