コード例 #1
0
        /// <summary>
        /// Reads a row of data from a CSV file
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public bool ReadRow(CsvRow row)
        {
            row.LineText = ReadLine();
            if (String.IsNullOrEmpty(row.LineText))
            {
                return(false);
            }

            int pos  = 0;
            int rows = 0;

            while (pos < row.LineText.Length)
            {
                string value;

                // Special handling for quoted field
                if (row.LineText[pos] == '"')
                {
                    // Skip initial quote
                    pos++;

                    // Parse quoted value
                    int start = pos;
                    while (pos < row.LineText.Length)
                    {
                        // Test for quote character
                        if (row.LineText[pos] == '"')
                        {
                            // Found one
                            pos++;

                            // If two quotes together, keep one
                            // Otherwise, indicates end of value
                            if (pos >= row.LineText.Length || row.LineText[pos] != '"')
                            {
                                pos--;
                                break;
                            }
                        }
                        pos++;
                    }
                    value = row.LineText.Substring(start, pos - start);
                    value = value.Replace("\"\"", "\"");
                }
                else
                {
                    // Parse unquoted value
                    int start = pos;
                    while (pos < row.LineText.Length && row.LineText[pos] != ',')
                    {
                        pos++;
                    }
                    value = row.LineText.Substring(start, pos - start);
                }

                // Add field to list
                if (rows < row.Count)
                {
                    row[rows] = value;
                }
                else
                {
                    row.Add(value);
                }
                rows++;

                // Eat up to and including next comma
                while (pos < row.LineText.Length && row.LineText[pos] != ',')
                {
                    pos++;
                }
                if (pos < row.LineText.Length)
                {
                    pos++;
                }
            }
            // Delete any unused items
            while (row.Count > rows)
            {
                row.RemoveAt(rows);
            }

            // Return true if any columns read
            return(row.Count > 0);
        }
コード例 #2
0
ファイル: ReadWriteCsv.cs プロジェクト: dfcvgb1234/StudyLock
        public bool ReadRow(CsvRow row)
        {
            row.LineText = ReadLine();
            if (String.IsNullOrEmpty(row.LineText))
            {
                return(false);
            }

            int pos  = 0;
            int rows = 0;

            while (pos < row.LineText.Length)
            {
                string value;

                if (row.LineText[pos] == '"')
                {
                    pos++;

                    int start = pos;
                    while (pos < row.LineText.Length)
                    {
                        if (row.LineText[pos] == '"')
                        {
                            pos++;

                            if (pos >= row.LineText.Length || row.LineText[pos] != '"')
                            {
                                pos--;
                                break;
                            }
                        }
                        pos++;
                    }
                    value = row.LineText.Substring(start, pos - start);
                    value = value.Replace("\"\"", "\"");
                }
                else
                {
                    int start = pos;
                    while (pos < row.LineText.Length && row.LineText[pos] != ',')
                    {
                        pos++;
                    }
                    value = row.LineText.Substring(start, pos - start);
                }

                if (rows < row.Count)
                {
                    row[rows] = value;
                }
                else
                {
                    row.Add(value);
                }
                rows++;

                while (pos < row.LineText.Length && row.LineText[pos] != ',')
                {
                    pos++;
                }
                if (pos < row.LineText.Length)
                {
                    pos++;
                }
            }
            while (row.Count > rows)
            {
                row.RemoveAt(rows);
            }

            return(row.Count > 0);
        }
コード例 #3
0
ファイル: ReadWriteCsv.cs プロジェクト: deeferg/UnityJumper
        public bool ReadRow(CsvRow row)
        {
            row.LineText = ReadLine();
            if (String.IsNullOrEmpty(row.LineText))
            {
                return(false);
            }

            int pos  = 0;
            int rows = 0;

            while (pos < row.LineText.Length)
            {
                string value;
                //Handling for quotations
                if (row.LineText [pos] == '"')
                {
                    pos++;                    //Skip initial quote
                    //Parse Quoted value
                    int start = pos;
                    while (pos < row.LineText.Length)
                    {
                        if (row.LineText[pos] == '"')
                        {
                            pos++;
                            //If two quotes together, keep one
                            //Otherwise, end of value reached
                            if (pos >= row.LineText.Length || row.LineText[pos] != '"')
                            {
                                pos--;
                                break;
                            }                    //End of second if in while
                        }                        //End of First if in while
                        pos++;
                    }                            //End of while loop
                    value = row.LineText.Substring(start, pos - start);
                    value = value.Replace("\"\"", "\"");
                }                        //End of first if in first while
                else
                {
                    //Parse unquoted value
                    int start = pos;
                    while (pos < row.LineText.Length && row.LineText [pos] != ',')
                    {
                        pos++;
                    }
                    value = row.LineText.Substring(start, pos - start);
                }                        //End of else
                                         //Add field to list
                if (rows < row.Count)
                {
                    row [rows] = value;
                }
                else
                {
                    row.Add(value);
                }
                rows++;
                //Eat up to and including next comma
                while (pos < row.LineText.Length && row.LineText [pos] != ',')
                {
                    pos++;
                }
                if (pos < row.LineText.Length)
                {
                    pos++;
                }
            }                    //End of while
                                 //Delete any unused items
            while (row.Count > rows)
            {
                row.RemoveAt(rows);
            }

            return(row.Count > 0);
        }                //End of ReadRow
コード例 #4
0
        public bool ReadRow(CsvRow row)
        {
            row.LineText = this.ReadLine();
            if (string.IsNullOrEmpty(row.LineText))
            {
                return(false);
            }
            int i   = 0;
            int num = 0;

            while (i < row.LineText.Length)
            {
                string text;
                if (row.LineText[i] == '"')
                {
                    i++;
                    int num2 = i;
                    for (; i < row.LineText.Length; i++)
                    {
                        if (row.LineText[i] != '"')
                        {
                            continue;
                        }
                        i++;
                        if (i < row.LineText.Length && row.LineText[i] == '"')
                        {
                            continue;
                        }
                        i--;
                        break;
                    }
                    text = row.LineText.Substring(num2, i - num2);
                    text = text.Replace("\"\"", "\"");
                }
                else
                {
                    int num3 = i;
                    for (; i < row.LineText.Length && row.LineText[i] != ','; i++)
                    {
                    }
                    text = row.LineText.Substring(num3, i - num3);
                }
                if (num < row.Count)
                {
                    ((List <string>)row)[num] = text;
                }
                else
                {
                    row.Add(text);
                }
                num++;
                for (; i < row.LineText.Length && row.LineText[i] != ','; i++)
                {
                }
                if (i < row.LineText.Length)
                {
                    i++;
                }
            }
            while (row.Count > num)
            {
                row.RemoveAt(num);
            }
            return(row.Count > 0);
        }