// project created on 28.04.2006 at 13:42
using System;
using System.IO;

namespace uebung2_gr2
{

    class Song
    {
        public string Artist;
        public string Title;
        public string Album;
        public int    Rate;
        public string Path;
    }

    class LinkNode
    {
        public LinkNode prev;
        public LinkNode next;
        public Song     data;
        }

    class MainClass
        {

        public static string SRC_PATH = "/home/cbx/FH/SE_2006_SS/web/mp3liste_unsorted_web.txt";
        
//****************************************************************************************************
            
        public static void Main(string[] args)
            {
            LinkNode TreeRoot;
            
                Console.WriteLine("Hello World!");
                
                TreeRoot = MP3FileLoadToTree(SRC_PATH);
                
                if (null == TreeRoot)
                    {
                    Console.WriteLine("Voll krass die Scheisse");
                    return;
                    }
                    
                // MP3TreeIntraverse(TreeRoot);
                
                LinkNode ListHead = new LinkNode();
                LinkNode ListCur  = ListHead;
                
                MP3ListFromTree(TreeRoot, ref ListCur);
                
                for (ListCur = ListHead.next ; ListCur != null ; ListCur = ListCur.next )
                    {
                    MP3RecordShow(ListCur.data);
                    }
            }

//****************************************************************************************************
            
        static public LinkNode MP3FileLoadToTree(string strPfad)
            {
            StreamReader inStream;
            
            LinkNode Root = null;

            try
                {
                inStream = new StreamReader(strPfad);
                }
            catch (FileNotFoundException ex)
                {
                Console.WriteLine("No file found. It wont work, sorry.");
                return null;
                }
            
            string strLine;
            
            // Titelzeile
            inStream.ReadLine();
            
            while ( (strLine = inStream.ReadLine()) != null ) 
                {
                Song curSong;
                curSong = MP3RecordParse(strLine);
                
                Root = MP3TreeAdd( Root, curSong);
                }
                
            inStream.Close();
            
            return Root;
            }
            

//****************************************************************************************************

public static LinkNode MP3TreeAdd(LinkNode Root, Song newSong)
    {
    
    if (Root == null)
        {
        // jetzt erst den neuen Linkknoten anlegen
        LinkNode newNode = new LinkNode();
        newNode.data     = newSong;
        return newNode;
        }
    else
        {
        if (string.Compare( Root.data.Title, newSong.Title, true ) < 0)
            {
            // ist grösser: rechts / next anhängen
            Root.next = MP3TreeAdd(Root.next, newSong);
            }
        else
            {
            // ist grösser: links / prev anhängen
            Root.prev = MP3TreeAdd(Root.prev, newSong);
            }
        return Root;    
        }
    }
    
//****************************************************************************************************
    
        public static void MP3TreeIntraverse(LinkNode Root)
            {
            if (Root != null)
                {
                // ganz nach links
                MP3TreeIntraverse(Root.prev);
                MP3RecordShow(Root.data);
                MP3TreeIntraverse(Root.next);       
                }
            }

//****************************************************************************************************
        public static void MP3ListFromTree(LinkNode Root, ref LinkNode ListCur)
            {
            
            if (Root != null)
                {
                // ganz nach links
                MP3ListFromTree(Root.prev, ref ListCur);
                
                LinkNode newListNode = new LinkNode();
                
                newListNode.data = Root.data;
                ListCur.next     = newListNode;
                newListNode.prev = ListCur;
                ListCur          = newListNode;
                
                MP3ListFromTree(Root.next, ref ListCur);
                }
            }

//****************************************************************************************************
                    
        static public Song MP3RecordParse(string strLine)
            {
            string[] astrElements;
            Song newSong = new Song();

            astrElements = strLine.Split('\t');

            newSong.Artist = astrElements[0].Trim('"');
            newSong.Title  = astrElements[1].Trim('"');
            newSong.Album  = astrElements[2].Trim('"');
            newSong.Path   = astrElements[4].Trim('"');
            
            if (astrElements[3].Length > 6)
                {
                newSong.Rate = Convert.ToInt32( astrElements[3].Split('K')[0].TrimStart('"') );
                }
            else
                {
                newSong.Rate = 128;
                }
                
            return newSong;
            }

//****************************************************************************************************
            
        static public void MP3RecordShow(Song thisSong)
            {
            Console.WriteLine(">{0}< by {2} @ {1} KBps",thisSong.Title, thisSong.Rate, thisSong.Artist );
            }


        } // class
    } // namespace
    
    
    
    
    
    
    
    

syntax highlighted by Code2HTML, v. 0.9.1