KDevelop API Documentation

buildtools/autotools/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 s = it.key() + " = " + it.data(); 00138 else 00139 s = QString::null; 00140 variables.remove( it ); 00141 } 00142 else 00143 { 00144 while ( !s.isEmpty() && s[ s.length() - 1 ] == '\\' && !ins.atEnd() ) 00145 { 00146 outs << s << endl; 00147 s = ins.readLine(); 00148 } 00149 } 00150 } 00151 00152 outs << s << endl; 00153 } 00154 00155 // Write new variables out 00156 QMap<QString, QString>::Iterator it2; 00157 for ( it2 = variables.begin(); it2 != variables.end(); ++it2 ){ 00158 if( !it2.data().stripWhiteSpace().isEmpty() ) 00159 outs << it2.key() + " = " + it2.data() << endl; 00160 } 00161 00162 fin.close(); 00163 fout.close(); 00164 00165 QDir().rename( fileName + "#", fileName ); 00166 } 00167 00168 void AutoProjectTool::removeFromMakefileam ( const QString &fileName, QMap <QString, QString> variables ) 00169 { 00170 QFile fin( fileName ); 00171 if ( !fin.open( IO_ReadOnly ) ) 00172 return ; 00173 QTextStream ins( &fin ); 00174 00175 QFile fout( fileName + "#" ); 00176 if ( !fout.open( IO_WriteOnly ) ) 00177 { 00178 fin.close(); 00179 return ; 00180 } 00181 QTextStream outs( &fout ); 00182 00183 QRegExp re( "^([A-Za-z][@A-Za-z0-9_]*)[ \t]*:?=[ \t]*(.*)$" ); 00184 00185 while ( !ins.atEnd() ) 00186 { 00187 bool found = false; 00188 QString s = ins.readLine(); 00189 00190 if ( re.exactMatch( s ) ) 00191 { 00192 QString lhs = re.cap( 1 ); 00193 QString rhs = re.cap( 2 ); 00194 QMap<QString, QString>::Iterator it; 00195 00196 for ( it = variables.begin(); it != variables.end(); ++it ) 00197 { 00198 if ( lhs == it.key() ) 00199 { 00200 // Skip continuation lines 00201 while ( !s.isEmpty() && s[ s.length() - 1 ] == '\\' && !ins.atEnd() ) 00202 s = ins.readLine(); 00203 00204 variables.remove ( it ); 00205 00206 found = true; 00207 00208 break; 00209 } 00210 } 00211 } 00212 00213 if ( !found ) 00214 outs << s << endl; 00215 } 00216 00217 fin.close(); 00218 fout.close(); 00219 00220 QDir().rename ( fileName + "#", fileName ); 00221 } 00222 00223 00224 QStringList AutoProjectTool::configureinLoadMakefiles(QString configureinpath) 00225 { 00226 QFile configurein(configureinpath); 00227 00228 configurein.open ( IO_ReadOnly ); 00229 //if ( !configurein.open( IO_ReadOnly ) ) 00230 // what should I return ?? 00231 00232 QTextStream stream( &configurein); 00233 QStringList list; 00234 00235 QString ac_match("^AC_OUTPUT"); 00236 00237 QRegExp ac_regex(ac_match); 00238 00239 while ( !stream.eof() ) { 00240 QString line = stream.readLine(); 00241 if ( ac_regex.search(line) >= 0 ) { 00242 QRegExp open("\\("); 00243 QRegExp close("\\)"); 00244 line = line.replace(ac_regex.search(line), ac_match.length() - 1, ""); 00245 00246 if (open.search(line) >= 0) 00247 line = line.replace(open.search(line), 1, ""); 00248 00249 if (close.search(line) >= 0) 00250 line = line.replace(close.search(line), 1, ""); 00251 00252 list = QStringList::split(" ", line); 00253 break; 00254 } 00255 } 00256 00257 configurein.close(); 00258 00259 // make a new object on the heap 00260 return list; 00261 00262 } 00263 00264 void AutoProjectTool::configureinSaveMakefiles(QString configureinpath, QStringList makefiles) 00265 { 00266 // read configure.in into buffer origfilecontent 00267 QFile configurein(configureinpath); 00268 00269 configurein.open ( IO_ReadOnly ); 00270 00271 QTextStream instream( &configurein); 00272 00273 QStringList origfilecontent; 00274 00275 while ( !instream.eof() ) { 00276 QString line = instream.readLine(); 00277 origfilecontent.push_back(line); 00278 } 00279 00280 configurein.close(); 00281 00282 00283 // put origfilecontent back into configure.in 00284 configurein.open ( IO_WriteOnly ); 00285 QTextStream outstream( &configurein); 00286 00287 QStringList::iterator it; 00288 for ( it = origfilecontent.begin(); it != origfilecontent.end(); it++ ) { 00289 QRegExp ac_regexp("^AC_OUTPUT"); 00290 QString currline = (QString) (*it); 00291 00292 if ( ac_regexp.search(currline) >= 0 ) { 00293 QString acline("AC_OUTPUT("); 00294 acline = acline.append(makefiles.join(" ")); 00295 acline = acline.append(")"); 00296 outstream << acline << "\n"; 00297 } 00298 else 00299 outstream << currline << "\n"; 00300 } 00301 00302 configurein.close(); 00303 }
KDE Logo
This file is part of the documentation for KDevelop Version 3.0.4.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Oct 6 17:39:09 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003