/// <summary>Writes the specified calculation result into the specified CSV file, using the specified delimiter.</summary> /// <param name="filePath">The <see cref="T:System.String"/> representing the file to write to.</param> /// <param name="margin">The <see cref="T:InitialMargin.Core.MarginTotal"/> object to write.</param> /// <param name="delimiter">The <see cref="T:System.Char"/> representing the delimiter.</param> /// <exception cref="T:System.ArgumentException">Thrown when <paramref name="filePath">filePath</paramref> is invalid or does not refer to a CSV file, or when <paramref name="delimiter">delimiter</paramref> is invalid (see <see cref="M:InitialMargin.IO.CsvUtilities.IsValidDelimiter(System.Char)"/>).</exception> /// <exception cref="T:System.ArgumentNullException">Thrown when <paramref name="margin">margin</paramref> is <c>null</c>.</exception> public void Write(String filePath, MarginTotal margin, Char delimiter) { if (String.IsNullOrWhiteSpace(filePath)) { throw new ArgumentException("Invalid file path specified.", nameof(filePath)); } if (Path.GetExtension(filePath).ToUpperInvariant() != ".CSV") { throw new ArgumentException("The specified file must be a CSV.", nameof(filePath)); } if (margin == null) { throw new ArgumentNullException(nameof(margin)); } if (!CsvUtilities.IsValidDelimiter(delimiter)) { throw new ArgumentException($"Invalid delimiter specified (accepted delimiters are: {CsvUtilities.ValidDelimiters}).", nameof(delimiter)); } List <String[]> fieldsMatrix = new List <String[]>(); WriteCsvRecursive(margin, fieldsMatrix); String result = CsvUtilities.FinalizeFieldsMatrix(s_CsvHeaderFields, fieldsMatrix, delimiter, Environment.NewLine); File.WriteAllText(filePath, result, m_Encoding); }
public static List <String[]> Parse(String filePath, Encoding encoding, Char delimiter, Boolean skipHeader) { if (String.IsNullOrWhiteSpace(filePath)) { throw new ArgumentException("Invalid CSV file specified.", nameof(filePath)); } if (Path.GetExtension(filePath).ToUpperInvariant() != ".CSV") { throw new ArgumentException("The specified file must be a valid CSV.", nameof(filePath)); } if (!File.Exists(filePath)) { throw new FileNotFoundException("The specified file could not be found.", filePath); } if (encoding == null) { throw new ArgumentNullException(nameof(encoding)); } if (!CsvUtilities.IsValidDelimiter(delimiter)) { throw new ArgumentException($"Invalid delimiter specified (accepted delimiters are: {CsvUtilities.ValidDelimiters}).", nameof(delimiter)); } CsvParser parser = null; List <String[]> fieldsMatrix = new List <String[]>(); try { parser = new CsvParser(filePath, delimiter, encoding); while (!parser.EndOfData) { fieldsMatrix.Add(parser.ReadFields()); } } finally { parser?.Dispose(); } if (skipHeader && (fieldsMatrix.Count > 0)) { fieldsMatrix.Remove(fieldsMatrix.ElementAt(0)); } fieldsMatrix.RemoveAll(x => x == null); return(fieldsMatrix); }
/// <summary>Creates a new rates file and writes the content of the specified rates provider to it, using the specified delimiter. A parameter specifies whether the file must contain a header. If the target file already exists, it is overwritten.</summary> /// <param name="filePath">The <see cref="T:System.String"/> representing the file to write to.</param> /// <param name="ratesProvider">The <see cref="T:InitialMargin.Core.FxRatesProvider"/> object to write.</param> /// <param name="delimiter">The <see cref="T:System.Char"/> representing the delimiter.</param> /// <param name="header"><c>true</c> if the rates file must contain a header; otherwise, <c>false</c>.</param> /// <exception cref="T:System.ArgumentException">Thrown when <paramref name="filePath">filePath</paramref> is invalid or does not refer to a CSV file, or when <paramref name="delimiter">delimiter</paramref> is invalid (see <see cref="M:InitialMargin.IO.CsvUtilities.IsValidDelimiter(System.Char)"/>).</exception> /// <exception cref="T:System.ArgumentNullException">Thrown when <paramref name="ratesProvider">ratesProvider</paramref> is <c>null</c>.</exception> public void Write(String filePath, FxRatesProvider ratesProvider, Char delimiter, Boolean header) { if (String.IsNullOrWhiteSpace(filePath)) { throw new ArgumentException("Invalid file path specified.", nameof(filePath)); } if (Path.GetExtension(filePath).ToUpperInvariant() != ".CSV") { throw new ArgumentException("The specified file must be a CSV.", nameof(filePath)); } if (ratesProvider == null) { throw new ArgumentNullException(nameof(ratesProvider)); } if (!CsvUtilities.IsValidDelimiter(delimiter)) { throw new ArgumentException($"Invalid delimiter specified (accepted delimiters are: {CsvUtilities.ValidDelimiters}).", nameof(delimiter)); } ReadOnlyDictionary <CurrencyPair, Decimal> rates = ratesProvider.OriginalRates; List <String[]> fieldsMatrix = new List <String[]>(rates.Count); foreach (KeyValuePair <CurrencyPair, Decimal> rate in rates) { CurrencyPair currencyPair = rate.Key; String[] row = new String[3]; row[0] = currencyPair.CurrencyBase.ToString().ToUpperInvariant(); row[1] = currencyPair.CurrencyCounter.ToString().ToUpperInvariant(); row[2] = rate.Value.ToString(m_FormatProvider); fieldsMatrix.Add(row); } String result = CsvUtilities.FinalizeFieldsMatrix(header ? s_HeaderFields : null, fieldsMatrix, delimiter, Environment.NewLine); File.WriteAllText(filePath, result, m_Encoding); }