dotFusion.net

Source code numbering remover

Posted on: January 20th, 2009.
Category: C#

I’ve been copying a lot of source code from different sites recently. But when I copy the source code it is being pasted with numberings in front of every line.
 

So I wrote this program to remove all the numbers before each line of source code.

 
Here’s the nuberings remover source:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
 
namespace RemFirstNums
{
    class Program
    {
        static void RemStartSymbols(ref String input, bool firstSpacesRemoved)
        {
            String numbers = "0123456789";
            CharEnumerator charEnum = numbers.GetEnumerator();
            bool foundMore = false, tmp = false;
 
            if (!firstSpacesRemoved)
                while (input.StartsWith(" ") || input.StartsWith("\\s+"))
                    input = input.Substring(1, input.Length - 1);
 
            while (charEnum.MoveNext())
                while (tmp = input.StartsWith(charEnum.Current.ToString()))
                {
                    foundMore = foundMore || tmp;
                    input = input.Substring(1, input.Length - 1);
                }
            if (foundMore)
                RemStartSymbols(ref input, true);
        }
        static void Main(String[] args)
        {
            TextReader TextRead = null;
            TextWriter TextWrite = null;
 
            Console.WriteLine("NumberingRemover for C# 0.1");
            Console.WriteLine("Written by Iskren Slavov");
            Console.WriteLine("Copyright (C) 2009");
 
            Console.Write("\nThis computer software must be ");
            Console.Write("redistributed and edited under the terms of GNU GPL v2. ");
            Console.Write("For more information about licensing read LICENSE.TXT\n\n");
 
            try
            {
                if ((args[0].Trim() != null) && (args[1].Trim() != null))
                {
                    String workStr = "";
                    TextRead = new StreamReader(args[0]);
                    TextWrite = new StreamWriter(args[1]);
 
                    Console.Write("=========================\nConverting... ");
                    while ((workStr = TextRead.ReadLine()) != null)
                    {
                        RemStartSymbols(ref workStr, false);
                        TextWrite.WriteLine(workStr);
                    }
                    Console.WriteLine("All done!\n=========================");
                }
 
                TextRead.Close();
                TextWrite.Close();
            }
            catch (IOException ex)
            {
                Console.Error.WriteLine("{0}", ex.Message);
                Console.WriteLine("\nPress any key to exit . . .");
                Console.ReadKey();
            }
            catch (IndexOutOfRangeException)
            {
                Console.WriteLine("==================");
                Console.WriteLine("Description: Removes leading numbers from source code.");
                Console.WriteLine("Usage: NumberingRemover.exe <input-file.txt> <output-file.txt>");
                Console.WriteLine("==================");
                Console.WriteLine("\nPress any key to exit . . .");
                Console.ReadKey();
            }
        }
    }
}

 
Download the source code and binaries (MS Visual C# Express edition project)

I hope this little program will be helpful for you, developers. I’m thinking of a rewrite in C++, so stay tuned…