예제 #1
0
파일: Files.cs 프로젝트: JeremyKuhne/XTask
        /// <summary>
        /// Finds all files in a given set of paths that contain the specified regex
        /// </summary>
        /// <param name="regex">The string to search for</param>
        /// <param name="ignoreCase">True to ignore case</param>
        /// <param name="paths">Paths to search</param>
        public static IEnumerable<string> ContainsRegex(this IFileService fileService, string regex, bool ignoreCase, params string[] paths)
        {
            RegexOptions options = RegexOptions.Compiled;
            if (ignoreCase) { options |= RegexOptions.IgnoreCase; }

            Regex r;
            try
            {
                r = new Regex(regex, options);
            }
            catch (ArgumentException exception)
            {
                // Bad regex
                throw new TaskArgumentException(exception.Message);
            }

            ConcurrentBag<string> matchingPaths = new ConcurrentBag<string>();

            Parallel.ForEach(paths, path =>
            {
                if (!fileService.FileExists(path)) { return; }

                Stream stream = fileService.CreateFileStream(path);
                using (StreamReader reader = new StreamReader(stream))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        if (r.IsMatch(line))
                        {
                            matchingPaths.Add(path);
                            break;
                        }
                    }
                }
            });

            return matchingPaths.OrderBy(s => s).ToList();
        }
예제 #2
0
 /// <summary>
 /// Simple helper to create a writer on an existing file. Dispose the writer when finished.
 /// </summary>
 public static TextWriter CreateWriter(this IFileService fileService, string path)
 {
     return new StreamWriter(fileService.CreateFileStream(path, FileMode.Append, FileAccess.Write));
 }
예제 #3
0
 /// <summary>
 /// Simple helper to create a reader on an existing file. Dispose the reader when finished.
 /// </summary>
 public static TextReader CreateReader(this IFileService fileService, string path)
 {
     return new StreamReader(fileService.CreateFileStream(path));
 }
예제 #4
0
파일: Files.cs 프로젝트: JeremyKuhne/XTask
        /// <summary>
        /// Simple line counter for a file, no error handling
        /// </summary>
        public static int CountLines(this IFileService fileService, string path)
        {
            int lineCount = 0;
            bool trailingCharacter = true;

            // 8K buffer
            char[] buffer = new char[8 * 1024];

            Stream stream = fileService.CreateFileStream(path);
            using (StreamReader reader = new StreamReader(stream))
            {
                int read;
                while ((read = reader.Read(buffer, 0, buffer.Length)) > 0)
                {
                    for (int i = 0; i < read; i++)
                    {
                        if (buffer[i] == '\n')
                        {
                            trailingCharacter = false;
                            lineCount++;
                        }
                        else
                        {
                            trailingCharacter = true;
                        }
                    }
                }

                if (trailingCharacter) { lineCount++; }
            }

            return lineCount;
        }
예제 #5
0
파일: Files.cs 프로젝트: JeremyKuhne/XTask
        /// <summary>
        /// Reads lines from the given path
        /// </summary>
        public static IEnumerable<string> ReadLines(this IFileService fileService, string path)
        {
            Stream stream = fileService.CreateFileStream(path);

            using (StreamReader reader = new StreamReader(stream))
            {
                string line = reader.ReadLine();

                while (line != null)
                {
                    yield return line;
                    line = reader.ReadLine();
                }
            }
        }
예제 #6
0
파일: Files.cs 프로젝트: JeremyKuhne/XTask
        /// <summary>
        /// Returns the MD5 hash for the given file's contents, or null if failed
        /// </summary>
        public static byte[] GetHash(this IFileService fileService, string path)
        {
            try
            {
                // Console.WriteLine("Hashing {0}...", localPath);
                if (!string.IsNullOrEmpty(path) & fileService.FileExists(path))
                {
                    using (Stream fileStream = fileService.CreateFileStream
                        (path, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        return s_MD5.ComputeHash(fileStream);
                    }
                }
            }
            catch (IOException)
            {
                // If there is an I/O problem, do nothing and allow this method to return null.
            }

            return null;
        }