Пример #1
0
		public CsvWriter(TextWriter writer, CsvSettings settings)
		{
			this.Writer = writer;
			this.Settings = settings;
			this.LineAlreadyStarted = false;
			this.FormattingCulture = Thread.CurrentThread.CurrentCulture;
		}
Пример #2
0
		public CsvParser(TextReader reader, CsvSettings settings)
		{
			if (reader == null) { throw new ArgumentNullException("reader"); }
			if (settings == null) { throw new ArgumentNullException("settings"); }

			Initialize(reader, settings);
		}
Пример #3
0
		public CsvParser(String csv, CsvSettings settings)
		{
			if (csv == null) { throw new ArgumentNullException("csv"); }
			if (settings == null) { throw new ArgumentNullException("settings"); }

			var reader = new StringReader(csv);
			this.OwnedReader = reader;
			Initialize(reader, settings);
		}
Пример #4
0
		internal CsvLexer(CsvSettings settings)
		{
			if (settings == null) { throw new ArgumentNullException("settings"); }

			this.Settings = settings;
			this.FieldDelimiter = new CsvLexeme(CsvSyntaxItem.Delimiter, settings.FieldDelimiter.ToString());
			this.LineSeparator = new CsvLexeme(CsvSyntaxItem.LineSeparator, settings.RowDelimiter);
			this.Quote = new CsvLexeme(CsvSyntaxItem.Quote, settings.QuotingCharacter.ToString());
		}
Пример #5
0
		private void Initialize(TextReader reader, CsvSettings settings)
		{
			var lexer = new CsvLexer(settings);
			this.Lexemes = lexer.Scan(reader).GetEnumerator();
		}
Пример #6
0
		/// <summary>
		/// Parses csv file to rows and values.
		/// </summary>
		/// <param name="csv"></param>
		/// <param name="settings"></param>
		/// <returns></returns>
		public static String[][] Parse(string csv, CsvSettings settings)
		{
			if (csv == null) { throw new ArgumentNullException("csv"); }
			if (settings == null) { throw new ArgumentNullException("settings"); }

			using (CsvParser parser = new CsvParser(csv, settings))
			{
				return parser.ReadToEnd();
			}
		}
Пример #7
0
		/// <summary>
		/// Splits a single row to a collection of csv items.
		/// </summary>
		/// <param name="singleRow"></param>
		/// <param name="settings"></param>
		/// <returns></returns>
		public static String[] ParseSingleRow(String singleRow, CsvSettings settings)
		{
			if (singleRow == null) { throw new ArgumentNullException("singleRow"); }
			if (settings == null) { throw new ArgumentNullException("settings"); }

			using (CsvParser parser = new CsvParser(singleRow, settings))
			{
				String[] firstRow = parser.ReadNextRow();
				return firstRow;
			}
		}
Пример #8
0
		/// <summary>
		/// Escapes given value based on CSV rules.
		/// </summary>
		/// <param name="value"></param>
		/// <param name="settings"></param>
		/// <returns>value suitable for using as single item in csv row.</returns>
		public static String FormatValue(String value, CsvSettings settings)
		{
			if (value == null) { return null; }
			if (value.Length == 0) { return value; }

			Boolean containsQuote = value.Contains(settings.QuotingCharacter);
			if (containsQuote
				|| value.Contains(settings.FieldDelimiter)
				|| value.Contains(settings.RowDelimiter))
			{
				return settings.QuotingCharacter
					+ (containsQuote
						? value.Replace(settings.QuotingCharacter.ToString(), settings.QuotingCharacter.ToString() + settings.QuotingCharacter)
						: value)
					+ settings.QuotingCharacter;
			}
			return value;
		}