/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright (C) 2006-2010 by Jim Pattee <jimp03@email.com>
* Copyright (C) 1998-2002 by Tal Davidson
* <http://www.gnu.org/licenses/lgpl-3.0.html>
*
* 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>
*
* Artistic Style 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 3 of the License, or
* (at your option) any later version.
*
* Artistic Style 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 Artistic Style. If not, see <http://www.gnu.org/licenses/>.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
#include "astyle.h"
#include <algorithm>
#include <fstream>
#include <iostream>
namespace astyle
{
// static member variables
int ASFormatter::formatterFileType = 9; // initialized with an invalid type
vector<const string*>* ASFormatter::headers = NULL;
vector<const string*>* ASFormatter::nonParenHeaders = NULL;
vector<const string*>* ASFormatter::preDefinitionHeaders = NULL;
vector<const string*>* ASFormatter::preCommandHeaders = NULL;
vector<const string*>* ASFormatter::operators = NULL;
vector<const string*>* ASFormatter::assignmentOperators = NULL;
vector<const string*>* ASFormatter::castOperators = NULL;
/**
* Constructor of ASFormatter
*/
ASFormatter::ASFormatter()
{
sourceIterator = NULL;
enhancer = new ASEnhancer;
preBracketHeaderStack = NULL;
bracketTypeStack = NULL;
parenStack = NULL;
structStack = NULL;
lineCommentNoIndent = false;
formattingStyle = STYLE_NONE;
bracketFormatMode = NONE_MODE;
pointerAlignment = ALIGN_NONE;
lineEnd = LINEEND_DEFAULT;
shouldPadOperators = false;
shouldPadParensOutside = false;
shouldPadParensInside = false;
shouldPadHeader = false;
shouldUnPadParens = false;
shouldBreakOneLineBlocks = true;
shouldBreakOneLineStatements = true;
shouldConvertTabs = false;
shouldIndentCol1Comments = false;
shouldBreakBlocks = false;
shouldBreakClosingHeaderBlocks = false;
shouldBreakClosingHeaderBrackets = false;
shouldDeleteEmptyLines = false;
shouldBreakElseIfs = false;
shouldAddBrackets = false;
shouldAddOneLineBrackets = false;
// initialize ASFormatter static member vectors
formatterFileType = 9; // reset to an invalid type
initVector(headers);
initVector(nonParenHeaders);
initVector(preDefinitionHeaders);
initVector(preCommandHeaders);
initVector(operators);
initVector(assignmentOperators);
initVector(castOperators);
// the following prevents warning messages with cppcheck
// it will NOT compile if activated
// init();
}
/**
* Destructor of ASFormatter
*/
ASFormatter::~ASFormatter()
{
// delete ASFormatter stack vectors
deleteContainer(preBracketHeaderStack);
deleteContainer(bracketTypeStack);
deleteContainer(parenStack);
deleteContainer(structStack);
// delete static member vectors using swap to eliminate memory leak reporting
// delete ASFormatter static member vectors
formatterFileType = 9; // reset to an invalid type
deleteVector(headers);
deleteVector(nonParenHeaders);
deleteVector(preDefinitionHeaders);
deleteVector(preCommandHeaders);
deleteVector(operators);
deleteVector(assignmentOperators);
deleteVector(castOperators);
// delete ASBeautifier static member vectors
// must be done when the ASFormatter object is deleted (not ASBeautifier)
ASBeautifier::deleteStaticVectors();
delete enhancer;
}
/**
* initialize the ASFormatter.
*
* init() should be called every time a ASFormatter object is to start
* formatting a NEW source file.
* init() recieves a pointer to a ASSourceIterator object that will be
* used to iterate through the source code.
*
* @param sourceIterator a pointer to the ASSourceIterator or ASStreamIterator object.
*/
void ASFormatter::init(ASSourceIterator *si)
{
buildLanguageVectors();
fixOptionVariableConflicts();
ASBeautifier::init(si);
enhancer->init(getFileType(),
getIndentLength(),
getIndentString(),
getCaseIndent(),
getEmptyLineFill());
sourceIterator = si;
initContainer(preBracketHeaderStack, new vector<const string*>);
initContainer(parenStack, new vector<int>);
initContainer(structStack, new vector<bool>);
parenStack->push_back(0); // parenStack must contain this default entry
initContainer(bracketTypeStack, new vector<BracketType>);
bracketTypeStack->push_back(NULL_TYPE);
currentHeader = NULL;
currentLine = "";
readyFormattedLine = "";
formattedLine = "";
currentChar = ' ';
previousChar = ' ';
previousCommandChar = ' ';
previousNonWSChar = ' ';
quoteChar = '"';
charNum = 0;
leadingSpaces = 0;
formattedLineCommentNum = 0;
preprocBracketTypeStackSize = 0;
spacePadNum = 0;
nextLineSpacePadNum = 0;
currentLineFirstBracketNum = string::npos;
previousReadyFormattedLineLength = string::npos;
templateDepth = 0;
traceLineNumber = 0;
horstmannIndentChars = 0;
tabIncrementIn = 0;
previousBracketType = NULL_TYPE;
previousOperator = NULL;
isVirgin = true;
isInLineComment = false;
isInComment = false;
noTrimCommentContinuation = false;
isInPreprocessor = false;
doesLineStartComment = false;
lineEndsInCommentOnly = false;
lineIsLineCommentOnly = false;
lineIsEmpty = false;
isImmediatelyPostCommentOnly = false;
isImmediatelyPostEmptyLine = false;
isInQuote = false;
isInVerbatimQuote = false;
haveLineContinuationChar = false;
isInQuoteContinuation = false;
isSpecialChar = false;
isNonParenHeader = false;
foundNamespaceHeader = false;
foundClassHeader = false;
foundStructHeader = false;
foundInterfaceHeader = false;
foundPreDefinitionHeader = false;
foundPreCommandHeader = false;
foundCastOperator = false;
foundQuestionMark = false;
isInLineBreak = false;
endOfCodeReached = false;
isInExecSQL = false;
isInAsm = false;
isInAsmOneLine = false;
isInAsmBlock = false;
isLineReady = false;
isPreviousBracketBlockRelated = false;
isInPotentialCalculation = false;
shouldReparseCurrentChar = false;
needHeaderOpeningBracket = false;
shouldBreakLineAtNextChar = false;
passedSemicolon = false;
passedColon = false;
clearNonInStatement = false;
isInTemplate = false;
isInBlParen = false;
isImmediatelyPostComment = false;
isImmediatelyPostLineComment = false;
isImmediatelyPostEmptyBlock = false;
isImmediatelyPostPreprocessor = false;
isImmediatelyPostReturn = false;
isImmediatelyPostOperator = false;
isCharImmediatelyPostReturn = false;
isCharImmediatelyPostOperator = false;
isCharImmediatelyPostComment = false;
isPreviousCharPostComment = false;
isCharImmediatelyPostLineComment = false;
isCharImmediatelyPostOpenBlock = false;
isCharImmediatelyPostCloseBlock = false;
isCharImmediatelyPostTemplate = false;
breakCurrentOneLineBlock = false;
isInHorstmannRunIn = false;
currentLineBeginsWithBracket = false;
isPrependPostBlockEmptyLineRequested = false;
isAppendPostBlockEmptyLineRequested = false;
prependEmptyLine = false;
appendOpeningBracket = false;
foundClosingHeader = false;
isImmediatelyPostHeader = false;
isInHeader = false;
isInCase = false;
isJavaStaticConstructor = false;
}
/**
* build vectors for each programing language
* depending on the file exte