Free , Fast and Small Automatic Formatter
for C , C++ , C# , Java Source Codes

Indenting source code

by Tal Davidson, Israel (E-mail: davidsont@bigfoot.com)

Main home Page http://sourceforge.net
Project Page http://www.sourceforge.net/projects/astyle

Artistic Style is a reindenter and reformatter of C, C++, C# and Java source code.

When indenting source code, we as programmers have a tendency to use both spaces and tab characters to create the wanted indentation. Moreover, some editors by default insert spaces instead of tabs when pressing the tab key, and other editors (Emacs for example) have the ability to "pretty up" lines by automatically setting up the white space before the code on the line, possibly inserting spaces in a code that up to now used only tabs for indentation.

Since the NUMBER of space characters showed on screen for each tab character in the source code changes between editors (until the user sets up the number to his liking...), one of the standard problems facing programmers when moving from one source code editor to another is that code containing both spaces and tabs that was up to now perfectly indented, suddently becomes a mess to look at when changing to another editor. Even if you as a programmer take care to ONLY use spaces or tabs, looking at other peoples source code can still be problematic.

To address this problem I have created Artistic Style - a series of filters, written in C++, that automatically reindent & reformat C/C++/C#/Java source files. These can be used from a command line, or it can be incorporated as classes in another C++ program.

Read Release Notes

Read License

Artistic Style may be used and  distributed under the GNU General Public License (GPL).

New versions, Comments, Questions, Bugs and Ideas for Improvement:

To use from the command line:

1) Unzip astyle.zip and compile 'astyle' (read README.txt for instructions).

2) Either place the resulting executable file's directory in your PATH system-variable, or move the executable to a directory that appears in your PATH system-variable.

3) Run it with the following syntax:

astyle [options] < OriginalSourceFile > BeautifiedSourceFile

OR

astyle [options] Foo.java Bar.javaAnotherBar.java [ . . . ]

The < and > characters are used to redirect the files into standard input and out of standard output - don't forget them!!!

The newly indented file RETAINS the original file name, while a copy of the original file is created with a ".orig" appended to the original file name. Thus, after indenting Foo.java as above, the indented result will be named Foo.java, while the original pre-indented file will be renamed to Foo.java.orig .

Options:

Not specifying any option will bring to C/C++/C# style indentation, with a default of 4 spaces per indent, and NO formatting.

Options may be written in two different ways:

  1. Long options:

    These options start with '--', and must be written one at a time
    (e.g. --brackets=attach --pad --indent=spaces=4 ).

  2. Short Options:

    These options start with a single '-', and may be appended together.
    Thus, writing -bps4 is the same as writing -b -p -s4 .

default options file may be used to define default options:

  • Artistic Style looks for the default options file in the following order: 
    1. The value of the the system variable ARTISTIC_STYLE_OPTIONS if one exists.
    2. The file named .astylerc in the directory pointed to by the HOME system variable (i.e. $PATH/.astylerc)
    3. The file named .astylerc in the directory pointed to by the HOMEPATH system variable (i.e. %HOMEPATH%\.astylerc)
  • Options may be set apart by new-lines, tabs or spaces.
  • Long options may be written in the options file without the preceding '--'.
  • Lines within the options file that begin with '#' are considered line-comments.
  • Example contents of a default options file:

    # default parsing is of java files
    mode=java
    # brackets should be attached to pre-bracket lines
    brackets=attach
    # set 6 spaces per indent<tt><br>indent=spaces=6
    # indent switch blocks
    indent-switches

    # suffix of original files should be .pre
    suffix=.pre

The following predefined style options are currently avaiable:

--style=ansi
ANSI style formatting/indenting.

namespace foospace
{
    int Foo()
    
{
        if (isBar)
        
{
            bar();
            return 1;
        
}
        else
            return 0;
    
}
}

--style=kr
Kernighan&Ritchie style formatting/indenting.

namespace foospace {
    int Foo()
 {
        if (isBar)
 {
            bar();
            return 1;
        
} else
            return 0;
    
}
}

--style=linux
Linux style formatting/indenting (brackets are broken apart from class/function declarations, but connected to command lines, and indents are set to 8 spaces).

namespace foospace
{
        int Foo()
        
{
                if (isBar)
 {
                        bar();
                        return 1;
                
} else 
                        return 0;
        
}
}

--style=gnu
GNU style formatting/indenting.

namespace foospace
  
{
    int Foo()
      
{
        if (isBar)
          
{
            bar();
            return 1;
          
}
        else
          return 0;
      
}
}

