Member Avatar for iamthwee

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.

Excizted commented: Great job and good of you putting it up for others +1
kvprajapati commented: Thanks buddy. +10
nbaztec commented: A job well done. +1
Member Avatar for iamthwee

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<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;
printf("value fetched from file: %.8x\n", array);
}
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;
printf("value fetched from file: %.8x\n", array);
}
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; 
}

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! :)

Thanks iamthwee.

Working nicely! See.

commented: Glad to be of help +11

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

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

Member Avatar for iamthwee

^^ 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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.