| 1 | #include "DebugSession.h" |
| 2 | #include <QXmlStreamReader> |
| 3 | #include <QXmlStreamWriter> |
| 4 | #include <QFile> |
| 5 | #include <QMessageBox> |
| 6 | |
| 7 | DebugSession::DebugSession() |
| 8 | : modified(false) |
| 9 | { |
| 10 | } |
| 11 | |
| 12 | bool DebugSession::existsAsFile() const |
| 13 | { |
| 14 | return !fileName.isEmpty(); |
| 15 | } |
| 16 | |
| 17 | const QString& DebugSession::filename() const |
| 18 | { |
| 19 | return fileName; |
| 20 | } |
| 21 | |
| 22 | bool DebugSession::isModified() const |
| 23 | { |
| 24 | if (fileName.isEmpty() && symTable.size() == 0) { |
| 25 | return false; |
| 26 | } else { |
| 27 | return modified; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | Breakpoints& DebugSession::breakpoints() |
| 32 | { |
| 33 | return breaks; |
| 34 | } |
| 35 | |
| 36 | SymbolTable& DebugSession::symbolTable() |
| 37 | { |
| 38 | return symTable; |
| 39 | } |
| 40 | |
| 41 | void DebugSession::clear() |
| 42 | { |
| 43 | // clear everything |
| 44 | symTable.clear(); |
| 45 | breaks.clear(); |
| 46 | fileName.clear(); |
| 47 | modified = false; |
| 48 | } |
| 49 | |
| 50 | void DebugSession::open(const QString& file) |
| 51 | { |
| 52 | QFile f(file); |
| 53 | if (!f.open(QFile::ReadOnly | QFile::Text)) { |
| 54 | QMessageBox::warning(nullptr, tr("Open session ..."), |
| 55 | tr("Cannot read file %1:\n%2.") |
| 56 | .arg(file) |
| 57 | .arg(f.errorString())); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | // clear current project and start reading xml file |
| 62 | clear(); |
| 63 | QXmlStreamReader ses; |
| 64 | ses.setDevice(&f); |
| 65 | while (!ses.atEnd()) { |
| 66 | ses.readNext(); |
| 67 | if (ses.isStartElement() && (ses.name() == "DebugSession")) { |
| 68 | // debug session data |
| 69 | while (!ses.atEnd()) { |
| 70 | ses.readNext(); |
| 71 | // end tag |
| 72 | if (ses.isEndElement()) break; |
| 73 | // begin tag |
| 74 | if (ses.isStartElement()) { |
| 75 | if (ses.name() == "Symbols") { |
| 76 | symTable.loadSymbols(ses); |
| 77 | } else if (ses.name() == "Breakpoints") { |
| 78 | breaks.loadBreakpoints(ses); |
| 79 | } else { |
| 80 | skipUnknownElement(ses); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | fileName = file; |
| 88 | modified = false; |
| 89 | } |
| 90 | |
| 91 | void DebugSession::skipUnknownElement(QXmlStreamReader& ses) |
| 92 | { |
| 93 | while (!ses.atEnd()) { |
| 94 | ses.readNext(); |
| 95 | if (ses.isEndElement()) break; |
| 96 | if (ses.isStartElement()) skipUnknownElement(ses); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | bool DebugSession::save() |
| 101 | { |
| 102 | return saveAs(fileName); |
| 103 | } |
| 104 | |
| 105 | bool DebugSession::saveAs(const QString& newFileName) |
| 106 | { |
| 107 | // open file for save |
| 108 | QFile file(newFileName); |
| 109 | if (!file.open(QFile::WriteOnly | QFile::Text)) { |
| 110 | QMessageBox::warning(nullptr, tr("Save session ..."), |
| 111 | tr("Cannot write file %1:\n%2.") |
| 112 | .arg(fileName) |
| 113 | .arg(file.errorString())); |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | // start xml file |
| 118 | QXmlStreamWriter ses; |
| 119 | ses.setDevice(&file); |
| 120 | ses.setAutoFormatting(true); |
| 121 | ses.writeDTD("<!DOCTYPE xomds>"); |
| 122 | ses.writeStartElement("DebugSession"); |
| 123 | ses.writeAttribute("version", "0.1"); |
| 124 | |
| 125 | // write symbols |
| 126 | ses.writeStartElement("Symbols"); |
| 127 | symTable.saveSymbols(ses); |
| 128 | ses.writeEndElement(); |
| 129 | |
| 130 | // write breakpoints |
| 131 | ses.writeStartElement("Breakpoints"); |
| 132 | breaks.saveBreakpoints(ses); |
| 133 | ses.writeEndElement(); |
| 134 | |
| 135 | // end |
| 136 | ses.writeEndDocument(); |
| 137 | modified = false; |
| 138 | fileName = newFileName; // only set after successful save |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | void DebugSession::sessionModified() |
| 143 | { |
| 144 | modified = true; |
| 145 | } |