Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        /// <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);
        }
Esempio n. 4
0
        /// <summary>Creates a new CRIF file and writes the specified entities to it. 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="dataEntities">The <see cref="System.Collections.Generic.ICollection{T}"/> of <see cref="T:InitialMargin.Core.DataEntity"/> objects to write.</param>
        /// <exception cref="T:System.ArgumentException">Thrown when <paramref name="filePath">filePath</paramref> is invalid or does not refer to a CSV file.</exception>
        /// <exception cref="T:System.ArgumentNullException">Thrown when <paramref name="dataEntities">dataEntities</paramref> is <c>null</c>, when <paramref name="dataEntities">dataEntities</paramref> contains <c>null</c> values or when <paramref name="dataEntities">dataEntities</paramref> contains sensitivities originated from a transformation process.</exception>
        public static void Write(String filePath, ICollection <DataEntity> dataEntities)
        {
            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 valid CSV.", nameof(filePath));
            }

            if (dataEntities == null)
            {
                throw new ArgumentNullException(nameof(dataEntities));
            }

            if (dataEntities.Any(x => x == null))
            {
                throw new ArgumentException("One or more data entities are null.", nameof(dataEntities));
            }

            if (dataEntities.Any(x => (x is Sensitivity sensitivity) && (String.IsNullOrEmpty(sensitivity.Identifier) || (sensitivity.Category == SensitivityCategory.Curvature))))
            {
                throw new ArgumentException("The specified data entities contain sensitivities originated from a transformation process.", nameof(dataEntities));
            }

            List <String[]> fieldsMatrix = new List <String[]>(dataEntities.Count);

            foreach (DataEntity dataEntity in dataEntities)
            {
                WriteDataEntity(dataEntity, fieldsMatrix);
            }

            String result = CsvUtilities.FinalizeFieldsMatrix(s_Properties, fieldsMatrix, '\t', "\n");

            File.WriteAllText(filePath, result, Encoding.UTF8);
        }