Added new ApplesoftFormatter and moved formatting functionality there.

This commit is contained in:
Mark Long 2015-12-14 04:48:19 -06:00
parent 2f7845229d
commit 808cbc32c5
8 changed files with 138 additions and 102 deletions

View File

@ -32,7 +32,8 @@ SOURCES += \
src/ui/catalogwidget.cxx \ src/ui/catalogwidget.cxx \
src/ui/mainwindow.cxx \ src/ui/mainwindow.cxx \
src/ui/viewers/hiresviewwidget.cxx \ src/ui/viewers/hiresviewwidget.cxx \
src/ui/viewers/applesoftfileviewer.cxx src/ui/viewers/applesoftfileviewer.cxx \
src/applesoftfile/applesoftformatter.cxx
HEADERS += \ HEADERS += \
src/diskfiles/dos33/diskfile.h \ src/diskfiles/dos33/diskfile.h \
@ -51,7 +52,8 @@ HEADERS += \
src/ui/catalogwidget.h \ src/ui/catalogwidget.h \
src/ui/mainwindow.h \ src/ui/mainwindow.h \
src/ui/viewers/hiresviewwidget.h \ src/ui/viewers/hiresviewwidget.h \
src/ui/viewers/applesoftfileviewer.h src/ui/viewers/applesoftfileviewer.h \
src/applesoftfile/applesoftformatter.h
FORMS += \ FORMS += \
src/ui/catalogwidget.ui \ src/ui/catalogwidget.ui \

View File

@ -3,8 +3,6 @@
ApplesoftFile::ApplesoftFile(QByteArray data) : GenericFile(data) ApplesoftFile::ApplesoftFile(QByteArray data) : GenericFile(data)
{ {
if (!data.isEmpty()) if (!data.isEmpty())
{ {
setData(data); setData(data);
@ -19,12 +17,13 @@ void ApplesoftFile::setData(QByteArray data)
quint8 addhi = m_data.at(1); quint8 addhi = m_data.at(1);
m_length = addlo + (addhi * 256); m_length = addlo + (addhi * 256);
m_data.remove(0,2); m_data.remove(0,2);
m_detokenized = detokenize(); parse();
// m_detokenized = detokenize();
} }
QList<ApplesoftLine> ApplesoftFile::detokenize(quint16 start_address) void ApplesoftFile::parse(quint16 start_address)
{ {
QList<ApplesoftLine> retval; // QList<ApplesoftLine> retval;
int idx = 0; int idx = 0;
quint8 val = 0; quint8 val = 0;
@ -44,50 +43,10 @@ QList<ApplesoftLine> ApplesoftFile::detokenize(quint16 start_address)
val = m_data[idx++]; val = m_data[idx++];
ApplesoftToken token(val); ApplesoftToken token(val);
line.tokens.append(token); 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); } 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; current_address = line.next_address;
retval.append(line); m_lines.append(line);
} }
m_data_end = idx; m_data_end = idx;
@ -95,25 +54,6 @@ QList<ApplesoftLine> ApplesoftFile::detokenize(quint16 start_address)
if (idx < m_data.length()) { if (idx < m_data.length()) {
qDebug() << QString("%1 byte(s) unaccounted for.").arg(m_data.length() - idx); 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() { QStringList ApplesoftFile::extraDataHexValues() {

View File

@ -14,9 +14,7 @@ struct ApplesoftLine {
quint16 next_address; quint16 next_address;
quint16 linenum; quint16 linenum;
QVector<ApplesoftToken> tokens; QVector<ApplesoftToken> tokens;
// QByteArray raw_tokens; // QString detokenized_line;
// QByteArray advanced_tokens;
QString detokenized_line;
}; };
@ -25,17 +23,23 @@ class ApplesoftFile : public GenericFile
public: public:
ApplesoftFile(QByteArray data = QByteArray()); ApplesoftFile(QByteArray data = QByteArray());
void setData(QByteArray data); void setData(QByteArray data);
void setFilename(QString filename) { m_filename = filename; }
QByteArray extraData(); QByteArray extraData();
QList<ApplesoftLine> detokenized() { return m_detokenized; } // QList<ApplesoftLine> detokenized() { return m_detokenized; }
void list();
QStringList extraDataHexValues(); QStringList extraDataHexValues();
private: QVector<ApplesoftLine> getLines() const { return m_lines; }
QList<ApplesoftLine> detokenize(quint16 start_address = 0x0801);
QString filename() const { return m_filename; }
private:
void parse(quint16 start_address = 0x0801);
QVector<ApplesoftLine> m_lines;
int m_data_end; int m_data_end;
quint16 m_length; quint16 m_length;
QList<ApplesoftLine> m_detokenized; QString m_filename;
// QList<ApplesoftLine> m_detokenized;
friend class Retokenizer; friend class Retokenizer;
}; };

View File

@ -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;
}

