Esempio n. 1
0
        public void TestTextReaderReadLine()
        {
            string text       = "Hello world" + Environment.NewLine + "My tests are good\nHow about \ryours?\n";
            int    lineNumber = 0;

            using (StringReader baseReader = new StringReader(text))
            {
                using (EolReader reader = new EolReader(baseReader))
                {
                    while (!reader.EndOfStream)
                    {
                        lineNumber++;
                        var line = reader.ReadLine();
                        if (lineNumber == 1)
                        {
                            Assert.Equal("Hello world" + Environment.NewLine, line);
                        }
                        if (lineNumber == 2)
                        {
                            Assert.Equal("My tests are good\n", line);
                        }
                        if (lineNumber == 3)
                        {
                            Assert.Equal("How about \r", line);
                        }
                        if (lineNumber == 4)
                        {
                            Assert.Equal("yours?\n", line);
                        }
                    }
                }
            }
            Assert.Equal(lineNumber, 4);
            text       = "Hello world";
            lineNumber = 0;
            using (StringReader baseReader = new StringReader(text))
            {
                using (EolReader reader = new EolReader(baseReader))
                {
                    while (!reader.EndOfStream)
                    {
                        lineNumber++;
                        var line = reader.ReadLine();
                        Assert.Equal("Hello world", line);
                    }
                }
            }
            Assert.Equal(lineNumber, 1);
        }
Esempio n. 2
0
        private bool replace(Stream inputStream, Stream outputStream, string searchPattern, string replacePattern, GrepSearchOption searchOptions, SearchDelegates.DoReplace replaceMethod, Encoding encoding)
        {
            using (StreamReader readStream = new StreamReader(inputStream, encoding))
            {
                StreamWriter writeStream = new StreamWriter(outputStream, encoding);

                string line    = null;
                int    counter = 1;

                // use first line to determine eol character(s);
                using (EolReader eolReader = new EolReader(readStream))
                {
                    line = eolReader.ReadLine();
                    if (line != null)
                    {
                        if (line.EndsWith("\r\n"))
                        {
                            writeStream.NewLine = "\r\n";
                        }
                        else if (line.EndsWith("\n"))
                        {
                            writeStream.NewLine = "\n";
                        }
                        else if (line.EndsWith("\r"))
                        {
                            writeStream.NewLine = "\r";
                        }
                    }
                }

                readStream.BaseStream.Seek(0, SeekOrigin.Begin);
                while (!readStream.EndOfStream)
                {
                    line = readStream.ReadLine();
                    line = replaceMethod(line, searchPattern, replacePattern, searchOptions);
                    writeStream.WriteLine(line);
                    counter++;
                }

                writeStream.Flush();
            }

            return(true);
        }
Esempio n. 3
0
        private bool Replace(Stream inputStream, Stream outputStream, string searchPattern, string replacePattern, GrepSearchOption searchOptions,
                             SearchDelegates.DoReplace replaceMethod, Encoding encoding, IEnumerable <GrepMatch> replaceItems)
        {
            using (StreamReader readStream = new StreamReader(inputStream, encoding))
            {
                bool hasUtf8bom     = encoding == Encoding.UTF8 && Utils.HasUtf8ByteOrderMark(inputStream);
                var  outputEncoding = encoding;
                if (hasUtf8bom)
                {
                    outputEncoding = new UTF8Encoding(true);
                }

                StreamWriter writeStream = new StreamWriter(outputStream, outputEncoding);

                string line         = null;
                int    lineNumber   = 1;
                int    filePosition = 0;

                // read with eol character(s);
                using (EolReader eolReader = new EolReader(readStream))
                {
                    while (!eolReader.EndOfStream)
                    {
                        line = eolReader.ReadLine();
                        if (lineNumber == 1 && hasUtf8bom)
                        {
                            line = line.Replace("\ufeff", ""); // remove BOM
                        }
                        int lineLength = line.Length;

                        line = replaceMethod(lineNumber, filePosition, line, searchPattern, replacePattern, searchOptions, replaceItems);
                        writeStream.Write(line);  // keep original eol

                        lineNumber++;
                        filePosition += lineLength;
                    }
                }

                writeStream.Flush();
            }

            return(true);
        }
