From 808cbc32c53991483539dc515ab9b8d0b9d06ad2 Mon Sep 17 00:00:00 2001 From: Mark Long Date: Mon, 14 Dec 2015 04:48:19 -0600 Subject: [PATCH] Added new ApplesoftFormatter and moved formatting functionality there. --- AppleSAWS.pro | 6 +- src/applesoftfile/applesoftfile.cxx | 70 ++---------------------- src/applesoftfile/applesoftfile.h | 20 ++++--- src/applesoftfile/applesoftformatter.cxx | 45 +++++++++++++++ src/applesoftfile/applesoftformatter.h | 44 +++++++++++++++ src/ui/mainwindow.cxx | 31 ++--------- src/ui/viewers/applesoftfileviewer.cxx | 16 ++++++ src/ui/viewers/applesoftfileviewer.h | 8 +++ 8 files changed, 138 insertions(+), 102 deletions(-) create mode 100644 src/applesoftfile/applesoftformatter.cxx create mode 100644 src/applesoftfile/applesoftformatter.h diff --git a/AppleSAWS.pro b/AppleSAWS.pro index 440be7d..64ed5ab 100644 --- a/AppleSAWS.pro +++ b/AppleSAWS.pro @@ -32,7 +32,8 @@ SOURCES += \ src/ui/catalogwidget.cxx \ src/ui/mainwindow.cxx \ src/ui/viewers/hiresviewwidget.cxx \ - src/ui/viewers/applesoftfileviewer.cxx + src/ui/viewers/applesoftfileviewer.cxx \ + src/applesoftfile/applesoftformatter.cxx HEADERS += \ src/diskfiles/dos33/diskfile.h \ @@ -51,7 +52,8 @@ HEADERS += \ src/ui/catalogwidget.h \ src/ui/mainwindow.h \ src/ui/viewers/hiresviewwidget.h \ - src/ui/viewers/applesoftfileviewer.h + src/ui/viewers/applesoftfileviewer.h \ + src/applesoftfile/applesoftformatter.h FORMS += \ src/ui/catalogwidget.ui \ diff --git a/src/applesoftfile/applesoftfile.cxx b/src/applesoftfile/applesoftfile.cxx index d664ca6..4cdf859 100644 --- a/src/applesoftfile/applesoftfile.cxx +++ b/src/applesoftfile/applesoftfile.cxx @@ -3,8 +3,6 @@ ApplesoftFile::ApplesoftFile(QByteArray data) : GenericFile(data) { - - if (!data.isEmpty()) { setData(data); @@ -19,12 +17,13 @@ void ApplesoftFile::setData(QByteArray data) quint8 addhi = m_data.at(1); m_length = addlo + (addhi * 256); m_data.remove(0,2); - m_detokenized = detokenize(); + parse(); + // m_detokenized = detokenize(); } -QList ApplesoftFile::detokenize(quint16 start_address) +void ApplesoftFile::parse(quint16 start_address) { - QList retval; +// QList retval; int idx = 0; quint8 val = 0; @@ -44,50 +43,10 @@ QList ApplesoftFile::detokenize(quint16 start_address) val = m_data[idx++]; ApplesoftToken token(val); line.tokens.append(token); - // line.raw_tokens.append(val); - -// if (val >= 0x80) { -// // line.detokenized_line.append("~"); -// line.detokenized_line.append(m_tokens[val]); -// } else { -// if (val >= 0x20) { -// line.detokenized_line.append(val); -// } else if (val == 0x7F) { -// QChar ch = QChar(0x2401); -// line.detokenized_line.append(ch); -// }else if (val > 0x00) { -// QChar ch = QChar(0x2400 + val); -// line.detokenized_line.append(ch); -// } -// } } while (val != 0x00); - Retokenizer::retokenize(line); - - //Make Detokenized line - foreach (ApplesoftToken token, line.tokens) { - quint16 tokenval = token.getTokenId(); - line.detokenized_line.append(token.getRawPrintableString()); -// if (tokenval >= 0x80 && tokenval < 0xFE) { -// line.detokenized_line.append("{kwd}"); -// } else if (tokenval >= 0xF0) { -// line.detokenized_line.append(ApplesoftToken::getStringForToken(tokenval)); -// } else { -// if (tokenval >= 0x20) { -// line.detokenized_line.append(tokenval); -// } else if (tokenval == 0x7F) { -// QChar ch = QChar(0x2401); -// line.detokenized_line.append(ch); -// }else if (tokenval > 0x00) { -// QChar ch = QChar(0x2400 + tokenval); -// line.detokenized_line.append(ch); -// } -// } - } - - current_address = line.next_address; - retval.append(line); + m_lines.append(line); } m_data_end = idx; @@ -95,25 +54,6 @@ QList ApplesoftFile::detokenize(quint16 start_address) if (idx < m_data.length()) { qDebug() << QString("%1 byte(s) unaccounted for.").arg(m_data.length() - idx); } - - return retval; -} - -void ApplesoftFile::list() { - foreach (ApplesoftLine line,detokenized()) { - QString debugline = QString("%1 %2 %3").arg(line.next_address,4,16,QChar('0')).arg(line.linenum).arg(line.detokenized_line); - qDebug() << debugline; -/* - debugline = ""; - foreach (quint8 val, line.tokens) { - debugline.append(QString("%1 ").arg(val,2,16,QChar('0'))); - } - qDebug() << " " << debugline; - qDebug() << "\n"; -*/ - } - - qDebug() << extraDataHexValues().join("\n"); } QStringList ApplesoftFile::extraDataHexValues() { diff --git a/src/applesoftfile/applesoftfile.h b/src/applesoftfile/applesoftfile.h index 233e326..b3378fc 100644 --- a/src/applesoftfile/applesoftfile.h +++ b/src/applesoftfile/applesoftfile.h @@ -14,9 +14,7 @@ struct ApplesoftLine { quint16 next_address; quint16 linenum; QVector tokens; - // QByteArray raw_tokens; - // QByteArray advanced_tokens; - QString detokenized_line; + // QString detokenized_line; }; @@ -25,17 +23,23 @@ class ApplesoftFile : public GenericFile public: ApplesoftFile(QByteArray data = QByteArray()); void setData(QByteArray data); + void setFilename(QString filename) { m_filename = filename; } QByteArray extraData(); - QList detokenized() { return m_detokenized; } - void list(); + // QList detokenized() { return m_detokenized; } QStringList extraDataHexValues(); -private: - QList detokenize(quint16 start_address = 0x0801); + QVector getLines() const { return m_lines; } + QString filename() const { return m_filename; } + +private: + void parse(quint16 start_address = 0x0801); + + QVector m_lines; int m_data_end; quint16 m_length; - QList m_detokenized; + QString m_filename; + // QList m_detokenized; friend class Retokenizer; }; diff --git a/src/applesoftfile/applesoftformatter.cxx b/src/applesoftfile/applesoftformatter.cxx new file mode 100644 index 0000000..bea8a9e --- /dev/null +++ b/src/applesoftfile/applesoftformatter.cxx @@ -0,0 +1,45 @@ +#include "applesoftformatter.h" + +ApplesoftFormatter::ApplesoftFormatter(QObject *parent) : + QObject(parent) +{ + m_file = 0; +} + +void ApplesoftFormatter::setFile(ApplesoftFile *file) +{ + m_file = file; + emit newFile(file); +} + +QString ApplesoftFormatter::formatText() +{ + if (!m_file) { + return ("No File"); + } + + QString retval; + foreach (ApplesoftLine line, m_file->getLines()) { + QString linestring = QString("%1 ").arg(line.linenum,5,10,QChar(' ')); + retval.append(linestring); + foreach (ApplesoftToken token, line.tokens) { + QString tokenstr = token.getRawPrintableString(); + + if (m_format_options.testFlag(ShowCtrlChars)) { + tokenstr.replace(QChar(0x7f),QChar(0x2401)); + + for (int idx = 1; idx <= 0x1f; idx++) { + tokenstr.replace(QChar(idx),QChar(idx+0x2400)); + } + } + + if (m_format_options.testFlag(BreakAfterReturn)) { + tokenstr.replace("RETURN","RETURN\n"); + } + + retval.append(tokenstr); + } + retval.append("\n"); + } + return retval; +} diff --git a/src/applesoftfile/applesoftformatter.h b/src/applesoftfile/applesoftformatter.h new file mode 100644 index 0000000..8309270 --- /dev/null +++ b/src/applesoftfile/applesoftformatter.h @@ -0,0 +1,44 @@ +#ifndef APPLESOFTFORMATTER_H +#define APPLESOFTFORMATTER_H + +#include +#include +#include "applesoftfile.h" + +class ApplesoftFormatter : public QObject +{ + Q_OBJECT + +public: + enum FormatOption { + NoOptions = 0x00, + FormatHTML = 0x01, + ShowCtrlChars = 0x02, + BreakAfterReturn = 0x04, + PrettyFlags = 0x07, + AllFlags = 0xffffffff + }; + Q_DECLARE_FLAGS(FormatOptions, FormatOption) + +public: + explicit ApplesoftFormatter(QObject *parent = 0); + void setFlags(FormatOptions options) { m_format_options = options; } + void setFile(ApplesoftFile *file); + + QString formatText(); + +signals: + void newFile(ApplesoftFile *file); + +public slots: + +private: + FormatOptions m_format_options; + + ApplesoftFile *m_file; + +}; + +Q_DECLARE_OPERATORS_FOR_FLAGS(ApplesoftFormatter::FormatOptions) + +#endif // APPLESOFTFORMATTER_H diff --git a/src/ui/mainwindow.cxx b/src/ui/mainwindow.cxx index 7a0b08f..2c5bd03 100644 --- a/src/ui/mainwindow.cxx +++ b/src/ui/mainwindow.cxx @@ -32,6 +32,7 @@ MainWindow::MainWindow(QWidget *parent) : connect(this, SIGNAL(diskFileUnloading(DiskFile*)), ui->catalogWidget, SLOT(unloadDisk(DiskFile*))); + } MainWindow::~MainWindow() @@ -95,34 +96,10 @@ void MainWindow::handleDiskItemSelectedDefaultOpen(DiskFile *disk, FileDescripti else if (dynamic_cast(file)) { ApplesoftFile *abf = dynamic_cast(file); - ApplesoftFileViewer *afv = new ApplesoftFileViewer(0); - QString title = QString("AppleSoft Viewer: %1").arg(AppleString(fde.filename).printable().trimmed()); - afv->setWindowTitle(title); - QStringList strings; - foreach (ApplesoftLine line, abf->detokenized()) - { - QString linestring = QString("%1 %2").arg(line.linenum,5,10,QChar(' ')).arg(line.detokenized_line); - strings.append(linestring); - } - QString fulltext = Qt::convertFromPlainText(strings.join("\n")); - // fulltext.replace("{kwd}","{kwd}"); - // fulltext.replace("{num}","{num}"); - // fulltext.replace("{string}","{string}"); - fulltext.replace("GOTO","GOTO"); - fulltext.replace("GOSUB","GOSUB"); -// fulltext.replace("0","0"); -// fulltext.replace("1","1"); -// fulltext.replace("2","2"); -// fulltext.replace("3","3"); -// fulltext.replace("4","4"); -// fulltext.replace("5","5"); -// fulltext.replace("6","6"); -// fulltext.replace("7","7"); -// fulltext.replace("8","8"); -// fulltext.replace("9","9"); - fulltext.replace("RETURN","RETURN

"); + abf->setFilename(AppleString(fde.filename).printable().trimmed()); - afv->setText(fulltext); + ApplesoftFileViewer *afv = new ApplesoftFileViewer(0); + afv->setFile(abf); afv->show(); } diff --git a/src/ui/viewers/applesoftfileviewer.cxx b/src/ui/viewers/applesoftfileviewer.cxx index 9ceff97..38af881 100644 --- a/src/ui/viewers/applesoftfileviewer.cxx +++ b/src/ui/viewers/applesoftfileviewer.cxx @@ -1,11 +1,17 @@ #include "applesoftfileviewer.h" #include "ui_applesoftfileviewer.h" +#include "applesoftformatter.h" ApplesoftFileViewer::ApplesoftFileViewer(QWidget *parent) : QWidget(parent), ui(new Ui::ApplesoftFileViewer) { ui->setupUi(this); + QString title = QString("AppleSoft Viewer"); + setWindowTitle(title); + + m_formatter = new ApplesoftFormatter(this); + m_formatter->setFlags(ApplesoftFormatter::PrettyFlags); } ApplesoftFileViewer::~ApplesoftFileViewer() @@ -13,6 +19,16 @@ ApplesoftFileViewer::~ApplesoftFileViewer() delete ui; } +void ApplesoftFileViewer::setFile(ApplesoftFile *file) { + m_file = file; + m_formatter->setFile(file); + + QString title = QString("AppleSoft Viewer: %1").arg(m_file->filename()); + setWindowTitle(title); + + ui->textArea->setText(m_formatter->formatText()); +} + void ApplesoftFileViewer::setData(QByteArray data) { ui->textArea->setText(data); diff --git a/src/ui/viewers/applesoftfileviewer.h b/src/ui/viewers/applesoftfileviewer.h index 1ac5323..0460955 100644 --- a/src/ui/viewers/applesoftfileviewer.h +++ b/src/ui/viewers/applesoftfileviewer.h @@ -2,6 +2,8 @@ #define APPLESOFTFILEVIEWER_H #include +#include "applesoftfile.h" +#include "applesoftformatter.h" namespace Ui { class ApplesoftFileViewer; @@ -15,11 +17,17 @@ public: explicit ApplesoftFileViewer(QWidget *parent = 0); ~ApplesoftFileViewer(); + void setFormatter(ApplesoftFormatter *formatter); + public slots: + void setFile(ApplesoftFile *m_file); void setData(QByteArray data); void setText(QString text); private: + ApplesoftFile *m_file; + ApplesoftFormatter *m_formatter; + Ui::ApplesoftFileViewer *ui; };