~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to buildtools/autotools/misc.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2010-05-05 07:21:55 UTC
  • mfrom: (1.2.3 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505072155-h78lx19pu04sbhtn
Tags: 4:4.0.0-2
* Upload to unstable (Closes: #579947, #481832).
* Acknowledge obsolete NMU fixes (Closes: #562410, #546961).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
*   Copyright (C) 2001-2002 by Bernd Gehrmann                             *
3
 
*   bernd@kdevelop.org                                                    *
4
 
*                                                                         *
5
 
*   This program is free software; you can redistribute it and/or modify  *
6
 
*   it under the terms of the GNU General Public License as published by  *
7
 
*   the Free Software Foundation; either version 2 of the License, or     *
8
 
*   (at your option) any later version.                                   *
9
 
*                                                                         *
10
 
***************************************************************************/
11
 
 
12
 
#include <qdir.h>
13
 
#include <qfile.h>
14
 
#include <qregexp.h>
15
 
#include <qtextstream.h>
16
 
#include <qdict.h>
17
 
 
18
 
#include <kdebug.h>
19
 
#include <kparts/componentfactory.h>
20
 
#include <kservice.h>
21
 
 
22
 
#include "misc.h"
23
 
 
24
 
#include "kdevcompileroptions.h"
25
 
 
26
 
 
27
 
static KDevCompilerOptions *createCompilerOptions( const QString &name, QObject *parent )
28
 
{
29
 
        KService::Ptr service = KService::serviceByDesktopName( name );
30
 
        if ( !service )
31
 
        {
32
 
                kdDebug( 9020 ) << "Can't find service " << name << endl;
33
 
                return 0;
34
 
        }
35
 
 
36
 
 
37
 
    KLibFactory *factory = KLibLoader::self()->factory(QFile::encodeName(service->library()));
38
 
    if (!factory) {
39
 
        QString errorMessage = KLibLoader::self()->lastErrorMessage();
40
 
        kdDebug(9020) << "There was an error loading the module " << service->name() << endl <<
41
 
            "The diagnostics is:" << endl << errorMessage << endl;
42
 
        exit(1);
43
 
    }
44
 
 
45
 
    QStringList args;
46
 
    QVariant prop = service->property("X-KDevelop-Args");
47
 
    if (prop.isValid())
48
 
        args = QStringList::split(" ", prop.toString());
49
 
 
50
 
    QObject *obj = factory->create(parent, service->name().latin1(),
51
 
                                   "KDevCompilerOptions", args);
52
 
 
53
 
    if (!obj->inherits("KDevCompilerOptions")) {
54
 
        kdDebug(9020) << "Component does not inherit KDevCompilerOptions" << endl;
55
 
        return 0;
56
 
    }
57
 
    KDevCompilerOptions *dlg = (KDevCompilerOptions*) obj;
58
 
 
59
 
    return dlg;
60
 
 
61
 
/*
62
 
        QStringList args;
63
 
        QVariant prop = service->property( "X-KDevelop-Args" );
64
 
        if ( prop.isValid() )
65
 
                args = QStringList::split( " ", prop.toString() );
66
 
 
67
 
        return KParts::ComponentFactory
68
 
               ::createInstanceFromService<KDevCompilerOptions>( service, parent,
69
 
                                                                 service->name().latin1(), args );*/
70
 
}
71
 
 
72
 
 
73
 
QString AutoProjectTool::execFlagsDialog( const QString &compiler, const QString &flags, QWidget *parent )
74
 
{
75
 
        KDevCompilerOptions * plugin = createCompilerOptions( compiler, parent );
76
 
 
77
 
        if ( plugin )
78
 
        {
79
 
                QString newflags = plugin->exec( parent, flags );
80
 
                delete plugin;
81
 
                return newflags;
82
 
        }
83
 
        return QString::null;
84
 
}
85
 
 
86
 
 
87
 
QString AutoProjectTool::canonicalize( const QString &str )
88
 
{
89
 
        QString res;
90
 
        for ( uint i = 0; i < str.length(); ++i )
91
 
                res += ( str[ i ].isLetterOrNumber() || str[ i ] == '@' ) ? str[ i ] : QChar( '_' );
92
 
 
93
 
        kdDebug(9020) << k_funcinfo << "normalized '" << str << "' to '" << res << "'" << endl;
94
 
        return res;
95
 
}
96
 
 
97
 
 
98
 
/**
99
 
 * Read the Makefile.am and return a map of all the variables.
100
 
 * Will take notice of backslash and += constructs.
101
 
 * @param fileName
102
 
 * @param variables
103
 
 */
104
 
void AutoProjectTool::parseMakefileam(const QString &fileName, QMap<QString, QString> *variables)
105
 
{
106
 
        QFile f(fileName);
107
 
        if (!f.open(IO_ReadOnly))
108
 
        {
109
 
                return ;
110
 
        }
111
 
        QTextStream stream(&f);
112
 
 
113
 
        QRegExp re("^(#kdevelop:[ \t]*)?([A-Za-z][@A-Za-z0-9_]*)[ \t]*([:\\+]?=)[ \t]*(.*)$");
114
 
 
115
 
        QString last;
116
 
        bool multiLine = false;
117
 
        while (!stream.atEnd())
118
 
        {
119
 
                QString s = stream.readLine().stripWhiteSpace();
120
 
                if (re.exactMatch(s))
121
 
                {
122
 
                        QString lhs = re.cap(2);
123
 
                        QString rhs = re.cap(4);
124
 
                        if (rhs[ rhs.length() - 1 ] == '\\')
125
 
                        {
126
 
                                multiLine = true;
127
 
                                last = lhs;
128
 
                                rhs[rhs.length() - 1] = ' ';
129
 
                        }
130
 
 
131
 
                        // The need for stripWhiteSpace seems to be a Qt bug.
132
 
                        // make our list nice and neat.
133
 
                        QStringList bits = QStringList::split(" ", rhs);
134
 
                        rhs = bits.join(" ");
135
 
                        if (re.cap(3) == "+=")
136
 
                        {
137
 
                                ((*variables)[lhs] += ' ') += rhs;
138
 
                        }
139
 
                        else
140
 
                        {
141
 
                                variables->insert(lhs, rhs);
142
 
                        }
143
 
                }
144
 
                else if (multiLine)
145
 
                {
146
 
                        if (s[s.length()-1] == '\\')
147
 
                        {
148
 
                                s[s.length()-1] = ' ';
149
 
                        }
150
 
                        else
151
 
                        {
152
 
                                multiLine = false;
153
 
                        }
154
 
                        QStringList bits = QStringList::split(" ", s);
155
 
                        ((*variables)[last] += ' ') += bits.join(" ");
156
 
                }
157
 
        }
158
 
        f.close();
159
 
 
160
 
        QMap<QString, QString> list;
161
 
 
162
 
        for (QMap<QString, QString>::iterator iter = variables->begin();iter != variables->end();iter++)
163
 
        {
164
 
                QStringList items = QStringList::split(" ", iter.data());
165
 
                QMap<QString, QString> unique;
166
 
                for (uint i = 0;i < items.size();i++)
167
 
                {
168
 
                        unique.insert(items[i], "");
169
 
                }
170
 
                QString line;
171
 
                for (QMap<QString, QString>::iterator it = unique.begin();it != unique.end();it++)
172
 
                {
173
 
                        line += it.key() + ' ';
174
 
                }
175
 
                if (line.length() > 1)
176
 
                {
177
 
                        line.setLength(line.length() - 1);
178
 
                }
179
 
 
180
 
                list.insert(iter.key(), line);
181
 
        }
182
 
        *variables = list;
183
 
}
184
 
 
185
 
/**
186
 
 * Add entries to a variable. Will just add the variables to the existing line, removing duplicates
187
 
 * Will preserve += constructs and make sure that the variable only has one copy of the value across
188
 
 * all += constructs
189
 
 * @param fileName
190
 
 * @param variables key=value string of entries to add
191
 
 */
192
 
void AutoProjectTool::addToMakefileam(const QString &fileName, QMap<QString, QString> variables)
193
 
{
194
 
        AutoProjectTool::addRemoveMakefileam(fileName,  variables,  true);
195
 
}
196
 
 
197
 
/**
198
 
 * Set entries to a variable. Will set the variables to the existing line, removing duplicates
199
 
 * Will preserve += constructs and make sure that the variable only has one copy of the value across
200
 
 * all += constructs
201
 
 * Adds line if it does not exist.
202
 
 * @param fileName
203
 
 * @param variables key=value string of entries to add
204
 
 */
205
 
void AutoProjectTool::setMakefileam ( const QString &fileName, QMap<QString, QString> variables )
206
 
{
207
 
        for ( QMap<QString, QString>::Iterator it0 = variables.begin(); it0 != variables.end(); ++it0 )
208
 
        {
209
 
                kdDebug ( 9020 ) << "key (set): " << it0.key() << "=" << it0.data() << endl;
210
 
        }
211
 
 
212
 
        // input file reading
213
 
        QFile fin ( fileName );
214
 
        if ( !fin.open ( IO_ReadOnly ) )
215
 
        {
216
 
                return ;
217
 
        }
218
 
        QTextStream ins ( &fin );
219
 
 
220
 
        // output file writing.
221
 
        QFile fout ( fileName + "#" );
222
 
        if ( !fout.open ( IO_WriteOnly ) )
223
 
        {
224
 
                fin.close();
225
 
                return ;
226
 
        }
227
 
        QTextStream outs ( &fout );
228
 
 
229
 
        // variables
230
 
        QRegExp re ( "^(#kdevelop:[ \t]*)?([A-Za-z][@A-Za-z0-9_]*)[ \t]*([:\\+]?=)[ \t]*(.*)$" );
231
 
 
232
 
        bool multiLine = false;
233
 
        QString lastLhs;
234
 
        QMap<QString, QString> seenLhs;
235
 
        while ( !fin.atEnd() )
236
 
        {
237
 
                QString s = ins.readLine();
238
 
                if ( re.exactMatch ( s ) )
239
 
                {
240
 
                        QString lhs = re.cap ( 2 );
241
 
                        bool notFound = ( variables.find ( lhs ) == variables.end() );
242
 
 
243
 
                        if ( notFound )
244
 
                        {
245
 
                                if ( seenLhs.find ( lhs ) == seenLhs.end() )
246
 
                                {
247
 
                                        // not interested in this line at all
248
 
                                        // write it out as is..
249
 
                                        outs << s << endl;
250
 
                                }
251
 
                                // we have seen this variable, but since we are setting the
252
 
                                // whole line - we skip this as it will be a += line.
253
 
                        }
254
 
                        else
255
 
                        {
256
 
                                // we are interested in this line..
257
 
                                QString rhs = re.cap ( 4 ).stripWhiteSpace();
258
 
                                if ( rhs[ rhs.length() - 1 ] == '\\' )
259
 
                                {
260
 
                                        // save it for when we have the whole line..
261
 
                                        multiLine = true;
262
 
                                        lastLhs = lhs;
263
 
                                }
264
 
                                else
265
 
                                {
266
 
                                        // deal with it now - a single line
267
 
                                        // we are adding our interested values to this line and writing it
268
 
                                        // now write the line out if it is not going to be empty.
269
 
                                        QString newLine ( lhs );
270
 
                                        newLine += " = ";
271
 
                                        bool added = false;
272
 
                                        int len = newLine.length();
273
 
                                        QStringList variableList = QStringList::split ( ' ', variables[lhs] );
274
 
                                        for ( uint count = 0; count < variableList.size(); count++ )
275
 
                                        {
276
 
                                                len += variableList[count].length() + 1;
277
 
                                                if ( len > 80 )
278
 
                                                {
279
 
                                                        newLine += "\\\n\t";
280
 
                                                        len = 8;
281
 
                                                }
282
 
                                                newLine += variableList[count];
283
 
                                                newLine += ' ';
284
 
                                                added = true;
285
 
 
286
 
                                        }
287
 
                                        // only print it out if there was a value to add..
288
 
                                        if ( added )
289
 
                                        {
290
 
                                                newLine.setLength ( newLine.length() - 1 );
291
 
                                                outs << newLine << endl;
292
 
                                        }
293
 
                                        seenLhs[lhs] = "done";
294
 
                                        variables.erase ( lhs );
295
 
                                }
296
 
                        }
297
 
                }
298
 
                else if ( multiLine )
299
 
                {
300
 
                        s = s.stripWhiteSpace();
301
 
                        // we are only here if were interested in this line..
302
 
                        if ( s[s.length()-1] == '\\' )
303
 
                        {
304
 
                                s.setLength ( s.length() - 1 );
305
 
                                // still more multi line we wait for..
306
 
                        }
307
 
                        else
308
 
                        {
309
 
                                // end of the multi line..
310
 
                                multiLine = false;
311
 
                        }
312
 
 
313
 
                        if ( !multiLine )
314
 
                        {
315
 
                                // we are adding our interested values to this line and writing it
316
 
                                // now write the line out if it is not going to be empty.
317
 
                                QString newLine ( lastLhs );
318
 
                                newLine += " = ";
319
 
                                bool added = false;
320
 
                                int len = newLine.length();
321
 
                                QStringList variableList = QStringList::split ( ' ', variables[lastLhs] );
322
 
                                for ( uint count = 0; count < variableList.size(); count++ )
323
 
                                {
324
 
                                        len += variableList[count].length() + 1;
325
 
                                        if ( len > 80 )
326
 
                                        {
327
 
                                                newLine += "\\\n\t";
328
 
                                                len = 8;
329
 
                                        }
330
 
                                        newLine += variableList[count];
331
 
                                        newLine += ' ';
332
 
                                        added = true;
333
 
                                }
334
 
                                // only print it out if there was a value to add..
335
 
                                if ( added )
336
 
                                {
337
 
                                        newLine.setLength ( newLine.length() - 1 );
338
 
                                        outs << newLine << endl;
339
 
                                }
340
 
                                seenLhs[lastLhs] = "done";
341
 
                                variables.erase ( lastLhs );
342
 
                                lastLhs.setLength ( 0 );
343
 
                        }
344
 
                }
345
 
                else
346
 
                {
347
 
                        // can write this line out..
348
 
                        // not a match, not a multi line,
349
 
                        outs << s << endl;
350
 
                }
351
 
        }
352
 
 
353
 
        for ( QMap<QString, QString>::Iterator it0 = variables.begin(); it0 != variables.end(); ++it0 )
354
 
        {
355
 
                QString newLine ( it0.key() );
356
 
                newLine += " = ";
357
 
                bool added = false;
358
 
                int len = newLine.length();
359
 
                QStringList variableList = QStringList::split ( ' ', it0.data() );
360
 
                for ( uint count = 0; count < variableList.size(); count++ )
361
 
                {
362
 
                        len += variableList[count].length() + 1;
363
 
                        if ( len > 80 )
364
 
                        {
365
 
                                newLine += "\\\n\t";
366
 
                                len = 8;
367
 
                        }
368
 
                        newLine += variableList[count];
369
 
                        newLine += ' ';
370
 
                        added = true;
371
 
 
372
 
                }
373
 
                // only print it out if there was a value to add..
374
 
                if ( added )
375
 
                {
376
 
                        newLine.setLength ( newLine.length() - 1 );
377
 
                        outs << newLine << endl;
378
 
                }
379
 
        }
380
 
 
381
 
        fin.close();
382
 
        fout.close();
383
 
 
384
 
        QDir().rename ( fileName + "#", fileName );
385
 
}
386
 
 
387
 
 
388
 
/**
389
 
 * Add entries to a variable. Will just add the variables to the existing line, removing duplicates
390
 
 * Will preserve += constructs and make sure that the variable only has one copy of the value across
391
 
 * all += constructs
392
 
 * @param fileName
393
 
 * @param variables key=value string of entries to add
394
 
 * @param add true= add these key,value pairs, false = remove. You can have empty values for an add - the whole line is
395
 
 * removed. For adding, we will not add an empty line.
396
 
 */
397
 
void AutoProjectTool::addRemoveMakefileam(const QString &fileName, QMap<QString, QString> variables,  bool add)
398
 
{
399
 
        // input file reading
400
 
        QFile fin(fileName);
401
 
        if (!fin.open(IO_ReadOnly))
402
 
        {
403
 
                return ;
404
 
        }
405
 
        QTextStream ins(&fin);
406
 
 
407
 
        // output file writing.
408
 
        QFile fout(fileName + "#");
409
 
        if (!fout.open(IO_WriteOnly))
410
 
        {
411
 
                fin.close();
412
 
                return ;
413
 
        }
414
 
        QTextStream outs(&fout);
415
 
 
416
 
        // variables
417
 
        QRegExp re("^(#kdevelop:[ \t]*)?([A-Za-z][@A-Za-z0-9_]*)[ \t]*([:\\+]?=)[ \t]*(.*)$");
418
 
 
419
 
        // build key=map of values to add
420
 
        // map can be empty.we never add an empty key, but do remove empty keys from the file..
421
 
        QDict< QMap<QString, bool> > interest;
422
 
        for (QMap<QString, QString>::Iterator it0 = variables.begin(); it0 != variables.end(); ++it0)
423
 
        {
424
 
                kdDebug(9020) << "key (" << add<<"): " << it0.key() << "="<< it0.data() << endl;
425
 
 
426
 
                QMap<QString, bool>* set = new QMap<QString, bool>();
427
 
                if (!it0.data().stripWhiteSpace().isEmpty())
428
 
                {
429
 
                        QStringList variableList = QStringList::split(' ', it0.data());
430
 
 
431
 
                        for (uint i = 0; i < variableList.count(); i++)
432
 
                        {
433
 
                                set->insert(variableList[i], true);
434
 
                        }
435
 
                }
436
 
                interest.insert(it0.key(), set);
437
 
        }
438
 
 
439
 
        bool multiLine = false;
440
 
        QString lastLhs;
441
 
        QStringList lastRhs;
442
 
        QMap<QString, QString> seenLhs;
443
 
        while (!fin.atEnd())
444
 
        {
445
 
                QString s = ins.readLine();
446
 
                if (re.exactMatch(s))
447
 
                {
448
 
                        QString lhs = re.cap(2);
449
 
                        QMap<QString, bool>* ourRhs = interest.find(lhs);
450
 
 
451
 
                        if (!ourRhs)
452
 
                        {
453
 
                                // not interested in this line at all
454
 
                                // write it out as is..
455
 
                                outs << s << endl;
456
 
                        }
457
 
                        else
458
 
                        {
459
 
                                // we are interested in this line..
460
 
                                QString rhs = re.cap(4).stripWhiteSpace();
461
 
                                if (rhs[ rhs.length() - 1 ] == '\\')
462
 
                                {
463
 
                                        // save it for when we have the whole line..
464
 
                                        multiLine = true;
465
 
                                        lastLhs = lhs;
466
 
                                        rhs.setLength(rhs.length() - 1);
467
 
                                        lastRhs += QStringList::split(" ", rhs);
468
 
                                }
469
 
                                else
470
 
                                {
471
 
                                        // deal with it now.
472
 
 
473
 
                                        QStringList bits = QStringList::split(" ", rhs);
474
 
                                        if (add)
475
 
                                        {
476
 
                                                // we are adding our interested values to this line and writing it
477
 
 
478
 
                                                // add this line to we we want to add to remove duplicates.
479
 
                                                for (uint index = 0; index < bits.size(); index++)
480
 
                                                {
481
 
                                                        QMap<QString, bool>::iterator findEntry = ourRhs->find(bits[index]);
482
 
                                                        if (findEntry == ourRhs->end())
483
 
                                                        {
484
 
                                                                // we haven't seen it, so add it, so we don't add it again later..
485
 
                                                                ourRhs->insert(bits[index], true);
486
 
                                                        }
487
 
                                                        // else we have this value in our 'to add list' , it is either already been
488
 
                                                        // added, so we don't want to add it again, or it hasn't been added, in which
489
 
                                                        // case we will do so soon. so we can ignore this now..
490
 
                                                }
491
 
                                                // now write the line out if it is not going to be empty.
492
 
                                                QString newLine(lhs);
493
 
                                                if (seenLhs.find(lhs) == seenLhs.end())
494
 
                                                {
495
 
                                                        newLine += " = ";
496
 
                                                        seenLhs[lhs] = "";
497
 
                                                }
498
 
                                                else
499
 
                                                {
500
 
                                                        newLine += " += ";
501
 
                                                }
502
 
 
503
 
                                                int len = newLine.length();
504
 
                                                bool added = false;
505
 
                                                QValueList<QString> keys = ourRhs->keys();
506
 
                                                for (uint count = 0; count < keys.size(); count++)
507
 
                                                {
508
 
                                                        // if out entry is true, add it..
509
 
                                                        if ((*ourRhs)[keys[count]])
510
 
                                                        {
511
 
                                                                added = true;
512
 
                                                                len += keys[count].length() + 1;
513
 
                                                                if (len > 80)
514
 
                                                                {
515
 
                                                                        newLine += "\\\n\t";
516
 
                                                                        len = 8;
517
 
                                                                }
518
 
                                                                newLine += keys[count];
519
 
                                                                newLine += ' ';
520
 
                                                                // set our value so we don't add it again.
521
 
                                                                (*ourRhs)[keys[count]] = false;
522
 
                                                        }
523
 
                                                }
524
 
                                                // only print it out if there was a value to add..
525
 
                                                if (added)
526
 
                                                {
527
 
                                                        newLine.setLength(newLine.length() - 1);
528
 
                                                        outs << newLine << endl;
529
 
                                                }
530
 
                                        }
531
 
                                        else
532
 
                                        {
533
 
                                                // we are removing our interested values from this line
534
 
 
535
 
                                                // special case - no values, remove the line..
536
 
                                                if (!ourRhs->empty())
537
 
                                                {
538
 
                                                        // check if any of these values are down to remove.
539
 
                                                        QString newLine(lhs);
540
 
                                                        if (seenLhs.find(lhs) == seenLhs.end())
541
 
                                                        {
542
 
                                                                newLine += " = ";
543
 
                                                                seenLhs[lhs] = "";
544
 
                                                        }
545
 
                                                        else
546
 
                                                        {
547
 
                                                                newLine += " += ";
548
 
                                                        }
549
 
 
550
 
                                                        int len = newLine.length();
551
 
                                                        bool added = false;
552
 
                                                        for (QStringList::Iterator posIter = bits.begin(); posIter != bits.end();posIter++)
553
 
                                                        {
554
 
                                                                QMap<QString, bool>::iterator findEntry = ourRhs->find(*posIter);
555
 
                                                                if (findEntry == ourRhs->end())
556
 
                                                                {
557
 
                                                                        // we do not want to remove it..
558
 
                                                                        added = true;
559
 
                                                                        len += (*posIter).length() + 1;
560
 
                                                                        if (len > 80)
561
 
                                                                        {
562
 
                                                                                newLine += "\\\n\t";
563
 
                                                                                len = 8;
564
 
                                                                        }
565
 
                                                                        newLine += (*posIter);
566
 
                                                                        newLine += ' ';
567
 
                                                                }
568
 
                                                                // else we have this value in our 'to remove list', so don't add it.
569
 
                                                        }
570
 
                                                        // only print it out if there was a value on it..
571
 
                                                        if (added)
572
 
                                                        {
573
 
                                                                newLine.setLength(newLine.length() - 1);
574
 
                                                                outs << newLine << endl;
575
 
                                                        }
576
 
                                                }
577
 
                                        }//if (add)
578
 
                                }//if ( rhs[ rhs.length() - 1 ] == '\\'  )
579
 
                        }//if ( found == interest.end())
580
 
                }
581
 
                else if (multiLine)
582
 
                {
583
 
                        s = s.stripWhiteSpace();
584
 
                        // we are only here if were interested in this line..
585
 
                        if (s[s.length()-1] == '\\')
586
 
                        {
587
 
                                s.setLength(s.length() - 1);
588
 
                                // still more multi line we wait for..
589
 
                        }
590
 
                        else
591
 
                        {
592
 
                                // end of the multi line..
593
 
                                multiLine = false;
594
 
                        }
595
 
                        lastRhs += QStringList::split(" ", s);
596
 
 
597
 
                        if (!multiLine)
598
 
                        {
599
 
                                // now we have to deal with this multiLine value..
600
 
                                // ourRhs will always be a value, as we only get multiLine if we're interested in it..
601
 
                                QMap<QString, bool>* ourRhs = interest.find(lastLhs);
602
 
 
603
 
                                if (add)
604
 
                                {
605
 
                                        // we are adding our interested values to this line and writing it
606
 
 
607
 
                                        // add this line to we we want to add to remove duplicates.
608
 
                                        for (uint index = 0; index < lastRhs.size(); index++)
609
 
                                        {
610
 
                                                QMap<QString, bool>::iterator findEntry = ourRhs->find(lastRhs[index]);
611
 
                                                if (findEntry == ourRhs->end())
612
 
                                                {
613
 
                                                        // we haven't seen it, so add it, so we don't add it again later..
614
 
                                                        ourRhs->insert(lastRhs[index], true);
615
 
                                                }
616
 
                                                // else we have this value in our 'to add list' , it is either already been
617
 
                                                // added, so we don't want to add it again, or it hasn't been added, in which
618
 
                                                // case we will do so soon. so we can ignore this now..
619
 
                                        }
620
 
                                        // now write the line out if it is not going to be empty.
621
 
                                        QString newLine(lastLhs);
622
 
                                        if (seenLhs.find(lastLhs) == seenLhs.end())
623
 
                                        {
624
 
                                                newLine += " = ";
625
 
                                                seenLhs[lastLhs] = "";
626
 
                                        }
627
 
                                        else
628
 
                                        {
629
 
                                                newLine += " += ";
630
 
                                        }
631
 
 
632
 
                                        int len = newLine.length();
633
 
                                        bool added = false;
634
 
                                        QValueList<QString> keys = ourRhs->keys();
635
 
                                        for (uint count = 0; count < keys.size(); count++)
636
 
                                        {
637
 
                                                // if out entry is true, add it..
638
 
                                                if ((*ourRhs)[keys[count]])
639
 
                                                {
640
 
                                                        added = true;
641
 
                                                        len += keys[count].length() + 1;
642
 
                                                        if (len > 80)
643
 
                                                        {
644
 
                                                                newLine += "\\\n\t";
645
 
                                                                len = 8;
646
 
                                                        }
647
 
                                                        newLine += keys[count];
648
 
                                                        newLine += ' ';
649
 
                                                        // set our value so we don't add it again.
650
 
                                                        (*ourRhs)[keys[count]] = false;
651
 
                                                }
652
 
                                        }
653
 
                                        // only print it out if there was a value to add..
654
 
                                        if (added)
655
 
                                        {
656
 
                                                newLine.setLength(newLine.length() - 1);
657
 
                                                outs << newLine << endl;
658
 
                                        }
659
 
                                }
660
 
                                else
661
 
                                {
662
 
                                        // we are removing our interested values from this line
663
 
                                        // special case - no values, remove the line..
664
 
                                        if (!ourRhs->empty())
665
 
                                        {
666
 
                                                // check if any of these values are down to remove.
667
 
                                                QString newLine(lastLhs);
668
 
                                                if (seenLhs.find(lastLhs) == seenLhs.end())
669
 
                                                {
670
 
                                                        newLine += " = ";
671
 
                                                        seenLhs[lastLhs] = "";
672
 
                                                }
673
 
                                                else
674
 
                                                {
675
 
                                                        newLine += " += ";
676
 
                                                }
677
 
                                                int len = newLine.length();
678
 
                                                bool added = false;
679
 
                                                for (QStringList::Iterator posIter = lastRhs.begin(); posIter != lastRhs.end();posIter++)
680
 
                                                {
681
 
                                                        QMap<QString, bool>::iterator findEntry = ourRhs->find(*posIter);
682
 
                                                        if (findEntry == ourRhs->end())
683
 
                                                        {
684
 
                                                                // we do not want to remove it..
685
 
                                                                added = true;
686
 
                                                                len += (*posIter).length() + 1;
687
 
                                                                if (len > 80)
688
 
                                                                {
689
 
                                                                        newLine += "\\\n\t";
690
 
                                                                        len = 8;
691
 
                                                                }
692
 
                                                                newLine += (*posIter);
693
 
                                                                newLine += ' ';
694
 
                                                        }
695
 
                                                        // else we have this value in our 'to remove list', so don't add it.
696
 
                                                }
697
 
                                                // only print it out if there was a value on it..
698
 
                                                if (added)
699
 
                                                {
700
 
                                                        newLine.setLength(newLine.length() - 1);
701
 
                                                        outs << newLine << endl;
702
 
                                                }
703
 
                                        }
704
 
                                }
705
 
 
706
 
                                lastLhs.setLength(0);
707
 
                                lastRhs.clear();
708
 
                        }
709
 
                }
710
 
                else
711
 
                {
712
 
                        // can write this line out..
713
 
                        // not a match, not a multi line,
714
 
                        outs << s << endl;
715
 
                }
716
 
        }
717
 
 
718
 
        if (add)
719
 
        {
720
 
                QDictIterator<QMap<QString, bool> > it(interest);
721
 
                for (; it.current(); ++it)
722
 
                {
723
 
                        QString lhs = it.currentKey();
724
 
                        QMap<QString, bool>* ourRhs = it.current();
725
 
 
726
 
                        QString newLine(lhs);
727
 
                        if (seenLhs.find(lhs) == seenLhs.end())
728
 
                        {
729
 
                                newLine += " = ";
730
 
                                seenLhs[lastLhs] = "";
731
 
                        }
732
 
                        else
733
 
                        {
734
 
                                newLine += " += ";
735
 
                        }
736
 
                        int len = newLine.length();
737
 
                        bool added = false;
738
 
                        QValueList<QString> keys = ourRhs->keys();
739
 
                        for (uint count = 0; count < keys.size(); count++)
740
 
                        {
741
 
                                if ((*ourRhs)[keys[count]])
742
 
                                {
743
 
                                        added = true;
744
 
                                        len += keys[count].length() + 1;
745
 
                                        if (len > 80)
746
 
                                        {
747
 
                                                newLine += "\\\n\t";
748
 
                                                len = 8;
749
 
                                        }
750
 
                                        newLine += keys[count];
751
 
                                        newLine += ' ';
752
 
                                        // set our value so we don't add it again.
753
 
                                        (*ourRhs)[keys[count]] = false;
754
 
                                }
755
 
                        }
756
 
                        // only print it out if there was a value to add..
757
 
                        if (added)
758
 
                        {
759
 
                                newLine.setLength(newLine.length() - 1);
760
 
                                outs << newLine << endl;
761
 
                        }
762
 
                }
763
 
        }
764
 
        interest.setAutoDelete(true);
765
 
        interest.clear();
766
 
 
767
 
        fin.close();
768
 
        fout.close();
769
 
 
770
 
        QDir().rename(fileName + "#", fileName);
771
 
}
772
 
 
773
 
/**
774
 
 * Any items in the map will be removed from the Makefile.am
775
 
 * Empty lines are removed. eg. foo_LDDADD if empty is removed.
776
 
 * @param fileName full path to Makefile.am
777
 
 * @param variables lines to remove items from.
778
 
 */
779
 
void AutoProjectTool::removeFromMakefileam(const QString &fileName, QMap <QString, QString> variables)
780
 
{
781
 
        AutoProjectTool::addRemoveMakefileam(fileName, variables,  false);
782
 
}
783
 
 
784
 
 
785
 
/**
786
 
 * Open the file and parse out the AC_OUTPUT line. following backslash continue lines..
787
 
 * @param configureinpath
788
 
 * @return list of all the values
789
 
 */
790
 
QStringList AutoProjectTool::configureinLoadMakefiles(QString configureinpath)
791
 
{
792
 
        QFile configurein(configureinpath);
793
 
 
794
 
        if (!configurein.open(IO_ReadOnly))
795
 
        {
796
 
                kdDebug(9020) << k_funcinfo << " - couldn't open file: " << configureinpath << endl;
797
 
                return QStringList();
798
 
        }
799
 
 
800
 
        QTextStream stream(&configurein);
801
 
        QStringList list;
802
 
 
803
 
        QString ac_match("^AC_OUTPUT");
804
 
 
805
 
        QRegExp ac_regex(ac_match);
806
 
        bool multiLine = false;
807
 
        QChar cont('\\');
808
 
        QRegExp close("\\)");
809
 
        QRegExp open("\\(");
810
 
        while (!stream.eof())
811
 
        {
812
 
                QString line = stream.readLine().stripWhiteSpace();
813
 
                if (multiLine)
814
 
                {
815
 
                        if (close.search(line) >= 0)
816
 
                        {
817
 
                                line = line.replace(close.search(line), 1, "");
818
 
                                list += QStringList::split(" ", line);
819
 
                                break;
820
 
                        }
821
 
                        else
822
 
                        {
823
 
                                if (line.endsWith(cont))
824
 
                                {
825
 
                                        line.setLength(line.length() - 1);
826
 
                                }
827
 
                                list += QStringList::split(" ", line);
828
 
                        }
829
 
                }
830
 
                else if (ac_regex.search(line) >= 0)
831
 
                {
832
 
                        line = line.replace(ac_regex.search(line), ac_match.length() - 1, "");
833
 
 
834
 
                        if (open.search(line) >= 0)
835
 
                        {
836
 
                                line = line.replace(open.search(line), 1, "");
837
 
                        }
838
 
 
839
 
                        if (line.endsWith(cont))
840
 
                        {
841
 
                                line.setLength(line.length() - 1);
842
 
                                multiLine = true;
843
 
                        }
844
 
                        else
845
 
                        {
846
 
                                if (close.search(line) >= 0)
847
 
                                {
848
 
                                        line = line.replace(close.search(line), 1, "");
849
 
                                }
850
 
                        }
851
 
 
852
 
                        list = QStringList::split(" ", line);
853
 
 
854
 
                        if (!multiLine)
855
 
                        {
856
 
                                break;
857
 
                        }
858
 
                }
859
 
        }
860
 
 
861
 
        configurein.close();
862
 
 
863
 
        // make a new object on the heap
864
 
        return list;
865
 
 
866
 
}
867
 
 
868
 
/**
869
 
 * Write the items to the AC_OUTPUT line. This replaces the exiting line.
870
 
 * @param configureinpath
871
 
 * @param makefiles
872
 
 */
873
 
void AutoProjectTool::configureinSaveMakefiles(QString fileName, QStringList makefiles)
874
 
{
875
 
        // input file reading
876
 
        QFile fin(fileName);
877
 
        if (!fin.open(IO_ReadOnly))
878
 
        {
879
 
                return ;
880
 
        }
881
 
        QTextStream ins(&fin);
882
 
 
883
 
        // output file writing.
884
 
        QFile fout(fileName + "#");
885
 
        if (!fout.open(IO_WriteOnly))
886
 
        {
887
 
                fin.close();
888
 
                return ;
889
 
        }
890
 
        QTextStream outs(&fout);
891
 
 
892
 
        // remove duplicates if any..
893
 
        QMap<QString, QString> toAdd;
894
 
        for (uint i = 0; i < makefiles.size();i++)
895
 
        {
896
 
                toAdd.insert(makefiles[i].stripWhiteSpace(), "");
897
 
        }
898
 
 
899
 
        QString ac_match("^AC_OUTPUT");
900
 
        QRegExp ac_regex(ac_match);
901
 
        bool multiLine = false;
902
 
        QChar cont('\\');
903
 
        QRegExp close("\\)");
904
 
        QRegExp open("\\(");
905
 
        bool done = false;
906
 
        while (!fin.atEnd())
907
 
        {
908
 
                QString line = ins.readLine();
909
 
                if (done)
910
 
                {
911
 
                        outs << line << endl;
912
 
                }
913
 
                else
914
 
                {
915
 
                        if (multiLine)
916
 
                        {
917
 
                                line = line.stripWhiteSpace();
918
 
                                if (close.search(line) >= 0)
919
 
                                {
920
 
                                        int len = 10;
921
 
                                        QString acline("AC_OUTPUT(");
922
 
                                        for (QMap<QString, QString>::iterator iter = toAdd.begin();iter != toAdd.end();iter++)
923
 
                                        {
924
 
                                                len += iter.key().length();
925
 
                                                if (len > 80)
926
 
                                                {
927
 
                                                        acline += "\\\n\t";
928
 
                                                        len = 8;
929
 
                                                }
930
 
                                                acline += iter.key();
931
 
                                                acline += ' ';
932
 
                                        }
933
 
                                        acline.setLength(acline.length() - 1);
934
 
                                        acline = acline.append(")");
935
 
                                        outs  << acline << endl;
936
 
                                        done = true;
937
 
                                }
938
 
                                else
939
 
                                {
940
 
                                        if (line.endsWith(cont))
941
 
                                        {
942
 
                                                line.setLength(line.length() - 1);
943
 
                                        }
944
 
                                }
945
 
                        }
946
 
                        else if (ac_regex.search(line) >= 0)
947
 
                        {
948
 
                                line = line.stripWhiteSpace();
949
 
                                line = line.replace(ac_regex.search(line), ac_match.length() - 1, "");
950
 
                                if (line.endsWith(cont))
951
 
                                {
952
 
                                        line.setLength(line.length() - 1);
953
 
                                        multiLine = true;
954
 
                                }
955
 
                                if (open.search(line) >= 0)
956
 
                                {
957
 
                                        line = line.replace(open.search(line), 1, "");
958
 
                                }
959
 
                                if (close.search(line) >= 0)
960
 
                                {
961
 
                                        line = line.replace(close.search(line), 1, "");
962
 
                                }
963
 
 
964
 
                                if (!multiLine)
965
 
                                {
966
 
                                        int len = 10;
967
 
                                        QString acline("AC_OUTPUT(");
968
 
                                        for (QMap<QString, QString>::iterator iter = toAdd.begin();iter != toAdd.end();iter++)
969
 
                                        {
970
 
                                                len += iter.key().length();
971
 
                                                if (len > 80)
972
 
                                                {
973
 
                                                        acline += "\\\n\t";
974
 
                                                        len = 8;
975
 
                                                }
976
 
                                                acline += iter.key();
977
 
                                                acline += ' ';
978
 
                                        }
979
 
                                        acline.setLength(acline.length() - 1);
980
 
                                        acline = acline.append(")");
981
 
                                        outs << acline << endl;
982
 
                                        done = true;
983
 
                                }
984
 
                        }
985
 
                        else
986
 
                        {
987
 
                                outs << line << endl;
988
 
                        }
989
 
                }
990
 
        }
991
 
 
992
 
        fin.close();
993
 
        fout.close();
994
 
 
995
 
        QDir().rename(fileName + "#", fileName);
996
 
 
997
 
}
998
 
 
999
 
//kate: indent-mode csands; space-indent off; tab-width 4;