Permalink
Browse files

Implement trace saving.

  • Loading branch information...
1 parent c174343 commit 9b31ffcdfff1b7e8528bea8e95870a31eaaa614f @zackr zackr committed Sep 14, 2011
Showing with 154 additions and 39 deletions.
  1. +3 −2 gui/apitrace.cpp
  2. +145 −33 gui/saverthread.cpp
  3. +6 −4 gui/saverthread.h
View
@@ -237,8 +237,9 @@ void ApiTrace::save()
QDir dir;
emit startedSaving();
dir.mkpath(fi.absolutePath());
- Q_ASSERT(!"saving not implemented");
- m_saver->saveFile(m_tempFileName, QVector<ApiTraceCall*>());
+ m_saver->saveFile(m_tempFileName,
+ m_fileName,
+ m_editedCalls);
}
void ApiTrace::slotSaved()
View
@@ -1,14 +1,16 @@
#include "saverthread.h"
#include "trace_writer.hpp"
+#include "trace_model.hpp"
+#include "trace_parser.hpp"
#include <QFile>
#include <QHash>
#include <QUrl>
#include <QDebug>
-
+#if 0
static Trace::FunctionSig *
createFunctionSig(ApiTraceCall *call, unsigned id)
{
@@ -203,54 +205,164 @@ writeValue(Trace::Writer &writer, const QVariant &var, unsigned &id)
}
}
}
+#endif
+
+class EditVisitor : public Trace::Visitor
+{
+public:
+ EditVisitor(const QVariant &variant)
+ : m_variant(variant),
+ m_editedValue(0)
+ {}
+ virtual void visit(Trace::Null *val)
+ {
+ m_editedValue = val;
+ }
+
+ virtual void visit(Trace::Bool *node)
+ {
+// Q_ASSERT(m_variant.userType() == QVariant::Bool);
+ bool var = m_variant.toBool();
+ m_editedValue = new Trace::Bool(var);
+ }
+
+ virtual void visit(Trace::SInt *node)
+ {
+// Q_ASSERT(m_variant.userType() == QVariant::Int);
+ m_editedValue = new Trace::SInt(m_variant.toInt());
+ }
+
+ virtual void visit(Trace::UInt *node)
+ {
+// Q_ASSERT(m_variant.userType() == QVariant::UInt);
+ m_editedValue = new Trace::SInt(m_variant.toUInt());
+ }
+
+ virtual void visit(Trace::Float *node)
+ {
+ m_editedValue = new Trace::Float(m_variant.toFloat());
+ }
+
+ virtual void visit(Trace::String *node)
+ {
+ QString str = m_variant.toString();
+ m_editedValue = new Trace::String(str.toLocal8Bit().constData());
+ }
+
+ virtual void visit(Trace::Enum *e)
+ {
+ m_editedValue = e;
+ }
+
+ virtual void visit(Trace::Bitmask *bitmask)
+ {
+ m_editedValue = bitmask;
+ }
+
+ virtual void visit(Trace::Struct *str)
+ {
+ m_editedValue = str;
+ }
+
+ virtual void visit(Trace::Array *array)
+ {
+ ApiArray apiArray = m_variant.value<ApiArray>();
+ QVector<QVariant> vals = apiArray.values();
+
+ Trace::Array *newArray = new Trace::Array(vals.count());
+ for (int i = 0; i < vals.count(); ++i) {
+ EditVisitor visitor(vals[i]);
+
+ array->values[i]->visit(visitor);
+ if (array->values[i] == visitor.value()) {
+ //non-editabled
+ delete newArray;
+ m_editedValue = array;
+ return;
+ }
+
+ newArray->values.push_back(visitor.value());
+ }
+ m_editedValue = newArray;
+ }
+
+ virtual void visit(Trace::Blob *blob)
+ {
+ m_editedValue = blob;
+ }
+
+ virtual void visit(Trace::Pointer *ptr)
+ {
+ m_editedValue = ptr;
+ }
+
+ Trace::Value *value() const
+ {
+ return m_editedValue;
+ }
+private:
+ QVariant m_variant;
+ Trace::Value *m_editedValue;
+};
+
+static void
+overwriteValue(Trace::Call *call, const QVariant &val, int index)
+{
+ EditVisitor visitor(val);
+ Trace::Value *origValue = call->args[index];
+ origValue->visit(visitor);
+
+ if (visitor.value() && origValue != visitor.value()) {
+ delete origValue;
+ call->args[index] = visitor.value();
+ }
+}
SaverThread::SaverThread(QObject *parent)
: QThread(parent)
{
}
-void SaverThread::saveFile(const QString &fileName,
- const QVector<ApiTraceCall*> &calls)
+void SaverThread::saveFile(const QString &writeFileName,
+ const QString &readFileName,
+ const QSet<ApiTraceCall*> &editedCalls)
{
- m_fileName = fileName;
- m_calls = calls;
+ m_writeFileName = writeFileName;
+ m_readFileName = readFileName;
+ m_editedCalls = editedCalls;
start();
}
void SaverThread::run()
{
- unsigned id = 0;
- qDebug() << "Saving : " << m_fileName;
+ qDebug() << "Saving " << m_readFileName
+ << ", to " << m_writeFileName;
+ QMap<int, ApiTraceCall*> callIndexMap;
+
+ foreach(ApiTraceCall *call, m_editedCalls) {
+ callIndexMap.insert(call->index(), call);
+ }
+
Trace::Writer writer;
- writer.open(m_fileName.toLocal8Bit());
- for (int i = 0; i < m_calls.count(); ++i) {
- ApiTraceCall *call = m_calls[i];
- Trace::FunctionSig *funcSig = createFunctionSig(call, ++id);
- unsigned callNo = writer.beginEnter(funcSig);
- {
- //args
- QVector<QVariant> vars = call->arguments();
- int index = 0;
- foreach(QVariant var, vars) {
- writer.beginArg(index++);
- writeValue(writer, var, ++id);
- writer.endArg();
- }
- }
- writer.endEnter();
- writer.beginLeave(callNo);
- {
- QVariant ret = call->returnValue();
- if (!ret.isNull()) {
- writer.beginReturn();
- writeValue(writer, ret, ++id);
- writer.endReturn();
+ writer.open(m_writeFileName.toLocal8Bit());
+
+ Trace::Parser parser;
+ parser.open(m_readFileName.toLocal8Bit());
+
+ Trace::Call *call;
+ while ((call = parser.parse_call())) {
+ if (callIndexMap.contains(call->no)) {
+ QVector<QVariant> values = callIndexMap[call->no]->editedValues();
+ for (int i = 0; i < values.count(); ++i) {
+ const QVariant &val = values[i];
+ overwriteValue(call, val, i);
}
- }
- writer.endLeave();
- deleteFunctionSig(funcSig);
+ } else {
+ writer.writeCall(call);
+ }
}
+
writer.close();
emit traceSaved();
View
@@ -16,8 +16,9 @@ class SaverThread : public QThread
SaverThread(QObject *parent=0);
public slots:
- void saveFile(const QString &fileName,
- const QVector<ApiTraceCall*> &calls);
+ void saveFile(const QString &saveFileName,
+ const QString &readFileName,
+ const QSet<ApiTraceCall*> &editedCalls);
signals:
void traceSaved();
@@ -26,8 +27,9 @@ public slots:
virtual void run();
private:
- QString m_fileName;
- QVector<ApiTraceCall*> m_calls;
+ QString m_readFileName;
+ QString m_writeFileName;
+ QSet<ApiTraceCall*> m_editedCalls;
};

0 comments on commit 9b31ffc

Please sign in to comment.