using System.IO; using System; using System.Collections; using System.Text.RegularExpressions; using System.Text; class Track { public Album album; public string name; public string location; public int time; public Track( Object aa, string n, string l, int t ) { this.album = (Album)aa; this.name = n; this.location = l; this.time = t; } } class Album { public Artist artist; public string name; public Hashtable tracks; public Album( Object aa, string n ) { this.artist = (Artist)aa; this.tracks = new Hashtable(); this.name = n; } } class Artist { public string name; public Hashtable albums; public Artist( string a ) { this.albums = new Hashtable(); this.name = a; } } public class Library { private static string libloc = "Library.xml"; private static string htmlloc = "html"; private static Hashtable arg = new Hashtable(); private static Hashtable art = new Hashtable(); private static Hashtable alb = new Hashtable(); /*private static Hashtable trk = new Hashtable(); private static Hashtable artalbidx = new Hashtable();*/ private static Regex r = new Regex( "^\t\t\t" + "(?.*)" + "<.+>(?.*)<.+.>" ); private static string kval = "value"; private static string kkey = "key"; private static void checklocs() { StreamReader sr; try { sr = File.OpenText( libloc ); sr.Close(); Directory.CreateDirectory( htmlloc + "/trk" ); } catch( FileNotFoundException fne ) { Console.WriteLine( fne.Message + ": " + libloc ); System.Environment.Exit(0); } catch( Exception ex ) { Console.WriteLine( ex.Message ); return; } } public static void add_artist_album( string artist, string album ) { try { if( art[artist] == null ) { art[artist] = album; } else { int found=0; string []p = ((string)art[artist]).Split( "\0".ToCharArray() ); for( int i=0,len=p.Length ; i" ) != -1 ) { return(ht); } return(null); } public static string df( string fn ) { StringBuilder sb = new StringBuilder(); for( int i=0; i= 'a' && fn[i] <= 'z' ) || ( fn[i] >= 'A' && fn[i] <= 'Z' ) || ( fn[i] >= '0' && fn[i] <= '9' ) ) { sb.Append( fn[i] ); } else { sb.Append( "_" ); } } return( sb.ToString() ); } public static void readin() { StreamReader sr = new StreamReader( libloc, Encoding.UTF8 ); Hashtable entry = new Hashtable(); Hashtable old; int i=0; while( sr.Peek() != -1 ) { string l = sr.ReadLine(); old = entry; entry = doline(l, entry ); if( entry == null ) { // do actions process_table(old); entry = new Hashtable(); } /* if( ++i == 1000 ) { break; } */ } sr.Close(); } public static void create_artist_directory( Object o ) { Artist a = (Artist)o; try { System.IO.Directory.CreateDirectory( "./" + htmlloc + "/" + df( a.name ) ); } catch( Exception ex ) { cw( "Err+ Creating " + df( a.name ) ); System.Environment.Exit(0); } } public static string artist_link( string artist ) { StringBuilder sb = new StringBuilder(); string a = df( artist ); sb.Append( arg["artistprefix"] ); sb.Append( "" + artist + "" ); sb.Append( arg["artistpostfix"] ); return( sb.ToString() ); } public static string album_link( Object aa ) { Album a = (Album)aa; StringBuilder sb = new StringBuilder(); sb.Append( arg["albumprefix"] ); sb.Append( "" + a.name + "" ); sb.Append( arg["albumpostfix"] ); return( sb.ToString() ); } public static string track_link( Object o ) { Track t = (Track)o; Album a = t.album; StringBuilder sb = new StringBuilder(); sb.Append( arg["trackprefix"] ); sb.Append( t.name ); sb.Append( arg["trackpostfix"] ); return( sb.ToString() ); } public static void write_artist_album_list( Object o, string s ) { Artist a = (Artist)o; StreamWriter sw = new StreamWriter( htmlloc + "/" + df( a.name ) + "/index.html", false, Encoding.UTF8 ); sw.Write( arg["htmlheader"] ); sw.Write( s ); sw.Write( arg["htmlfooter"] ); sw.Flush(); sw.Close(); } public static void write_album_track_list( Object o, string s ) { Album a = (Album)o; StreamWriter sw = new StreamWriter( htmlloc + "/" + df( a.artist.name ) + "/" + df( a.name ) + ".html", false, Encoding.UTF8 ); sw.Write( arg["htmlheader"] ); sw.Write( s ); sw.Write( arg["htmlfooter"] ); sw.Flush(); sw.Close(); } public static void do_tracks( Object o ) { Album a = (Album)o; SortedList sl = new SortedList(); StringBuilder sb = new StringBuilder(); IEnumerator ie; if( (bool)arg["sorttracks"] == true ) { ie = a.tracks.Keys.GetEnumerator(); while(ie.MoveNext()) { Track t = (Track)a.tracks[ie.Current]; sl[t.name] = t; } ie = sl.Keys.GetEnumerator(); } else { ie = a.tracks.Keys.GetEnumerator(); } while(ie.MoveNext()) { Track t = (Track)a.tracks[ie.Current]; cw( " + " + t.name ); sb.Append( track_link(t) ); } write_album_track_list( a, sb.ToString() ); } public static void do_albums( Object o ) { Artist a = (Artist)o; SortedList sl = new SortedList(); StringBuilder sb = new StringBuilder(); IEnumerator ie; if( (bool)arg["sortalbums"] == true ) { ie = a.albums.Keys.GetEnumerator(); while( ie.MoveNext() ) { Album al = (Album)a.albums[ie.Current]; cw( " + " + al.name); sl[al.name] = al; } ie = sl.Keys.GetEnumerator(); } else { ie = a.albums.Keys.GetEnumerator(); } while( ie.MoveNext() ) { sb.Append( album_link( a.albums[ie.Current] ) ); do_tracks( a.albums[ie.Current] ); } write_artist_album_list( a, sb.ToString() ); } public static void sortedout() { StringBuilder sb = new StringBuilder(); SortedList sl = new SortedList(); IEnumerator ie; sb.Append( arg["htmlheader"] ); if( (bool)arg["sortartists"] == true ) { ie = art.Keys.GetEnumerator(); while( ie.MoveNext() ) { sl[ie.Current.ToString()] = ((Artist)art[ie.Current]).name; } ie = sl.Keys.GetEnumerator(); } else { ie = art.Keys.GetEnumerator(); } while( ie.MoveNext() ) { Artist a = ((Artist)art[ie.Current]); Console.WriteLine( a.name ); sb.Append( artist_link( a.name ) ); create_artist_directory( a ); // now we are ready to create the album files do_albums( a ); } StreamWriter sw = new StreamWriter( htmlloc + "/file.html", false, Encoding.UTF8 ); sb.Append( arg["htmlfooter"] ); sw.Write( sb.ToString() ); sw.Close(); } public static string file_contents( string fn ) { StringBuilder sb = new StringBuilder(); try { StreamReader sr = File.OpenText( fn ); while( sr.Peek() != -1 ) { sb.Append( sr.ReadLine() + System.Environment.NewLine ); } sr.Close(); } catch( Exception ex ) { Console.WriteLine( "Processing: " + fn ); Console.WriteLine( ex.Message ); System.Environment.Exit(1); } return( sb.ToString() ); } public static void processargs( string[] a ) { try { arg["htmlheader"] = arg["htmlfooter"] = arg["artistprefix"] = arg["artistpostfix"] = arg["albumprefix"] = arg["albumpostfix"] = arg["trackprefix"] = arg["trackpostfix"] = ""; arg["sortartists"] = arg["sortalbums"] = arg["sorttracks"] = false; StreamReader sr = File.OpenText( "config.ini" ); while( sr.Peek() != -1 ) { string s = sr.ReadLine(); if( s.StartsWith( "htmlheader" ) ) { arg["htmlheader"] = file_contents( s.Substring( s.IndexOf( " " )+1 ) ); } if( s.StartsWith( "htmlfooter" ) ) { arg["htmlfooter"] = file_contents( s.Substring( s.IndexOf( " " )+1 ) ); } if( s.StartsWith( "artistprefix" ) ) { arg["artistprefix"] = file_contents( s.Substring( s.IndexOf( " " )+1 ) ); } if( s.StartsWith( "artistpostfix" ) ) { arg["artistpostfix"] = file_contents( s.Substring( s.IndexOf( " " )+1 ) ); } if( s.StartsWith( "albumprefix" ) ) { arg["albumprefix"] = file_contents( s.Substring( s.IndexOf( " " )+1 ) ); } if( s.StartsWith( "albumpostfix" ) ) { arg["albumpostfix"] = file_contents( s.Substring( s.IndexOf( " " )+1 ) ); } if( s.StartsWith( "trackprefix" ) ) { arg["trackprefix"] = file_contents( s.Substring( s.IndexOf( " " )+1 ) ); } if( s.StartsWith( "trackpostfix" ) ) { arg["trackpostfix"] = file_contents( s.Substring( s.IndexOf( " " )+1 ) ); } if( s.StartsWith( "sortartists" ) ) { string v = s.Substring( s.IndexOf( " " )+1 ).ToLower(); arg["sortartists" ] = v.Equals( "yes" ) == true ? true : false; } if( s.StartsWith( "sortalbums" ) ) { string v = s.Substring( s.IndexOf( " " )+1 ).ToLower(); arg["sortalbums" ] = v.Equals( "yes" ) == true ? true : false; } if( s.StartsWith( "sorttracks" ) ) { string v = s.Substring( s.IndexOf( " " )+1 ).ToLower(); arg["sorttracks" ] = v.Equals( "yes" ) == true ? true : false; } } } catch( Exception ex ) { cw( "Error here" ); Console.WriteLine( ex.Message ); } } public static void Main( string[] args ) { processargs( args ); checklocs(); readin(); sortedout(); // writeout(); Console.WriteLine( "Exit" ); } }