View File

@ -0,0 +1,44 @@
#ifndef APPLESOFTFORMATTER_H
#define APPLESOFTFORMATTER_H
#include <QObject>
#include <QFlags>
#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

View File

@ -32,6 +32,7 @@ MainWindow::MainWindow(QWidget *parent) :
connect(this, SIGNAL(diskFileUnloading(DiskFile*)), connect(this, SIGNAL(diskFileUnloading(DiskFile*)),
ui->catalogWidget, SLOT(unloadDisk(DiskFile*))); ui->catalogWidget, SLOT(unloadDisk(DiskFile*)));
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
@ -95,34 +96,10 @@ void MainWindow::handleDiskItemSelectedDefaultOpen(DiskFile *disk, FileDescripti
else if (dynamic_cast<ApplesoftFile *>(file)) else if (dynamic_cast<ApplesoftFile *>(file))
{ {
ApplesoftFile *abf = dynamic_cast<ApplesoftFile *>(file); ApplesoftFile *abf = dynamic_cast<ApplesoftFile *>(file);
ApplesoftFileViewer *afv = new ApplesoftFileViewer(0); abf->setFilename(AppleString(fde.filename).printable().trimmed());
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}","<font color=\"red\">{kwd}</font>");
// fulltext.replace("{num}","<font color=\"red\">{num}</font>");
// fulltext.replace("{string}","<font color=\"red\">{string}</font>");
fulltext.replace("GOTO","<b><font color=\"green\">GOTO</font></b>");
fulltext.replace("GOSUB","<b><font color=\"green\">GOSUB</font></b>");
// fulltext.replace("0","<font color=\"orange\">0</font>");
// fulltext.replace("1","<font color=\"orange\">1</font>");
// fulltext.replace("2","<font color=\"orange\">2</font>");
// fulltext.replace("3","<font color=\"orange\">3</font>");
// fulltext.replace("4","<font color=\"orange\">4</font>");
// fulltext.replace("5","<font color=\"orange\">5</font>");
// fulltext.replace("6","<font color=\"orange\">6</font>");
// fulltext.replace("7","<font color=\"orange\">7</font>");
// fulltext.replace("8","<font color=\"orange\">8</font>");
// fulltext.replace("9","<font color=\"orange\">9</font>");
fulltext.replace("RETURN","RETURN<p>");
afv->setText(fulltext); ApplesoftFileViewer *afv = new ApplesoftFileViewer(0);
afv->setFile(abf);
afv->show(); afv->show();
} }

View File

@ -1,11 +1,17 @@
#include "applesoftfileviewer.h" #include "applesoftfileviewer.h"
#include "ui_applesoftfileviewer.h" #include "ui_applesoftfileviewer.h"
#include "applesoftformatter.h"
ApplesoftFileViewer::ApplesoftFileViewer(QWidget *parent) : ApplesoftFileViewer::ApplesoftFileViewer(QWidget *parent) :
QWidget(parent), QWidget(parent),
ui(new Ui::ApplesoftFileViewer) ui(new Ui::ApplesoftFileViewer)
{ {
ui->setupUi(this); ui->setupUi(this);
QString title = QString("AppleSoft Viewer");
setWindowTitle(title);
m_formatter = new ApplesoftFormatter(this);
m_formatter->setFlags(ApplesoftFormatter::PrettyFlags);
} }
ApplesoftFileViewer::~ApplesoftFileViewer() ApplesoftFileViewer::~ApplesoftFileViewer()
@ -13,6 +19,16 @@ ApplesoftFileViewer::~ApplesoftFileViewer()
delete ui; 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) void ApplesoftFileViewer::setData(QByteArray data)
{ {
ui->textArea->setText(data); ui->textArea->setText(data);

View File

@ -2,6 +2,8 @@
#define APPLESOFTFILEVIEWER_H #define APPLESOFTFILEVIEWER_H
#include <QWidget> #include <QWidget>
#include "applesoftfile.h"
#include "applesoftformatter.h"
namespace Ui { namespace Ui {
class ApplesoftFileViewer; class ApplesoftFileViewer;
@ -15,11 +17,17 @@ public:
explicit ApplesoftFileViewer(QWidget *parent = 0); explicit ApplesoftFileViewer(QWidget *parent = 0);
~ApplesoftFileViewer(); ~ApplesoftFileViewer();
void setFormatter(ApplesoftFormatter *formatter);
public slots: public slots:
void setFile(ApplesoftFile *m_file);
void setData(QByteArray data); void setData(QByteArray data);
void setText(QString text); void setText(QString text);
private: private:
ApplesoftFile *m_file;
ApplesoftFormatter *m_formatter;
Ui::ApplesoftFileViewer *ui; Ui::ApplesoftFileViewer *ui;
}; };