Esempio n. 4
0
        private List <GrepSearchResult> Search(Stream input, string fileName, string searchPattern, GrepSearchOption searchOptions, SearchDelegates.DoSearch searchMethod, Encoding encoding)
        {
            List <GrepSearchResult> searchResults = new List <GrepSearchResult>();

            using (StreamReader baseReader = new StreamReader(input, encoding))
            {
                using (EolReader readStream = new EolReader(baseReader))
                {
                    string           line         = null;
                    int              lineNumber   = 1;
                    int              filePosition = 0;
                    List <GrepMatch> matches      = new List <GrepMatch>();
                    while (!readStream.EndOfStream)
                    {
                        line = readStream.ReadLine();

                        if (Utils.CancelSearch)
                        {
                            return(searchResults);
                        }
                        List <GrepMatch> results = searchMethod(lineNumber, filePosition, line, searchPattern, searchOptions, false);
                        if (results.Count > 0)
                        {
                            foreach (GrepMatch m in results)
                            {
                                //matches.Add(new GrepMatch(lineNumber, m.StartLocation + filePosition, m.Length));
                                matches.Add(m);
                            }
                        }
                        filePosition += line.Length;
                        lineNumber++;
                    }
                    if (matches.Count > 0)
                    {
                        searchResults.Add(new GrepSearchResult(fileName, searchPattern, matches, encoding));
                    }
                }
            }
            return(searchResults);
        }
Esempio n. 5
0
        public GrepMatch[] GetFilePositions(string text, List <XPathPosition> positions)
        {
            bool[] endFound = new bool[positions.Count];
            // Getting line lengths
            List <int> lineLengths = new List <int>();

            using (StringReader baseReader = new StringReader(text))
            {
                using (EolReader reader = new EolReader(baseReader))
                {
                    while (!reader.EndOfStream)
                    {
                        lineLengths.Add(reader.ReadLine().Length);
                    }
                }
            }
            // These are absolute positions
            GrepMatch[] results = new GrepMatch[positions.Count];
            if (positions.Count == 0)
            {
                return(results);
            }

            using (StringReader textReader = new StringReader(text))
                using (XmlReader reader = XmlReader.Create(textReader))
                {
                    List <int> currPos = new List <int>();

                    try
                    {
                        IXmlLineInfo lineInfo = ((IXmlLineInfo)reader);
                        if (lineInfo.HasLineInfo())
                        {
                            // Parse the XML and display each node.
                            while (reader.Read())
                            {
                                switch (reader.NodeType)
                                {
                                case XmlNodeType.Element:
                                case XmlNodeType.Comment:

                                    if (currPos.Count <= reader.Depth)
                                    {
                                        currPos.Add(1);
                                    }
                                    else
                                    {
                                        currPos[reader.Depth]++;
                                    }

                                    break;

                                case XmlNodeType.EndElement:
                                    while (reader.Depth < currPos.Count - 1)
                                    {
                                        currPos.RemoveAt(reader.Depth + 1); // currPos.Count - 1 would work too.
                                    }

                                    for (int i = 0; i < positions.Count; i++)
                                    {
                                        if (XPathPositionsMatch(currPos, positions[i].Path))
                                        {
                                            endFound[i] = true;
                                        }
                                    }
                                    break;

                                default:
                                    break;
                                }

                                if (reader.NodeType == XmlNodeType.EndElement ||
                                    reader.NodeType == XmlNodeType.Comment)
                                {
                                    int tokenOffset = reader.NodeType == XmlNodeType.Element ? 3 : 5;
                                    for (int i = 0; i < positions.Count; i++)
                                    {
                                        if (endFound[i] && !XPathPositionsMatch(currPos, positions[i].Path))
                                        {
                                            if (results[i] != null)
                                            {
                                                results[i].EndPosition = GetAbsoluteCharPosition(lineInfo.LineNumber - 1, lineInfo.LinePosition - tokenOffset, text, lineLengths, true) + 1;
                                            }
                                            endFound[i] = false;
                                        }
                                    }
                                }

                                if (reader.NodeType == XmlNodeType.Element)
                                {
                                    for (int i = 0; i < positions.Count; i++)
                                    {
                                        if (endFound[i])
                                        {
                                            if (results[i] != null)
                                            {
                                                results[i].EndPosition = GetAbsoluteCharPosition(lineInfo.LineNumber - 1, lineInfo.LinePosition - 3, text, lineLengths, true) + 1;
                                            }
                                            endFound[i] = false;
                                        }

                                        if (XPathPositionsMatch(currPos, positions[i].Path))
                                        {
                                            results[i] = new GrepMatch(string.Empty, lineInfo.LineNumber, GetAbsoluteCharPosition(lineInfo.LineNumber - 1, lineInfo.LinePosition - 2, text, lineLengths, false), 0);
                                        }

                                        // If empty element (e.g.<element/>)
                                        if (reader.IsEmptyElement)
                                        {
                                            if (XPathPositionsMatch(currPos, positions[i].Path))
                                            {
                                                endFound[i] = true;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    finally
                    {
                        for (int i = 0; i < positions.Count; i++)
                        {
                            if (results[i] != null && results[i].Length == 0)
                            {
                                results[i].EndPosition = text.Length - 1;
                            }
                        }

                        reader.Close();
                    }
                    // Close the reader.
                }
            return(results);
        }