--style=java
Java style formatting/indenting.

class foospace {
    int Foo()
 {
        if (isBar)
 {
            bar();
            return 1;
        
} else
            return 0;
    
}
}

The following indentation options are currently available:

-c OR --mode=c
Indent a C, C++ or C# file.

-j OR --mode=java
Indent a Java file.

-s# OR --indent=spaces=#
Indent using # spaces per indent (e.g. -s4 OR --indent=spaces=4).

-t OR -t# OR --indent=tab=#
Indent using tab characters. Treat each tab as # spaces. If no '#' is set, treats tabs as 4 spaces.

-T# OR --force-indent=tab=#
Indent using tab characters. Treat each tab as # spaces. Uses tabs as indents in areas '--indent=tab' prefers to use spaces, such as inside multi-line statements.

-C OR --indent-classes
Indent 'class' blocks so that the headers 'public:', 'protected:' and 'private:' are indented in the class block.

The default:

class Foo
{
public:
    Foo();
    virtual ~Foo();
};

becomes:

class Foo
{
    public:
        Foo();
        virtual ~Foo();
};

-S OR --indent-switches
Indent 'switch' blocks so that the 'case XXX:' headers are indented in the class block.

The default:

switch (foo)
{
case 1:
    a += 2;
    break;

default:
    a += 2;
    break;
}

becomes:

switch (foo)
{
    case 1:
        a += 2;
        break;

default:
        a += 2;
        break;
}

-K OR --indent-cases
Indent 'case XXX:' lines so that they are flush with the comand lines under them.

The default:

switch (foo)
{
case 1:
    
{
        a += 2;
        break;
    
}

default:
    
{
        a += 2;
        break;
    
}
}

becomes:

switch (foo)
{
    case 1:
    
{
        a += 2;
        break;
    
}

    default:
    
{
        a += 2;
        break;
    
}
}

-B OR --indent-brackets
Add extra indentation to brackets.

The default:

if (isFoo) 
{ 
    bar(); 
} 
else 
{ 
    anotherBar(); 
}

becomes:

if (isFoo) 
    { 
    bar(); 
    } 
else 
    { 
    anotherBar(); 
    }

-G OR --indent-blocks
Add extra indentation to entire blocks.

The default:

if (isFoo) 
{ 
    bar(); 
} 
else 
    anotherBar();

becomes:

if (isFoo) 
    { 
        bar(); 
    } 
else 
    anotherBar();

-N OR --indent-namespaces
Add extra indentation to namespaces.

The default:

namespace foospace
{
class Foo
{
    public:
        Foo();
        virtual ~Foo();
};
}

becomes:

namespace foospace
{
    class Foo
    
{
        public:
            Foo();
            virtual ~Foo();
    
};
}

-L OR --indent-labels
Add extra indentation to labels so they they appear 1 indent less than the current indentation, rather than being flushed to the left (the default).

The default:

int foospace()
{
    while (isFoo)
    
{
        ...
        goto error;

error:
        ...
    
}
}

becomes:

int foospace()
{
    while (isFoo)
    
{
        ...
        goto error;

error:
        ...
    
}
}

-M# OR --max-instatement-indent=# 
Indent a maximal # spaces in a continuous statement, relatively to the previous line (e.g. --max-instatement-indent=40)

-m# OR --min-conditional-indent=#
Set the minimal indent that is added when a header is built of multiple-lines. This indent makes helps to easily separate the header from the command statements that follow. The default setting for this option is twice the current indent. (e.g. --min-conditional-indent=8)

The default:

// default setting makes this non-bracketed code clear
if (a < b
        || c > d)
    foo++;

// but creates an exaggerated indent in this bracketed code
if (a < b
        || c > d)
{
    foo++;
}

When setting --min-conditional=0 :

// setting makes this non-bracketed code less clear
if (a < b
    || c > d)
    foo++;

// but makes this bracketed code prettier
if (a < b
    || c > d)
{
    foo++;
}

--indent-preprocessor
Indent multi-line preprocessor definitions. should be used with --convert-tabs for proper results. Does a pretty good job, but can not perform miracles in obfuscated preprocessor definitions.

--convert-tabs
Converts tabs into single spaces.

-E OR --fill-empty-lines
Fill empty lines with the white space of their previous lines.

The following formatting options are currently available:

-b OR --brackets=break
Break brackets  from their pre-block statements ( i.e. ANSI C, C++ style ).

