Neal Wilson 8 years ago
parent
commit
b08137b6be
1 changed files with 56 additions and 37 deletions
  1. 56
    37
      src/main.cpp

+ 56
- 37
src/main.cpp View File

26
 #include <QDomElement>
26
 #include <QDomElement>
27
 #include <QXmlStreamWriter>
27
 #include <QXmlStreamWriter>
28
 #include <QtDebug>
28
 #include <QtDebug>
29
+#include <QHash>
30
+#include <QHashIterator>
29
 
31
 
30
 struct Context
32
 struct Context
31
 {
33
 {
36
     QString reportDirectory;
38
     QString reportDirectory;
37
 };
39
 };
38
 
40
 
41
+struct Locations
42
+{
43
+    QStringList filenames;
44
+    QStringList linenumbers;
45
+};
46
+
47
+QHash<QString, Locations*> globalContext;
48
+
49
+void addMessage(QString source, QString file, int line);
39
 Context* buildContext(QString filename, QString basedir);
50
 Context* buildContext(QString filename, QString basedir);
40
 int generate(QString configfile, QString transfile);
51
 int generate(QString configfile, QString transfile);
41
 int main(int argc, char **argv);
52
 int main(int argc, char **argv);
42
 
53
 
54
+// Add a message to the context.
55
+void addMessage(QString source, QString file, int line)
56
+{
57
+    Locations *location;
58
+    if(globalContext.contains(source))
59
+    {
60
+        location = globalContext.value(source);
61
+    }
62
+    else
63
+    {
64
+        location = new Locations;
65
+    }
66
+    location->filenames.append(file);
67
+    location->linenumbers.append(QString("%1").arg(line));
68
+    globalContext.insert(source, location);
69
+}
70
+
43
 // Create a Context* with messages from a file.
71
 // Create a Context* with messages from a file.
44
 Context* buildContext(QString filename, QString basedir)
72
 Context* buildContext(QString filename, QString basedir)
