📄 astyle_main.cpp
字号:
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* astyle_main.cpp
*
* This file is a part of "Artistic Style" - an indentation and
* reformatting tool for C, C++, C# and Java source files.
* http://astyle.sourceforge.net
*
* The "Artistic Style" project, including all files needed to
* compile it, is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this project; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
#include "astyle.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <ctime>
#ifndef ASTYLE_LIB // for console build only
#if defined(_MSC_VER) || defined(__DMC__)
#include <sys/utime.h>
#include <sys/stat.h>
//#define utimbuf _utimbuf
//#define utime _utime
#else
#include <utime.h>
#include <sys/stat.h>
#endif // end compiler checks
#endif // end ASTYLE_LIB
// for G++ implementation of string.compare:
// compare((str), (place), (length)) instead of compare(place, length, str)
#if defined(__GNUC__) && __GNUC__ < 3
#error - Use GNU C compiler release 3 or higher
#endif
// for namespace problem in version 5.0
#if defined(_MSC_VER) && _MSC_VER < 1200 // check for V6.0
#error - Use Microsoft compiler version 6 or higher
#endif
#ifdef _WIN32
#define STDCALL __stdcall
#define EXPORT __declspec(dllexport)
#else
#define STDCALL
#define EXPORT
#endif
#define IS_OPTION(arg,op) ((arg).compare(op)==0)
#define IS_OPTIONS(arg,a,b) (IS_OPTION((arg),(a)) || IS_OPTION((arg),(b)))
#define GET_PARAM(arg,op) ((arg).substr(strlen(op)))
#define GET_PARAMS(arg,a,b) (isParamOption((arg),(a)) ? GET_PARAM((arg),(a)) : GET_PARAM((arg),(b)))
using namespace astyle;
const char* _version = "1.21";
// some compilers want this declared
bool parseOption(ASFormatter &formatter, const string &arg, const string &errorInfo);
#ifdef ASTYLE_LIB
// GUI function pointers
typedef void (STDCALL *fpError)(int, char*); // pointer to callback error handler
typedef char* (STDCALL *fpAlloc)(unsigned long); // pointer to callback memory allocation
// GUI variables
stringstream *_err = NULL;
#else
// console variables
ostream *_err = &cerr;
bool _purgeOrigIn = false;
bool _preserveDate = false;
string _suffix = ".orig";
stringstream _msg; // info messages are not printed until a file is read
#endif
bool _modeManuallySet = false;
// typename will be istringstream for GUI and istream otherwise
template<typename T>
class ASStreamIterator :
public ASSourceIterator
{
public:
ASStreamIterator(T *in);
virtual ~ASStreamIterator();
bool hasMoreLines() const;
string nextLine();
private:
T * inStream;
string buffer;
bool inStreamEOF;
};
template<typename T>
ASStreamIterator<T>::ASStreamIterator(T *in)
{
inStream = in;
buffer.reserve(200);
inStreamEOF = false;
}
template<typename T>
ASStreamIterator<T>::~ASStreamIterator()
{
}
template<typename T>
bool ASStreamIterator<T>::hasMoreLines() const
{
return !inStreamEOF;
}
/**
* read the input stream, delete any end of line characters,
* and build a string that contains the input line.
*
* @return string containing the next input line minus any end of line characters
*/
template<typename T>
string ASStreamIterator<T>::nextLine()
{
char ch;
char LF = '\n';
char CR = '\r';
inStream->get(ch);
buffer.clear();
while (!inStream->eof() && ch != LF && ch != CR)
{
buffer.append(1, ch);
inStream->get(ch);
}
if (inStream->eof())
{
inStreamEOF = true;
return buffer;
}
int peekch = inStream->peek();
if (ch == CR) // CR+LF is windows otherwise Mac OS 9
{
if (peekch == LF)
{
inStream->get();
eolWindows++;
}
else
eolMacOld++;
}
else // LF is Linux, allow for improbable LF/CR
{
if (peekch == CR)
{
inStream->get();
eolWindows++;
}
else
eolLinux++;
}
// set output end of line character
if (eolWindows >= eolLinux)
if (eolWindows >= eolMacOld)
strcpy(outputEOL, "\r\n"); // Windows (CR+LF)
else
strcpy(outputEOL, "\r"); // MacOld (CR)
else
if (eolLinux >= eolMacOld)
strcpy(outputEOL, "\n"); // Linux (LF)
else
strcpy(outputEOL, "\r"); // MacOld (CR)
return buffer;
}
template<typename ITER>
bool parseOptions(ASFormatter &formatter,
const ITER &optionsBegin,
const ITER &optionsEnd,
const string &errorInfo)
{
ITER option;
bool ok = true;
string arg, subArg;
for (option = optionsBegin; option != optionsEnd; ++option)
{
arg = *option;
if (arg.compare(0, 2, "--") == 0)
ok &= parseOption(formatter, arg.substr(2), errorInfo);
else if (arg[0] == '-')
{
size_t i;
for (i = 1; i < arg.length(); ++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;
}
void importOptions(istream &in, vector<string> &optionsVector)
{
char ch;
string currentToken;
while (in)
{
currentToken = "";
do
{
in.get(ch);
if (in.eof())
break;
// treat '#' as line comments
if (ch == '#')
while (in)
{
in.get(ch);
if (ch == '\n')
break;
}
// break options on spaces, tabs or new-lines
if (in.eof() || ch == ' ' || ch == '\t' || ch == '\n')
break;
else
currentToken.append(1, ch);
}
while (in);
if (currentToken.length() != 0)
optionsVector.push_back(currentToken);
}
}
bool isParamOption(const string &arg, const char *option)
{
bool retVal = arg.compare(0, strlen(option), option) == 0;
// if comparing for short option, 2nd char of arg must be numeric
if (retVal && strlen(option) == 1 && arg.length() > 1)
if (!isdigit(arg[1]))
retVal = false;
return retVal;
}
void isOptionError(const string &arg, const string &errorInfo)
{
#ifdef ASTYLE_LIB
if (_err->str().length() == 0)
{
(*_err) << errorInfo << endl; // need main error message
(*_err) << arg; // output the option in error
}
else
(*_err) << endl << arg; // put endl after previous option
#else
if (errorInfo.length() > 0) // to avoid a compiler warning
(*_err) << "Error in param: " << arg << endl;
#endif
}
bool isParamOption(const string &arg, const char *option1, const char *option2)
{
return isParamOption(arg, option1) || isParamOption(arg, option2);
}
bool parseOption(ASFormatter &formatter, const string &arg, const string &errorInfo)
{
if ( IS_OPTION(arg, "style=ansi") )
{
formatter.setSpaceIndentation(4);
formatter.setBracketFormatMode(BREAK_MODE);
formatter.setBracketIndent(false);
formatter.setClassIndent(false);
formatter.setSwitchIndent(false);
formatter.setNamespaceIndent(false);
}
else if ( IS_OPTION(arg, "style=gnu") )
{
formatter.setSpaceIndentation(2);
formatter.setBracketFormatMode(BREAK_MODE);
formatter.setBlockIndent(true);
formatter.setClassIndent(false);
formatter.setSwitchIndent(false);
formatter.setNamespaceIndent(false);
}
else if ( IS_OPTION(arg, "style=java") )
{
// formatter.setJavaStyle();
// _modeManuallySet = true;
formatter.setSpaceIndentation(4);
formatter.setBracketFormatMode(ATTACH_MODE);
formatter.setBracketIndent(false);
formatter.setSwitchIndent(false);
}
else if ( IS_OPTION(arg, "style=kr") )
{
//manuallySetCStyle(formatter);
formatter.setSpaceIndentation(4);
formatter.setBracketFormatMode(ATTACH_MODE);
formatter.setBracketIndent(false);
formatter.setClassIndent(false);
formatter.setSwitchIndent(false);
formatter.setNamespaceIndent(false);
}
else if ( IS_OPTION(arg, "style=linux") )
{
formatter.setSpaceIndentation(8);
formatter.setBracketFormatMode(BDAC_MODE);
formatter.setBracketIndent(false);
formatter.setClassIndent(false);
formatter.setSwitchIndent(false);
formatter.setNamespaceIndent(false);
}
// must check for mode=cs before mode=c !!!
else if ( IS_OPTION(arg, "mode=cs") )
{
formatter.setSharpStyle();
_modeManuallySet = true;
}
else if ( IS_OPTION(arg, "mode=c") )
{
formatter.setCStyle();
_modeManuallySet = true;
}
else if ( IS_OPTION(arg, "mode=java") )
{
formatter.setJavaStyle();
_modeManuallySet = true;
}
else if ( isParamOption(arg, "t", "indent=tab=") )
{
int spaceNum = 4;
string spaceNumParam = GET_PARAMS(arg, "t", "indent=tab=");
if (spaceNumParam.length() > 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -