Added AltSpider project to implement MySQL-based view enabled solution with different...
[LanSpider.git] / src / LanSpider / TextNetworkBrowser.cs
blob521503759f555685b3e117b4882ec57db5bedbb6
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
6 namespace LanSpider
8 /// <summary>
9 /// Fake network browser which takes information from file.
10 /// </summary>
11 internal class TextNetworkBrowser : INetworkBrowser
13 private readonly string _filename;
15 public TextNetworkBrowser( string filename )
17 _filename = filename;
20 public IEnumerable<string> GetNetworkComputers()
22 return ReadItems( 2 );
25 public IEnumerable<string> GetShares( string serverName )
27 return ReadItems( serverName, 3, true );
30 public IEnumerable<FileInfo> GetSharedFiles( string shareName )
32 return from filename in ReadItems( shareName, true )
33 select new FileInfo( filename );
36 #region Overloads
38 private IEnumerable<string> ReadItems( string mask, int clipDepth )
40 return ReadItems( mask, clipDepth, false );
43 private IEnumerable<string> ReadItems( int clipDepth )
45 return ReadItems( clipDepth, false );
48 private IEnumerable<string> ReadItems( int clipDepth, bool entriesGroupped )
50 return ReadItems( null, clipDepth, entriesGroupped );
53 private IEnumerable<string> ReadItems( string mask, bool entriesGroupped )
55 return ReadItems( mask, 0, entriesGroupped );
58 #endregion Overloads
60 private IEnumerable<string> ReadItems( string mask, int clipDepth, bool entriesGroupped )
62 List<string> items = new List<string>();
64 using ( StreamReader reader = File.OpenText( _filename ) )
66 string line;
68 while ( !( String.IsNullOrEmpty( line = reader.ReadLine() ) ) )
70 string path = line.Trim( new[] { '[', ']', ' ', '\t' } );
71 if ( String.IsNullOrEmpty( mask ) || path.StartsWith( mask ) )
73 string item;
75 if ( clipDepth > 0 )
77 int lastIndex = 0;
78 for ( int i = 0; i < clipDepth; ++i )
80 lastIndex = path.IndexOf( '\\', lastIndex + 1 );
83 item = path.Substring( 0, lastIndex );
85 else
87 item = path;
90 if ( !items.Contains( item ) )
92 items.Add( item );
95 else if ( entriesGroupped && items.Count > 0 )
97 break;
102 return items;