if (isFoo) 
{ 
    bar(); 
} 
else 
{ 
    anotherBar(); 
}

-a OR --brackets=attach
Attach brackets to their pre-block statements ( i.e. Java , K&R style ).

if (isFoo){ 
    bar(); 
} else { 
    anotherBar(); 
}

-l OR --brackets=linux
Break brackets from class/function declarations, but attach brackets to pre-block command statements.

namespace foospace
{
    int Foo()
    
{
        if (isBar)
 {
            bar();
            return 1;
        
} else
            return 0;
    
}
}

--brackets=break-closing-headers
When used with either '--brackets=attach' or '--brackets= linux' , breaks closing headers (e.g. 'else', 'catch', ...) from their immediately preceding closing brackets.).

if (isFoo){ 
    bar();
 
}else {
 
    anotherBar();
 
}

becomes:

if (isFoo) { 
    bar(); 
}
 
else { 
    anotherBar(); 
}

--break-blocks
Pad empty lines around header blocks (e.g. 'if', 'while'...).

isFoo = true;
if (isFoo) { 
    bar(); 
} else {
    anotherBar(); 

}
isBar = false;

becomes:

isFoo = true;

if (isFoo) { 
    bar(); 
} else {
    anotherBar(); 
}

isBar = false;

--break-blocks=all
Pad empty lines around header blocks (e.g. 'if', 'while'...). Treat closing header blocks (e.g. 'else', 'catch') as stand-alone blocks.

isFoo = true;
if (isFoo) { 
    bar(); 
} else {
    anotherBar(); 

}
isBar = false;

becomes:

isFoo = true;

if (isFoo) { 
    bar(); 

} else
 {
    anotherBar(); 
}

isBar = false;

--break-elseifs
Break 'else if()' header combinations into seperate lines.

if (isFoo) { 
    bar(); 
} else if (isBar()){
    anotherBar(); 

}

becomes:

if (isFoo) {
    bar(); 
} else 
    if (isBar())
{
        anotherBar();

    }

-p OR --pad=oper
Insert space padding around operators only.

if (isFoo) 
    a = bar((b-c)*a,*d--);

becomes:

if (isFoo) 
    a = bar((b - c) * a, *d--);

--pad=paren
Insert space padding around parenthesies only.

if (isFoo) 
    a = bar((b-c)*a,*d--);

becomes:

if ( isFoo ) 
    a = bar( ( b-c )*a, *d-- );

-POR --pad=all
Insert space padding around operators AND parenthesies.

if (isFoo) 
    a = bar((b-c)*a,*d--);

becomes:

if ( isFoo ) 
    a = bar( ( b - c ) * a, *d-- );

-o OR--one-line=keep-statements
Don't break complex statements and multiple statements residing in a single line.

if (isFoo)
{
  
    isFoo = false; cout << isFoo << endl;
}

remains as is.

if (isFoo) DoBar();

remains as is.

-O OR--one-line=keep-blocks
Don't break one-line blocks.

if (isFoo)
{ isFoo = false; cout << isFoo << endl; }

remains as is.

The following other options are currently available:

--suffix=####
Append the suffix #### instead of '.orig' to original filename. (e.g. --suffix=.prev)

-X OR --errors-to-standard-output
Print errors and help information to standard-output rather than to standard-error.
This option should be helpful for systems/shells that do not have this option, such as in Windows-95.

-v OR --version
Print version number.

-h OR -? OR --help
Print a help message and quit.
 

Acknowledgements:

-Special thanks to: Jim Watson, Fred Shwartz, W. Nathaniel Mills III, Danny Deschenes, Andre Houde, Richard Bullington, Paul-Michael Agapow, Daryn Adler for their patches and contributions to Artistic Style !!!

-Special thanks to Richard Bullington andMicroState for giving Artistic Style its original mailing-list !!!

-Special thanks toSourceForge for giving Artistic Style itshome !!!

-Special thanks to Paul Michael Agapow for creating a GUI interface for Artistic Style on the Macintosh.

-Thanks to all the dedicated beta-testers and bug notifiers !!!

ENJOY !!!

reference:

URL:http://www.desy.de/~njwalker/astyle.html

