Esempio n. 1
0
        /// <summary>
        /// <para>Reads the stream as a dictonary</para>
        /// <para>The stream is expected to be a text with the following format</para>
        /// <para>1. One line of the file is one caliber</para>
        /// <para>2. Fields are separated with semicolon (`;`)</para>
        /// <para>3. Fields are:</para>
        /// <para>3a. Type: pistol, rifle, shotgun or cannon</para>
        /// <para>3b. Group: a caliber group in inches or millimeters</para>
        /// <para>3c. Bullet diameter in inches or millimeters</para>
        /// <para>3d. The primary name</para>
        /// <para>3e. The optional names separated by a comma</para>
        /// <para>Example:</para>
        /// <para>`rifle;4mm;0.172';.17 HMR;.17 Hornady Magnum Rimfire, .17 Hornady Magnum`</para>
        /// </summary>
        /// <param name="dictionary"></param>
        /// <param name="stream"></param>
        /// <param name="encoding"></param>
        /// <param name="ignoreErrors"></param>
        public static void ReadStream(this AmmunitionCaliberDictionary dictionary, Stream stream, Encoding encoding = null, bool ignoreErrors = false)
        {
            using var ts = new StreamReader(stream, encoding ?? Encoding.ASCII, true, 4096, true);
            int ln = 0;

            while (true)
            {
                ln++;
                var line = ts.ReadLine();
                if (line == null)
                {
                    break;
                }
                if (string.IsNullOrEmpty(line))
                {
                    continue;
                }
                try
                {
                    var item = ParseLine(line, ln);
                    dictionary.Add(item);
                }
                catch (Exception)
                {
                    if (ignoreErrors)
                    {
                        continue;
                    }
                    throw;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a dictionary and prefills it with the default set
        /// </summary>
        /// <returns></returns>
        public static AmmunitionCaliberDictionary Create()
        {
            var dictionary = new AmmunitionCaliberDictionary();

            using var stream = typeof(AmmunitionCaliberFactory).Assembly.GetManifestResourceStream("BallisticCalculator.Resources.Calibers.csv");
            dictionary.ReadStream(stream);
            return(dictionary);
        }