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,6 +26,8 @@
26 26
 #include <QDomElement>
27 27
 #include <QXmlStreamWriter>
28 28
 #include <QtDebug>
29
+#include <QHash>
30
+#include <QHashIterator>
29 31
 
30 32
 struct Context
31 33
 {
@@ -36,10 +38,36 @@ struct Context
36 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 50
 Context* buildContext(QString filename, QString basedir);
40 51
 int generate(QString configfile, QString transfile);
41 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 71
 // Create a Context* with messages from a file.
44 72
 Context* buildContext(QString filename, QString basedir)
45 73
 {
@@ -58,25 +86,22 @@ Context* buildContext(QString filename, QString basedir)
58 86
         for(int i = 0; i < currentSet.length(); i++)
59 87
         {
60 88
             currentNode = currentSet.at(i);
61
-            context->locations.append(QString("%1").arg(currentNode.lineNumber()));
62 89
             currentElement = currentNode.toElement();
63
-            context->messages.append(currentElement.attribute("name"));
90
+            addMessage(currentElement.attribute("name"), filename, currentNode.lineNumber());
64 91
         }
65 92
         currentSet = document.elementsByTagName("reporttitle");
66 93
         for(int i = 0; i < currentSet.length(); i++)
67 94
         {
68 95
             currentNode = currentSet.at(i);
69
-            context->locations.append(QString("%1").arg(currentNode.lineNumber()));
70 96
             currentElement = currentNode.toElement();
71
-            context->messages.append(currentElement.text());
97
+            addMessage(currentElement.text(), filename, currentNode.lineNumber());
72 98
         }
73 99
         currentSet = document.elementsByTagName("menu");
74 100
         for(int i = 0; i < currentSet.length(); i++)
75 101
         {
76 102
             currentNode = currentSet.at(i);
77
-            context->locations.append(QString("%1").arg(currentNode.lineNumber()));
78 103
             currentElement = currentNode.toElement();
79
-            context->messages.append(currentElement.attribute("name"));
104
+            addMessage(currentElement.attribute("name"), filename, currentNode.lineNumber());
80 105
             if(currentElement.attribute("type") == "reports")
81 106
             {
82 107
                 context->reportDirectory = currentElement.attribute("src");
@@ -86,25 +111,22 @@ Context* buildContext(QString filename, QString basedir)
86 111
         for(int i = 0; i < currentSet.length(); i++)
87 112
         {
88 113
             currentNode = currentSet.at(i);
89
-            context->locations.append(QString("%1").arg(currentNode.lineNumber()));
90 114
             currentElement = currentNode.toElement();
91
-            context->messages.append(currentElement.text());
115
+            addMessage(currentElement.text(), filename, currentNode.lineNumber());
92 116
         }
93 117
         currentSet = document.elementsByTagName("label");
94 118
         for(int i = 0; i < currentSet.length(); i++)
95 119
         {
96 120
             currentNode = currentSet.at(i);
97
-            context->locations.append(QString("%1").arg(currentNode.lineNumber()));
98 121
             currentElement = currentNode.toElement();
99
-            context->messages.append(currentElement.text());
122
+            addMessage(currentElement.text(), filename, currentNode.lineNumber());
100 123
         }
101 124
         currentSet = document.elementsByTagName("decoration");
102 125
         for(int i = 0; i < currentSet.length(); i++)
103 126
         {
104 127
             currentNode = currentSet.at(i);
105
-            context->locations.append(QString("%1").arg(currentNode.lineNumber()));
106 128
             currentElement = currentNode.toElement();
107
-            context->messages.append(currentElement.attribute("name"));
129
+            addMessage(currentElement.attribute("name"), filename, currentNode.lineNumber());
108 130
         }
109 131
         currentSet = document.elementsByTagName("spinbox");
110 132
         for(int i = 0; i < currentSet.length(); i++)
@@ -113,13 +135,11 @@ Context* buildContext(QString filename, QString basedir)
113 135
             currentElement = currentNode.toElement();
114 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 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 145
         currentSet = document.elementsByTagName("measurementtable");
@@ -137,8 +157,7 @@ Context* buildContext(QString filename, QString basedir)
137 157
                         QDomElement childElement = childNode.toElement();
138 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,8 +178,7 @@ Context* buildContext(QString filename, QString basedir)
159 178
                         QDomElement childElement = childNode.toElement();
160 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,13 +196,12 @@ Context* buildContext(QString filename, QString basedir)
178 196
                 if(scriptLines.at(j).contains("TTR"))
179 197
                 {
180 198
                     QString currentLine = scriptLines.at(j);
181
-                    context->locations.append(QString("%1").arg(currentNode.lineNumber() + j + 1));
182 199
                     currentLine = currentLine.remove(0, currentLine.indexOf("TTR"));
183 200
                     currentLine = currentLine.remove(0, currentLine.indexOf("\"")+1);
184 201
                     currentLine = currentLine.remove(0, currentLine.indexOf("\"")+1);
185 202
                     currentLine = currentLine.remove(0, currentLine.indexOf("\"")+1);
186 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,30 +274,32 @@ int generate(QString configfile, QString transfile)
257 274
     xmlout.writeDTD("<!DOCTYPE TS>");
258 275
     xmlout.writeStartElement("TS");
259 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 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 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 294
             xmlout.writeEndElement();
280 295
         }
296
+        xmlout.writeStartElement("translation");
297
+        xmlout.writeCharacters("T" + i.key());
298
+        xmlout.writeEndElement();
281 299
         xmlout.writeEndElement();
282 300
     }
283 301
     xmlout.writeEndElement();
302
+    xmlout.writeEndElement();
284 303
     xmlout.writeEndDocument();
285 304
     ts.close();
286 305
     return 0;

Loading…
Cancel
Save