一个离开.NET的程序员

ryhan

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  1 DirectoryListParser
  2 using System;
  3 using System.IO;
  4 using System.Net;
  5 using System.Text.RegularExpressions;
  6 using System.Collections;
  7 using System.Collections.Generic;
  8 
  9 namespace WindowsApplicationFTP
 10 {
 11     public struct FileStruct
 12     {
 13         public string Flags;
 14         public string Owner;
 15         public bool IsDirectory;
 16         public string CreateTime;
 17         public string Name;
 18     }
 19 
 20     public enum FileListStyle
 21     {
 22         UnixStyle,
 23         WindowsStyle,
 24         Unknown
 25     }
 26 
 27     public class DirectoryListParser
 28     {
 29         private List<FileStruct> _myListArray;
 30 
 31         public FileStruct[] FullListing
 32         {
 33             get
 34             {
 35                 return _myListArray.ToArray();                
 36             }
 37         }
 38 
 39         public FileStruct[] FileList
 40         {            
 41             get
 42             {
 43                 List<FileStruct> _fileList = new List<FileStruct>();
 44                 foreach(FileStruct thisstruct in _myListArray)
 45                 {
 46                     if(!thisstruct.IsDirectory)
 47                     {
 48                         _fileList.Add(thisstruct);                        
 49                     }
 50                 }
 51                 return _fileList.ToArray();
 52             }
 53         }
 54 
 55         public FileStruct[] DirectoryList
 56         {
 57             get
 58             {
 59                 List<FileStruct> _dirList = new List<FileStruct>();
 60                 foreach(FileStruct thisstruct in _myListArray)
 61                 {
 62                     if(thisstruct.IsDirectory)
 63                     {
 64                         _dirList.Add(thisstruct);                        
 65                     }
 66                 }
 67                 return _dirList.ToArray();
 68             }
 69         }
 70 
 71         public DirectoryListParser(string responseString)
 72         {
 73             _myListArray = GetList(responseString);            
 74         }
 75         
 76         private List<FileStruct> GetList(string datastring)
 77         {
 78             List<FileStruct> myListArray = new List<FileStruct>(); 
 79             string[] dataRecords = datastring.Split('\n');
 80             FileListStyle _directoryListStyle = GuessFileListStyle(dataRecords);
 81             foreach (string s in dataRecords)
 82             {
 83                 if (_directoryListStyle != FileListStyle.Unknown && s != "")
 84                 {
 85                     FileStruct f = new FileStruct();
 86                     f.Name = "..";
 87                     switch (_directoryListStyle)
 88                     {
 89                         case FileListStyle.UnixStyle:                            
 90                             f = ParseFileStructFromUnixStyleRecord(s);
 91                             break;
 92                         case FileListStyle.WindowsStyle:
 93                             f = ParseFileStructFromWindowsStyleRecord(s);
 94                             break;
 95                     }
 96                     if (f.Name != "" && f.Name != "." && f.Name != "..")
 97                     {
 98                         myListArray.Add(f);     
 99                     }
100                 }
101             }
102             return myListArray;
103         }
104         
105         private FileStruct ParseFileStructFromWindowsStyleRecord(string Record)
106         {
107             ///Assuming the record style as 
108             /// 02-03-04  07:46PM       <DIR>          Append
109             FileStruct f = new FileStruct();
110             string processstr = Record.Trim();
111             string dateStr = processstr.Substring(0,8);      
112             processstr = (processstr.Substring(8, processstr.Length - 8)).Trim();
113             string timeStr = processstr.Substring(0, 7);
114             processstr = (processstr.Substring(7, processstr.Length - 7)).Trim();
115             f.CreateTime = dateStr + " " + timeStr;
116             if (processstr.Substring(0,5) == "<DIR>")
117             {
118                 f.IsDirectory = true;    
119                 processstr = (processstr.Substring(5, processstr.Length - 5)).Trim();
120             }
121             else
122             {
123                 string[] strs = processstr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
124                 processstr = strs[1];
125                 f.IsDirectory = false;
126             }
127             f.Name = processstr;  //Rest is name   
128             return f;
129         }
130         
131         public FileListStyle GuessFileListStyle(string[] recordList)
132         {
133             foreach (string s in recordList)
134             {
135                 if(s.Length > 10 
136                     && Regex.IsMatch(s.Substring(0,10),"(-|d)((-|r)(-|w)(-|x)){3}"))
137                 {
138                     return FileListStyle.UnixStyle;
139                 }    
140                 else if (s.Length > 8 
141                     && Regex.IsMatch(s.Substring(0, 8),  "[0-9]{2}-[0-9]{2}-[0-9]{2}"))
142                 {
143                     return FileListStyle.WindowsStyle;
144                 }
145             }
146             return FileListStyle.Unknown;
147         }
148         
149         private FileStruct ParseFileStructFromUnixStyleRecord(string record)
150         {
151             ///Assuming record style as
152             /// dr-xr-xr-x   1 owner    group               0 Nov 25  2002 bussys
153             FileStruct f = new FileStruct();
154             if (record[0] == '-' || record[0] == 'd')
155             {// its a valid file record
156                 string processstr = record.Trim();
157                 f.Flags = processstr.Substring(0, 9);
158                 f.IsDirectory = (f.Flags[0] == 'd');
159                 processstr = (processstr.Substring(11)).Trim();
160                 _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);   //skip one part
161                 f.Owner = _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);
162                 f.CreateTime = getCreateTimeString(record);
163                 int fileNameIndex = record.IndexOf(f.CreateTime)+f.CreateTime.Length;
164                 f.Name = record.Substring(fileNameIndex).Trim();   //Rest of the part is name                
165             }
166             else
167             {
168                 f.Name = "";
169             }
170             return f;
171         }
172 
173         private string getCreateTimeString(string record)
174         {
175             //Does just basic datetime string validation for demo, not an accurate check
176             //on date and time fields
177             string month = "(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)";
178             string space = @"(\040)+";
179             string day = "([0-9]|[1-3][0-9])";
180             string year = "[1-2][0-9]{3}";
181             string time = "[0-9]{1,2}:[0-9]{2}";            
182             Regex dateTimeRegex = new Regex(month+space+day+space+"("+year+"|"+time+")", RegexOptions.IgnoreCase);
183             Match match = dateTimeRegex.Match(record);
184             return match.Value;
185         }    
186     
187         private string _cutSubstringFromStringWithTrim(ref string s, char c, int startIndex)
188         {
189             int pos1 = s.IndexOf(c, startIndex);
190             string retString = s.Substring(0,pos1);
191             s = (s.Substring(pos1)).Trim();
192             return retString;
193        }
194 
195     }
196 }
from : http://www.cnblogs.com/lidaohang/archive/2010/06/25/1764864.html
posted on 2012-07-25 15:39  ryhan  阅读(621)  评论(0)    收藏  举报