A Free , Fast and Small Automatic Formatter for C , C++ , C# , Java Source Codes的更多相关文章

  1. Artistic Style 3.1 A Free, Fast, and Small Automatic Formatter for C, C++, C++/CLI, Objective‑C, C#, and Java Source Code

    Artistic Style - Index http://astyle.sourceforge.net/ Artistic Style 3.1 A Free, Fast, and Small Aut ...

  2. Indenting source code

    Artistic Style 1.15.3 A Free , Fast and Small Automatic Formatterfor C , C++ , C# , Java Source Code ...

  3. ArtisticStyle----很好用的C/C++样式格式化工具

    下载地址:http://srgb.googlecode.com/files/AStyle_2.02_windows.7z 把astyle.exe 复制到 C:\WINDOWS 目录里,省的指定路径VC ...

  4. astyle代码格式化

    Artistic Style 1.24 A Free, Fast and Small Automatic Formatterfor C, C++, C#, and Java Source Code 项 ...

  5. 【转帖】ArtisticStyle----很好用的C/C++样式格式化工具

    下载地址:http://srgb.googlecode.com/files/AStyle_2.02_windows.7z 把astyle.exe 复制到 C:\WINDOWS 目录里,省的指定路径VC ...

  6. BlackArch-Tools

    BlackArch-Tools 简介 安装在ArchLinux之上添加存储库从blackarch存储库安装工具替代安装方法BlackArch Linux Complete Tools List 简介 ...

  7. JavaScript 空间分析库——JSTS和Turf【转】

    https://blog.csdn.net/neimeng0/article/details/80363468 前言 项目中有管线的空间拓扑关系查询需求,在npm中检索到JSTS和Turf两个Java ...

  8. 【译】android的审计和hacking工具

    原文:Best Android Tools For Security Audit and Hacking android系统占移动市场份额的80%且有恶意软件,这是一个问题.Hacker会对手机恶意操 ...

  9. RASPBERRY PI 外设学习资源

    参考: http://www.siongboon.com/projects/2013-07-08_raspberry_pi/index.html Raspberry Pi         Get st ...

随机推荐

  1. 【剑指offer】堆栈推弹出序列

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/26077863 剑指offer上的第22题,九度OJ上AC. 题目描写叙述: 输入两个整数序 ...

  2. 发布一个参考ssdb,使用go类似的实现redis高性能nosql:ledisdb

    起因 ledisdb是一个參考ssdb.採用go实现,底层基于leveldb,相似redis的高性能nosql数据库,提供了kv,list,hash以及zset数据结构的支持. 我们如今的应用极大的依 ...

  3. 一些有用的javascript实例分析(一)

    原文:一些有用的javascript实例分析(一) 本文以http://fgm.cc/learn/链接的实例索引为基础,可参见其实际效果.分析和整理了一些有用的javascript实例,相信对一些初学 ...

  4. 采纳ajax提交POST样本数据

    问题叙述性说明 我们会form该input和checkbox提交给异步数据phpserver.处理后的回. 提交之后显示的位置: 难点分析 採用from表单的onsubmit属性阻止表单的提交 < ...

  5. 终于会用c#中的delegate(委托)和event(事件)了

    一.开篇忏悔 对自己最拿手的编程语言C#,我想对你说声对不起,因为我到现在为止才明白c#中的delegate和event是怎么用的,惭愧那.好了,那今天就趁月黑风高的夜晚简单来谈谈delegate和e ...

  6. uva757 - Gone Fishing(馋)

    题目:uva757 - Gone Fishing(贪心) 题目大意:有N个湖泊仅仅有一条通路将这些湖泊相连. 每一个湖泊都会给最開始5分钟间隔内能够调到的鱼(f).然后给每过5分钟降低的鱼的数量(d) ...

  7. 前端学习笔记(zepto或jquery)——对li标签的相关操作(四)

    对li标签的相关操作——五种方式给奇数项li标签设置样式 demo演示: 1 2 3 4 5 6 7 // 详解: 通常我们为多个li添加样式时常用的是使用filter,但我们在第三节中可以看到fil ...

  8. HDOJ 3518 Boring counting

    SAM基本操作 拓扑寻求每个节点  最左边的出现left,最右边的出现right,已经有几个num ...... 对于每个出现两次以上的节点.对其所相应的一串子串的长度范围 [fa->len+1 ...

  9. C++学习笔记32 断言函数

    首先,让我们来看看百度百科上"断言函数"定义说明: 1定义fr=aladdin#" class="nslog:1019" title="编辑 ...

  10. iOS开展 - 中国 iOS/Mac 开发博客列表

    博客地址 RSS地址 OneV's Den http://onevcat.com/atom.xml 破船之家 http://beyondvincent.com/atom.xml NSHipster h ...