45
 {
73
 {
58
         for(int i = 0; i < currentSet.length(); i++)
86
         for(int i = 0; i < currentSet.length(); i++)
59
         {
87
         {
60
             currentNode = currentSet.at(i);
88
             currentNode = currentSet.at(i);
61
-            context->locations.append(QString("%1").arg(currentNode.lineNumber()));
62
             currentElement = currentNode.toElement();
89
             currentElement = currentNode.toElement();
63
-            context->messages.append(currentElement.attribute("name"));
90
+            addMessage(currentElement.attribute("name"), filename, currentNode.lineNumber());
64
         }
91
         }
65
         currentSet = document.elementsByTagName("reporttitle");
92
         currentSet = document.elementsByTagName("reporttitle");
66
         for(int i = 0; i < currentSet.length(); i++)
93
         for(int i = 0; i < currentSet.length(); i++)
67
         {
94
         {
68
             currentNode = currentSet.at(i);
95
             currentNode = currentSet.at(i);
69
-            context->locations.append(QString("%1").arg(currentNode.lineNumber()));
70
             currentElement = currentNode.toElement();
96
             currentElement = currentNode.toElement();
71
-            context->messages.append(currentElement.text());
97
+            addMessage(currentElement.text(), filename, currentNode.lineNumber());
72
         }
98
         }
73
         currentSet = document.elementsByTagName("menu");
99
         currentSet = document.elementsByTagName("menu");
74
         for(int i = 0; i < currentSet.length(); i++)
100
         for(int i = 0; i < currentSet.length(); i++)
75
         {
101
         {
76
             currentNode = currentSet.at(i);
102
             currentNode = currentSet.at(i);
77
-            context->locations.append(QString("%1").arg(currentNode.lineNumber()));
78
             currentElement = currentNode.toElement();
103
             currentElement = currentNode.toElement();
79
-            context->messages.append(currentElement.attribute("name"));
104
+            addMessage(currentElement.attribute("name"), filename, currentNode.lineNumber());
80
             if(currentElement.attribute("type") == "reports")
105
             if(currentElement.attribute("type") == "reports")
81
             {
106
             {
82
                 context->reportDirectory = currentElement.attribute("src");
107
                 context->reportDirectory = currentElement.attribute("src");
86
         for(int i = 0; i < currentSet.length(); i++)
111
         for(int i = 0; i < currentSet.length(); i++)
87
         {
112
         {
88
             currentNode = currentSet.at(i);
113
             currentNode = currentSet.at(i);
89
-            context->locations.append(QString("%1").arg(currentNode.lineNumber()));
90
             currentElement = currentNode.toElement();
114
             currentElement = currentNode.toElement();
91
-            context->messages.append(currentElement.text());
115
+            addMessage(currentElement.text(), filename, currentNode.lineNumber());
92
         }
116
         }
93
         currentSet = document.elementsByTagName("label");
117
         currentSet = document.elementsByTagName("label");
94
         for(int i = 0; i < currentSet.length(); i++)
118
         for(int i = 0; i < currentSet.length(); i++)
95
         {
119
         {
96
             currentNode = currentSet.at(i);
120
             currentNode = currentSet.at(i);
97
-            context->locations.append(QString("%1").arg(currentNode.lineNumber()));
98
             currentElement = currentNode.toElement();
121
             currentElement = currentNode.toElement();
99
-            context->messages.append(currentElement.text());
122
+            addMessage(currentElement.text(), filename, currentNode.lineNumber());
100
         }
123
         }
101
         currentSet = document.elementsByTagName("decoration");
124
         currentSet = document.elementsByTagName("decoration");
102
         for(int i = 0; i < currentSet.length(); i++)
125
         for(int i = 0; i < currentSet.length(); i++)
103
         {
126
         {
104
             currentNode = currentSet.at(i);
127
             currentNode = currentSet.at(i);
105
-            context->locations.append(QString("%1").arg(currentNode.lineNumber()));
106
             currentElement = currentNode.toElement();
128
             currentElement = currentNode.toElement();
107
-            context->messages.append(currentElement.attribute("name"));
129
+            addMessage(currentElement.attribute("name"), filename, currentNode.lineNumber());
108
         }
130
         }
109
         currentSet = document.elementsByTagName("spinbox");
131
         currentSet = document.elementsByTagName("spinbox");
110
         for(int i = 0; i < currentSet.length(); i++)
132
         for(int i = 0; i < currentSet.length(); i++)
113
             currentElement = currentNode.toElement();
135
             currentElement = currentNode.toElement();
114
             if(currentElement.hasAttribute("pretext"))
136
             if(currentElement.hasAttribute("pretext"))
115
             {
137
             {
116
-                context->locations.append(QString("%1").arg(currentNode.lineNumber()));
117
-                context->messages.append(currentElement.attribute("pretext"));
138
+                addMessage(currentElement.attribute("pretext"), filename, currentNode.lineNumber());
118
             }
139
             }
119
             if(currentElement.hasAttribute("posttext"))
140
             if(currentElement.hasAttribute("posttext"))
120
             {
141
             {
121
-                context->locations.append(QString("%1").arg(currentNode.lineNumber()));
122
-                context->messages.append(currentElement.attribute("posttext"));
142
+                addMessage(currentElement.attribute("posttext"), filename, currentNode.lineNumber());
123
             }
143
             }
124
         }
144
         }
125
         currentSet = document.elementsByTagName("measurementtable");
145
         currentSet = document.elementsByTagName("measurementtable");
137
                         QDomElement childElement = childNode.toElement();
157
                         QDomElement childElement = childNode.toElement();
138
                         if(childElement.tagName() == "column")
158
                         if(childElement.tagName() == "column")
139
                         {
159
                         {
140
-                            context->locations.append(QString("%1").arg(childNode.lineNumber()));
141
-                            context->messages.append(childElement.text());
160
+                            addMessage(childElement.text(), filename, childNode.lineNumber());
142
                         }
161
                         }
143
                     }
162
                     }
144
                 }
163
                 }
159
                         QDomElement childElement = childNode.toElement();
178
                         QDomElement childElement = childNode.toElement();
160
                         if(childElement.tagName() == "column")
179
                         if(childElement.tagName() == "column")
161
                         {
180
                         {
162
-                            context->locations.append(QString("%1").arg(childNode.lineNumber()));
163
-                            context->messages.append(childElement.attribute("name"));
181
+                            addMessage(childElement.attribute("name"), filename, childNode.lineNumber());
164
                         }
182
                         }
165
                     }
183
                     }
166
                 }
184
                 }
