KDevelop API Documentation

misc.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002 *   Copyright (C) 2001-2002 by Bernd Gehrmann                             *
00003 *   bernd@kdevelop.org                                                    *
00004 *                                                                         *
00005 *   This program is free software; you can redistribute it and/or modify  *
00006 *   it under the terms of the GNU General Public License as published by  *
00007 *   the Free Software Foundation; either version 2 of the License, or     *
00008 *   (at your option) any later version.                                   *
00009 *                                                                         *
00010 ***************************************************************************/
00011 
00012 #include <qdir.h>
00013 #include <qfile.h>
00014 #include <qregexp.h>
00015 #include <qtextstream.h>
00016 
00017 #include <kdebug.h>
00018 #include <kparts/componentfactory.h>
00019 #include <kservice.h>
00020 
00021 #include "misc.h"
00022 
00023 #include "kdevcompileroptions.h"
00024 
00025 
00026 static KDevCompilerOptions *createCompilerOptions( const QString &name, QObject *parent )
00027 {
00028     KService::Ptr service = KService::serviceByDesktopName( name );
00029     if ( !service )
00030     {
00031         kdDebug( 9020 ) << "Can't find service " << name;
00032         return 0;
00033     }
00034 
00035     QStringList args;
00036     QVariant prop = service->property( "X-KDevelop-Args" );
00037     if ( prop.isValid() )
00038         args = QStringList::split( " ", prop.toString() );
00039 
00040     return KParts::ComponentFactory
00041            ::createInstanceFromService<KDevCompilerOptions>( service, parent,
00042                                                              service->name().latin1(), args );
00043 }
00044 
00045 
00046 QString AutoProjectTool::execFlagsDialog( const QString &compiler, const QString &flags, QWidget *parent )
00047 {
00048     KDevCompilerOptions * plugin = createCompilerOptions( compiler, parent );
00049 
00050     if ( plugin )
00051     {
00052         QString newflags = plugin->exec( parent, flags );
00053         delete plugin;
00054         return newflags;
00055     }
00056     return QString::null;
00057 }
00058 
00059 
00060 QString AutoProjectTool::canonicalize( const QString &str )
00061 {
00062     QString res;
00063     for ( uint i = 0; i < str.length(); ++i )
00064         res += ( str[ i ].isLetterOrNumber() || str[ i ] == '@' ) ? str[ i ] : QChar( '_' );
00065 
00066     return res;
00067 }
00068 
00069 
00070 void AutoProjectTool::parseMakefileam( const QString &fileName, QMap<QString, QString> *variables )
00071 {
00072     QFile f( fileName );
00073     if ( !f.open( IO_ReadOnly ) )
00074         return ;
00075     QTextStream stream( &f );
00076 
00077     QRegExp re( "^(#kdevelop:[ \t]*)?([A-Za-z][@A-Za-z0-9_]*)[ \t]*:?=[ \t]*(.*)$" );
00078 
00079     while ( !stream.atEnd() )
00080     {
00081         QString line;
00082         QString s = stream.readLine();
00083         while ( !s.isEmpty() && s[ s.length() - 1 ] == '\\' && !stream.atEnd() )
00084         {
00085             // Read continuation lines
00086             line += s.left( s.length() - 1 );
00087             s = stream.readLine();
00088         }
00089         line += s;
00090 
00091         if ( re.exactMatch( line ) )
00092         {
00093             QString lhs = re.cap( 2 );
00094             // The need for stripWhitespace seems to be a Qt bug.
00095             QString rhs = re.cap( 3 ).stripWhiteSpace();
00096             variables->insert( lhs, rhs );
00097         }
00098     }
00099 
00100     f.close();
00101 }
00102 
00103 
00104 void AutoProjectTool::modifyMakefileam( const QString &fileName, QMap<QString, QString> variables )
00105 {
00106     QFile fin( fileName );
00107     if ( !fin.open( IO_ReadOnly ) )
00108         return ;
00109     QTextStream ins( &fin );
00110 
00111     QFile fout( fileName + "#" );
00112     if ( !fout.open( IO_WriteOnly ) )
00113     {
00114         fin.close();
00115         return ;
00116     }
00117     QTextStream outs( &fout );
00118 
00119     QRegExp re( "^([A-Za-z][@A-Za-z0-9_]*)[ \t]*:?=[ \t]*(.*)$" );
00120 
00121     while ( !ins.atEnd() )
00122     {
00123         QString line;
00124         QString s = ins.readLine();
00125         if ( re.exactMatch( s ) )
00126         {
00127             QString lhs = re.cap( 1 );
00128             QString rhs = re.cap( 2 );
00129             QMap<QString, QString>::Iterator it = variables.find( lhs );
00130 
00131             if ( it != variables.end() )
00132             {
00133                 // Skip continuation lines
00134                 while ( !s.isEmpty() && s[ s.length() - 1 ] == '\\' && !ins.atEnd() )
00135                     s = ins.readLine();
00136                 if( !it.data().stripWhiteSpace().isEmpty() ) {
00137                     QStringList variableList = QStringList::split(' ', it.data());
00138                     s = it.key() + " = ";
00139                     int l = s.length();
00140                     for (uint i = 0; i < variableList.count(); i++) {
00141                         l += variableList[i].length() + 1;
00142                         if (l > 80) {
00143                             s += "\\\n\t";
00144                             l = 8;
00145                         }
00146                         s += variableList[i];
00147                         if( i != variableList.count() - 1 )
00148                             s += ' ';
00149                     }
00150                 }
00151                 else
00152                     s = QString::null;
00153                 variables.remove( it );
00154             }
00155             else
00156             {
00157                 while ( !s.isEmpty() && s[ s.length() - 1 ] == '\\' && !ins.atEnd() )
00158                 {
00159                     outs << s << endl;
00160                     s = ins.readLine();
00161                 }
00162             }
00163         }
00164 
00165         outs << s << endl;
00166     }
00167 
00168     // Write new variables out
00169     QMap<QString, QString>::Iterator it2;
00170     for ( it2 = variables.begin(); it2 != variables.end(); ++it2 ){
00171         if( !it2.data().stripWhiteSpace().isEmpty() ) {
00172         QStringList variableList = QStringList::split(' ', it2.data());
00173         outs << it2.key() + " =";
00174         int l = it2.key().length() + 2;
00175         for (uint i = 0; i < variableList.count(); i++) {
00176             l += variableList[i].length() + 1;
00177             if (l > 80) {
00178                 outs << "\\\n\t" << variableList[i];
00179                 l = 8 + variableList[i].length();
00180             } else
00181             {
00182                 outs << ' ' << variableList[i];
00183             }
00184         }
00185         outs << endl;
00186             }
00187     }
00188 
00189     fin.close();
00190     fout.close();
00191 
00192     QDir().rename( fileName + "#", fileName );
00193 }
00194 
00195 void AutoProjectTool::removeFromMakefileam ( const QString &fileName, QMap <QString, QString> variables )
00196 {
00197     QFile fin( fileName );
00198     if ( !fin.open( IO_ReadOnly ) )
00199         return ;
00200     QTextStream ins( &fin );
00201 
00202     QFile fout( fileName + "#" );
00203     if ( !fout.open( IO_WriteOnly ) )
00204     {
00205         fin.close();
00206         return ;
00207     }
00208     QTextStream outs( &fout );
00209 
00210     QRegExp re( "^([A-Za-z][@A-Za-z0-9_]*)[ \t]*:?=[ \t]*(.*)$" );
00211 
00212     while ( !ins.atEnd() )
00213     {
00214         bool found = false;
00215         QString s = ins.readLine();
00216 
00217         if ( re.exactMatch( s ) )
00218         {
00219             QString lhs = re.cap( 1 );
00220             QString rhs = re.cap( 2 );
00221             QMap<QString, QString>::Iterator it;
00222 
00223             for ( it = variables.begin(); it != variables.end(); ++it )
00224             {
00225                 if ( lhs == it.key() )
00226                 {
00227                     // Skip continuation lines
00228                     while ( !s.isEmpty() && s[ s.length() - 1 ] == '\\' && !ins.atEnd() )
00229                         s = ins.readLine();
00230 
00231                     variables.remove ( it );
00232 
00233                     found = true;
00234 
00235                     break;
00236                 }
00237             }
00238         }
00239 
00240         if ( !found )
00241             outs << s << endl;
00242     }
00243 
00244     fin.close();
00245     fout.close();
00246 
00247     QDir().rename ( fileName + "#", fileName );
00248 }
00249 
00250 
00251 QStringList AutoProjectTool::configureinLoadMakefiles(QString configureinpath)
00252 {
00253     QFile configurein(configureinpath);
00254 
00255     configurein.open ( IO_ReadOnly );
00256     //if ( !configurein.open( IO_ReadOnly ) )
00257     //  what should I return ??
00258 
00259     QTextStream stream( &configurein);
00260     QStringList list;
00261 
00262     QString ac_match("^AC_OUTPUT");
00263 
00264     QRegExp ac_regex(ac_match);
00265 
00266     while ( !stream.eof() ) {
00267         QString line = stream.readLine();
00268         if ( ac_regex.search(line) >= 0 ) {
00269             QRegExp open("\\(");
00270             QRegExp close("\\)");
00271             line = line.replace(ac_regex.search(line), ac_match.length() - 1, "");
00272 
00273             if (open.search(line) >= 0)
00274                 line = line.replace(open.search(line), 1, "");
00275 
00276             if (close.search(line) >= 0)
00277                 line = line.replace(close.search(line), 1, "");
00278 
00279             list = QStringList::split(" ", line);
00280             break;
00281         }
00282     }
00283 
00284     configurein.close();
00285 
00286     // make a new object on the heap
00287     return list;
00288 
00289 }
00290 
00291 void AutoProjectTool::configureinSaveMakefiles(QString configureinpath, QStringList makefiles)
00292 {
00293     // read configure.in into buffer origfilecontent
00294     QFile configurein(configureinpath);
00295 
00296     configurein.open ( IO_ReadOnly );
00297 
00298     QTextStream instream( &configurein);
00299 
00300     QStringList origfilecontent;
00301 
00302     while ( !instream.eof() ) {
00303         QString line = instream.readLine();
00304         origfilecontent.push_back(line);
00305     }
00306 
00307     configurein.close();
00308 
00309 
00310     // put origfilecontent back into configure.in
00311     configurein.open ( IO_WriteOnly );
00312     QTextStream outstream( &configurein);
00313 
00314     QStringList::iterator it;
00315     for ( it = origfilecontent.begin(); it != origfilecontent.end(); it++ ) {
00316         QRegExp ac_regexp("^AC_OUTPUT");
00317         QString currline = (QString) (*it);
00318 
00319         if ( ac_regexp.search(currline) >= 0 ) {
00320             QString acline("AC_OUTPUT(");
00321             acline = acline.append(makefiles.join(" "));
00322             acline = acline.append(")");
00323             outstream << acline << "\n";
00324         }
00325         else
00326             outstream << currline << "\n";
00327     }
00328 
00329     configurein.close();
00330 }
KDE Logo
This file is part of the documentation for KDevelop Version 3.1.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 23 00:03:53 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003