📄 astyle_main.cpp
字号:
{
(*_err) << errorInfo << arg << endl;
return false; // unknown option
}
return true; //o.k.
}
void importOptions(istream &in, vector<string> &optionsVector)
{
char ch;
string currentToken;
while (in)
{
currentToken = "";
do
{
in.get(ch);
// treat '#' as line comments
if (ch == '#')
while (in)
{
in.get(ch);
if (ch == '\n')
break;
}
// break options on spaces, tabs or new-lines
if (ch == ' ' || ch == '\t' || ch == '\n')
break;
else
currentToken.append(1, ch);
}
while (in);
if (currentToken.length() != 0)
optionsVector.push_back(currentToken);
}
}
bool stringEndsWith(const string &str, const string &suffix)
{
int strIndex = str.length() - 1;
int suffixIndex = suffix.length() - 1;
while (strIndex >= 0 && suffixIndex >= 0)
{
if (tolower(str[strIndex]) != tolower(suffix[suffixIndex]))
return false;
--strIndex;
--suffixIndex;
}
return true;
}
void printHelp()
{
(*_err) << endl;
(*_err) << "Artistic Style " << _version << " (http://www.bigfoot.com/~davidsont/astyle)\n";
(*_err) << " (created by Tal Davidson, davidsont@bigfoot.com)\n";
(*_err) << endl;
(*_err) << "Usage : astyle [options] < Original > Beautified\n";
(*_err) << " astyle [options] Foo.cpp Bar.cpp [...]\n";
(*_err) << endl;
(*_err) << "When indenting a specific file, the resulting indented file RETAINS the\n";
(*_err) << "original file-name. The original pre-indented file is renamed, with a\n";
(*_err) << "suffix of \".orig\" added to the original filename.\n";
(*_err) << endl;
(*_err) << "By default, astyle is set up to indent C/C++/C# files, with 4 spaces per\n" ;
(*_err) << "indent, a maximal indentation of 40 spaces inside continuous statements,\n";
(*_err) << "and NO formatting.\n";
(*_err) << endl;
(*_err) << "Option's Format:\n";
(*_err) << "----------------\n";
(*_err) << " Long options (starting with '--') must be written one at a time.\n";
(*_err) << " Short options (starting with '-') may be appended together.\n";
(*_err) << " Thus, -bps4 is the same as -b -p -s4.\n";
(*_err) << endl;
(*_err) << "Predefined Styling options:\n";
(*_err) << "--------------------\n";
(*_err) << " --style=ansi\n";
(*_err) << " ANSI style formatting/indenting.\n";
(*_err) << endl;
(*_err) << " --style=kr\n";
(*_err) << " Kernighan&Ritchie style formatting/indenting.\n";
(*_err) << endl;
(*_err) << " --style=gnu\n";
(*_err) << " GNU style formatting/indenting.\n";
(*_err) << endl;
(*_err) << " --style=java\n";
(*_err) << " Java mode, with standard java style formatting/indenting.\n";
(*_err) << endl;
(*_err) << " --style=linux\n";
(*_err) << " Linux mode (i.e. 8 spaces per indent, break definition-block\n";
(*_err) << " brackets but attach command-block brackets.\n";
(*_err) << endl;
(*_err) << "Indentation options:\n";
(*_err) << "--------------------\n";
(*_err) << " -c OR --mode=c\n";
(*_err) << " Indent a C, C++ or C# source file (default)\n";
(*_err) << endl;
(*_err) << " -j OR --mode=java\n";
(*_err) << " Indent a Java(TM) source file\n";
(*_err) << endl;
(*_err) << " -s OR -s# OR --indent=spaces=#\n";
(*_err) << " Indent using # spaces per indent. Not specifying #\n" ;
(*_err) << " will result in a default of 4 spacec per indent.\n" ;
(*_err) << endl;
(*_err) << " -t OR -t# OR --indent=tab=#\n";
(*_err) << " Indent using tab characters, assuming that each\n";
(*_err) << " tab is # spaces long. Not specifying # will result\n";
(*_err) << " in a default assumption of 4 spaces per tab.\n" ;
(*_err) << endl;
(*_err) << " -T# OR --force-indent=tab=#";
(*_err) << " Indent using tab characters, assuming that each\n";
(*_err) << " tab is # spaces long. Force tabs to be used in areas\n";
(*_err) << " Astyle would prefer to use spaces.\n" ;
(*_err) << endl;
(*_err) << " -C OR --indent-classes\n";
(*_err) << " Indent 'class' blocks, so that the inner 'public:',\n";
(*_err) << " 'protected:' and 'private: headers are indented in\n";
(*_err) << " relation to the class block.\n";
(*_err) << endl;
(*_err) << " -S OR --indent-switches\n";
(*_err) << " Indent 'switch' blocks, so that the inner 'case XXX:'\n";
(*_err) << " headers are indented in relation to the switch block.\n";
(*_err) << endl;
(*_err) << " -K OR --indent-cases\n";
(*_err) << " Indent 'case XXX:' lines, so that they are flush with\n";
(*_err) << " their bodies..\n";
(*_err) << endl;
(*_err) << " -N OR --indent-namespaces\n";
(*_err) << " Indent the contents of namespace blocks.\n";
(*_err) << endl;
(*_err) << " -B OR --indent-brackets\n";
(*_err) << " Add extra indentation to '{' and '}' block brackets.\n";
(*_err) << endl;
(*_err) << " -G OR --indent-blocks\n";
(*_err) << " Add extra indentation entire blocks (including brackets).\n";
(*_err) << endl;
(*_err) << " -L OR --indent-labels\n";
(*_err) << " Indent labels so that they appear one indent less than\n";
(*_err) << " the current indentation level, rather than being\n";
(*_err) << " flushed completely to the left (which is the default).\n";
(*_err) << endl;
(*_err) << " -m# OR --min-conditional-indent=#\n";
(*_err) << " Indent a minimal # spaces in a continuous conditional\n";
(*_err) << " belonging to a conditional header.\n";
(*_err) << endl;
(*_err) << " -M# OR --max-instatement-indent=#\n";
(*_err) << " Indent a maximal # spaces in a continuous statement,\n";
(*_err) << " relatively to the previous line.\n";
(*_err) << endl;
(*_err) << " -E OR --fill-empty-lines\n";
(*_err) << " Fill empty lines with the white space of their\n";
(*_err) << " previous lines.\n";
(*_err) << endl;
(*_err) << " --indent-preprocessor\n";
(*_err) << " Indent multi-line #define statements\n";
(*_err) << endl;
(*_err) << "Formatting options:\n";
(*_err) << "-------------------\n";
(*_err) << " -b OR --brackets=break\n";
(*_err) << " Break brackets from pre-block code (i.e. ANSI C/C++ style).\n";
(*_err) << endl;
(*_err) << " -a OR --brackets=attach\n";
(*_err) << " Attach brackets to pre-block code (i.e. Java/K&R style).\n";
(*_err) << endl;
(*_err) << " -l OR --brackets=linux\n";
(*_err) << " Break definition-block brackets and attach command-block\n";
(*_err) << " brackets.\n";
(*_err) << endl;
(*_err) << " --brackets=break-closing-headers\n";
(*_err) << " Break brackets before closing headers (e.g. 'else', 'catch', ..).\n";
(*_err) << " Should be appended to --brackets=attach or --brackets=linux.\n";
(*_err) << endl;
(*_err) << " -o OR --one-line=keep-statements\n";
(*_err) << " Don't break lines containing multiple statements into\n";
(*_err) << " multiple single-statement lines.\n";
(*_err) << endl;
(*_err) << " -O OR --one-line=keep-blocks\n";
(*_err) << " Don't break blocks residing completely on one line\n";
(*_err) << endl;
(*_err) << " -p OR --pad=oper\n";
(*_err) << " Insert space paddings around operators only.\n";
(*_err) << endl;
(*_err) << " --pad=paren\n";
(*_err) << " Insert space paddings around parenthesies only.\n";
(*_err) << endl;
(*_err) << " -P OR --pad=all\n";
(*_err) << " Insert space paddings around operators AND parenthesies.\n";
(*_err) << endl;
(*_err) << " --convert-tabs\n";
(*_err) << " Convert tabs to spaces.\n";
(*_err) << endl;
(*_err) << " --break-blocks\n";
(*_err) << " Insert empty lines around unrelated blocks, labels, classes, ...\n";
(*_err) << endl;
(*_err) << " --break-blocks=all\n";
(*_err) << " Like --break-blocks, except also insert empty lines \n";
(*_err) << " around closing headers (e.g. 'else', 'catch', ...).\n";
(*_err) << endl;
(*_err) << " --break-elseifs\n";
(*_err) << " Break 'else if()' statements into two different lines.\n";
(*_err) << endl;
(*_err) << "Other options:\n";
(*_err) << "-------------\n";
(*_err) << " --suffix=####\n";
(*_err) << " Append the suffix #### instead of '.orig' to original filename.\n";
(*_err) << endl;
(*_err) << " -X OR --errors-to-standard-output\n";
(*_err) << " Print errors and help information to standard-output rather than\n";
(*_err) << " to standard-error.\n";
(*_err) << endl;
(*_err) << " -v OR --version\n";
(*_err) << " Print version number\n";
(*_err) << endl;
(*_err) << " -h OR -? OR --help\n";
(*_err) << " Print this help message\n";
(*_err) << endl;
(*_err) << "Default options file:\n";
(*_err) << "---------------------\n";
(*_err) << " Artistic Style looks for a default options file in the\n";
(*_err) << " following order:\n";
(*_err) << " 1. The contents of the ARTISTIC_STYLE_OPTIONS environment\n";
(*_err) << " variable if it exists.\n";
(*_err) << " 2. The file called .astylerc in the directory pointed to by the\n";
(*_err) << " HOME environment variable ( i.e. $HOME/.astylerc ).\n";
(*_err) << " 3. The file called .astylerc in the directory pointed to by the\n";
(*_err) << " HOMEPATH environment variable ( i.e. %HOMEPATH%\\.astylerc ).\n";
(*_err) << " If a default options file is found, the options in this file\n";
(*_err) << " will be parsed BEFORE the command-line options.\n";
(*_err) << " Options within the default option file may be written without\n";
(*_err) << " the preliminary '-' or '--'.\n";
(*_err) << endl;
}
int main(int argc, char *argv[])
{
ASFormatter formatter;
vector<string> fileNameVector;
vector<string> optionsVector;
string optionsFileName = "";
string arg;
bool ok = true;
bool shouldPrintHelp = false;
bool shouldParseOptionsFile = true;
_err = &cerr;
_suffix = ".orig";
_modeManuallySet = false;
// manage flags
for (int i=1; i<argc; i++)
{
arg = string(argv[i]);
if ( IS_PARAM_OPTION(arg ,"--options=none") )
{
shouldParseOptionsFile = false;
}
else if ( IS_PARAM_OPTION(arg ,"--options=") )
{
optionsFileName = GET_PARAM(arg, "--options=");
}
else if ( IS_OPTION(arg, "-h")
|| IS_OPTION(arg, "--help")
|| IS_OPTION(arg, "-?") )
{
shouldPrintHelp = true;
}
else if (arg[0] == '-')
{
optionsVector.push_back(arg);
}
else // file-name
{
fileNameVector.push_back(arg);
}
}
// parse options file
if (shouldParseOptionsFile)
{
if (optionsFileName.compare("") == 0)
{
char* env = getenv("ARTISTIC_STYLE_OPTIONS");
if (env != NULL)
optionsFileName = string(env);
}
if (optionsFileName.compare("") == 0)
{
char* env = getenv("HOME");
if (env != NULL)
optionsFileName = string(env) + string("/.astylerc");
}
if (optionsFileName.compare("") == 0)
{
char* env = getenv("HOMEPATH");
if (env != NULL)
optionsFileName = string(env) + string("/.astylerc");
}
if (optionsFileName.compare("") != 0)
{
ifstream optionsIn(optionsFileName.c_str());
if (optionsIn)
{
vector<string> fileOptionsVector;
importOptions(optionsIn, fileOptionsVector);
ok = parseOptions(formatter,
fileOptionsVector.begin(),
fileOptionsVector.end(),
string("Unknown option in default options file: "));
}
optionsIn.close();
if (!ok)
{
(*_err) << "For help on options, type 'astyle -h' " << endl;
}
}
}
// parse options from command line
ok = parseOptions(formatter,
optionsVector.begin(),
optionsVector.end(),
string("Unknown command line option: "));
if (!ok)
{
(*_err) << "For help on options, type 'astyle -h' " << endl;
exit(1);
}
if (shouldPrintHelp)
{
printHelp();
exit(1);
}
// if no files have been given, use cin for input and cout for output
if (fileNameVector.empty())
{
formatter.init( new ASStreamIterator(&cin) );
while (formatter.hasMoreLines() )
{
cout << formatter.nextLine();
if (formatter.hasMoreLines())
cout << endl;
}
cout.flush();
}
else
{
// indent the given files
for (int i=0; i<fileNameVector.size(); i++)
{
string originalFileName = fileNameVector[i];
string inFileName = originalFileName + _suffix;
remove(inFileName.c_str());
if ( rename(originalFileName.c_str(), inFileName.c_str()) < 0)
error("Could not rename ", string(originalFileName + " to " + inFileName).c_str());
ifstream in(inFileName.c_str());
if (!in)
error("Could not open input file", inFileName.c_str());
ofstream out(originalFileName.c_str());
if (!out)
error("Could not open output file", originalFileName.c_str());
// Unless a specific language mode has been, set the language mode
// according to the file's suffix.
if (!_modeManuallySet)
{
if (stringEndsWith(originalFileName, string(".java")))
{
formatter.setJavaStyle();
}
else
{
formatter.setCStyle();
}
}
formatter.init( new ASStreamIterator(&in) );
while (formatter.hasMoreLines() )
{
out << formatter.nextLine();
if (formatter.hasMoreLines())
out << endl;
}
out.flush();
out.close();
in.close();
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -