Posts


I am reading through a chapter on binary trees. The first simple example they give is traversing through directory structures. As the book is for java the attached file is the example translated to c#.

Once I have finished the chapter I will upload the binary tree example translated to c#.

Here is an example postorder recursion through a directory tree (attachment main.cs). See attachment Main.cs for a different example.

using System.IO;

class RecurseDirectory
{
	public static void Main(string[] args)
	{
		RecurseDirectory r = new RecurseDirectory();
		RecurseDirectory.PrintDirsRecursively(new System.IO.DirectoryInfo(@"\\Directory\Location"), 0);

		Console.Read();
	}
	
	
	public static void PrintDirsRecursively(System.IO.DirectoryInfo source, int depth) 
	{
		foreach (System.IO.DirectoryInfo dir in source.GetDirectories())
		{
			PrintTabs(depth);
			System.Console.WriteLine(source.FullName);
			PrintDirsRecursively(dir, depth+1);
		}
		foreach (System.IO.FileInfo file in source.GetFiles())
		{
			PrintTabs(depth);
			System.Console.WriteLine(file.FullName);
		}
	}


	private static void PrintTabs(int depth)
	{
		for (int j=0; j < depth; j++)
		{
			System.Console.Write("\t");
		}
	}
}

Here is a simple recursive directory/file copy function.

using System.IO;

public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target) 
{
    foreach (DirectoryInfo dir in source.GetDirectories())
    {
        CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name));
    }
    foreach (FileInfo file in source.GetFiles())
    {
        file.CopyTo(Path.Combine(target.FullName, file.Name), true); //overwrite
    }
}

I am rereading some chapters in “Data Structures & Problem Solving Using Java”. I just finished reading chapter 17 on linked lists. Here is the singly linked list and sorted linked list from this chapter done in c#.

LinkedList - the list itself SortedLinkedList - the sorted linked list ListNode - represents the node LinkedListIterator - represents the position

using System;
using System.Collections.Generic;

namespace LinkedLists
{
	public class ListNode<T>
	{
		private T elem;
		private ListNode<T> nextNode;
		
		public T Element
		{
			get {return elem;}
			set {elem = value;}
		}
		
		public ListNode<T> Next
		{
			get {return nextNode;}
			set {nextNode = value;}
		}
		
		public ListNode(T theElement) : 
			this(theElement, null)
		{
		}
		
		public ListNode(T theElement, ListNode<T> n)
		{
			elem = theElement; nextNode = n;
		}
		
		
	}
	
	public class LinkedListIterator<T>
	{
		private ListNode <T> cur;
		
		public LinkedListIterator(ListNode<T> theNode)
		{
			cur = theNode;
		}
		
		public ListNode<T> Current
		{
			get{return cur;}
			set{cur = value;}
		}
		
		public bool IsValid()
		{
			return cur != null;
		}
		
		public T Retrieve()
		{
			if ( IsValid() )
			{ 
				return cur.Element;
			}
			else
			{
				return default(T); //null;
			}
		}
		
		public void Advance()
		{
			if (IsValid())
			{
				cur	= cur.Next;
			}
		}
	}
	
	public class LinkedList<T>
	{
		private ListNode<T> header;
		
		/// <summary>
		/// Construct the List.
		/// </summary>
		public LinkedList()
		{
			this.header = new ListNode<T> (default(T)); //(null);
		}
		
		public bool IsEmpty()
		{
			return this.header.Next == null;
		}
		
		public void MakeEmpty()
		{
			this.header.Next = null;
		}
		
		/// <summary>
		/// Return an iterator representing the header node.
		/// </summary>
		/// <returns>
		/// A <see cref="LinkedListIterator"/>
		/// </returns>
		public LinkedListIterator<T> Zeroth()
		{
			return new LinkedListIterator<T> (this.header);
		}	
		
		/// <summary>
		/// Return an iterator representing the first node in the list.
		/// </summary>
		/// <returns>
		/// A <see cref="LinkedListIterator"/>
		/// </returns>
		public LinkedListIterator<T> First()
		{
			return new LinkedListIterator<T> (this.header.Next);
		}
		
		public LinkedListIterator<T> Find(T x)
		{
			ListNode<T> itr = this.header;
			
			while(itr != null && !itr.Element.Equals(x))
			{
				itr = itr.Next;
			}
			
			return new LinkedListIterator<T>(itr);
		}
		
		/// <summary>
		/// Remove the first occurrence of an item.  The code is not foolproof.
		/// There may be two iterators, and one can left dangling if the other
		/// removes a node.
		/// </summary>
		/// <param name="x">
		/// A <see cref="T"/>
		/// </param>
		public void Remove(T x)
		{
			LinkedListIterator<T> p = FindPrevious(x);
			if(p.Current.Next != null)
			{
				p.Current.Next = p.Current.Next.Next;
			}
		}
		
		/// <summary>
		/// Return iterator prior to the first node containing an item.
		/// </summary>
		/// <param name="x">
		/// A <see cref="T"/>
		/// </param>
		/// <returns>
		/// A <see cref="LinkedListIterator"/>
		/// </returns>
		public LinkedListIterator<T> FindPrevious(T x)
		{
			ListNode<T> itr = this.header;
			
			while (itr.Next != null && !itr.Next.Element.Equals(x))
			{
				itr = itr.Next;
			}
			return new LinkedListIterator<T>(itr);
		}
		
		public void Insert(T x, LinkedListIterator<T> p)
		{
			if(p != null && p.Current != null)
			{
				p.Current.Next = new ListNode<T>(x, p.Current.Next);
			}
		}
		
		public static void PrintList(LinkedList<T> theList)
		{
			if (theList.IsEmpty())
			{
				System.Console.WriteLine("Empty List");
			}
			else
			{
				LinkedListIterator<T> itr = theList.First();
				for( ; itr.IsValid(); itr.Advance())
				{
					System.Console.WriteLine(itr.Retrieve());
				}
			}
			
			System.Console.WriteLine("");
		}
	}
	
	public class SortedLinkedList<T> : LinkedList<T>
	{
		public new void Insert(T x, LinkedListIterator<T> p)
		{
			Insert(x);
		}
		
		public void Insert(T x)
		{
			LinkedListIterator<T> prev = Zeroth();
			LinkedListIterator<T> curr = First();
			
			//while(curr.IsValid() && ( x.CompareTo(curr.Retrieve() ) > 0) )
			while(curr.IsValid() && (!EqualityComparer<T>.Default.Equals(x, curr.Retrieve())) ) 
			{
				prev.Advance();
				curr.Advance();
			}
			
			base.Insert(x, prev);
		}
	}


        class MainClass
	{
		public static void Main(string[] args)
		{
			
			
	        LinkedList<int> theList = new LinkedList<int>( );
	        LinkedListIterator<int> theItr;
	        int i;
	
	        theItr = theList.Zeroth( );
	        theList.PrintList(theList);
	
	        for( i = 0; i < 10; i++ )
	        {
	            theList.Insert( i , theItr );
	            theList.PrintList( theList );
	            theItr.Advance( );
	        }
	        
	
	        for( i = 0; i < 10; i += 2 )
			{
	            theList.Remove( i );
			}
	
	        for( i = 0; i < 10; i++ )
			{
	            if( ( i % 2 == 0 ) == ( theList.Find( i ).IsValid( ) ) )
				{
	                Console.WriteLine( "Find fails!" );
				}
			}
	
	        Console.WriteLine( "Finished deletions" );
	        theList.PrintList( theList );
		}
	}

}



Looking through the code I have for the .NET dreamhost library I can now say that I have 100% of the private server API covered. Unless I have overlooked something, as far as I can tell I only have the Announcement list left to do.

This has been an interesting project and a nice introduction to a few simple aspects of .NET programming. Once the announcement API is finished I am not sure if I will continue with this project. If I do continue I will clean up the code somewhat and make the error reporting more useful.

The example user interface that I have been programming along with the library is also coming along. It does not implement all the library features yet but you can adjust your memory usage, list private server history and stats, list: users, domains, dns information.

Still to be done with the GUI is to is: save user information in a configuration file and load it when the program opens, setting most of the private server settings, announcements, and adding and removing DNS.


A couple of months ago I sort of forked off a version of oggconvert and called it video2theora. Basically what I did was take the oggconverts projects glade file as a base and instead of using gstreamer I used ffmpeg to create a program that would allow converting video files to theora videos for use with the new firefox and opera video tags.

I hope this is only a temporary situation until gstreamer codecs on windows is in better shape. I attempted to get oggconvert to work on windows following the instructions on the website but gstreamer kept crashing. Hopefully this will improve in the future. Until then I will be using the gui based on ffmpeg.