dotFusion.net

Source code numberings remover (C++ Rewrite)

Posted on: January 23rd, 2009.
Category: C++

This is the C++ rewrite of the Numberings remover tool. It compiles with MingW/GCC and any other major C++ compiler under Windows. Under Linux it is tested with GCC only. I think it should work fine on some other Unix-like operating systems but it is not tested. If you test it on some OS other than Linux or Windows you can send me your fixes/suggestions and/or the results of the test.
 

Here is the source code:

#ifndef __linux__
       #include <conio.h>
#else
       #include "getch.h"
#endif
 
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
 
using namespace std;
 
    void RemStartingChars(string &input, bool spacesRemoved)
    {
         if (!spacesRemoved)
               while (input[0] == ' ')
                    input = input.substr( 1, strlen(input.c_str()) );
 
         char numbers[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
         int i = -1;
         bool hasMore = false, tmp = false;
 
         while (numbers[++i])
               while(tmp = (input[0] == numbers[i]))
               {
                     hasMore = hasMore || tmp;
                     input = input.substr( 1, strlen(input.c_str()) );
               }
 
         if (hasMore)
            RemStartingChars(input, true);
    }
 
    int main(int argc, char *argv[])
    {
		cout<<"Source code numberings remover (C++ rewrite) 0.1"<<endl;
		cout<<"Written by Iskren Slavov"<<endl;
		cout<<"Copyright (C) 2009"<<endl;
 
		cout<<"\nThis computer software must be ";
		cout<<"redistributed and edited under the terms of GNU GPLv2. ";
		cout<<"For more information about licensing read LICENSE.TXT\n\n";
 
 
        if (argc > 1)
        {
                 ifstream inFile;
                 ofstream outFile;
 
                 inFile.open(argv[1]);
                 outFile.open(argv[2]);
 
                 if (!inFile)
                 {
                    cerr<<"Unable to open file: "<<argv[1]<<endl;
                    system("PAUSE");
                    exit(1);
                 }
 
                 string input;
                 cout<<"=========================\nConverting... ";
                 while (getline(inFile, input))
                 {
                       RemStartingChars(input, false);
                       outFile<<input<<endl;
                 }
                 cout<<"All done!\n========================="<<endl;
 
                 outFile.flush();
                 outFile.close();
                 inFile.close();
        }
		else
		{
			#ifdef __linux__
				char * progname = argv[0];
			#else
				char * progname = strrchr(argv[0], '\\')+1;
			#endif
            cout<<"======================"<<endl;
            cout<<"Description: Removes leading numbers from source code."<<endl;
            cout<<"Usage: "<<progname<<" <input-file.txt> <output-file.txt>"<<endl;
            cout<<"======================"<<endl;
      		cout<<"Press any key to exit . . ."<<endl;
            getch();
		}
        return 0;
    }

 
Download the full Source Code (C++)