Exemple #1
0
    public static List <T> ReadFile <T>(string filename, bool hasheader, char delimiter, ToOBJ <T> mapper) //where T : new()
    {
        COLUMNS.MGSpan[] cols;
        List <T>         list = new List <T>(10000);

        int          linenum = 0;
        CreateObject co      = FastCreateInstance <T>();
        var          br      = new BufReader(File.OpenText(filename), 64 * 1024);
        var          line    = br.ReadLine();

        if (line.Count == 0)
        {
            return(list);
        }

        if (hasheader)
        {
            // actual col count
            int cc = CountOccurence(line, delimiter);
            if (cc == 0)
            {
                throw new Exception("File does not have '" + delimiter + "' as a delimiter");
            }
            cols = new COLUMNS.MGSpan[cc + 1];
        }
        else
        {
            cols = new COLUMNS.MGSpan[_COLCOUNT];
        }

        while (true)
        {
            try
            {
                line = br.ReadLine();
                linenum++;
                if (line.Count == 0)
                {
                    break;
                }

                var c = ParseLine(line, delimiter, cols);

                T o = (T)co();
                //new T();
                var b = mapper(o, new COLUMNS(c));
                if (b)
                {
                    list.Add(o);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("error on line " + linenum + "\r\n" + line, ex);
            }
        }

        return(list);
    }
Exemple #2
0
    private static List <T> ReadData <T>(TextReader sr, bool hasheader, int colcount, char delimiter, ToOBJ <T> mapper)
    {
        COLUMNS.MGSpan[] cols;
        List <T>         list = new List <T>(10000);

        int          linenum = 0;
        CreateObject co      = FastCreateInstance <T>();
        var          br      = new BufReader(sr, 64 * 1024);

        COLUMNS.MGSpan line = new COLUMNS.MGSpan();

        if (hasheader)
        {
            line = br.ReadLine();
            if (line.Count == 0)
            {
                return(list);
            }
            ColName = line.ToString().Split(delimiter);
            // actual col count
            int cc = CountOccurence(line, delimiter);
            if (cc == 0)
            {
                throw new Exception("File does not have '" + delimiter + "' as a delimiter");
            }
            cols = new COLUMNS.MGSpan[cc + 1];
        }
        else
        {
            cols    = new COLUMNS.MGSpan[colcount];
            ColName = new string[colcount];
        }

        while (true)
        {
            try
            {
                line = br.ReadLine();
                linenum++;
                if (line.Count == 0)
                {
                    break;
                }

                var c = ParseLine(line, delimiter, cols);

                T o = (T)co();
                //new T();
                var b = mapper(o, new COLUMNS(c));
                if (b)
                {
                    list.Add(o);
                }
            }
            catch (Exception ex)
            {
                sr.Close();
                throw new Exception("error on line " + linenum + "\r\n" + line, ex);
            }
        }

        sr.Close();
        return(list);
    }