Example #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);
    }
Example #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);
    }
Example #3
0
 public static List <T> ReadStream <T>(TextReader sr, int colcount, char delimiter, ToOBJ <T> mapper)
 {
     return(ReadData(sr, false, colcount, delimiter, mapper));
 }
Example #4
0
 public static List <T> ReadStream <T>(TextReader sr, bool hasheader, char delimiter, ToOBJ <T> mapper)
 {
     return(ReadData(sr, hasheader, _COLCOUNT, delimiter, mapper));
 }
Example #5
0
 public static List <T> ReadFile <T>(string filename, int colcount, char delimiter, ToOBJ <T> mapper)
 {
     return(ReadData(File.OpenText(filename), false, colcount, delimiter, mapper));
 }
Example #6
0
 public static List <T> ReadFile <T>(string filename, bool hasheader, char delimiter, ToOBJ <T> mapper)
 {
     return(ReadData(File.OpenText(filename), hasheader, _COLCOUNT, delimiter, mapper));
 }