📄 astyle_main.cpp
字号:
{
TRACE( INFO, "break-blocks" );
formatter.breakBlocks = true;
}
else if (arg == "break-elseifs")
{
TRACE( INFO, "break-elseifs" );
formatter.breakElseIfs = true;
}
else if ( (arg == "X") || (arg == "errors-to-standard-output") )
{
TRACE( INFO, "errors-to-standard-output" );
_err = &cout;
}
else if ( (arg == "v") || (arg == "version") )
{
TRACE( INFO, "version" );
cout << "Artistic Style " << _version << endl;
exit(0);
}
// parameterized short options at the end of the else-if cascade, or
// they might be confused with long options starting with the same char
else if ( BEGINS_WITH( arg, "t", 1 ) || BEGINS_WITH( arg, "indent=tab=", 11 ) )
{
TRACE( ENTRY, "indent=tab=" );
int spaceNum = 4;
string spaceNumParam = (arg[0] == 't') ? arg.substr(1) : arg.substr(strlen("indent=tab="));
if (spaceNumParam.size() > 0)
spaceNum = atoi(spaceNumParam.c_str());
TRACE( EXIT, "indent=tab=" << spaceNum );
formatter.indentString = "\t";
formatter.indentLength = spaceNum;
formatter.forceTabIndent = false;
if ( formatter.minConditionalIndent == INT_MAX )
{
formatter.minConditionalIndent = formatter.indentLength * 2;
}
}
else if ( BEGINS_WITH( arg, "T", 1 ) || BEGINS_WITH( arg, "force-indent=tab=", 17 ) )
{
TRACE( ENTRY, "force-indent=tab=" );
int spaceNum = 4;
string spaceNumParam = (arg[0] == 'T') ? arg.substr(1) : arg.substr(strlen("force-indent=tab="));
if (spaceNumParam.size() > 0)
spaceNum = atoi(spaceNumParam.c_str());
TRACE( EXIT, "force-indent=tab=" << spaceNum );
formatter.indentString = "\t";
formatter.indentLength = spaceNum;
formatter.forceTabIndent = true;
if ( formatter.minConditionalIndent == INT_MIN )
{
formatter.minConditionalIndent = formatter.indentLength * 2;
}
}
else if ( BEGINS_WITH( arg, "s", 1 ) || BEGINS_WITH( arg, "indent=spaces=", 14 ) )
{
TRACE( ENTRY, "indent=spaces=" );
int spaceNum = 4;
string spaceNumParam = (arg[0] == 's') ? arg.substr(1) : arg.substr(strlen("indent=spaces="));
if (spaceNumParam.size() > 0)
spaceNum = atoi(spaceNumParam.c_str());
TRACE( EXIT, "indent=spaces=" << spaceNum );
formatter.indentLength = spaceNum;
formatter.indentString = string(spaceNum, ' ');
if ( formatter.minConditionalIndent == INT_MIN )
{
formatter.minConditionalIndent = formatter.indentLength * 2;
}
}
else if ( BEGINS_WITH( arg, "M", 1 ) || BEGINS_WITH( arg, "max-instatement-indent=", 23 ) )
{
TRACE( ENTRY, "max-instatement-indent=" );
int maxIndent = 40;
string maxIndentParam = (arg[0] == 'M') ? arg.substr(1) : arg.substr(strlen("max-instatement-indent="));
if (maxIndentParam.size() > 0)
maxIndent = atoi(maxIndentParam.c_str());
TRACE( EXIT, "max-instatement-indent=" << maxIndent );
formatter.maxInStatementIndent = maxIndent;
}
else if ( BEGINS_WITH( arg, "m", 1 ) || BEGINS_WITH( arg, "min-conditional-indent=", 23 ) )
{
TRACE( ENTRY, "min-conditional-indent=" );
int minIndent = 0;
string minIndentParam = (arg[0] == 'm') ? arg.substr(1) : arg.substr(strlen("min-conditional-indent="));
if (minIndentParam.size() > 0)
minIndent = atoi(minIndentParam.c_str());
TRACE( EXIT, "min-conditional-indent=" << minIndent );
formatter.minConditionalIndent = minIndent;
}
else
{
(*_err) << errorInfo << arg << endl;
return false; // unknown option
}
return true; //o.k.
}
bool parseOptions(ASFormatter & formatter,
vector<string>::iterator optionsBegin,
vector<string>::iterator optionsEnd,
const string & errorInfo)
{
vector<string>::iterator option;
bool ok = true;
string arg, subArg;
for (option = optionsBegin; option != optionsEnd; ++option)
{
arg = *option;
TRACE( INFO, "Parsing '" << arg << "'." );
if (BEGINS_WITH(arg, "--", 2))
ok &= parseOption(formatter, arg.substr(2), errorInfo);
else if (arg[0] == '-')
{
for (unsigned i=1; i < arg.size(); ++i)
{
if (isalpha(arg[i]) && i > 1)
{
ok &= parseOption(formatter, subArg, errorInfo);
subArg = "";
}
subArg.append(1, arg[i]);
}
ok &= parseOption(formatter, subArg, errorInfo);
subArg = "";
}
else
{
ok &= parseOption(formatter, arg, errorInfo);
subArg = "";
}
}
return ok;
}
bool stringEndsWith(const string &str, const string &suffix)
{
int strIndex = str.size() - 1;
int suffixIndex = suffix.size() - 1;
while (strIndex >= 0 && suffixIndex >= 0)
{
if (tolower(str[strIndex]) != tolower(suffix[suffixIndex]))
return false;
--strIndex;
--suffixIndex;
}
return true;
}
bool isWriteable( char const * const filename )
{
std::ifstream in(filename);
if (!in)
{
(*_err) << "File '" << filename << "' does not exist." << endl;
return false;
}
in.close();
std::ofstream out(filename, std::ios_base::app);
if (!out)
{
(*_err) << "File '" << filename << "' is not writeable." << endl;
return false;
}
out.close();
return true;
}
void printHelp()
{
cout << endl
<< "Artistic Style " << _version << " (http://www.bigfoot.com/~davidsont/astyle)" << endl
<< " (created by Tal Davidson, davidsont@bigfoot.com)" << endl
<< " (maintained by Martin Baute, solar@rootdirectory.de)" << endl;
cout << endl
<< "Usage : astyle [options] < Original > Beautified" << endl
<< " astyle [options] Foo.cpp Bar.cpp [...]" << endl;
cout << endl
<< "When indenting a specific file, the resulting indented file RETAINS the" << endl
<< "original file-name. The original pre-indented file is renamed, with a" << endl
<< "suffix of \".orig\" added to the original filename." << endl;
cout << endl
<< "By default, astyle is set up to indent C/C++/C# files, with 4 spaces per" << endl
<< "indent, a maximal indentation of 40 spaces inside continuous statements," << endl
<< "and NO formatting." << endl;
cout << endl
<< "Option's Format:" << endl
<< "----------------" << endl;
cout << endl
<< " Long options (starting with '--') must be written one at a time." << endl
<< " Short options (starting with '-') may be appended together." << endl
<< " Thus, -bps4 is the same as -b -p -s4." << endl;
cout << endl
<< "Predefined Styling options:" << endl
<< "--------------------" << endl;
cout << endl
<< " --style=ansi" << endl
<< " ANSI style formatting/indenting." << endl;
cout << endl
<< " --style=kr" << endl
<< " Kernighan&Ritchie style formatting/indenting." << endl;
cout << endl
<< " --style=gnu" << endl
<< " GNU style formatting/indenting." << endl;
cout << endl
<< " --style=java" << endl
<< " Java mode, with standard java style formatting/indenting." << endl;
cout << endl
<< " --style=linux" << endl
<< " Linux mode (i.e. 8 spaces per indent, break definition-block" << endl
<< " brackets but attach command-block brackets." << endl;
cout << endl
<< "Indentation options:" << endl
<< "--------------------" << endl;
cout << endl
<< " -c\tOR\t--mode=c" << endl
<< " Indent a C, C++ or C# source file (default)" << endl;
cout << endl
<< " -j\tOR\t--mode=java" << endl
<< " Indent a Java(TM) source file" << endl;
cout << endl
<< " --mode=csharp" << endl
<< " Indent a C# source file" << endl;
cout << endl
<< " -s\tOR\t-s#\tOR\t--indent=spaces=#" << endl
<< " Indent using # spaces per indent. Not specifying #" << endl
<< " will result in a default of 4 spaces per indent." << endl;
cout << endl
<< " -t\tOR\t-t#\tOR\t--indent=tab=#" << endl
<< " Indent using tab characters, assuming that each" << endl
<< " tab is # spaces long. Not specifying # will result" << endl
<< " in a default assumption of 4 spaces per tab." << endl;
cout << endl
<< " -T#\tOR\t--force-indent=tab=#" << endl
<< " Indent using tab characters, assuming that each" << endl
<< " tab is # spaces long. Force tabs to be used in areas" << endl
<< " Astyle would prefer to use spaces." << endl;
cout << endl
<< " -C\tOR\t--indent-classes" << endl
<< " Indent 'class' blocks, so that the inner 'public:'," << endl
<< " 'protected:' and 'private: headers are indented in" << endl
<< " relation to the class block." << endl;
cout << endl
<< " -S\tOR\t--indent-switches" << endl
<< " Indent 'switch' blocks, so that the inner 'case XXX:'" << endl
<< " headers are indented in relation to the switch block." << endl;
cout << endl
<< " -K\tOR\t--indent-cases" << endl
<< " Indent 'case XXX:' lines, so that they are flush with" << endl
<< " their bodies.." << endl;
cout << endl
<< " -N\tOR\t--indent-namespaces" << endl
<< " Indent the contents of namespace blocks." << endl;
cout << endl
<< " -B\tOR\t--indent-brackets" << endl
<< " Add extra indentation to '{' and '}' block brackets." << endl;
cout << endl
<< " -G\tOR\t--indent-blocks" << endl
<< " Add extra indentation entire blocks (including brackets)." << endl;
cout << endl
<< " -L\tOR\t--indent-labels" << endl
<< " Indent labels so that they appear one indent less than" << endl
<< " the current indentation level, rather than being" << endl
<< " flushed completely to the left (which is the default)." << endl;
cout << endl
<< " -m#\tOR\t--min-conditional-indent=#" << endl
<< " Indent a minimal # spaces in a continuous conditional" << endl
<< " belonging to a conditional header." << endl;
cout << endl
<< " -M#\tOR\t--max-instatement-indent=#" << endl
<< " Indent a maximal # spaces in a continuous statement," << endl
<< " relatively to the previous line." << endl;
cout << endl
<< " -E\tOR\t--fill-empty-lines" << endl
<< " Fill empty lines with the white space of their" << endl
<< " previous lines." << endl;
cout << endl
<< " --indent-preprocessor" << endl
<< " Indent multi-line #define statements" << endl;
cout << endl
<< "Formatting options:" << endl
<< "-------------------" << endl;
cout << endl
<< " -b\tOR\t--brackets=break" << endl
<< " Break brackets from pre-block code (i.e. ANSI C/C++ style)." << endl;
cout << endl
<< " -a\tOR\t--brackets=attach" << endl
<< " Attach brackets to pre-block code (i.e. Java/K&R style)." << endl;
cout << endl
<< " -l\tOR\t--brackets=linux" << endl
<< " Break definition-block brackets and attach command-block" << endl
<< " brackets." << endl;
cout << endl
<< " --brackets=break-closing-headers" << endl
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -