Example #1
0
        private static void Run(string[] args)
        {
            long read = 0, loc = 0;

            Options opts = ParseOptions(args);

            byte[] previous = new byte[opts.Output.BytesPerLine];
            byte[] buf = new byte[opts.Output.BytesPerLine];
            bool previousIdentical = false;
            Stream file;

            if (opts.Files.Count == 0)
            {
                file = new ForwardSeekingStream(Console.OpenStandardInput());
            }
            else
            {
                file = new ConcatenatedStream(opts.Files.ToArray(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            }

            using (file)
            {
                loc += file.Seek(opts.Skip, SeekOrigin.Begin);

                while (opts.Length == -1 || read < opts.Length)
                {
                    int len = file.Read(buf, 0, buf.Length);

                    if (!opts.Verbose && len == opts.Output.BytesPerLine && loc > opts.Skip)
                    {
                        bool identical = true;

                        for (int i = 0; i < opts.Output.BytesPerLine; i++)
                        {
                            if (previous[i] != buf[i])
                            {
                                identical = false;
                                break;
                            }
                        }

                        if (identical)
                        {
                            if (!previousIdentical)
                            {
                                Console.Out.WriteLine("*");
                            }

                            previousIdentical = true;
                            read += len;
                            loc += opts.Output.BytesPerLine;

                            continue;
                        }
                    }

                    previousIdentical = false;

                    Console.Out.Write(String.Format("{0,8:x8}", loc));

                    if (len == 0)
                    {
                        break;
                    }

                    int writelen = (opts.Length >= 0) ? (int)Math.Min(opts.Length - read, len) : len;

                    Console.Out.WriteLine(opts.Output.FormatLine(buf, writelen));

                    read += len;
                    loc += len;

                    Array.Copy(buf, previous, len);
                }

                file.Close();
            }
        }
Example #2
0
        private static void Run(string[] args)
        {
            long read = 0, loc = 0;

            Options opts = ParseOptions(args);

            byte[] previous          = new byte[opts.Output.BytesPerLine];
            byte[] buf               = new byte[opts.Output.BytesPerLine];
            bool   previousIdentical = false;
            Stream file;

            if (opts.Files.Count == 0)
            {
                file = new ForwardSeekingStream(Console.OpenStandardInput());
            }
            else
            {
                file = new ConcatenatedStream(opts.Files.ToArray(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            }

            using (file)
            {
                loc += file.Seek(opts.Skip, SeekOrigin.Begin);

                while (opts.Length == -1 || read < opts.Length)
                {
                    int len = file.Read(buf, 0, buf.Length);

                    if (!opts.Verbose && len == opts.Output.BytesPerLine && loc > opts.Skip)
                    {
                        bool identical = true;

                        for (int i = 0; i < opts.Output.BytesPerLine; i++)
                        {
                            if (previous[i] != buf[i])
                            {
                                identical = false;
                                break;
                            }
                        }

                        if (identical)
                        {
                            if (!previousIdentical)
                            {
                                Console.Out.WriteLine("*");
                            }

                            previousIdentical = true;
                            read += len;
                            loc  += opts.Output.BytesPerLine;

                            continue;
                        }
                    }

                    previousIdentical = false;

                    Console.Out.Write(String.Format("{0,8:x8}", loc));

                    if (len == 0)
                    {
                        break;
                    }

                    int writelen = (opts.Length >= 0) ? (int)Math.Min(opts.Length - read, len) : len;

                    Console.Out.WriteLine(opts.Output.FormatLine(buf, writelen));

                    read += len;
                    loc  += len;

                    Array.Copy(buf, previous, len);
                }

                file.Close();
            }
        }