Sign in to follow this  
Followers 0
Markus

Tidy tool for C++

4 posts in this topic

Hi

I just wrote a mini tidy tool for cpp, as some weeks ago i was using the dev-cpp IDE my indents were very bad placed and the source was nearly impossible to read. Now i changed my IDE but wanted to have my old cpp sources. As it would have been much work to do all indents afterwards on my own i wrote this script. (I'm sure there are scripts available, that do the same as my prog, but it needed less time to write that tool than search such a program)

For example it changes something like that:

for(int i=1; i<=line_nr; i++)
                {
            for(int k=1; k<=gradue_of_indents; k++)
                    {
                lines[i]="\t" + lines[i];
        }
                    for(int u=0; u<=lines[i].length(); u++)
            {
                    if(lines[i].substr(u,1)=="{" )
                {
                    gradue_of_indents++;
            }
                else if(lines[i].substr(u,1)=="}" )
                {
  gradue_of_indents--;
           debugcounter++; 
}
        }
        lines[i]=lines[i].substr(debugcounter,lines[i].length()+debugcounter);
        debugcounter=0;
    }

to that:

for(int i=1; i<=line_nr; i++)
{
    for(int k=1; k<=gradue_of_indents; k++)
    {
        lines[i]="\t" + lines[i];
    }
    for(int u=0; u<=lines[i].length(); u++)
    {
        if(lines[i].substr(u,1)=="{" )
            {
                gradue_of_indents++;
            }
        else if(lines[i].substr(u,1)=="}" )
        {
            gradue_of_indents--;
            debugcounter++; 
        }
    }
    lines[i]=lines[i].substr(debugcounter,lines[i].length()+debugcounter);
    debugcounter=0;
}

I don't know, if it's useful for you, for me it was ;)

Here's the code (just copy it as a usual cpp console project):

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
char * sourcepath="C:\\Dokumente und Einstellungen\\Markus\\Desktop\\part_to_clean.txt";
char * destpath="C:\\Dokumente und Einstellungen\\Markus\\Desktop\\part_to_clean_cleaned.txt";
int main()
{
    ifstream work_file;
    work_file.open(sourcepath);
    string lines[10000];
    int line_nr=0;
    string temp_line;
    while (getline(work_file, temp_line) )
    {
        lines[++line_nr] = temp_line;
    }

    //remove all indents and empties
    for(int i=1; i<=line_nr; i++)
    {
        for(int u=0; u<=lines[i].length(); u++)
        {
            if(lines[i].substr(u,1)!=" " && lines[i].substr(u,1)!="\t")
            {
                if(u>0)
                {
                    lines[i]=lines[i].substr(u--,lines[i].length()-u);
                }
                break;
            }
        }
    }

    //add right indents
    int gradue_of_indents=0;
    int debugcounter=0;
    for(int i=1; i<=line_nr; i++)
    {
        for(int k=1; k<=gradue_of_indents; k++)
        {
            lines[i]="\t" + lines[i];
        }
        for(int u=0; u<=lines[i].length(); u++)
        {
            if(lines[i].substr(u,1)=="{" )
            {
                gradue_of_indents++;
            }
            else if(lines[i].substr(u,1)=="}" )
            {
                gradue_of_indents--;
                debugcounter++; //wird gebraucht, da schließende klammer eigtl nicht mehr soweit eingerückt wird wie gradue of indents am anfang angibt, daher wird wenn schließende klammer auftaucht debugger hoch gezählt und die tabs removt am ende
            }
        }
        lines[i]=lines[i].substr(debugcounter,lines[i].length()+debugcounter);
        debugcounter=0;
    }

    ofstream write_file;
    write_file.open(destpath);
    for (int i=1; i<=line_nr; i++)
    {
        cout<<lines[i]<<endl;
        write_file.write(lines[i].c_str(), lines[i].length() );
        write_file.write("\n", 1 );
    }

    system("PAUSE");
    return 0;
}

You only have to change the two vars "sourcepath" and "destpath", that's all, have fun with it^^

Markus


"It's easier to disintegrate an atom than a prejudice." (A.Einstein)---------------------------------------------------------------------------My C++ - tools:Tidy tool-->indents your c++ sourceCleanscript --> cleans autoit-code before compiling (co-author: peethebee)My tools:GUIBuilder-->build your window and get the source; german versionMy Games:OnlineGameCenter-->Online Chess and Connect4 with a rtf-chatSnake-->including a level editor to build your own levelsTetris-->the well known game, big funOther things:Tower of Hanoi-->perfect riddler with graphic output

Share this post


Link to post
Share on other sites



Markus, I appreciate the contributions in the C++ area you have made to the AutoIt community. I would like to recommend an IDE that has several notations built in.

Code::Blocks.

Again thank you for your additions,

JS


AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Share this post


Link to post
Share on other sites

Nice job, but what happens if it runs into code errors?

Share this post


Link to post
Share on other sites

Yeah, Code::Blocks seems to be quite nice. For sure much better than old dev-cpp.

Nice job, but what happens if it runs into code errors?

If there is a missing closing bracket or something like this, you might notice that very fast after tidying your source with that tool, because your indents could be a little strange than in the part following the missing bracket. :P


"It's easier to disintegrate an atom than a prejudice." (A.Einstein)---------------------------------------------------------------------------My C++ - tools:Tidy tool-->indents your c++ sourceCleanscript --> cleans autoit-code before compiling (co-author: peethebee)My tools:GUIBuilder-->build your window and get the source; german versionMy Games:OnlineGameCenter-->Online Chess and Connect4 with a rtf-chatSnake-->including a level editor to build your own levelsTetris-->the well known game, big funOther things:Tower of Hanoi-->perfect riddler with graphic output

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0