示例#1
0
 public CSVParser(String fileDir, string targetDate)
 {
     this.itemsList = new List <RowItem>();
     this.maxRow    = new RowItem();
     this.takes     = 0;
     this.Parse(fileDir, targetDate, true);
 }
示例#2
0
 public void saveLastItem(RowItem lastRow)
 {
     using (StreamWriter writer = new StreamWriter(@"lastitem.json", false))
     {
         writer.Write(JsonConvert.SerializeObject(lastRow));
         writer.Close();
     }
 }
示例#3
0
        public bool Equals(RowItem obj)
        {
            DateTime thisDateTime = DateTime.Parse((String)this.inputDateTime);
            DateTime objDateTime  = DateTime.Parse((String)obj.inputDateTime);
            int      result       = DateTime.Compare(thisDateTime, objDateTime);

            if (result == 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
示例#4
0
        public bool Greater(RowItem obj)
        {
            DateTime thisDateTime = DateTime.Parse((String)this.inputDateTime);
            DateTime objDateTime  = DateTime.Parse((String)obj.inputDateTime);
            int      result       = DateTime.Compare(thisDateTime, objDateTime);

            if (result < 0)
            {
                return(false);
            }
            else if (result > 0)
            {
                return(true);
            }
            else
            {
                return(this.id > obj.id);
            }
        }
示例#5
0
        private void Parse(String fileDir, string targetDate, Boolean withLastItem)
        {
            bool         rowFound = false;
            StreamReader reader   = null;
            RowItem      lastRow  = new RowItem();

            if ((File.Exists(@"lastitem.json")) && (withLastItem == true))
            {
                reader  = new StreamReader(File.Open(@"lastitem.json", FileMode.Open, FileAccess.Read));
                lastRow = JsonConvert.DeserializeObject <RowItem>(reader.ReadToEnd());
                reader.Close();
            }
            String fullPath = "";

            if (targetDate == null)
            {
                fullPath = fileDir + "GKMS.csv";
            }
            else
            {
                fullPath = fileDir + "GK" + DateTime.Parse(targetDate).ToString("ddMMyyyy") + ".csv";
            }

            if (this.takes < 31 || !withLastItem)
            {
                this.takes++;
                if (File.Exists(fullPath))
                {
                    FileStream fileStreamIn = File.Open(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    FileInfo   fi           = new FileInfo(fullPath);
                    //reader = new StreamReader(fileStreamIn);
                    byte[]   bytes = new byte[fi.Length];
                    string[] lines;
                    int      bytesRead  = fileStreamIn.Read(bytes, 0, bytes.Length);
                    String   fileString = System.Text.Encoding.ASCII.GetString(bytes);
                    lines = fileString.Split('\n');

                    if (this.maxRow == null && lastRow != null)
                    {
                        this.maxRow = lastRow;
                    }
                    foreach (String eachRow in lines) //(!reader.EndOfStream)
                    {
                        if (!String.IsNullOrWhiteSpace(eachRow) && !eachRow.Contains("Event date"))
                        {
                            var     line = eachRow;
                            RowItem row  = new RowItem(line);
                            if (row.Equals(lastRow) || lastRow.id == 0)
                            {
                                rowFound = true;
                            }
                            if (row.Greater(lastRow))
                            {
                                this.itemsList.Add(row);
                            }
                            if (row.Greater(this.maxRow))
                            {
                                this.maxRow = row;
                            }
                        }
                    }
                    fileStreamIn.Dispose();
                }

                if (!rowFound)
                {
                    DateTime startDate = DateTime.Today;
                    if (targetDate != null)
                    {
                        startDate = DateTime.Parse(targetDate);
                    }
                    startDate = startDate.AddDays(-1);
                    //String targetFileDate = startDate.ToString("ddMMyyyy");
                    this.Parse(fileDir, startDate.ToString(), true);
                }
            }
            else
            {
                this.Parse(fileDir, targetDate, false);
            }
        }