Exemplo n.º 1
0
        /// <summary>
        /// Feldoglogzza CSV fájlt
        /// </summary>
        public void Parser(List <string> lines, ref string[,] table)
        {
            var coulmnsCount = 0;

            for (int rowIndex = 0; rowIndex < lines.Count; rowIndex++)
            {
                int             coulmnIndex = 0;
                var             row         = lines[rowIndex];
                string          datafield   = string.Empty;
                LineParseStates state       = LineParseStates.FIND_DATAFIELD_START_ESCAPE;

                for (int chIndex = 0; chIndex < row.Length; chIndex++)
                {
                    char ch = row[chIndex];
                    switch (state)
                    {
                    case LineParseStates.FIND_DATAFIELD_START_ESCAPE:
                    {
                        if (ch == '\"')
                        {
                            state = LineParseStates.IN_DATAFIELD;
                        }
                        break;
                    };

                    case LineParseStates.IN_DATAFIELD:
                    {
                        if (ch != '\"')
                        {
                            datafield += row[chIndex];
                        }
                        if (ch == '\"')
                        {
                            table[rowIndex, coulmnIndex] = datafield;
                            coulmnIndex++;
                            datafield = string.Empty;
                            state     = LineParseStates.FIND_SEPARATOR;

                            if (coulmnIndex > coulmnsCount)
                            {
                                coulmnsCount = coulmnIndex;
                            }
                        }
                        break;
                    }

                    case LineParseStates.FIND_SEPARATOR:
                    {
                        if (ch == ',')
                        {
                            state = LineParseStates.FIND_DATAFIELD_START_ESCAPE;
                        }
                        break;
                    }
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Visszadja a használt mezők számát.
        /// </summary>
        private int CalcColumnCount(List <string> lines)
        {
            var coulmnsCount = 0;

            for (int rowIndex = 0; rowIndex < lines.Count; rowIndex++)
            {
                int             coulmnIndex = 0;
                var             row         = lines[rowIndex];
                LineParseStates state       = LineParseStates.FIND_DATAFIELD_START_ESCAPE;

                for (int chIndex = 0; chIndex < row.Length; chIndex++)
                {
                    char ch = row[chIndex];
                    switch (state)
                    {
                    case LineParseStates.FIND_DATAFIELD_START_ESCAPE:
                    {
                        if (ch == '\"')
                        {
                            state = LineParseStates.IN_DATAFIELD;
                        }
                        break;
                    };

                    case LineParseStates.IN_DATAFIELD:
                    {
                        if (ch == '\"')
                        {
                            coulmnIndex++;
                            state = LineParseStates.FIND_SEPARATOR;

                            if (coulmnIndex > coulmnsCount)
                            {
                                coulmnsCount = coulmnIndex;
                            }
                        }
                        break;
                    }

                    case LineParseStates.FIND_SEPARATOR:
                    {
                        if (ch == ',')
                        {
                            state = LineParseStates.FIND_DATAFIELD_START_ESCAPE;
                        }
                        break;
                    }
                    }
                }
            }
            return(coulmnsCount);
        }