178
                 if(scriptLines.at(j).contains("TTR"))
196
                 if(scriptLines.at(j).contains("TTR"))
179
                 {
197
                 {
180
                     QString currentLine = scriptLines.at(j);
198
                     QString currentLine = scriptLines.at(j);
181
-                    context->locations.append(QString("%1").arg(currentNode.lineNumber() + j + 1));
182
                     currentLine = currentLine.remove(0, currentLine.indexOf("TTR"));
199
                     currentLine = currentLine.remove(0, currentLine.indexOf("TTR"));
183
                     currentLine = currentLine.remove(0, currentLine.indexOf("\"")+1);
200
                     currentLine = currentLine.remove(0, currentLine.indexOf("\"")+1);
184
                     currentLine = currentLine.remove(0, currentLine.indexOf("\"")+1);
201
                     currentLine = currentLine.remove(0, currentLine.indexOf("\"")+1);
185
                     currentLine = currentLine.remove(0, currentLine.indexOf("\"")+1);
202
                     currentLine = currentLine.remove(0, currentLine.indexOf("\"")+1);
186
                     currentLine = currentLine.remove(currentLine.indexOf("\""), currentLine.length());
203
                     currentLine = currentLine.remove(currentLine.indexOf("\""), currentLine.length());
187
-                    context->messages.append(currentLine);
204
+                    addMessage(currentLine, filename, currentNode.lineNumber() + j + 1);
188
                 }
205
                 }
189
             }
206
             }
190
         }
207
         }
257
     xmlout.writeDTD("<!DOCTYPE TS>");
274
     xmlout.writeDTD("<!DOCTYPE TS>");
258
     xmlout.writeStartElement("TS");
275
     xmlout.writeStartElement("TS");
259
     xmlout.writeAttribute("version", "2.0");
276
     xmlout.writeAttribute("version", "2.0");
260
-    foreach(Context *c, contexts)
277
+    QHashIterator<QString, Locations*> i(globalContext);
278
+    xmlout.writeStartElement("context");
279
+    xmlout.writeStartElement("name");
280
+    xmlout.writeCharacters("configuration");
281
+    xmlout.writeEndElement();
282
+    while(i.hasNext())
261
     {
283
     {
262
-        xmlout.writeStartElement("context");
263
-        xmlout.writeStartElement("name");
264
-        xmlout.writeCharacters(c->name);
284
+        i.next();
285
+        xmlout.writeStartElement("message");
286
+        xmlout.writeStartElement("source");
287
+        xmlout.writeCharacters(i.key());
265
         xmlout.writeEndElement();
288
         xmlout.writeEndElement();
266
-        for(int i = 0; i < c->messages.length(); i++)
289
+        for(int j = 0; j < i.value()->filenames.length(); j++)
267
         {
290
         {
268
-            xmlout.writeStartElement("message");
269
             xmlout.writeStartElement("location");
291
             xmlout.writeStartElement("location");
270
-            xmlout.writeAttribute("filename", c->filename);
271
-            xmlout.writeAttribute("line", c->locations.at(i));
272
-            xmlout.writeEndElement();
273
-            xmlout.writeStartElement("source");
274
-            xmlout.writeCharacters(c->messages.at(i));
275
-            xmlout.writeEndElement();
276
-            xmlout.writeStartElement("translation");
277
-            xmlout.writeCharacters("T" + c->messages.at(i));
278
-            xmlout.writeEndElement();
292
+            xmlout.writeAttribute("filename", i.value()->filenames.at(j));
293
+            xmlout.writeAttribute("line", i.value()->linenumbers.at(j));
279
             xmlout.writeEndElement();
294
             xmlout.writeEndElement();
280
         }
295
         }
296
+        xmlout.writeStartElement("translation");
297
+        xmlout.writeCharacters("T" + i.key());
298
+        xmlout.writeEndElement();
281
         xmlout.writeEndElement();
299
         xmlout.writeEndElement();
282
     }
300
     }
283
     xmlout.writeEndElement();
301
     xmlout.writeEndElement();
302
+    xmlout.writeEndElement();
284
     xmlout.writeEndDocument();
303
     xmlout.writeEndDocument();
285
     ts.close();
304
     ts.close();
286
     return 0;
305
     return 0;

Loading…
Cancel
Save