⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 astyle_main.cpp

📁 Artistic Style 源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (c) 1998,1999,2000,2001,2002 Tal Davidson. All rights reserved.
 *
 * astyle_main.cpp
 * Copyright (c) 1998,1999,2000 Tal Davidson (davidsont@bigfoot.com). All rights reserved.
 *
 * This file is a part of "Artistic Style" - an indentater and reformatter
 * of C, C++, C# and Java source files.
 *
 * The "Artistic Style" project, including all files needed to compile it,
 * is free software; you can redistribute it and/or use it and/or modify it
 * under the terms of the GNU General Public License as published 
 * by the Free Software Foundation; either version 2 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.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.
 *
 */

#include "compiler_defines.h"
#include "astyle.h"

#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>


#define IS_OPTION(arg,op)          ((arg).compare(op)==0)
#define IS_OPTIONS(arg,a,b)        (IS_OPTION((arg),(a)) || IS_OPTION((arg),(b)))

#define IS_PARAM_OPTION(arg,op)    ((arg).COMPARE(0, strlen(op) , string(op))==0)
#define IS_PARAM_OPTIONS(arg,a,b)  (IS_PARAM_OPTION((arg),(a)) || IS_PARAM_OPTION((arg),(b)))

#define GET_PARAM(arg,op)          ((arg).substr(strlen(op)))
#define GET_PARAMS(arg,a,b)        (IS_PARAM_OPTION((arg),(a)) ? GET_PARAM((arg),(a)) : GET_PARAM((arg),(b)))


#ifdef USES_NAMESPACE
using namespace std;
using namespace astyle;
#endif

// default options:
ostream *_err = &cerr;
string _suffix = ".orig";
bool _modeManuallySet;

const string _version = "1.15.3";


class ASStreamIterator :
            public ASSourceIterator
{
    public:
        ASStreamIterator(istream *in);
        virtual ~ASStreamIterator();
        bool hasMoreLines() const;
        string nextLine();

    private:
        istream * inStream;
        char buffer[2048];
};

ASStreamIterator::ASStreamIterator(istream *in)
{
    inStream = in;
}

ASStreamIterator::~ASStreamIterator()
{
    delete inStream;
}


bool ASStreamIterator::hasMoreLines() const
{
    if (*inStream)
        return true;
    else
        return false;
}

/*
string ASStreamIterator::nextLine()
{
   char theInChar;
   char peekedChar;
   int  theBufferPosn = 0;
 
   //
   // treat '\n', '\r', '\n\r' and '\r\n' as an endline.
   //
   while (theBufferPosn < 2047 && inStream->get(theInChar))
   // while not eof
   {
      if (theInChar != '\n' && theInChar != '\r')
      {
	 buffer[theBufferPosn] = theInChar;
         theBufferPosn++;
      }
      else
      {
	peekedChar = inStream->peek();
	if (peekedChar != theInChar && (peekedChar == '\r' || peekedChar == '\n') )
         {
            inStream->get(theInChar);
         }
         break;
      }
   }
   buffer[theBufferPosn] = '\0';
 
   return string(buffer);
}
*/


string ASStreamIterator::nextLine()
{
    char *srcPtr;
    char *filterPtr;

    inStream->getline(buffer, 2047);
    srcPtr = filterPtr = buffer;

    while (*srcPtr != 0)
    {
        if (*srcPtr != '\r')
            *filterPtr++ = *srcPtr;
        srcPtr++;
    }
    *filterPtr = 0;

    return string(buffer);
}



void error(const char *why, const char* what)
{
    (*_err) << why << ' ' << what <<'\n';
    exit(1);
}



