Exemple #1
0
 /// <summary>
 /// Read the first line of a namedStreamCreator after any comments.
 /// </summary>
 /// <param name="namedStreamCreator">The namedStreamCreator from which to read.</param>
 /// <returns>The first line of a file after skipping any comments.</returns>
 public static string ReadLine(INamedStreamCreator namedStreamCreator)
 {
     using (var streamReader = namedStreamCreator.OpenUncommentedText())
     {
         return(streamReader.ReadLine());
     }
 }
        public void ValidateWriteToStream()
        {
            string fileName    = Path.GetTempFileName();
            string textToWrite = string.Empty;
            Random random      = new Random(1);

            for (int i = 0; i < 5; i++)
            {
                textToWrite += random.Next().ToString((IFormatProvider)null) + "\n";
            }
            using (File.Create(fileName)) { }
            //File.WriteAllText(fileName, textToWrite);
            FileInfo            fileinfo = new FileInfo(fileName);
            INamedStreamCreator creator  = INamedStreamCreatorExtensions.ToNamedStreamCreator(fileinfo, "Stream");
            string fileName1             = Path.GetTempFileName();

            using (FileStream stream = new FileStream(fileName1, FileMode.Create))
            {
                INamedStreamCreatorExtensions.WriteToStream(creator, stream);
            }
            string line = INamedStreamCreatorExtensions.ReadToEnd(creator);

            Assert.IsNotNull(line);
            Assert.AreEqual(textToWrite, line);
        }
        public void ValidateToNamedStreamCreatorFromFileName()
        {
            string streamname           = "StreamName";
            INamedStreamCreator creator =
                INamedStreamCreatorExtensions.ToNamedStreamCreatorFromFileName("tmp.txt", streamname);

            Assert.AreEqual(streamname, creator.Name);
            Assert.IsNotNull(creator.Creator);
        }
        public void ValidateToNamedStreamCreatorFromString()
        {
            string strstream            = "this is a test string";
            string streamname           = "StreamName";
            INamedStreamCreator creator =
                INamedStreamCreatorExtensions.ToNamedStreamCreatorFromString(strstream, streamname);

            Assert.AreEqual(streamname, creator.Name);
            Assert.IsNotNull(creator.Creator);
        }
        public void ValidateToNamedStreamCreator()
        {
            string              fileName   = "tmp.txt";
            string              streamname = "StreamName";
            FileInfo            fileinfo   = new FileInfo(fileName);
            INamedStreamCreator creator    =
                INamedStreamCreatorExtensions.ToNamedStreamCreator(fileinfo, streamname);

            Assert.AreEqual(streamname, creator.Name);
            Assert.IsNotNull(creator.Creator);
        }
Exemple #6
0
        /// <summary>
        /// Open a INamedStreamCreator for reading as uncommented text. This should be used is a Using statement. (According to http://msdn.microsoft.com/en-us/library/hh40558e.aspx, the stream will be disposed of, too.)
        /// </summary>
        /// <param name="namedStreamCreator">The INamedStreamCreator to read from</param>
        /// <returns>a StreamReader</returns>
        public static CommentedStreamReader OpenUncommentedText(this INamedStreamCreator namedStreamCreator)
        {
            if (namedStreamCreator == null)
            {
                throw new ArgumentNullException("namedStreamCreator");
            }

            Stream stream = namedStreamCreator.Creator();

            return(new CommentedStreamReader(stream));
        }
        public void ValidateToNamedStreamCreatorWithManifestResourceName()
        {
            string              fileName     = "tmp.txt";
            string              streamname   = "StreamName";
            string              resourcename = "FileStream";
            FileInfo            fileinfo     = new FileInfo(fileName);
            INamedStreamCreator creator      =
                INamedStreamCreatorExtensions.ToNamedStreamCreator(SpecialFunctions.GetEntryOrCallingAssembly(), resourcename, streamname);

            Assert.AreEqual(streamname, creator.Name);
            Assert.IsNotNull(creator.Creator);
        }
Exemple #8
0
        /// <summary>
        /// Returns all the data of a INamedStreamCreator as a string.
        /// </summary>
        /// <param name="namedStreamCreator">The INamedStreamCreator to read from</param>
        /// <returns>The data as text</returns>
        public static string ReadToEnd(this INamedStreamCreator namedStreamCreator)
        {
            if (namedStreamCreator == null)
            {
                throw new ArgumentNullException("namedStreamCreator");
            }

            using (TextReader textReader = namedStreamCreator.OpenText())
            {
                return(textReader.ReadToEnd());
            }
        }
