File indexing completed on 2024-04-28 15:54:22

0001 /* This file is part of kdev-pg-qt
0002    Copyright (C) 2005 Roberto Raggi <roberto@kdevelop.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "kdev-pg-beautifier.h"
0021 
0022 #include <QFile>
0023 #include <QFileInfo>
0024 
0025 namespace KDevPG
0026 {
0027 
0028 IteratorQTextStream::IteratorQTextStream( QTextStream& stream )
0029   : m_stream(stream)
0030   , m_peekStart(-1)
0031 {
0032 }
0033 
0034 bool IteratorQTextStream::hasMoreLines() const
0035 {
0036   return !m_stream.atEnd();
0037 }
0038 
0039 std::string IteratorQTextStream::nextLine(bool emptyLineWasDeleted)
0040 {
0041   Q_UNUSED(emptyLineWasDeleted)
0042   return m_stream.readLine().toStdString();
0043 }
0044 
0045 string IteratorQTextStream::peekNextLine()
0046 {
0047     if (m_peekStart == -1) {
0048         m_peekStart = m_stream.pos();
0049     }
0050     return m_stream.readLine().toUtf8().data();
0051 }
0052 
0053 void IteratorQTextStream::peekReset()
0054 {
0055     if(m_peekStart != -1)
0056         m_stream.seek(m_peekStart);
0057     m_peekStart = -1; // invalid
0058 }
0059 
0060 void format(QTextStream& in, const QString& oname)
0061 {
0062   QFile ofile(oname);
0063   ofile.open(QIODevice::WriteOnly);
0064   QTextStream out(&ofile);
0065   
0066   astyle::ASFormatter f;
0067 
0068   f.setCStyle();
0069 
0070   f.setBlockIndent(false);
0071   f.setBracketIndent(false);
0072   f.setSpaceIndentation(4);
0073   f.setTabSpaceConversionMode(true);
0074   f.setMinConditionalIndentLength(f.getIndentLength());
0075   f.setMaxInStatementIndentLength(40);
0076   f.setOperatorPaddingMode(false);
0077   f.setBracketFormatMode(astyle::BREAK_MODE);
0078   f.setClassIndent(false);
0079   f.setSwitchIndent(false);
0080   f.setNamespaceIndent(false);
0081   f.setBreakElseIfsMode(false);
0082   f.setParensUnPaddingMode(false);
0083   f.setEmptyLineFill(false);
0084   IteratorQTextStream strm(in);
0085   f.init(&strm);
0086 
0087   do
0088   {
0089     out << QString::fromStdString( f.nextLine() ) << endl;
0090   }
0091   while (f.hasMoreLines());
0092 }
0093 
0094 }