Example #1
0
 /// <summary>
 /// Initializes an empty CSV file.
 /// </summary>
 /// <param name="separator">The separator to be used.</param>
 public CsvFile(CsvSeparator separator)
 {
     Separator = separator;
     Encoding  = DefaultEncoding;
     Columns   = new CsvColumnList(this);
     Rows      = new CsvRowList(this);
 }
Example #2
0
        /// <summary>
        /// Loads a new CSV file from the specified <paramref name="stream"/>.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="separator">The separator used in the CSV file.</param>
        /// <param name="encoding">The encoding of the CSV file.</param>
        /// <returns>An instance of <see cref="CsvFile"/>.</returns>
        public static CsvFile Load(Stream stream, CsvSeparator separator, Encoding encoding)
        {
            // Make sure we have an encoding
            encoding = encoding ?? DefaultEncoding;

            // Load the contents of the file/stream into a byte array
            byte[] bytes;
            using (BinaryReader reader = new BinaryReader(stream)) {
                const int bufferSize = 4096;
                using (var ms = new MemoryStream()) {
                    byte[] buffer = new byte[bufferSize];
                    int    count;
                    while ((count = reader.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        ms.Write(buffer, 0, count);
                    }
                    bytes = ms.ToArray();
                }
            }

            // Convert the byte array to a string (using the specified encoding)
            string contents = encoding.GetString(bytes);

            // Initialize a new CSV file
            CsvFile file = new CsvFile {
                Separator = separator
            };

            // Parse the contents
            ParseInternal(file, contents, separator);

            return(file);
        }
Example #3
0
        /// <summary>
        /// Parses the specified <paramref name="text"/> into an instance of <see cref="CsvFile"/>, using the specified
        /// <paramref name="separator"/>.
        /// </summary>
        /// <param name="text">The text representing the contents of the CSV file.</param>
        /// <param name="separator">The separator used in the CSV file.</param>
        /// <returns>An instance of <see cref="CsvFile"/>.</returns>
        public static CsvFile Parse(string text, CsvSeparator separator)
        {
            // Initialize a new CSV file
            CsvFile file = new CsvFile();

            // Parse the contents
            return(ParseInternal(file, text, separator));
        }
Example #4
0
        /// <summary>
        /// Internal helper method for parsing the contents of a CSV file.
        /// </summary>
        /// <param name="file"></param>
        /// <param name="contents"></param>
        /// <param name="separator"></param>
        /// <returns><paramref name="file"/>.</returns>
        private static CsvFile ParseInternal(CsvFile file, string contents, CsvSeparator separator = CsvSeparator.SemiColon)
        {
            // Normalize line endings
            contents = contents.Replace("\r\n", "\n");
            contents = contents.Replace("\r", "\n");

            // Get the separator as a "char"
            char sep;

            switch (separator)
            {
            case CsvSeparator.Comma: sep = ','; break;

            case CsvSeparator.Colon: sep = ':'; break;

            case CsvSeparator.SemiColon: sep = ';'; break;

            case CsvSeparator.Space: sep = ' '; break;

            case CsvSeparator.Tab: sep = '\t'; break;

            default: sep = ';'; break;
            }

            // Parse each line into a list of cell values
            List <List <string> > lines = ParseLines(contents, sep);

            if (lines.Count == 0)
            {
                throw new Exception("WTF?");
            }

            // If malformed, each line/row may not have the same amount of cells
            int maxColumns = lines.Max(x => x.Count);

            // Parse the columns (column headers)
            for (int c = 0; c < maxColumns; c++)
            {
                string name = lines[0].Skip(c).FirstOrDefault() ?? "";
                file.AddColumn(name);
            }

            // Parse the rows
            for (int r = 1; r < lines.Count; r++)
            {
                CsvRow row = file.AddRow();
                for (int c = 0; c < maxColumns; c++)
                {
                    CsvColumn column = file.Columns[c];
                    string    value  = lines[r].Skip(c).FirstOrDefault() ?? "";
                    row.AddCell(column, value);
                }
            }

            return(file);
        }
Example #5
0
        /// <summary>
        /// Loads the CSV file at the specified <paramref name="path"/>. <paramref name="separator"/> is used as
        /// separator. <paramref name="encoding"/> is assumed as encoding.
        /// </summary>
        /// <param name="path">The path to the CSV file.</param>
        /// <param name="separator">The separator used in the CSV file.</param>
        /// <param name="encoding">The encoding of the CSV file.</param>
        /// <returns>An instance of <see cref="CsvFile"/>.</returns>
        public static CsvFile Load(string path, CsvSeparator separator, Encoding encoding)
        {
            // Load the contents of the CSV file
            string contents = File.ReadAllText(path, encoding ?? Encoding.UTF8).Trim();

            // Initialize a new CSV file
            CsvFile file = new CsvFile {
                Path = path
            };

            // Parse the contents
            ParseInternal(file, contents, separator);

            return(file);
        }
Example #6
0
        /// <summary>
        /// Returns a string representation of the CSV file, using the specified <paramref name="separator"/>.
        /// </summary>
        /// <param name="separator">The separator to be used.</param>
        /// <returns>A string representation of the CSV file.</returns>
        public string ToString(CsvSeparator separator)
        {
            StringBuilder sb = new StringBuilder();

            // Get the separator as a "char"
            char sep;

            switch (separator)
            {
            case CsvSeparator.Comma: sep = ','; break;

            case CsvSeparator.Colon: sep = ':'; break;

            case CsvSeparator.SemiColon: sep = ';'; break;

            case CsvSeparator.Space: sep = ' '; break;

            case CsvSeparator.Tab: sep = '\t'; break;

            default: sep = ';'; break;
            }

            // Append the first line with the column headers
            for (int i = 0; i < Columns.Length; i++)
            {
                if (i > 0)
                {
                    sb.Append(sep);
                }
                sb.Append(Escape(Columns[i].Name, sep));
            }

            foreach (CsvRow row in Rows)
            {
                sb.AppendLine();
                for (int i = 0; i < Columns.Length; i++)
                {
                    if (i > 0)
                    {
                        sb.Append(sep);
                    }
                    CsvCell cell = i < row.Cells.Length ? row.Cells[i] : null;
                    sb.Append(Escape(cell == null ? "" : cell.Value, sep));
                }
            }

            return(sb.ToString());
        }
Example #7
0
        /// <summary>
        /// Return a string which represent real CSV separator which can be used as plain text.
        /// </summary>
        public static string ToRealSeparator(this CsvSeparator separator)
        {
            switch (separator)
            {
            case CsvSeparator.Comma:
                return(",");

            case CsvSeparator.Semicolon:
                return(";");

            case CsvSeparator.CurrentCulture:
                return(CultureInfo.CurrentCulture.TextInfo.ListSeparator);

            default:
                throw new ArgumentOutOfRangeException(nameof(separator));
            }
        }
Example #8
0
        public static string ToText(this CsvSeparator separator)
        {
            switch (separator)
            {
            case CsvSeparator.Comma:
                return(Resource.PresentationFileFormatCsvSeparatorComma);

            case CsvSeparator.Pipe:
                return(Resource.PresentationFileFormatCsvSeparatorPipe);

            case CsvSeparator.Semicolon:
                return(Resource.PresentationFileFormatCsvSeparatorSemicolon);

            case CsvSeparator.Tab:
                return(Resource.PresentationFileFormatCsvSeparatorTab);

            default:
                return(string.Empty);
            }
        }
 public CsvMeasurementsExporter(CsvSeparator separator)
 {
     Separator = separator.ToRealSeparator();
 }
 public CsvMeasurementsExporter(CsvSeparator separator)
 {
     Separator = separator.ToRealSeparator();
 }
Example #11
0
 public CsvExporterAttribute(CsvSeparator separator = CsvSeparator.CurrentCulture) : base(new CsvExporter(separator))
 {
 }
 public CsvExporterAttribute(CsvSeparator separator = CsvSeparator.CurrentCulture) : base(new CsvExporter(separator))
 {
 }
Example #13
0
 public CsvExporter(CsvSeparator separator)
 {
     this.separator = separator;
 }
Example #14
0
 public CsvExporter(CsvSeparator separator)
 {
     this.separator = separator.ToRealSeparator();
 }
Example #15
0
 /// <summary>
 /// Loads a new CSV file from the specified <paramref name="stream"/>.
 /// </summary>
 /// <param name="stream">The stream.</param>
 /// <param name="separator">The separator to </param>
 /// <returns>An instance of <see cref="CsvFile"/>.</returns>
 public static CsvFile Load(Stream stream, CsvSeparator separator)
 {
     return(Load(stream, separator, DefaultEncoding));
 }
Example #16
0
 /// <summary>
 /// Loads the CSV file at the specified <paramref name="path"/>. <see cref="Encoding.UTF8"/> is assumed as
 /// encoding. <paramref name="separator"/> is used as separator.
 /// </summary>
 /// <param name="path">The path to the CSV file.</param>
 /// <param name="separator">The separator used in the CSV file.</param>
 /// <returns>An instance of <see cref="CsvFile"/>.</returns>
 public static CsvFile Load(string path, CsvSeparator separator)
 {
     return(Load(path, separator, Encoding.UTF8));
 }
Example #17
0
 /// <summary>
 /// Saves the CSV file at the specified <paramref name="path"/>, using the specified
 /// <paramref name="separator"/> and <see cref="CsvFile.Encoding"/>.
 /// </summary>
 /// <param name="path">The path to where the CSV file should be saved.</param>
 /// <param name="separator">The separator to be used.</param>
 /// <returns>The original instance of <see cref="CsvFile"/>.</returns>
 public CsvFile Save(string path, CsvSeparator separator)
 {
     return(File.Save(path, separator));
 }
Example #18
0
 /// <summary>
 /// Saves the CSV file at the specified <paramref name="path"/>, using the specified
 /// <paramref name="separator"/> and <paramref name="encoding"/>.
 /// </summary>
 /// <param name="path">The path to where the CSV file should be saved.</param>
 /// <param name="separator">The separator to be used.</param>
 /// <param name="encoding">The encoding to be used.</param>
 /// <returns>The original instance of <see cref="CsvFile"/>.</returns>
 public CsvFile Save(string path, CsvSeparator separator, Encoding encoding)
 {
     return(File.Save(path, separator, encoding));
 }
Example #19
0
 /// <summary>
 /// Saves the CSV file at the specified <paramref name="path"/>, using the specified
 /// <paramref name="separator"/> and <paramref name="encoding"/>.
 /// </summary>
 /// <param name="path">The path of the file to write to.</param>
 /// <param name="separator">The separator to be used.</param>
 /// <param name="encoding">The encoding to be used.</param>
 /// <returns>The original instance of <see cref="CsvFile"/>.</returns>
 public CsvFile Save(string path, CsvSeparator separator, Encoding encoding)
 {
     File.WriteAllText(path, ToString(separator), encoding);
     return(this);
 }
Example #20
0
 /// <summary>
 /// Saves the CSV file at the specified <paramref name="path"/>, using the specified
 /// <paramref name="separator"/> and <see cref="Encoding.UTF8"/>..
 /// </summary>
 /// <param name="path">The path of the file to write to.</param>
 /// <param name="separator">The separator to be used.</param>
 /// <returns>The original instance of <see cref="CsvFile"/>.</returns>
 public CsvFile Save(string path, CsvSeparator separator)
 {
     return(Save(path, separator, Encoding.UTF8));
 }
 public CsvExporter(CsvSeparator separator) : this(separator, SummaryStyle.Default)
 {
 }
Example #22
0
 public CsvExporter(CsvSeparator separator) : this(separator, SummaryStyle.Default.WithZeroMetricValuesInContent())
 {
 }
 public CsvExporter(CsvSeparator separator, ISummaryStyle style)
 {
     this.style     = style;
     this.separator = separator;
 }
Example #24
0
 /// <summary>Initializes a new instance of the <see cref="CsvTimingsExporter"/> class.</summary>
 /// <param name="separator">The separator.</param>
 public CsvTimingsExporter(CsvSeparator separator)
 {
     _separator = separator.ToRealSeparator();
 }
Example #25
0
        /// <summary>Stores advanced settings to <see cref="ConfigurationManager"/></summary>
        public static void Store()
        {
            ConfigurationManager.AppSettings["COMPort"]          = ComPort;
            ConfigurationManager.AppSettings["BaudRate"]         = BaudRate.ToString();
            ConfigurationManager.AppSettings["UseWindowsDriver"] = UseWindowsDriver ? "true" : "false";
            ConfigurationManager.AppSettings["PollingInterval"]  = PollingInterval.ToString(System.Globalization.CultureInfo.InvariantCulture);
            ConfigurationManager.AppSettings["LogToTextBox"]     = LogToTextBox ? "true" : "false";
            ConfigurationManager.AppSettings["LogToFile"]        = LogToLogFile ? "true" : "false";
            switch (LogFormat)
            {
            case LogFormat.KML:
                ConfigurationManager.AppSettings["LogFormat"] = "KML";
                break;

            case LogFormat.CSV:
                ConfigurationManager.AppSettings["LogFormat"] = "CSV";
                break;

            default:
                ConfigurationManager.AppSettings["LogFormat"] = "GPX";
                break;
            }

            switch (TrackType)
            {
            case TrackType.Track: ConfigurationManager.AppSettings["TrackType"] = "Track"; break;

            default: ConfigurationManager.AppSettings["TrackType"] = "Points"; break;
            }
            ConfigurationManager.AppSettings["LogAltitude"]     = LogAltitude ? "true" : "false";
            ConfigurationManager.AppSettings["LogFileLocation"] = LogFileLocation;

            ConfigurationManager.AppSettings["UseGPSTime"]          = UseGpsTime ? "true" : "false";
            ConfigurationManager.AppSettings["StatusBar"]           = StatusBar ? "true" : "false";
            ConfigurationManager.AppSettings["MaxLogLength"]        = MaxLogLength.ToString(System.Globalization.CultureInfo.InvariantCulture);
            ConfigurationManager.AppSettings["AltitudeCorrection"]  = AltitudeCorrection.ToString(System.Globalization.CultureInfo.InvariantCulture);
            ConfigurationManager.AppSettings["StartImmediatelly"]   = StartImmediatelly ? "true" : "false";
            ConfigurationManager.AppSettings["KMLNameFormat"]       = KmlNameFormat;
            ConfigurationManager.AppSettings["KMLDescFormat"]       = KmlDescFormat;
            ConfigurationManager.AppSettings["KMLLineColor"]        = KmlLineColor.ToArgb().ToString(System.Globalization.CultureInfo.InvariantCulture);
            ConfigurationManager.AppSettings["MinimalDistance"]     = MinimalDistance.ToString(System.Globalization.CultureInfo.InvariantCulture);
            ConfigurationManager.AppSettings["TextLogFormat"]       = TextLogFormat;
            ConfigurationManager.AppSettings["InvalidPositionsMax"] = InvalidPositionsMax.ToString(System.Globalization.CultureInfo.InvariantCulture);
            ConfigurationManager.AppSettings["NMEALog"]             = NmeaLog ? "true" : "false";
            ConfigurationManager.AppSettings["SeaLevelAltitude"]    = SeaLevelAltitude ? "true" : "false";
            ConfigurationManager.AppSettings["InfoPane"]            = InfoPane ? "true" : "false";
            ConfigurationManager.AppSettings["BeepTimer"]           = BeepTimer.ToString(System.Globalization.CultureInfo.InvariantCulture);
            ConfigurationManager.AppSettings["KeepAwake"]           = KeepAwakeList == null ? "" : string.Join(" ", KeepAwakeList);
            ConfigurationManager.AppSettings["SpeedUnit"]           = SpeedUnit.ToString();
            ConfigurationManager.AppSettings["DistanceUnit"]        = DistanceUnit.ToString();
            ConfigurationManager.AppSettings["ElevationUnit"]       = ElevationUnit.ToString();
            ConfigurationManager.AppSettings["CSVSeparator"]        = CsvSeparator.ToString();
            ConfigurationManager.AppSettings["CSVTextQualifier"]    = CsvTextQualifier.ToString();
            ConfigurationManager.AppSettings["CSVNewLine"]          = CsvNewLine;
            ConfigurationManager.AppSettings["CSVQualifierUsage"]   = ((int)CsvQualifierUsage).ToString(System.Globalization.CultureInfo.InvariantCulture);
            ConfigurationManager.AppSettings["CSVHeader"]           = CsvHeader;
            ConfigurationManager.AppSettings["CSVFields"]           = CsvFields;
            ConfigurationManager.AppSettings["CSVDateFormat"]       = CsvDateFormat;
            ConfigurationManager.AppSettings["CSVUTC"]   = CsvUtc ? "true" : "false";
            ConfigurationManager.AppSettings["Language"] = Language;
            ConfigurationManager.AppSettings["MainFormBottomEmptySpace"] = MainFormBottomEmptySpace.ToString(System.Globalization.CultureInfo.InvariantCulture);
        }
 public CsvMeasurementsExporter(CsvSeparator separator)
 {
     this.separator = separator;
 }
Example #27
0
 public CsvMeasurementsExporter(CsvSeparator separator, ISummaryStyle style = null)
 {
     this.separator = separator;
     Style          = style ?? SummaryStyle.Default;
 }
Example #28
0
 /// <summary>
 /// Loads the CSV file at the specified <paramref name="path"/>.
 /// </summary>
 /// <param name="path">The path to the CSV file.</param>
 /// <param name="separator">The separator used in the CSV file.</param>
 /// <returns>An instance of <see cref="CsvFile"/>.</returns>
 public static CsvFile Load(string path, CsvSeparator separator)
 {
     return(Load(path, separator, DefaultEncoding));
 }