Code Golf Extended...

by Mwwhited 10. June 2009 22:06

After running across a "code golf" question this afternoon on stack overFlow I answered with a short answer...


using (var inFs = File.OpenText(args[0]))
using (var outfs = File.AppendText(args[1]))
{
    outfs.Write("<table border="0">");
    while (!inFs.EndOfStream)
        outfs.Write("<tbody><tr><td>" + string.Join("</td><td>", inFs.ReadLine().Split(',')) + "</td></tr>");
    outfs.Write("</tbody></table>");
}

... I was thinking about how I could get this in LINQ but that solution would break one of the rules. It needs to be able to handle 500MB files. But by demand here is the long answer ...



using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;

namespace cvs2html_codegolf
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var lp = args[0].Load())
                lp.Select(l => "<td> </td> " + string.Join("<td> </td> ", l.Split(',')) + "")
                .Write("<table border="0">", "<tbody></tbody></table>", args[1]);
        }
    }
    public static class LineProcessorLinq
    {
        public static LineProcessor Load(this string fileName)
        {
            return new LineProcessor(fileName);
        }

        public static void Write(this IEnumerable lines, string header, string footer, string fileName)
        {
            using (var fs = File.AppendText(fileName))
                lines.Write(header, footer, fs);
        }

        public static void Write(this IEnumerable lines, string header, string footer, StreamWriter writer)
        {
            if (writer == null)
                throw new ArgumentNullException("writer");

            if (!string.IsNullOrEmpty(header))
                writer.Write(header);

            foreach (var line in lines)
                writer.Write(line);

            if (!string.IsNullOrEmpty(footer))
                writer.Write(footer);
        }
    }
    public class LineProcessor : Component, IEnumerable
    {
        private StreamReader _reader = null;

        public LineProcessor(string fileName) : this(File.OpenText(fileName)) { }

        public LineProcessor(StreamReader reader)
        {
            if (reader == null)
                throw new ArgumentNullException("reader");
            _reader = reader;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && _reader != null)
                _reader.Close();
        }

        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            var currentPos = _reader.BaseStream.Position;
            while (!_reader.EndOfStream)
                yield return _reader.ReadLine();
            if (_reader.BaseStream.CanSeek)
                _reader.BaseStream.Seek(currentPos, SeekOrigin.Begin);
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); }

        #endregion
    }
}



Tags: , ,

Programming

Comments are closed

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen