1. 程式人生 > >ArtisticStyle----很好用的C/C++樣式格式化工具(支援VC6)

ArtisticStyle----很好用的C/C++樣式格式化工具(支援VC6)

下載地址:
http://srgb.googlecode.com/files/AStyle_2.02_windows.7z

把astyle.exe 複製到 C:\WINDOWS 目錄裡,省的指定路徑
VC6++ 設定方法
選單
->工具->定製->工具選單內容->新建選單,引數如下
命令列:astyle.exe
變數:
--style=k&r--brackets=break--indent=spaces--indent-cases--indent-preprocessor--pad-header--pad-oper--unpad-paren--keep-one-line-statements

--keep-one-line-blocks--convert-tabs $(FileName)$(FileExt)

初始目錄: $(FileDir)



VC2008 外部工具裡設定,還可以新增快捷鍵
命令:astyle.exe
引數:
--style=k&r--brackets=break--indent=spaces--indent-cases--indent-preprocessor--pad-header--pad-oper--unpad-paren--keep-one-line-statements--keep-one-line-blocks--convert-tabs $(ItemFileName)$(ItemExt)

初始目錄: $(ItemDir)

CodeBlocks 設定差不多,很多綠色版的已經設定好了


AStyle_2.02_windows.zip
bin 目錄裡有官方編譯好的

  Artistic Style 2.01
  Maintained by: Jim Pattee
  Original Author: Tal Davidson

Usage : astyle [options] Source1.cpp Source2.cpp [...]
  astyle [options] < Original > Beautified

When indenting a specific file, the resulting indented file RETAINS the


original file-name. The original pre-indented file is renamed, with a
suffix of ".orig" added to the original filename.   

在source Insight中加入Artistic Style實現程式碼的格式化

Artistic style是一個開源的用於格式化程式碼的專案,

1.首先安裝Source Insight(以下簡稱SI),然後下個Artistic Style(以下簡稱AS)的Windows版本,解壓。

解壓後有4個資料夾,bin目錄下就是編譯好的AS,src和doc下分別是原始碼和文件。

假設解壓路徑為E:\astyle121,則E:\astyle121\bin下就是AS的可執行程式,新增到SI中的方法如下:

2.在SI的選單中選擇“選項->自定義命令”,開啟自定義命令對話方塊,然後單擊新增,輸入一個命令名稱,這裡隨意輸入,它只用來在選單中顯示,比如輸入AS之後確定。然後在執行裡填入你的AS可執行程式名,需要輸入完整路徑以便SI找到,後面跟引數

--options=c.opt和檔名,表示用c.opt來做格式模板,檔名用SI的萬用字元%f代替。比如此例中應輸入:

E:\astyle121\bin\AStyle.exe --options=c.opt %f

選中“控制”項中的“首先儲存檔案”和“完整後終止”。其他預設。

3.然後點選“選單”按鈕,調出選單對話方塊。在左邊找到你剛剛新建的命令“自定義命令:AS”,單擊選中。在menu中選擇你要把這個命令新增到的選單,比如選擇“檢視”,在下面找到“<選單結束>”,單擊選中。然後點插入。此時AS命令就被插入到你的檢視選單下了,你可以在你的選單->檢視中找到他。

4.開啟你的原始碼,選擇試圖選單中的AS。SI會提示你檔案被外部編輯器修改,是否重新載入,選擇是。就可以看到格式化好的程式碼了。

快捷鍵的新增:

1.選項->鍵分配

2.左側找到“自定義命令:AS”,單擊,然後選擇分配新鍵,按下新的快捷鍵,儲存。

以後就可以不用去選單裡找,直接按快捷鍵就行了。

關於c.opt的建立:

新建個文字文件,寫入以下內容:

style=ansi
# set default parsing to c/cpp files
mode=c
# brackets should be attached to pre-bracket lines
brackets=break
# set 6 spaces per indent
indent=spaces=4
# indent switch blocks
indent-switches
# suffix of original files should be .pre
suffix=.pre
#Add extra indentation to namespace blocks
indent-namespaces
max-instatement-indent=80
min-conditional-indent=0
pad=oper

然後把文字文件的檔名改為c.opt

Artistic Style 2.02

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


Artistic Style is a source code indenter, formatter, and beautifier for the C, C++, C# and Java programming languages.

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 (unless the user sets up the number to his liking...), one of the standard problems programmers are facing when moving from one editor to another is that code containing both spaces and tabs that was up to now perfectly indented, suddenly 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 people's source code can still be problematic.

To address this problem, Artistic Style was created - a filter written in C++ that automatically re-indents and re-formats C / C++ / C# / Java source files. It can be used from a command line, or it can be incorporated as classes in another C++ program.

Linux Version Install

GCC Compiler

To compile using the GCC compiler you must have GCC (3.1 or better) installed. There is no autoconf dependency. To build the Artistic Style configurations use the makefile located in the astyle/build/gcc directory. The executables will be in the astyle/build/gcc/bin directory. To build the command line configuration enter the following:

cd astyle/build/gcc
make

To build the other astyle configurations you can enter the file name or a symbolic name. Entering "make astyle" or "make release" will build the command line configuration. Following are the symbolic names and file names (in parens) of the various configurations:

  • release builds the Artistic Style command line program (astyle). This is the default option.
  • shared builds the Artistic Style program as a shared library (libastyle.so).
  • static builds the Artistic Style program as a static library (libastyle.a).
  • debug builds the Artistic Style command line program with debugging information (astyled).
  • shareddebug builds the Artistic Style program as a shared library with debugging information (libastyled.so).
  • staticdebug builds the Artistic Style program as a static library with debugging information (libastyled.a).
  • all builds all of the above configurations.

    The following Java shared library builds are separate from the above. They include a Java Native Interface (JNI) and require that the Java Development Kit (JDK) be installed. The environment variable JAVA_HOME should be defined. It defines the install directory for the JDK. The makefile also has default install directories for Java 5 and 6. If the compile cannot find the file jni.h, either set the variable or change the value in the makefile.

  • java builds the Artistic Style program as a shared library which includes the JNI (libastylej.so).
  • javadebug builds the Artistic Style program as a shared library which includes the JNI and debugging information (libastylejd.so).
  • javaall builds all of the above java configurations.

More than one configuration can be built at the same time. For example, to build all of the release configurations enter:

cd astyle/build/gcc
make release shared static

Usage

Artistic style is a console program that receives information from the command line. The format of the command line is:

astyle  [options]  SourceFile1  SourceFile2  SourceFile3  [ . . . ]

The block parens [ ] indicate that more than one option or more than one filename can be entered. They are NOT actually included in the command. For the options format see the following Options section.

  Example to format a single file:

astyle  --style=allman  /home/user/project/foo.cpp

  Example to format all .cpp and .h files recursively:

astyle  --style=allman --recursive  /home/user/project/*.cpp  /home/user/project/*.h

Another option will format a single file and change the name:

astyle [options] < OriginalSourceFile > BeautifiedSourceFile 

The < and > characters are used to redirect the files into standard input (cin) and out of standard output (cout) - don't forget them! With this option only one file at a time can be formatted. Wildcards are not recognized, there are no console messages, and a backup is not created.

關於google的C++編碼規範

看google的C++編碼規範也有一段時間了,但是實施起來有些難度。

一來寫程式碼的時候有些細節就忘了,比如if後面的空格,花括號左邊的空格神馬的...

二來有時候拿到別人寫的程式碼,看不慣格式又不想去改,心裡還總是彆扭...

於是開始找格式化程式碼的工具,於是今天找到了Artistic Style。

今天記錄下Artistic Style的配置,怎麼新增為VS的外掛就不說了,網上一搜一大把。本人的配置命令如下:

--style=java -N -S -M -p -H -U -k1 $(ItemDir)$(ItemFileName)$(ItemExt)

--style=java是風格是java的編碼風格,選java是因為離google的編碼規範比較近;

-N是縮排namespace;

-S是縮排switch下的case子句;

-M是縮排函式多個引數放在不同行的情況,這個說不清楚,得放個程式碼示例:

格式化前:

1 fooArray[] = { red,2          green,3          blue };4 5 fooFunction(barArg1,6          barArg2,7          barArg3);

格式化後:

1 fooArray[] = { red,2                green,3                blue };4 5 fooFunction(barArg1,6             barArg2,7             barArg3);

-p是操作符左右留空格(++,--這樣的就不留了);

-H是'if', 'for', 'while'等關鍵字右邊留空格;

-U是移除括號裡外的空格;

-k1是*和&在表示指標和引用型別時,和型別名稱並緊,和變數名之間留空格。

後面三個就是VS的環境變量了,分別表示路徑名、檔名和副檔名。合起來就是要格式化的檔案的絕對路徑名。

工具不大,但是用起來超讚。

用法:

AStyle.exe --style=ansi --indent=spaces=4 -M80 -k1 -p -j -D -H -c -w test.cpp

--style=ansi ansi C風格程式碼 我個人喜歡用這個風格

     void Foo(bool isFoo)
     {
         if (isFoo)
         {
             bar();
         }
         else
         {
             anotherBar();
         }
     }

--indent=spaces=4 縮排用4個空格



-p  在運算子號左右加上空格

     if (foo==2)
         a=bar((b-c)*a,d--);
     becomes:

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

-j  給每個if增加大括號

     if (isFoo)
         isFoo = false;
     becomes:

     if (isFoo) {
         isFoo = false;
     }

-D  在小括號邊上增加上一個空格

     if (isFoo(a, b))
         bar(a, b);
     becomes:

     if ( isFoo( a, b ) )
         bar( a, b );

-H 在c/c++ 關鍵字的後面增加一個空格

     if(isFoo(a, b))
         bar(a, b);

     becomes:

     if (isFoo(a, b))
         bar(a, b);

-c

     把TAB字元替換成空格為什麼要把TAB替換成空格?主要是因為各種不同的編輯器對TAB的解釋不一樣造成的。

     有的編輯器把TAB解釋成4個空格,有的是8個,還有的是2個。這樣會破壞原有的程式碼的對齊的結構,

     因此最好把TAB替換成空格,這樣不管在任何編輯器下面程式碼都是以對齊的風格來顯示。

-k1 --align-pointer=type

     char *foo1;
     char &foo2;

     becomes (with align-pointer=type):

     char* foo1;
     char& foo2;

-M# 對定義的引數和變數進行對齊

     fooArray[] = { red,
             green,
             blue };

     fooFunction(barArg1,
             barArg2,
             barArg3);
     becomes (with larger value):

     fooArray[] = { red,
                   green,
                   blue };

     fooFunction(barArg1,
                 barArg2,
                 barArg3);

-w  對巨集進行對齊處理

     #define Is_Bar(arg,a,b) \
     (Is_Foo((arg), (a)) \
     || Is_Foo((arg), (b)))
     becomes:

     #define Is_Bar(arg,a,b) \
         (Is_Foo((arg), (a)) \
         || Is_Foo((arg), (b)))



看到上面這麼多的美化程式碼的引數,真的很hi。

AStyle.exe --style=ansi --indent=spaces=4 -M80 -k1 -p -j -D -H -c -w $(ItemFileName)$(ItemExt)
這個引數是我自已用的,大夥可以根據自已的風格來設定。

如果整合到VS當中呢?

1. 把AStyle.exe 拷到 “C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\”目錄下

2. Tools –> External Tools

如圖增加一個外部工具配置

C++製作的開原始碼美化工具:AStyle_33161



在Tools選單下面會多出一個Beautify點選它就可以來美化當前的檔案 如下圖:

C++製作的開原始碼美化工具:AStyle_33162



總體來說這個工具美化的效果還是很不錯的,喜歡的人大家都可以試試。

Astyle 的全稱是Artistic Style,某種程度是目前最好用的程式碼整理工具,很多所謂的程式碼整理工具其實都是拿這個東東改進,開發的。其網址在,http://astyle.sourceforge.net/,最新版本是2.02,基本已經穩定,最近升級很少了。

Astyle好在那兒呢,一方面他支援大部分程式碼格式,一方面他支援C, C++, C#, and Java等語言,一方面他可以編譯成LINUX版本,直接用命令列方式,一方面也可以和大部分IDE環境整合。所以其是編寫程式碼處理的利器。

另外,雖然很多IDE環境有自己的一些程式碼格式化方法,比如visual studio下選擇程式碼,快捷鍵Ctrl +KF(或者Alt +F8,Ctrl+E,F,Ctrl+E,D之類),就可以格式化整理程式碼。但是還是術有專攻,visual studio的程式碼格式化不會自動幫我將程式碼中間的舊有tab 替換成空格,在分析某些稍微複雜的語法(特別是模版)時,經常會出現換行對齊錯誤的問題,而且visual studio不同版本下,自己預設的某些對齊方式也有所不同。

所以我個人還是推薦定期用Astyle整理一下自己的程式碼,可以讓自己的程式碼更加養眼。從網站下載回來的Astyle有Windows編譯版本,也有原始碼。在Linux下你可以自己編譯。整日駭客帝國的兄弟也可以使用。

我們先介紹一下Astyle如何整合到IDE開放工具中,

如果單獨是命令列,Astyle的使用比較簡單,我一般都將選項放入到配置檔案裡面。當然如果你記憶力超凡,可以直接輸入引數另說。

astyle --options=astyle.conf  檔名稱   [生成檔名稱]

如果你和我一樣,雖然喜歡用visual studio基礎環境,你可以開啟選單Tools\External Tools。開啟外部工具設定介面。

你可以根據你自己的喜好一次格式化一個檔案,配置如下:

Title:  AStyle File

【說明:任意輸入】

Command: F:\MyTools\Astyle\bin\AStyle.exe

【說明:絕對路徑】

Arguments: --options=F:\MyTools\Astyle\bin\astyle.conf$(ItemFileName)$(ItemExt)

【說明:選項檔案也用絕對路徑,$(ItemFileName)$(ItemExt) 表示當前要處理的檔案】

Initial directory: $(ItemDir)    表示處理檔案的目錄

【說明:在那個目錄進行處理,一般選擇$(ItemDir)】

Use Output window: 勾選

【說明:將astyle的輸出放到output視窗】

也可以一次將Porject下所有的檔案全部格式化了。配置如下:

Title:  AStyle Project

【說明:任意輸入】

Command: F:\MyTools\Astyle\bin\AStyle.exe

【說明:絕對路徑】

Arguments: --options=F:\MyTools\Astyle\bin\astyle.conf*.h *.cpp *.hpp *.c

【說明:選項檔案也用絕對路徑,一次將所有的檔案都進行處理掉】

Initial directory: $(ItemDir)    表示處理檔案的目錄

【說明:在那個目錄進行處理,一般選擇$(ItemDir)】

Source insight的配置也大同小異。主要就是run這個地方配置成

F:\MyTools\Astyle\bin\AStyle.exe --options=F:\MyTools\Astyle\bin\astyle.conf %f

其中%f表示當前正在處理的檔案。

#指定檔案處理模式,C/C++模式,其他選擇還有cs,java

--mode=c

#ANSI標準的檔案格式,就是{另啟一行,}另啟一行的那種,其他選擇很多,不介紹了

--style=ansi

#縮排採用4個空格

--indent=spaces=4

#對於單行的語句增加括號

#Add brackets to unbracketed one line conditional statements  (e.g. 'if', 'for', 'while'...).

--add-brackets

#強制轉換TAB為空格

--convert-tabs

#將preprocessor (#define) 這類預定的語句,如果有多行時前面填充對齊,(是對單語句(多行)進行填充呀)

--indent-preprocessor

#Attach a pointer or reference operator (* or &) to either the variable type (left) or variable name (right), or place it between the type and name.

#*,&這類字元靠近型別

#--align-pointer=type

#*,&這類字元靠近變數名字,目前選擇這個,

--align-pointer=name

#在操作符號兩邊增加空格字元 ==

--pad-oper

#在幾個關鍵字後面增加空格

#insert space padding after paren headers only (e.g. 'if', 'for', 'while'...).

--pad-header

#在if,for,while等程式碼段前後增加空行,這個其實挺好,但如果你不喜歡,可以關閉

#Pad empty lines around header blocks (e.g. 'if', 'for', 'while'...).

--break-blocks

#去掉括號兩邊多餘的空格,

#暫時放棄,不是特別喜歡這個

#Remove extra space padding around parenthesis on the inside and outside.

#--unpad-paren

#備份檔案的字尾,預設是.orig,=none表示不進行備份,

#放棄備份檔案,以Astyle的版本,目前做這個沒有什麼差別。而且備份檔案會影響你的程式碼

--suffix=none

#行結束用什麼檔案格式,UNIX的還是Windows的,

#--lineend=windows

#後臺開放統一用UNIX的方式

--lineend=linux

#switch case的程式碼也按照標準縮排方式縮排

--indent-switches

#如果函式開始後面({後面)第一行是註釋,也進行縮排

--indent-col1-comments

#namespace class內部的public,等我沒有選擇縮排

#--indent-namespaces

#--indent-classes

#多行條件語句如何進行縮排,=0表示和(對齊,=1表示縮排一次,=2表示縮排兩次,=3縮排一個半

#MS visual studio的預設格式化是縮排一個,

--min-conditional-indent=1

#最大的縮排對齊長度,預設40,最大120,超過這個長度的多行()內語句不進行縮排,考慮到現在語句都較長

--max-instatement-indent=80

如果你是一名VC程式設計師,你是否被一個問題所困擾?專案小組有程式設計規範要求嚴格執行,但是專案進度緊張,每天都有大 量的程式碼需要完成。很難做到既能保證開發進度,又能編寫出良好風格、縮排規範的程式碼。如果採用其他工具對原始檔進行程式碼整理,又需在VC之外重新開啟源代 碼,整理結果不能直接在VC中檢視編譯。能不能在VC中整合一種工具,輕鬆點選VC按鈕即可整理原始碼呢?回答是有。

  下面我就介紹一個名為astyle的開原始碼整理工具以及在VC開發環境中整合配置的方法。可以迅速實現在VC中進行程式碼整理的功能,希望能給你一定的幫助。

  1、 訪問http://sourceforge.net/projects/astyle,下載astyle包,包括原始檔、可執行檔案、使用說明等。

  2、 拷貝astyle.exe到c:\winnt\system32下。

  3、 接下來配置VC,新增astyle程式碼整理功能。

   1) 在VC中新建選單項"格式整理"。在工具欄上點選右鍵開啟Customize視窗,在Tools標籤頁新建一個選單項:"格式整理",並在Command 中輸入選單項對應外掛工具程式當前所在路徑:"C:\winnt\system32\astyle.exe"。


  2) 在Arguments中輸入外掛工具程式配置引數--style=ansi ${FileName}${FileExt}。其中 --style=ansi表示選擇使用ANSI 風格格式和縮排。當然也可選用其他風格,可選的風格和樣例如下:

  --style=ansi:ANSI 風格格式和縮排

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

astyle是一個我自己常用的開放原始碼工具。它可以方便的將程式程式碼格式化成自己想要的樣式而不必人工修改。本來嘛,作為高等生物應該優先去做一些智慧的事情,而不是把時間消耗在機器可以完美完成的事情上。

想要立刻開始?請先去主頁http://sourceforge.net/projects/astyle下載最新版本。可以選擇二進位制版本,也可以下載原始碼自行編譯。總之得到可執行檔案後請將astyle放在Path(C:\Program Files\Microsoft Visual Studio 8\Common7\IDE)中,這樣會方便很多。

astyle是一個命令列工具,命令語法很簡單:
           astyle [options] < original > Beautified
            astyle [options] Foo.cpp Bar.cpp   [...]

例如:

           astyle --style=ansi foo.cpp

上面的命令將美化foo.cpp檔案,更改其風格為ANSI,並將原始檔案備份到foo.cpp.orgin。所以,你可以安全的使用該軟體而不必擔心會將程式碼改得無法回頭。

具體的來說,astyle包含了以下幾種預定義風格,只需在引數中簡單指定即可使用:

  --style=ansi:ANSI 風格格式和縮排

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


  --style=kr :Kernighan&Ritchie 風格格式和縮排

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


  --style=linux :Linux 風格格式和縮排

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


  --style=gnu :GNU 風格格式和縮排

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


  --style=java :Java 風格格式和縮排

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

從這裡開始介紹astyle的高階應用!這裡要介紹的是兩種應用情形,一是在Visual Studio中整合,二是批量處理。

先看如何在Visual Studio中整合。看圖說話!

第一步:點選“工具”選單

第一步:點選“工具”選單

第二步:點選“外部工具”

第二步:點選“外部工具”

第三步:配置並儲存

在對話方塊中點選“新增”,如圖填入各項。其中引數填寫 --style=ansi $(ItemFileName)$(ItemExt)

可以勾選“使用輸出視窗”,這樣將不會顯示黑色的命令視窗。相關資訊都會顯示在Visual Studio中。

經過上面設定之後,只需點選該選單項就可以將當前文件格式化成ansi風格。如果你想要其它風格,可以自行設定引數。

值得注意的是在低版本的Visual Studio中,預設設定執行外部程式不會儲存當前文件。這樣的話如果在未儲存的情況下執行該命令,未儲存部分將會丟失。這個可以通過設定一個選項來解決。Visual Studio 6.0中:Options -> Editor -> Save Options -> Save before running tools 將該項勾選即可。我已經驗證,在Visual Studio 2005中不用擔心這類問題,可以放心使用。但是作為一個好習慣,我仍然建議你隨時儲存你的工作,尤其是做這種大幅度改動之前,甚至應該對原始碼進行Check in操作。不知道Check in是什麼?沒關係,過幾天我還會寫一篇關於程式碼控制的文章,應該可以解決你的疑惑。

1.常用功能
(1) 單個檔案--預設美化
astyle --style=ansi Form1.cs
處理前的程式碼:
      private void Form1_Load(object sender, EventArgs e)
      {
          int s;
          for (int i=0;i<10;i++){
              for (int j=0;j<10; j++){
                  s = s+j+i;}
          }
      }
處理後:
      private void Form1_Load(object sender, EventArgs e)
      {
          int s;
          for (int i=0;i<10;i++)
          {
              for (int j=0;j<10; j++)
              {
                  s = s+j+i;
              }
          }
      }

(2) 單個檔案--更改縮排2個空格
astyle --style=ansi --indent=spaces=2 Form1.cs
預設縮排一個TAB,也可以顯式說明使用Tab,如下:
astyle --style=ansi --indent=tab Form1.cs

(3) 處理多個檔案--有限個
astyle --style=ansi Form1.cs Form2.cs

(4) 批量處理多個檔案--無限個
for /R .\ %f in (*.cs) do astyle --style=ansi "%f"
說明:/R表明遍歷一個目錄樹,後面緊跟的路徑是根,預設為當前目錄。
本例中,根為.\表示當前目錄,命令等價於:
for /R %f in (*.cs) do astyle --style=ansi "%f"
作用是從(目錄樹根)當前目錄開始,查詢所有java檔案,包含子目錄中的檔案;然後交給astyle處理。
當然,目錄樹根也可以使用絕對路徑,下面的命令查詢C盤所有的java檔案並處理。
for /R c:\ %f in (*.cs) do astyle --style=ansi "%f"

2. 其他比較有用的開關:
(1) -f
在兩行不相關的程式碼之間插入空行,如import和public class之間、public class和成員之間等;
(2) -p
在操作符兩邊插入空格,如=、+、-等。
如:int a=10*60;
處理後變成int a = 10 * 60;
(3) -P
在括號兩邊插入空格。另,-d只在括號外面插入空格,-D只在裡面插入。
如:MessageBox.Show ("aaa");
處理後變成MessageBox.Show ( "aaa" );
(4) -U
移除括號兩邊不必要的空格。
如:MessageBox.Show ( "aaa" );
處理後變成MessageBox.Show ("aaa");
(5) -V
將Tab替換為空格。

下面再介紹第二項獨門絕技:批量格式化!

有時候你會有很多檔案需要格式化成統一風格,難道一個個點選選單?不!那樣太累了。

在Windows中,我們可以用命令列來解決問題。這裡用到一個超級命令 for

我來寫個範例,大家就知道該怎麼處理了。

      for /R %f in (*.cpp;*.c;*.h) do astyle --style=ansi "%f"

該命令在當前目錄中尋找檔名匹配模式 *.cpp;*.c;*.h 的所有檔案(不同模式可用英文逗號隔開),並且對每個檔案%f執行操作:

      astyle --style=ansi "%f"

好了,本教程可以結束了。希望對你有所幫助。

使用astyle格式化程式碼

一。基本命令

astyle --style=ansi main.cs (使用ansi風格格式化main.cs)

瞭解上面的命令就可以格式化一個檔案了,下面來看如何格式化目錄下的檔案

二。格式化目錄

for /R %f in (*.cpp;*.cs;) do astyle --style=ansi "%f" (使用ansi風格格式下當前目錄下的所有cpp,cs檔案,注意:批處理檔案時,"%f" 要改為"%%f")

三。引數說明:

(1) -f
在兩行不相關的程式碼之間插入空行,如import和public class之間、public class和成員之間等;
(2) -p
在操作符兩邊插入空格,如=、+、-等。
如:int a=10*60;
處理後變成int a = 10 * 60;
(3) -P
在括號兩邊插入空格。另,-d只在括號外面插入空格,-D只在裡面插入。
如:System.out.println(1);
處理後變成System.out.println( 1 );
(4) -U
移除括號兩邊不必要的空格。
如:System.out.println( 1 );
處理後變成System.out.println(1);
(5) -V
將Tab替換為空格。

(6)-N

本條主要針對namespaces,如果沒有此引數,效果如下:

namespace foospace
{
class Foo
{
     public:
         Foo();
         virtual ~Foo();
};
}
有此引數就會變成這樣:

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

(7) -n

不生成備份檔案,即預設的 .orig檔案。

C#的預設方式為第二種,所以如果你是用來格式化C#程式碼的話,這個引數就有用了。

四:加入到VS2008,VS2005中

估計加入到VS2005中也是一樣,不過我這裡沒有VS2005,就說一下VS2008的做法。

工具——>外部工具——>新增

標題:astyle

命令:AStyle.exe (填好astyle.exe的路徑)

引數:--style=allman -N $(ItemDir)$(ItemFileName)$(ItemExt)

初始目錄:$(TargetDir)

勾上“使用初始目錄”

點選確定完成。以後就可以在工具選單中找到“astyle“這一項了,點選它,就可以對當前檔案進行格式化操作。

五:加入到VS6中

Tools——>Customize——>Tools

標題:astyle

命令:AStyle.exe (填好astyle.exe的路徑)

引數:--style=ansi -s4 --suffix=.orig $(FileName)$(FileExt)

初始目錄:$(FileDir)

勾上“Using Output Window”

點選確定完成。以後就可以在工具選單中找到“astyle“這一項了,點選它,就可以對當前檔案進行格式化操作。

六:加入到Ultraedit和UltraStudio

高階-->工具配置——>外部工具——>新增

命令:AStyle.exe -v --style=ansi -s4 --suffix=.orig "%f"(填好astyle.exe的路徑)

Optiones:選擇 Windows program和Save Active File.

Output: 選擇output to list box,show dos box 和no replace。

點選確定完成。以後就可以在工具選單中找到“astyle“這一項了,點選它,就可以對當前檔案進行格式化操作。

七:加入到Source insight

Options-->Custom Command-->Add

Command:astyle

Run "astyle.exe" --style=ansi --pad=oper --unpad=paren -s4 --suffix=.orig %f(填好astyle.exe的路徑)

Output:不選.

Control: 選擇pause when done和exit to window.

source links in output:file, then line

-->menu

add to work menu.

點選確定完成。以後就可以在Work選單中找到“astyle“這一項了,點選它,就可以對當前檔案進行格式化操作。

八:控制檯目錄批處理(Astyle.bat)

REM [email protected]

REM 批量將本目錄中的所有C++檔案用Astyle進行程式碼美化操作

REM 2009-01-05

REM 設定Astyle命令位置和引數

@echo off

set astyle="astyle.exe"

REM 迴圈遍歷目錄

for /r . %%a in (*.cpp;*.c) do %astyle% --style=ansi --pad=oper --unpad=paren -s4 -n "%%a"

for /r . %%a in (*.hpp;*.h) do %astyle% --style=ansi --pad=oper --unpad=paren -s4 -n "%%a"

REM 刪除所有的astyle生成檔案

for /r . %%a in (*.orig) do del "%%a"

pause

為UltraEdit新增astyle按鈕

開啟ultraedit,高階->工具配置

在命令列填寫:(注意修改ultraedit的安裝路徑)
D:\UltraEdit-32\GNU\astyle.exe --style=ansi  "%f"  "%f"

工作目錄填寫:%P

選項不改

輸出那裡把捕獲輸出去掉就可以了。--style=ansi 是astyle的命令,"%f" "%f"是ultraedit的引數。

點選開大圖

Tips:

可以簡單地安裝bin檔案的astyle程式到cygwin系統

eg: tar xjvf astyle-2.02-1.tar.bz2  -C /    (預設以根目錄作為跟路徑打包,所以解壓縮要-C /)

或者在Cygwin中用cygport 方式安裝src的astyle, astyle-2.02-1-src.tar.bz2

1 通過cygwin release的site下載安裝包

2 下載後放到使用者目錄下解壓縮

tar xjvf astyle-2.02.1-1-src.tar.bz2 ( j 表示檔案為bzip2的壓縮檔案, x表示解壓縮)

繼續解壓縮出來的.tar.gz

3 檢視cygport檔案可以看到會在src的build/gcc目錄下cygmake,  並且將astyle.exe放在/user/bin下面

4  檢視vim astyle-2.02.1-1.cygwin.patch,可知現在src已經解壓縮

   只需要cygport ./astyle-2.02.1-X.cygport all 即可

+Build instructions:
  23 +    unpack astyle-2.02.1-X-src.tar.bz2
  24 +        if you use setup to install this src package, it will be unpacked
  25 +        under /usr/src automatically
  26 +    cd /usr/src
  27 +    cygport ./astyle-2.02.1-X.cygport all
  28 +
  29 +This will create:
  30 +    /usr/src/astyle-2.02.1-X-src.tar.bz2
  31 +    /usr/src/astyle-2.02.1-X.tar.bz2
  32 +
  33 +Or use 'cygport ./astyle-2.02.1-X.cygport prep' to get the source

5 cygport ./astyle-2.02.1-X.cygport all

Bonus:

常規的Cygwin的resource pagage 管理工具cygport 安裝軟體的使用方法

     a.確保本地有cygport,如果沒有的話下載二進位制包,解壓縮到相應目錄

       eg: tar xjvf cygport-0.10.9-1.tar.bz2  -C /

     b.通過下面的網址下載想要的軟體包
       http://mirrors.xmission.com/cygwin/release/
      c.解壓後應該會有一個字尾名是.cygport的的檔案。
        執行
        cygport <檔名>.cygport all

新增:再windows批量格式原始檔的指令碼

@echo off
set astyle="D:\Program Files\AStyle\bin\astyle.exe"
set mode=ansi
for /r . %%a in (*.cpp;*.c;*.h) do %astyle% --style=%mode%  "%%a"
for /r . %%a in (*.orig) do del "%%a"

pause

相關推薦

ArtisticStyle----C/C++樣式格式化工具(支援VC6)

下載地址:http://srgb.googlecode.com/files/AStyle_2.02_windows.7z把astyle.exe 複製到 C:\WINDOWS 目錄裡,省的指定路徑VC6++ 設定方法選單->工具->定製->工具選單內容->新建選單,引數如下 命令列:a

Snapshot截圖軟件(一個小幾百k又的的截圖工具)

mar png 窗口 軟件 截屏 ffffff 編輯 tor 截圖 Snapshot截圖軟件(一個很小幾百k又很好用的的截圖工具)Snapshotor 是一款免費的截屏工具。軟件功能強大,支持截取窗口或一塊區域,也可以增加箭頭線,多邊形,文本等註釋。非常使用經常需要截圖並編

MathType初體驗——一款的數學公式輸入工具

記得上大學電路分析課時,看到老師在PPT中熟練的輸入複雜的數學公式,在畢業後的十幾年裡,再也沒遇到過這種場景。 直到昨晚,發現自己也有這個需要,於是,第一次接觸了MathType這個工具。用下來,感覺

C語言

好用 store ejb wot gin userinfo mcs sina cbe JW83HX撓9墑JZ揭嶄http://shufang.docin.com/xbgoz81833 8PEyr4辛訪枚扒3http://jz.docin.com/hhbo9279 190k2

高精度大數c++類模板

details lean 代碼 sprintf printf span tdi 我只 tar 首先聲明這是大佬寫的,我只是記錄下,拿來學習。附上大佬的鏈接 : https://blog.csdn.net/code4101/article/details/2302052

c#.net畫圖的控制元件 dotnetCHARTING

最近發現一個很好用的控制元件  dotnetCHARTING ,畫圖真的很方便,而且功能很多,可以畫各種各樣的圖 在官方網站上下載的需要註冊,現在網上有破解辦的下載的(需要的朋友可以給我留言,發到郵箱) 下面介紹一個我寫的例子(c#) 1.在的aspx也面上新增chart

給大家介紹兩個C語言編輯器

注意到很多小夥伴經常反饋在學習C語言時該選擇什麼 C語言 編輯器好呢?都說慾善其事必先利其器,網路上也有很多不同型別的編輯器,讓人很難進行抉擇!C語言 編輯器選擇哪個好? 所以我今天根據我個人的使用情況,整理了幾款C語言編輯器分享給大家,需要的看官看下文了解一下喲~ (小編推薦一個學C語

大家一起學python-day4-簡單的字串格式化("{}")

#format的第一個用法 #1.1普通的功能 msg = '大家好我叫{},今年{}歲' print(msg.format('阿衰',20))#大家好我叫阿衰,今年20歲 #1.2按照括號中的數字大小進行賦值,從0開始 msg = '大家好我叫{1},今年{0}歲,明年{0}歲' prin

C++API直連MySql方法

今天在網上本來想為我的畢業設計找個連線資料庫的方法,看到什麼C++用ADO連MySQL,MySql和連線驅動是裝了,但是在ODBC配置連線源的時候,沒有連線驅動。算了,換個方法吧!翻著翻著發現通過API直連的方法,試了一下,感覺挺好用,寫下來分享給大家,順便記錄下來方便下次

map C. Equal Sums

C. Equal Sumstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given kk sequen

C語言編譯器哪個?幾款C語言編譯器推薦

Zh iNengJ  ia   ng   H   u一、Dev-C++Dev-C++ 是一個C++ 開發工具。它包括多頁面視窗、工程編輯器,在工程編輯器中集合了編輯器、編譯器、連線程式和執行程式。它也提供高亮度語法顯示的,以減少編輯錯誤。Dev-C++是一個Windows下的

極力推薦一個簡單C++JSON庫

  極力推薦一個簡單好用的C++JSON庫CJsonObject,讓使用json如使用C++原生的結構體那般方便,隨心所欲。CJsonObject是個優秀的C++JSON庫,也許會是你見過的最為簡單易用的C++json庫。CJsonObject的開源地址是https://github.com/Bwar/CJs

gooreplacer

acer stack rep row flow cer replace 喜歡 畫面 國內上 StackOverflow, hackernews 之類的站點會慢。 因為頁面裏有鏈接指向 goolge, 會被墻。 於是拖累了整個頁面的顯示。 gooreplacer 可以把這

的log4j

.cn mage com log4j image idt bsp blog 技術分享 很好用的log4j

的谷歌字體以及Gravatar頭像一鍵替換WordPress插件----WP Acceleration for China 插件

ati 以及 content none 應對 集合 .org ref 多余 WordPress總是被新上手的朋友詬病說速度慢,其實多半都要歸功於谷歌字體的功勞。在應對字體這個問題的時候,大家都會有各種不同的解決方案。今天我給大家推薦一款插件,它集合了多個替代方案,可以方便的

static (php)

靜態變量 簡單 技術 images lin target -- functions es2017 一、靜態變量具有這樣的特性: 當在某函數裏定義一個靜態變量後,這個變量不會即使函數退出了,在下次調用這個函數時,它會使用前次被調用後留下的值。 (這樣就不用把一個變

jstat 監控調整GC

mman 版本 capacity sta 52.0 最小 容量 ring keyword jstat命令使用 jstat命令可以查看堆內存各部分的使用量,以及加載類的數量。命令的格式如下: jstat [-命令選項] [vmid] [間隔時間/毫秒] [查詢次數] 註意:使

一個的自動生成工具——mybatis generator

led ron 很好 user runtime rim mod 文件 path mybatis generator-自動生成代碼 準備材料:   一個文件夾,一個數據庫的驅動包,mybatis-generator-core-1.3.5.jar,一條生成語句   如圖:(我用

一個的在線編輯、展示、分享、交流JavaScript 代碼的平臺

找到 png ron bubuko eight 就會 很好 str 技術分享 在發表博客時,有一些代碼只能粘貼進去,而不能看到代碼運行的效果,需要讀者把代碼粘貼進自己的編輯器,然後再運行看效果,這是一件很耗時的事情 在平時百度的時候,我發現一些網站可以在線預覽功能,而且可以

layerdate一款日期插件

可選 itl 選擇 render 元素 charset scrip head layer <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>l