Diffstat (limited to 'development/translation/shared/proparser.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | development/translation/shared/proparser.cpp | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/development/translation/shared/proparser.cpp b/development/translation/shared/proparser.cpp new file mode 100644 index 0000000..21d2f86 --- a/dev/null +++ b/development/translation/shared/proparser.cpp | |||
@@ -0,0 +1,87 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qt Linguist. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | |||
21 | #include "proparser.h" | ||
22 | |||
23 | #include <qregexp.h> | ||
24 | #include <qstringlist.h> | ||
25 | |||
26 | QMap<QString, QString> proFileTagMap( const QString& text ) | ||
27 | { | ||
28 | QString t = text; | ||
29 | |||
30 | /* | ||
31 | Strip comments, merge lines ending with backslash, add | ||
32 | spaces around '=' and '+=', replace '\n' with ';', and | ||
33 | simplify white spaces. | ||
34 | */ | ||
35 | t.replace( QRegExp(QString("#[^\n]$")), QString(" ") ); | ||
36 | t.replace( QRegExp(QString("\\\\\\s*\n")), QString(" ") ); | ||
37 | t.replace( "=", QString(" = ") ); | ||
38 | t.replace( "+ =", QString(" += ") ); | ||
39 | t.replace( "\n", QString(";") ); | ||
40 | t = t.simplifyWhiteSpace(); | ||
41 | |||
42 | QMap<QString, QString> tagMap; | ||
43 | |||
44 | QStringList lines = QStringList::split( QChar(';'), t ); | ||
45 | QStringList::Iterator line; | ||
46 | for ( line = lines.begin(); line != lines.end(); ++line ) { | ||
47 | QStringList toks = QStringList::split( QChar(' '), *line ); | ||
48 | |||
49 | if ( toks.count() >= 3 && | ||
50 | (toks[1] == QString("=") || toks[1] == QString("+=")) ) { | ||
51 | QString tag = toks.first(); | ||
52 | int k = tag.findRev( QChar(':') ); // as in 'unix:' | ||
53 | if ( k != -1 ) | ||
54 | tag = tag.mid( k + 1 ); | ||
55 | toks.remove( toks.begin() ); | ||
56 | |||
57 | QString action = toks.first(); | ||
58 | toks.remove( toks.begin() ); | ||
59 | |||
60 | if ( tagMap.contains(tag) ) { | ||
61 | if ( action == QString("=") ) | ||
62 | tagMap.replace( tag, toks.join(QChar(' ')) ); | ||
63 | else | ||
64 | tagMap[tag] += QChar( ' ' ) + toks.join( QChar(' ') ); | ||
65 | } else { | ||
66 | tagMap[tag] = toks.join( QChar(' ') ); | ||
67 | } | ||
68 | } | ||
69 | } | ||
70 | |||
71 | QRegExp var( "\\$\\$[a-zA-Z0-9_]+" ); | ||
72 | QMap<QString, QString>::Iterator it; | ||
73 | for ( it = tagMap.begin(); it != tagMap.end(); ++it ) { | ||
74 | int i = 0; | ||
75 | |||
76 | while ( (i = var.search(it.data(), i)) != -1 ) { | ||
77 | int len = var.matchedLength(); | ||
78 | QString invocation = (*it).mid( i + 2, len - 2 ); | ||
79 | QString after; | ||
80 | if ( tagMap.contains(invocation) ) | ||
81 | after = tagMap[invocation]; | ||
82 | (*it).replace( i, len, after ); | ||
83 | i += after.length(); | ||
84 | } | ||
85 | } | ||
86 | return tagMap; | ||
87 | } | ||