示例#1
0
        public static List <List <string> > ReadCsvFile(string fn, Encoding enc, int maxlines = -1)
        {
            List <List <string> > lines = new List <List <string> >();
            int nline = 0;

            StreamReaderEx.OpenSteamForRead(fn, enc, sr =>
            {
                while (!sr.EndOfStream && (maxlines <= 0 || nline < maxlines))
                {
                    nline++;
                    var line        = CsvFileHelper.GetLine(sr);
                    bool hasNullVal = true;
                    foreach (var fieldVal in line)
                    {
                        if (!string.IsNullOrEmpty(fieldVal))
                        {
                            hasNullVal = false;
                            break;
                        }
                    }
                    if (!hasNullVal)
                    {
                        lines.Add(line);
                    }
                }
            });
            return(lines);
        }
示例#2
0
        private static int GetNextQuote(ref string s, int sidx, StreamReader sr)
        {
            int i = -1;

            while (i < 0)
            {
                i = s.IndexOf('"', sidx);
                if (i < 0)
                {
                    int length = s.Length;
                    s    = s + "\r\n" + sr.ReadLine();
                    sidx = length + 2;
                }
                else
                {
                    int cnt = CsvFileHelper.CountContinuousQuotes(s, i);
                    if (cnt % 2 == 1)
                    {
                        i += cnt - 1;
                    }
                    else
                    {
                        sidx = i + cnt;
                        i    = -1;
                    }
                }
            }
            return(i);
        }
示例#3
0
        private static int GetRightQuote(ref string s, int startIdx, StreamReader sr)
        {
            int nextQuote = CsvFileHelper.GetNextQuote(ref s, startIdx, sr);

            Util.Assert(CsvFileHelper.IsRightQuote(s, nextQuote));
            return(nextQuote);
        }
示例#4
0
        private static List <string> GetLine(StreamReader sr)
        {
            var line = new List <string>();
            var val  = sr.ReadLine();

            while (!string.IsNullOrEmpty(val))
            {
                string oneField = CsvFileHelper.GetOneField(ref val, sr);
                line.Add(oneField);
            }
            return(line);
        }
示例#5
0
        private static string GetOneField(ref string s, StreamReader sr)
        {
            int n = CsvFileHelper.CountContinuousQuotes(s, 0);

            if (CsvFileHelper.IsStartWithQuote(s, n))
            {
                return(CsvFileHelper.GetQuoteField(ref s, n, sr));
            }

            var rtVal = string.Empty;
            int len   = s.IndexOf(',');

            if (len < 0)
            {
                len = s.Length;
            }
            rtVal = ((len == 0) ? "" : s.Substring(0, len));
            s     = ((len >= s.Length - 1) ? "" : s.Substring(len + 1));

            return(rtVal);
        }
示例#6
0
        private static string GetQuoteField(ref string s, int n, StreamReader sr)
        {
            var fieldVal = string.Empty;

            if (n % 2 == 0)
            {
                Util.Assert(s[n] == ',');
                fieldVal = ((n == 2) ? "" : s.Substring(1, n - 2));
                s        = ((s.Length > n + 1) ? s.Substring(n + 1) : "");
            }
            else
            {
                int rightQuote = CsvFileHelper.GetRightQuote(ref s, n, sr);
                Util.Assert(rightQuote > n);
                fieldVal = s.Substring(1, rightQuote - 1);
                s        = ((s.Length > rightQuote + 2) ? s.Substring(rightQuote + 2) : "");
            }
            if (fieldVal.Length > 0 && fieldVal[0] == '\t')
            {
                fieldVal = fieldVal.Substring(1);
            }
            return(fieldVal.Replace("\"\"", "\""));
        }
示例#7
0
 public static void WriteCsvFile(List <List <string> > lines, string fn, HashSet <int> dontEncodeColumns = null)
 {
     CsvFileHelper.WriteCsvFile(lines, fn, Encoding.GetEncoding("gb2312"), dontEncodeColumns);
 }
示例#8
0
 public static List <List <string> > ReadCsvFile(string fn, int maxlines = -1)
 {
     return(CsvFileHelper.ReadCsvFile(fn, Encoding.GetEncoding("gb2312"), maxlines));
 }