using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; namespace CondoAutomation.CondoCreator { public class CondoCreationInfo { public string Name; public string ShortName; public string Url; public string BusinessId; } public class Parser { public List Parse(string fileName) { try { if (string.IsNullOrEmpty(fileName)) { log("Can't parse file: file name is empty"); return new List(); } log("Start parsing file '{0}'", fileName); var lines = new List(File.ReadAllLines(fileName)).Where(s => !string.IsNullOrEmpty(s)).ToList(); var result = new List(); int num = 1; foreach (string line in lines) { var info = this.parse(line, num++); if (info != null) { result.Add(info); } } log("Parsing is finished. Number of parsed condoss: {0} (total number of rows: {1})", result.Count, num); return result; } catch (Exception x) { log("Error occured during parsing file '{0}':\n{1}\n{2}", fileName, x.Message, x.StackTrace); return new List(); } } private CondoCreationInfo parse(string line, int num) { try { if (string.IsNullOrEmpty(line)) { log("Can't parse line {0} because it is empty", num); return null; } string[] vals = line.Split(new[]{";"}, StringSplitOptions.None); if (vals.Length != 4) { log("Can't parse line {0} ({1}): it doesn't contain 4 columns", num, line); return null; } var info = new CondoCreationInfo {Name = vals[0], ShortName = vals[1], Url = vals[2], BusinessId = vals[3]}; return info; } catch (Exception x) { log("Error occured during parsing line {0} ('{1}'):\n{2}\n{3}", num, line, x.Message, x.StackTrace); return null; } } private void log(string msg, params object[] args) { try { if (args.Length > 0) { msg = string.Format(msg, args); } msg = string.Format("{0} (thread id: {1})\t{2}\n", DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss"), Thread.CurrentThread.ManagedThreadId, msg); Console.WriteLine(msg); File.AppendAllText("log_condoCreator.txt", msg); } catch { } } } }