/// <summary> /// Creates a descriptor from any possible combination of inputs / configuration. /// </summary> /// <param name="type">May be InputStream or OutputStream if stream is not null, or /// Unknown if stream is null.</param> /// <param name="path">Path to the CSV file or directory. One of path, reader, writer /// must be set.</param> /// <param name="writer">The writer to "insert" to. One of path, reader, writer /// must be set.</param> /// <param name="reader">The reader to "query" against. One of path, reader, writer /// must be set.</param> /// <param name="quoteLevel">How verbosely do we quote values we write.</param> protected CsvDescriptor(CsvConnectionType type, string path, TextWriter writer, StreamReader reader, CsvQuoteLevel quoteLevel) { if (StringHelper.IsNonBlank(path)) { if (writer != null) { throw new LoggingException( "Cannot create a CSV 'connection' using both a file path and a writer."); } if (reader != null) { throw new LoggingException( "Cannot create a CSV 'connection' using both a file path and a reader."); } switch (type) { case CsvConnectionType.Unknown: if ((!File.Exists(path)) && (!Directory.Exists(path))) { throw new LoggingException( "Path '" + path + "' does not exist. If you want it to be created, you must specify the connection type (FileName or Directory)."); } // Check if it's a directory or a file. if ((File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory) { Type = CsvConnectionType.Directory; } else { Type = CsvConnectionType.FileName; } break; case CsvConnectionType.FileName: if (File.Exists(path)) { // Check if it's a directory or a file. if ((File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory) { throw new LoggingException( "You specified a type of FileName, but the path you provided (" + path + ") is a directory."); } } Type = CsvConnectionType.FileName; break; case CsvConnectionType.Directory: if (Directory.Exists(path)) { // Check if it's a directory or a file. if ((File.GetAttributes(path) & FileAttributes.Directory) != FileAttributes.Directory) { throw new LoggingException( "You specified a type of Directory, but the path you provided (" + path + ") is not a directory."); } } else { // Go ahead and create it. Directory.CreateDirectory(path); } Type = CsvConnectionType.Directory; break; default: throw new LoggingException("Connection type " + type + " is invalid when configuring using a path."); } Path = path; } else if (writer != null) { if (reader != null) { throw new LoggingException( "Cannot create a CSV 'connection' using both a writer and a reader."); } switch (type) { case CsvConnectionType.Unknown: case CsvConnectionType.Writer: Type = CsvConnectionType.Writer; break; default: throw new LoggingException("Connection type " + type + " is invalid when configuring using a writer."); } Writer = writer; } else { if (reader == null) { throw new LoggingException( "You must either specify a path, a reader, or a writer to use."); } switch (type) { case CsvConnectionType.Unknown: case CsvConnectionType.Reader: if (!reader.BaseStream.CanRead) { throw new LoggingException( "You passed a reader wrapped around a stream that does not support reading."); } if (!reader.BaseStream.CanSeek) { throw new LoggingException( "You passed a reader wrapped around a stream that does not support seeking. Support for a single query without seeking has not yet been implemented."); } break; default: throw new LoggingException("Connection type " + type + " is invalid when configuring using a reader."); } Reader = reader; } OutputQuoteLevel = quoteLevel; _connectionStr = "CSV[" + Type + "," + quoteLevel + "," + (Path ?? Writer.ToString() ?? Reader.ToString()) + "]"; }
/// <summary> /// Creates a descriptor using a StreamReader. See CsvConnectionType for a description /// of the behavior when using a StreamReader. /// Allows you to specify the verbosity of quotes when we write to the stream. /// </summary> /// <param name="reader">The reader to "query" against.</param> /// <param name="quoteLevel">How verbosely do we quote values we write.</param> public CsvDescriptor(StreamReader reader, CsvQuoteLevel quoteLevel) : this(CsvConnectionType.Reader, null, null, reader, quoteLevel) { }
/// <summary> /// Creates a descriptor for the given path. If the path may either be a file /// or a directory, see CsvConnectionType for descriptions of the behavior in /// either case. /// Allows you to specify the verbosity of quotes when we write the file. /// </summary> /// <param name="type">Which type is it, a file or a directory.</param> /// <param name="path">Path to the CSV file or directory. Will be created if it does not exist.</param> /// <param name="quoteLevel">How verbosely do we quote values we write.</param> public CsvDescriptor(CsvConnectionType type, string path, CsvQuoteLevel quoteLevel) : this(type, path, null, null, quoteLevel) { }
/// <summary> /// Creates a descriptor using a TextWriter. See CsvConnectionType for a description /// of the behavior when using a TextWriter. /// Allows you to specify the verbosity of quotes when we write to the stream. /// </summary> /// <param name="writer">The writer to "insert" to.</param> /// <param name="quoteLevel">How verbosely do we quote values we write.</param> public CsvDescriptor(TextWriter writer, CsvQuoteLevel quoteLevel) : this(CsvConnectionType.Writer, null, writer, null, quoteLevel) { }
/// <summary> /// Creates a descriptor for the given path. If the path may either be a file /// or a directory, see CsvConnectionType for descriptions of the behavior in /// either case. /// Allows you to specify the verbosity of quotes when we write the file. /// </summary> /// <param name="path">Path to the CSV file or directory. Must exist.</param> /// <param name="quoteLevel">How verbosely do we quote values we write.</param> public CsvDescriptor(string path, CsvQuoteLevel quoteLevel) : this(CsvConnectionType.Unknown, path, null, null, quoteLevel) { }