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 | |||
@@ -10,2 +10,6 @@ | |||
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 |
@@ -14,2 +18,4 @@ | |||
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 | ** |
@@ -22,6 +28,32 @@ | |||
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 | { |
@@ -30,2 +62,13 @@ QMap<QString, QString> proFileTagMap( const QString& text ) | |||
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 |
@@ -34,4 +77,4 @@ QMap<QString, QString> proFileTagMap( const QString& text ) | |||
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(" = ") ); |
@@ -41,4 +84,6 @@ QMap<QString, QString> proFileTagMap( const QString& text ) | |||
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 ); |
@@ -70,3 +115,7 @@ QMap<QString, QString> proFileTagMap( const QString& text ) | |||
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; |
@@ -74,7 +123,13 @@ QMap<QString, QString> proFileTagMap( const QString& text ) | |||
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) ) |
@@ -82,2 +137,33 @@ QMap<QString, QString> proFileTagMap( const QString& text ) | |||
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(); |
@@ -85,2 +171,3 @@ QMap<QString, QString> proFileTagMap( const QString& text ) | |||
85 | } | 171 | } |
172 | |||
86 | return tagMap; | 173 | return tagMap; |