Пример #1
0
        /// <summary>
        /// Try to parse a line of CSV data.  Can only return false if an unterminated text qualifier is encountered.
        ///
        /// This function cannot recognize 'sep=' lines because it does not know whether it is parsing the first line
        /// in the overall CSV stream.
        /// </summary>
        /// <returns>False if there was an unterminated text qualifier in the <paramref name="line"/></returns>
        /// <param name="line">The line of text to parse</param>
        /// <param name="settings">The CSV settings to use for this parsing operation (Default: CSV)</param>
        /// <param name="row">The array of fields found in the line</param>
        public static bool TryParseLine(string line, out string[] row, CSVSettings settings = null)
        {
            row = null;
            var machine = new CSVStateMachine(settings);

            while (machine.State == CSVState.CanKeepGoing)
            {
                row  = machine.ParseChunk(line, true);
                line = string.Empty;
            }
            return(machine.State == CSVState.Done);
        }
Пример #2
0
        /// <summary>
        /// Parse a CSV stream into <![CDATA[ IEnumerable<string[]> ]]> asynchronously, while permitting embedded newlines
        /// </summary>
        /// <param name="inStream">The stream to read</param>
        /// <param name="settings">The CSV settings to use for this parsing operation (Default: CSV)</param>
        /// <returns>An enumerable object that can be examined to retrieve rows from the stream.</returns>
        public static async IAsyncEnumerable <string[]> ParseStreamAsync(StreamReader inStream, CSVSettings settings = null)
        {
            var machine = new CSVStateMachine(settings);

            while (machine.State == CSVState.CanKeepGoing)
            {
                var line = string.Empty;
                if (!inStream.EndOfStream)
                {
                    line = await inStream.ReadLineAsync();
                }
                var row = machine.ParseLine(line, inStream.EndOfStream);
                if (row != null)
                {
                    yield return(row);
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Parse a line from a CSV file and return an array of fields, or null if it fails
        /// </summary>
        /// <param name="line">One line of text from a CSV file</param>
        /// <param name="settings">The CSV settings to use for this parsing operation (Default: CSV)</param>
        /// <param name="throwOnFailure">If this value is true, throws an exception if parsing fails</param>
        /// <returns>An array containing all fields in the next row of data, or null if it could not be parsed.</returns>
        public static string[] ParseLine(string line, CSVSettings settings = null, bool?throwOnFailure = null)
        {
            string[] row     = null;
            var      machine = new CSVStateMachine(settings);

            while (machine.State == CSVState.CanKeepGoing)
            {
                row  = machine.ParseChunk(line, true);
                line = string.Empty;
            }

            // Anything other than success throws an error here
            if (machine.State != CSVState.Done)
            {
                throw new Exception($"Malformed CSV structure: {machine.State}");
            }
            return(row);
        }