author | zecke <zecke> | 2004-01-09 19:13:35 (UTC) |
---|---|---|
committer | zecke <zecke> | 2004-01-09 19:13:35 (UTC) |
commit | 56bb9961db1158250bbfe971f4556b56c0bd6581 (patch) (unidiff) | |
tree | a4f40da98f695b5aadd845b68aaab41ac3d81826 /development/translation/shared/proparser.cpp | |
parent | 6875988077013544246ae0df7cc2e5d538a7f152 (diff) | |
download | opie-56bb9961db1158250bbfe971f4556b56c0bd6581.zip opie-56bb9961db1158250bbfe971f4556b56c0bd6581.tar.gz opie-56bb9961db1158250bbfe971f4556b56c0bd6581.tar.bz2 |
Update lupdate and shared to Qt3.3 qt-copy sources
Some special replacement for $$(OPIEDIR) to make ar_* happy
Diffstat (limited to 'development/translation/shared/proparser.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | development/translation/shared/proparser.cpp | 103 |
1 files changed, 95 insertions, 8 deletions
diff --git a/development/translation/shared/proparser.cpp b/development/translation/shared/proparser.cpp index 21d2f86..f616c5a 100644 --- a/development/translation/shared/proparser.cpp +++ b/development/translation/shared/proparser.cpp | |||
@@ -1,87 +1,174 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of Qt Linguist. | 4 | ** This file is part of Qt Linguist. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 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 | 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 | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition | ||
12 | ** licenses may use this file in accordance with the Qt Commercial License | ||
13 | ** Agreement provided with the Software. | ||
14 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 15 | ** 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. | 16 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 17 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 18 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
19 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for | ||
20 | ** information about Qt Commercial License Agreements. | ||
15 | ** | 21 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 22 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 23 | ** not clear to you. |
18 | ** | 24 | ** |
19 | **********************************************************************/ | 25 | **********************************************************************/ |
20 | 26 | ||
21 | #include "proparser.h" | 27 | #include "proparser.h" |
22 | 28 | ||
29 | #include <qfile.h> | ||
23 | #include <qregexp.h> | 30 | #include <qregexp.h> |
24 | #include <qstringlist.h> | 31 | #include <qstringlist.h> |
32 | #include <qtextstream.h> | ||
33 | |||
34 | #ifdef Q_OS_UNIX | ||
35 | #include <unistd.h> | ||
36 | #endif | ||
37 | |||
38 | #ifdef Q_OS_WIN32 | ||
39 | #define QT_POPEN _popen | ||
40 | #else | ||
41 | #define QT_POPEN popen | ||
42 | #endif | ||
25 | 43 | ||
26 | QMap<QString, QString> proFileTagMap( const QString& text ) | 44 | QString loadFile( const QString &fileName ) |
45 | { | ||
46 | QFile file( fileName ); | ||
47 | if ( !file.open(IO_ReadOnly) ) { | ||
48 | fprintf( stderr, "error: Cannot load '%s': %s\n", | ||
49 | file.name().latin1(), | ||
50 | file.errorString().latin1() ); | ||
51 | return QString(); | ||
52 | } | ||
53 | |||
54 | QTextStream in( &file ); | ||
55 | return in.read(); | ||
56 | } | ||
57 | |||
58 | QMap<QString, QString> proFileTagMap( const QString& text, const QString& opieDir ) | ||
27 | { | 59 | { |
28 | QString t = text; | 60 | QString t = text; |
29 | 61 | ||
30 | /* | 62 | /* |
63 | Process include() commands. | ||
64 | */ | ||
65 | QRegExp callToInclude("include\\s*\\(\\s*([^()\\s]+)\\s*\\)"); | ||
66 | int i = 0; | ||
67 | while ( (i = callToInclude.search(t, i)) != -1 ) { | ||
68 | QString after = loadFile( callToInclude.cap(1) ); | ||
69 | t.replace( i, callToInclude.matchedLength(), after ); | ||
70 | i += after.length(); | ||
71 | } | ||
72 | |||
73 | /* | ||
31 | Strip comments, merge lines ending with backslash, add | 74 | Strip comments, merge lines ending with backslash, add |
32 | spaces around '=' and '+=', replace '\n' with ';', and | 75 | spaces around '=' and '+=', replace '\n' with ';', and |
33 | simplify white spaces. | 76 | simplify white spaces. |
34 | */ | 77 | */ |
35 | t.replace( QRegExp(QString("#[^\n]$")), QString(" ") ); | 78 | t.replace( QRegExp(QString("#[^\n]*\n")), QString(" ") ); |
36 | t.replace( QRegExp(QString("\\\\\\s*\n")), QString(" ") ); | 79 | t.replace( QRegExp(QString("\\\\[^\n\\S]*\n")), QString(" ") ); |
37 | t.replace( "=", QString(" = ") ); | 80 | t.replace( "=", QString(" = ") ); |
38 | t.replace( "+ =", QString(" += ") ); | 81 | t.replace( "+ =", QString(" += ") ); |
39 | t.replace( "\n", QString(";") ); | 82 | t.replace( "\n", QString(";") ); |
40 | t = t.simplifyWhiteSpace(); | 83 | t = t.simplifyWhiteSpace(); |
41 | 84 | ||
85 | /* | ||
86 | Populate tagMap with 'key = value' entries. | ||
87 | */ | ||
42 | QMap<QString, QString> tagMap; | 88 | QMap<QString, QString> tagMap; |
43 | |||
44 | QStringList lines = QStringList::split( QChar(';'), t ); | 89 | QStringList lines = QStringList::split( QChar(';'), t ); |
45 | QStringList::Iterator line; | 90 | QStringList::Iterator line; |
46 | for ( line = lines.begin(); line != lines.end(); ++line ) { | 91 | for ( line = lines.begin(); line != lines.end(); ++line ) { |
47 | QStringList toks = QStringList::split( QChar(' '), *line ); | 92 | QStringList toks = QStringList::split( QChar(' '), *line ); |
48 | 93 | ||
49 | if ( toks.count() >= 3 && | 94 | if ( toks.count() >= 3 && |
50 | (toks[1] == QString("=") || toks[1] == QString("+=")) ) { | 95 | (toks[1] == QString("=") || toks[1] == QString("+=")) ) { |
51 | QString tag = toks.first(); | 96 | QString tag = toks.first(); |
52 | int k = tag.findRev( QChar(':') ); // as in 'unix:' | 97 | int k = tag.findRev( QChar(':') ); // as in 'unix:' |
53 | if ( k != -1 ) | 98 | if ( k != -1 ) |
54 | tag = tag.mid( k + 1 ); | 99 | tag = tag.mid( k + 1 ); |
55 | toks.remove( toks.begin() ); | 100 | toks.remove( toks.begin() ); |
56 | 101 | ||
57 | QString action = toks.first(); | 102 | QString action = toks.first(); |
58 | toks.remove( toks.begin() ); | 103 | toks.remove( toks.begin() ); |
59 | 104 | ||
60 | if ( tagMap.contains(tag) ) { | 105 | if ( tagMap.contains(tag) ) { |
61 | if ( action == QString("=") ) | 106 | if ( action == QString("=") ) |
62 | tagMap.replace( tag, toks.join(QChar(' ')) ); | 107 | tagMap.replace( tag, toks.join(QChar(' ')) ); |
63 | else | 108 | else |
64 | tagMap[tag] += QChar( ' ' ) + toks.join( QChar(' ') ); | 109 | tagMap[tag] += QChar( ' ' ) + toks.join( QChar(' ') ); |
65 | } else { | 110 | } else { |
66 | tagMap[tag] = toks.join( QChar(' ') ); | 111 | tagMap[tag] = toks.join( QChar(' ') ); |
67 | } | 112 | } |
68 | } | 113 | } |
69 | } | 114 | } |
70 | 115 | ||
71 | QRegExp var( "\\$\\$[a-zA-Z0-9_]+" ); | 116 | /* |
117 | Expand $$variables within the 'value' part of a 'key = value' | ||
118 | pair. | ||
119 | */ | ||
120 | QRegExp var( "\\$\\$[({]?([a-zA-Z0-9_]+)[)}]?" ); | ||
72 | QMap<QString, QString>::Iterator it; | 121 | QMap<QString, QString>::Iterator it; |
73 | for ( it = tagMap.begin(); it != tagMap.end(); ++it ) { | 122 | for ( it = tagMap.begin(); it != tagMap.end(); ++it ) { |
74 | int i = 0; | 123 | int i = 0; |
75 | 124 | while ( (i = var.search((*it), i)) != -1 ) { | |
76 | while ( (i = var.search(it.data(), i)) != -1 ) { | ||
77 | int len = var.matchedLength(); | 125 | int len = var.matchedLength(); |
78 | QString invocation = (*it).mid( i + 2, len - 2 ); | 126 | QString invocation = var.cap(1); |
79 | QString after; | 127 | QString after; |
128 | |||
129 | if ( invocation == "system" ) { | ||
130 | // skip system(); it will be handled in the next pass | ||
131 | ++i; | ||
132 | } else if ( invocation == "OPIEDIR") { | ||
133 | (*it).replace( i, len, opieDir ); | ||
134 | }else { | ||
80 | if ( tagMap.contains(invocation) ) | 135 | if ( tagMap.contains(invocation) ) |
81 | after = tagMap[invocation]; | 136 | after = tagMap[invocation]; |
82 | (*it).replace( i, len, after ); | 137 | (*it).replace( i, len, after ); |
138 | } | ||
139 | } | ||
140 | } | ||
141 | |||
142 | /* | ||
143 | Execute system() calls. | ||
144 | */ | ||
145 | QRegExp callToSystem( "\\$\\$system\\s*\\(([^()]*)\\)" ); | ||
146 | for ( it = tagMap.begin(); it != tagMap.end(); ++it ) { | ||
147 | int i = 0; | ||
148 | while ( (i = callToSystem.search((*it), i)) != -1 ) { | ||
149 | /* | ||
150 | This code is stolen from qmake's project.cpp file. | ||
151 | Ideally we would use the same parser, so we wouldn't | ||
152 | have this code duplication. | ||
153 | */ | ||
154 | QString after; | ||
155 | char buff[256]; | ||
156 | FILE *proc = QT_POPEN( callToSystem.cap(1).latin1(), "r" ); | ||
157 | while ( proc && !feof(proc) ) { | ||
158 | int read_in = fread( buff, 1, 255, proc ); | ||
159 | if ( !read_in ) | ||
160 | break; | ||
161 | for ( int i = 0; i < read_in; i++ ) { | ||
162 | if ( buff[i] == '\n' || buff[i] == '\t' ) | ||
163 | buff[i] = ' '; | ||
164 | } | ||
165 | buff[read_in] = '\0'; | ||
166 | after += buff; | ||
167 | } | ||
168 | (*it).replace( i, callToSystem.matchedLength(), after ); | ||
83 | i += after.length(); | 169 | i += after.length(); |
84 | } | 170 | } |
85 | } | 171 | } |
172 | |||
86 | return tagMap; | 173 | return tagMap; |
87 | } | 174 | } |