template<class 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; //string(*option);

        if (arg.COMPARE(0, 2, string("--")) == 0)
            ok &= parseOption(formatter, arg.substr(2), errorInfo);
        else if (arg[0] == '-')
        {
            int 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 manuallySetJavaStyle(ASFormatter &formatter)
{
    formatter.setJavaStyle();
    _modeManuallySet = true;
}

void manuallySetCStyle(ASFormatter &formatter)
{
    formatter.setCStyle();
    _modeManuallySet = true;
}


bool parseOption(ASFormatter &formatter, const string &arg, const string &errorInfo)
{
    if ( IS_PARAM_OPTION(arg, "suffix=") )
    {
        string suffixParam = GET_PARAM(arg, "suffix=");
        if (suffixParam.length() > 0)
            _suffix = suffixParam;
    }
    else if ( IS_OPTION(arg ,"style=ansi") )
    {
        formatter.setBracketIndent(false);
        formatter.setSpaceIndentation(4);
        formatter.setBracketFormatMode(BREAK_MODE);
        formatter.setClassIndent(false);
        formatter.setSwitchIndent(false);
        formatter.setNamespaceIndent(false);
    }
    else if ( IS_OPTION(arg ,"style=gnu") )
    {
        formatter.setBlockIndent(true);
        formatter.setSpaceIndentation(2);
        formatter.setBracketFormatMode(BREAK_MODE);
        formatter.setClassIndent(false);
        formatter.setSwitchIndent(false);
        formatter.setNamespaceIndent(false);
    }
    else if ( IS_OPTION(arg ,"style=java") )
    {
        manuallySetJavaStyle(formatter);
        formatter.setBracketIndent(false);
        formatter.setSpaceIndentation(4);
        formatter.setBracketFormatMode(ATTACH_MODE);
        formatter.setSwitchIndent(false);
    }
    else if ( IS_OPTION(arg ,"style=kr") )
    {
        //manuallySetCStyle(formatter);
        formatter.setBracketIndent(false);
        formatter.setSpaceIndentation(4);
        formatter.setBracketFormatMode(ATTACH_MODE);
        formatter.setClassIndent(false);
        formatter.setSwitchIndent(false);
        formatter.setNamespaceIndent(false);
    }
    else if ( IS_OPTION(arg ,"style=linux") )
    {
        formatter.setBracketIndent(false);
        formatter.setSpaceIndentation(8);
        formatter.setBracketFormatMode(BDAC_MODE);
        formatter.setClassIndent(false);
        formatter.setSwitchIndent(false);
        formatter.setNamespaceIndent(false);
    }
    else if ( IS_OPTIONS(arg ,"c", "mode=c") )
    {
        manuallySetCStyle(formatter);
    }
    else if ( IS_OPTIONS(arg ,"j", "mode=java") )
    {
        manuallySetJavaStyle(formatter);
    }
    else if ( IS_OPTIONS(arg, "t", "indent=tab=") )
    {
        int spaceNum = 4;
        string spaceNumParam = GET_PARAMS(arg, "t", "indent=tab=");
        if (spaceNumParam.length() > 0)
            spaceNum = atoi(spaceNumParam.c_str());
        formatter.setTabIndentation(spaceNum, false);
    }
    else if ( IS_OPTIONS(arg, "T", "force-indent=tab=") )
    {
        int spaceNum = 4;
        string spaceNumParam = GET_PARAMS(arg, "T", "force-indent=tab=");
        if (spaceNumParam.length() > 0)
            spaceNum = atoi(spaceNumParam.c_str());
        formatter.setTabIndentation(spaceNum, true);
    }
    else if ( IS_PARAM_OPTION(arg, "indent=tab") )
    {
        formatter.setTabIndentation(4);
    }
    else if ( IS_PARAM_OPTIONS(arg, "s", "indent=spaces=") )
    {
        int spaceNum = 4;
        string spaceNumParam = GET_PARAMS(arg, "s", "indent=spaces=");
        if (spaceNumParam.length() > 0)
            spaceNum = atoi(spaceNumParam.c_str());
        formatter.setSpaceIndentation(spaceNum);
    }
    else if ( IS_PARAM_OPTION(arg, "indent=spaces") )
    {
        formatter.setSpaceIndentation(4);
    }
    else if ( IS_PARAM_OPTIONS(arg, "m", "min-conditional-indent=") )
    {
        int minIndent = 0;
        string minIndentParam = GET_PARAMS(arg, "m", "min-conditional-indent=");
        if (minIndentParam.length() > 0)
            minIndent = atoi(minIndentParam.c_str());
        formatter.setMinConditionalIndentLength(minIndent);
    }
    else if ( IS_PARAM_OPTIONS(arg, "M", "max-instatement-indent=") )
    {
        int maxIndent = 40;
        string maxIndentParam = GET_PARAMS(arg, "M", "max-instatement-indent=");
        if (maxIndentParam.length() > 0)
            maxIndent = atoi(maxIndentParam.c_str());
        formatter.setMaxInStatementIndentLength(maxIndent);
    }
    else if ( IS_OPTIONS(arg, "B", "indent-brackets") )
    {
        formatter.setBracketIndent(true);
    }
    else if ( IS_OPTIONS(arg, "G", "indent-blocks") )
    {
        formatter.setBlockIndent(true);
    }
    else if ( IS_OPTIONS(arg, "N", "indent-namespaces") )
    {
        formatter.setNamespaceIndent(true);
    }
    else if ( IS_OPTIONS(arg, "C", "indent-classes") )
    {
        formatter.setClassIndent(true);
    }
    else if ( IS_OPTIONS(arg, "S", "indent-switches") )
    {
        formatter.setSwitchIndent(true);
    }
    else if ( IS_OPTIONS(arg, "K", "indent-cases") )
    {
        formatter.setCaseIndent(true);
    }
    else if ( IS_OPTIONS(arg, "L", "indent-labels") )
    {
        formatter.setLabelIndent(true);
    }
    else if ( IS_OPTION(arg, "brackets=break-closing-headers") )
    {
        formatter.setBreakClosingHeaderBracketsMode(true);
    }
    else if ( IS_OPTIONS(arg, "b", "brackets=break") )
    {
        formatter.setBracketFormatMode(BREAK_MODE);
    }
    else if ( IS_OPTIONS(arg, "a", "brackets=attach") )
    {
        formatter.setBracketFormatMode(ATTACH_MODE);
    }
    else if ( IS_OPTIONS(arg, "l", "brackets=linux") )
    {
        formatter.setBracketFormatMode(BDAC_MODE);
    }
    else if ( IS_OPTIONS(arg, "O", "one-line=keep-blocks") )
    {
        formatter.setBreakOneLineBlocksMode(false);
    }
    else if ( IS_OPTIONS(arg, "o", "one-line=keep-statements") )
    {
        formatter.setSingleStatementsMode(false);
    }
    else if ( IS_OPTION(arg, "pad=paren") )
    {
        formatter.setParenthesisPaddingMode(true);
    }
    else if ( IS_OPTIONS(arg, "P", "pad=all") )
    {
        formatter.setOperatorPaddingMode(true);
        formatter.setParenthesisPaddingMode(true);
    }
    else if ( IS_OPTIONS(arg, "p", "pad=oper") )
    {
        formatter.setOperatorPaddingMode(true);
    }
    else if ( IS_OPTIONS(arg, "E", "fill-empty-lines") )
    {
        formatter.setEmptyLineFill(true);
    }
    else if (IS_OPTION(arg, "indent-preprocessor"))
    {
        formatter.setPreprocessorIndent(true);
    }
    else if (IS_OPTION(arg, "convert-tabs"))
    {
        formatter.setTabSpaceConversionMode(true);
    }
    else if (IS_OPTION(arg, "break-blocks=all"))
    {
        formatter.setBreakBlocksMode(true);
        formatter.setBreakClosingHeaderBlocksMode(true);
    }
    else if (IS_OPTION(arg, "break-blocks"))
    {
        formatter.setBreakBlocksMode(true);
    }
    else if (IS_OPTION(arg, "break-elseifs"))
    {
        formatter.setBreakElseIfsMode(true);
    }
    else if ( IS_OPTIONS(arg, "X", "errors-to-standard-output") )
    {
        _err = &cout;
    }
    else if ( IS_OPTIONS(arg ,"v", "version") )
    {
        (*_err) << "Artistic Style " << _version << endl;
        exit(1);
    }
    else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -