views:

719

answers:

4

I wonder if there exist some free software that let me pass a C file and it outputs a more structured C-file.

I'm dealing with a short piece of C-Code that has been written long ago. I'd like to extract a clever algorithm from it, but working with the code itself is hard because it lacks everything that makes C-code readable.

Just a short example:

node *first(node *p)
{
  node *aux=p;

  if (aux)
  do aux=aux->next;
  while(aux!=p && (!aux->intersect || aux->intersect && aux->visited));
  return aux;
}

What I'd like to get as an output would be something like this:

node *first(node *p)
{
  node *aux=p;

  if (aux)
  {
    do 
    { 
      aux=aux->next;
    } 
    while( (aux!=p) && (!aux->intersect || (aux->intersect && aux->visited)));
  }
  return aux;
}

Does such an automatic code cleanup tool exist?

+3  A: 

Try GreatCode

Ferruccio
+7  A: 

You are looking for indent very flexible and well documented code beautifier.

Ilya
+3  A: 

Artistic Style is another source code beautifier for C, C++, C#, and Java code that will do what you want.

ChrisN
+1  A: 

indent is the classic tool for the job, as Ilya mentions. What I'm not sure, though, is whether it ever adds the braces as requested for the 'do { ... } while (...)' loop or the extra parentheses in the conditional of the same loop. I think not.

<subjective> I would also configure to ensure there were spaces around the assignment and inequality operators. There should be a space after while and before the parenthesis; there should not be a space after the parenthesis.</subjective>

A really sophisticated tool would observe that the loop condition should be simplified. You know that the second term (after the or operator) will only ever be evaluated if the first is false, so the retest of aux->intersect is redundant.

Jonathan Leffler