Exemple #9
0
        /// <summary>
        /// Enumerates the lines of a INamedStreamCreator.
        /// </summary>
        /// <param name="namedStreamCreator">The INamedStreamCreator to read from</param>
        /// <returns>an enumerator of lines</returns>
        public static IEnumerable <string> ReadEachLine(this INamedStreamCreator namedStreamCreator)
        {
            if (namedStreamCreator == null)
            {
                throw new ArgumentNullException("namedStreamCreator");
            }

            using (TextReader textReader = namedStreamCreator.OpenText())
            {
                string line;
                while (null != (line = textReader.ReadLine()))
                {
                    yield return(line);
                }
            }
        }
Exemple #10
0
        /// <summary>
        /// Write the context of a INamedStreamCreator to a stream.
        /// </summary>
        /// <param name="namedStreamCreator">The INamedStreamCreator to read from</param>
        /// <param name="stream">The stream to write to</param>
        public static void WriteToStream(this INamedStreamCreator namedStreamCreator, Stream stream)
        {
            if (namedStreamCreator == null)
            {
                throw new ArgumentNullException("namedStreamCreator");
            }

            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            using (TextWriter textWriter = new StreamWriter(stream))
            {
                foreach (string line in namedStreamCreator.ReadEachLine())
                {
                    textWriter.WriteLine(line);
                }
            }
        }
        public void ValidateReadUncommentedLine()
        {
            string fileName    = Path.GetTempFileName();
            string textToWrite = string.Empty;
            Random random      = new Random(1);

            for (int i = 0; i < 5; i++)
            {
                textToWrite += random.Next().ToString((IFormatProvider)null) + "\n";
            }
            using (File.Create(fileName)) { }
            File.WriteAllText(fileName, textToWrite);
            FileInfo            fileinfo = new FileInfo(fileName);
            INamedStreamCreator creator  = INamedStreamCreatorExtensions.ToNamedStreamCreator(fileinfo, "Stream");
            string line = INamedStreamCreatorExtensions.ReadUncommentedLine(creator);

            Assert.IsNotNull(line);
            double num;

            Assert.IsTrue(double.TryParse(line, out num));
        }
Exemple #12
0
        public static bool TryParseRFileWithDefaultMissing(INamedStreamCreator namedStreamCreator, TValue missingValue,
                                                           char[] separatorArray, ParallelOptions parallelOptions, out Matrix <TRowKey, TColKey, TValue> result, out string errorMsg)
        {
            if (namedStreamCreator == null)
            {
                throw new ArgumentNullException("namedStreamCreator");
            }

            errorMsg = "";
            var matrix = new DenseMatrix <TRowKey, TColKey, TValue>();

            result = matrix;
            matrix._missingValue = missingValue;

            int rowCount = namedStreamCreator.ReadEachUncommentedLine().Count();


            using (var textReader = namedStreamCreator.OpenUncommentedText())
            {
                string firstLine = textReader.ReadLine();
                //Helper.CheckCondition(null != firstLine, "Expect file to have first line. ");
                if (null == firstLine)
                {
                    errorMsg = "Expect file to have first line. ";
                    return(false);
                }
                Debug.Assert(rowCount >= 0); // real assert

                List <string> unparsedRowNames = new List <string>(rowCount);
                List <string> unparsedColNames = firstLine.Split(separatorArray).ToList();
                try
                {
                    matrix.ValueArray = new TValue[rowCount, unparsedColNames.Count];
                }
                catch (Exception e)
                {
                    errorMsg = e.Message;
                    return(false);
                }
                string line;
                int    rowIndex = -1;

                //while (null != (line = textReader.ReadLine()))
                while (!string.IsNullOrEmpty(line = textReader.ReadLine()))
                {
                    ++rowIndex;
                    string[] fields = line.Split(separatorArray);
                    //Helper.CheckCondition(fields.Length >= 1, string.Format("Expect each line to have at least one field (file={0}, rowIndex={1})", rFileName, rowIndex));
                    if (fields.Length < 2)
                    {
                        errorMsg = string.Format(CultureInfo.InvariantCulture, "Expect each line to have at least two field (file={0}, rowIndex={1})", namedStreamCreator.Name, rowIndex);
                        return(false);
                    }

                    string rowKey = fields[0];
                    unparsedRowNames.Add(rowKey);

                    // if the first data row has same length as header row, then header row much contain a name for the column of row names. Remove it and proceed.
                    if (rowIndex == 0 && fields.Length == unparsedColNames.Count)
                    {
                        unparsedColNames.RemoveAt(0);
                    }

                    //Helper.CheckCondition(fields.Length == matrix.ColKeys.Count + 1, string.Format("Line has {0} fields instead of the epxected {1} fields (file={2}, rowKey={3}, rowIndex={4})", fields.Length, matrix.ColKeys.Count + 1, rFileName, rowKey, rowIndex));
                    if (fields.Length != unparsedColNames.Count + 1)
                    {
                        errorMsg = string.Format(CultureInfo.InvariantCulture, "Line has {0} fields instead of the expected {1} fields (file={2}, rowKey={3}, rowIndex={4})", fields.Length, unparsedColNames.Count + 1, namedStreamCreator.Name, rowKey, rowIndex);
                        return(false);
                    }

                    //for (int colIndex = 0; colIndex < matrix.ValueArray.GetLength(0); ++colIndex)
                    for (int colIndex = 0; colIndex < unparsedColNames.Count; ++colIndex)
                    {
                        TValue r;
                        if (!Parser.TryParse <TValue>(fields[colIndex + 1], out r))
                        {
                            errorMsg = string.Format(CultureInfo.InvariantCulture, "Unable to parse {0} because field {1} cannot be parsed into an instance of type {2}", namedStreamCreator.Name, fields[colIndex + 1], typeof(TValue));
                            return(false);
                        }
                        matrix.ValueArray[rowIndex, colIndex] = r;
                    }
                }

                IList <TRowKey> rowKeys;
                if (!Parser.TryParseAll <TRowKey>(unparsedRowNames, out rowKeys))
                {
                    errorMsg = string.Format(CultureInfo.InvariantCulture, "Unable to parse {0} because row names cannot be parsed into an instance of type {1}", namedStreamCreator.Name, typeof(TRowKey));
                    return(false);
                }
                IList <TColKey> colKeys;
                if (!Parser.TryParseAll <TColKey>(unparsedColNames, out colKeys))
                {
                    errorMsg = string.Format(CultureInfo.InvariantCulture, "Unable to parse {0} because col names cannot be parsed into an instance of type {1}", namedStreamCreator.Name, typeof(TColKey));
                    return(false);
                }
                matrix._rowKeys = new ReadOnlyCollection <TRowKey>(rowKeys);
                matrix._colKeys = new ReadOnlyCollection <TColKey>(colKeys);
            }

            //In the case of sparse files, many of the row keys will be the same and so we return false
            if (matrix._rowKeys.Count != matrix._rowKeys.Distinct().Count())
            {
                errorMsg = string.Format(CultureInfo.InvariantCulture, "Some rows have the same values as other (look for blank rows). " + namedStreamCreator.Name);
                return(false);
            }


            matrix._indexOfRowKey = matrix.RowKeys.Select((key, index) => new { key, index }).ToDictionary(pair => pair.key, pair => pair.index);
            matrix._indexOfColKey = matrix.ColKeys.Select((key, index) => new { key, index }).ToDictionary(pair => pair.key, pair => pair.index);

            return(true);
        }
Exemple #13
0
 /// <summary>
 /// Read the first line of a namedStreamCreator after any comments.
 /// </summary>
 /// <param name="namedStreamCreator">The namedStreamCreator from which to read.</param>
 /// <returns>The first line of a file after skipping any comments.</returns>
 public static string ReadLine(INamedStreamCreator namedStreamCreator)
 {
     using (var streamReader = namedStreamCreator.OpenUncommentedText())
     {
         return streamReader.ReadLine();
     }
 }
Exemple #14
0
 /// <summary>
 /// Return the first line of namedStreamCreator after any comments.
 /// </summary>
 /// <param name="namedStreamCreator">The namedStreamCreator from which to read.</param>
 /// <returns>The first line of a file after skipping any comments.</returns>
 public static string ReadUncommentedLine(this INamedStreamCreator namedStreamCreator)
 {
     return(FileUtils.ReadLine(namedStreamCreator));
 }