954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Code formatting for c++/c/java

I don't know if this is helpful for moderators but I've put together a code formatter in dotnet which automatically beautifies someone's code.

Numerous times you will see newbies posting without code tags. Now the moderators can easily quote the post so the indentation is preserved, but sometimes the posters do not even indent their code or use tabs instead of spaces.

The main exe is called CFormat.

Dotnet 2.0 is required, only works on windows although I guess I could write one for linux.

Any questions... Post them here.

Notes
It also, removes comments as well (if necessary) and uses dotnets regex utility.

It uses the Astyle engine to do the beautifying and can be customized in the stuff.bat file.

Attachments Debug.zip (58.97KB)
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Take this one such example

http://www.daniweb.com/forums/post1228489.html#post1228489

int writefile(char *str, int size)
{
uint32_t c;
ofstream outfile;
outfile.open("output.txt");
for(int i=0;i>array[i];
printf("value fetched from file: %.8x\n", array[i]);
}
hash = array[ctr];
printf("Current value fetched from file: %.8x\n", hash);
myfile.close();
return hash;
}

int main()
{
//uint32_t c;
//int ctr =0;
char str[80];
cout<<"enter string: ";
cin.get(str,80);
cout<>array[i];
printf("value fetched from file: %.8x\n", array[i]);
}
hash = array[ctr];
printf("value fetched from file: %.8x\n", hash);
myfile.close();
*/

}

Ok if you quote it you can see indentation has been used but it is still pretty much awful.

After beautification:

int writefile ( char *str, int size )
{
   uint32_t c;
   ofstream outfile;
   outfile.open ( "output.txt" );
   for ( int i = 0; i < size; i++ )
   {
      c = hashlittle ( str, strlen ( str ), 0 );
      printf ( "%.8x\n", c );
      outfile << c << endl;
      sprintf ( str, "%u", c );
   }
   outfile.close();
   return 1;
}

uint32_t fetchfile ( int ctr )
{
   uint32_t hash, array[10];
   ;
   ifstream myfile ( "output.txt" );
   myfile.is_open();
   for ( int i = 0; i < 10; i++ )
   {
      myfile >> array[i];
      printf ( "value fetched from file: %.8x\n", array[i] );
   }
   hash = array[ctr];
   printf ( "Current value fetched from file: %.8x\n", hash );
   myfile.close();
   return hash;
}

int main()
{
//uint32_t c;
//int ctr =0;
   char str[80];
   cout << "enter string: ";
   cin.get ( str, 80 );
   cout << endl;
   writefile ( str, 10 );
   fetchfile ( 0 );
   return 0;
   /*
   ofstream outfile;
   outfile.open("output.txt");
   for(int i=0; i<10; i++)
   {
   c = hashlittle(str, strlen(str), 0);
   printf("%.8x\n", c);
   outfile<<c<<endl;
   sprintf(str, "%u", c);
   }
   uint32_t hash, array[10]; ;
   ifstream myfile ("output.txt");
   myfile.is_open();
   for(int i=0; i<10; i++)
   {
   myfile>>array[i];
   printf("value fetched from file: %.8x\n", array[i]);
   }
   hash = array[ctr];
   printf("value fetched from file: %.8x\n", hash);
   myfile.close();
   */

}


After beautifcation removing comments...

int writefile ( char *str, int size )
{
   uint32_t c;
   ofstream outfile;
   outfile.open ( "output.txt" );
   for ( int i = 0; i < size; i++ )
   {
      c = hashlittle ( str, strlen ( str ), 0 );
      printf ( "%.8x\n", c );
      outfile << c << endl;
      sprintf ( str, "%u", c );
   }
   outfile.close();
   return 1;
}

uint32_t fetchfile ( int ctr )
{
   uint32_t hash, array[10];
   ;
   ifstream myfile ( "output.txt" );
   myfile.is_open();
   for ( int i = 0; i < 10; i++ )
   {
      myfile >> array[i];
      printf ( "value fetched from file: %.8x\n", array[i] );
   }
   hash = array[ctr];
   printf ( "Current value fetched from file: %.8x\n", hash );
   myfile.close();
   return hash;
}

int main()
{
   char str[80];
   cout << "enter string: ";
   cin.get ( str, 80 );
   cout << endl;
   writefile ( str, 10 );
   fetchfile ( 0 );
   return 0; 
}
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Very nice! :)
I think that could come in handy for someone at least ;)
I could probably use it - would it be too hard to build it for Linux? :)
You could also put up the source, then maybe I could rewrite it myself...
If you want to of course! :)

Excizted
Posting Whiz
309 posts since Oct 2009
Reputation Points: 94
Solved Threads: 27
 

Thanks iamthwee.

Working nicely! See .

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

Well done, I was thinking of myself to incorporate a formatting technique to use with my wrapper program for Alex's Syntax Highlighter.

nbaztec
Posting Pro in Training
475 posts since May 2010
Reputation Points: 57
Solved Threads: 60
 

Thank you for the effort, but I always use the inbuild beautifier in VS. I like the idea of removing comments however.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

^^ Thanks for the feedback Nick. I appreciate there are IDEs out there that auto format code. Code blocks is another which uses Astyle as well. There are even online ones.

This was just meant to quickly help mods fix code without having to open and wait for their bloated IDE to load up with a few extra facilities to boot.

Cheers.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: