0

How do I reformat the 'for' statement separated into three lines into one, in a automatically generated c++ code? I tried uncrustify, but I could not make it format the parts within parenthesis. sed seems not suitable for this. Could any one suggest me other formatter or some linux command that can take care of it?

Code to be formatted:

void func(double* s, Quaternion& a, int n)
{
 int size((n<4)?n:4);
 for (int i=0;
      i<size;
           i++)
 {
  a[i] = s[i];
 }
}

I want the 'for' statement above to be formatted into one line, like:

 for (int i=0; i<size; i++)
user22097
  • 229
  • 3
  • 13
  • 2
    If the code is automatically generated, why try to format it? It will be changed again the next time the file is generated anyway. – Some programmer dude Sep 24 '13 at 09:23
  • @JoachimPileborg well I guess that's why he asks for a way to do it automatically. – flyx Sep 24 '13 at 09:24
  • In addition to JoachimPileborgs's comment you have one more option - edit the code that generates this. – Ivaylo Strandjev Sep 24 '13 at 09:25
  • @IvayloStrandjev The generated code has few newlines. I replaced ';' with ';\n' using sed. Now I am bothered with ';' in for-statements. (Why to format? To aid debugging.) – user22097 Sep 24 '13 at 23:04

2 Answers2

2

Try clang-format. It's kinda new, but a talk on GoingNative2013 showed promising results.

mitchnull
  • 6,161
  • 2
  • 31
  • 23
  • `sudo apt-get install clang` did not give me clang-format. `sudo apt-get install clang-format` didn't work. Could you help me with the correct command? – user22097 Sep 25 '13 at 00:01
  • @user22097 You have to follow documentation on installing extra tools here http://clang.llvm.org/docs/ClangTools.html – kgpdeveloper Jan 15 '14 at 10:25
1

You can make use of gnu indent.

Saying indent -npsl inputfile.c for your snippet would result in:

void func (double *s, Quaternion & a, int n)
{
  int size ((n < 4) ? n : 4);
  for (int i = 0; i < size; i++)
    {
      a[i] = s[i];
    }
}

The manual can be accessed here.

devnull
  • 118,548
  • 33
  • 236
  • 227