#include <QFileDialog>
#include <QMessageBox>
#include <QTextCodec>
#include "qsfile.h"
QSFile::QSFile(QObject *parent) : QObject(parent)
{
qFd = 0;
iType = 0;
fName.clear();
setObjectName("QSFile");
}
//QSFile::QSFile(QString Name, int type) : QObject(0)
//{
// qFd = 0;
// iType = type;
// fName = Name;
// setObjectName("QSFile");
// Open(Name, type);
//}
QSFile::~QSFile()
{
if (qFd)
Close();
}
bool QSFile::Open(QString fn, int type) // Y
{
bool rc;
QString fPath;
if (qFd && qFd->isOpen())
Close();
iType = type;
QIODevice::OpenMode Mode = QIODevice::NotOpen;
if (type & 1) Mode |= QIODevice::ReadOnly;
if (type & 2) Mode |= QIODevice::WriteOnly;
if (type & 16) Mode |= QIODevice::Text;
if (fn.isEmpty())
fName = QFileDialog::getOpenFileName(0, "Open File", fPath, "files (*.*)");
else fName = fn;
if (fName.isEmpty())
return false;
qFd = new QFile(fName);
rc = qFd->open(Mode);
if (!rc)
{
QMessageBox::warning(0, "MyProg", "Error open File !");
return false;
}
return true;
}
void QSFile::Close()
{
if (!qFd) return;
if (qFd->isOpen())
qFd->close();
delete qFd;
qFd = 0;
}
QByteArray QSFile::Read(qint64 maxSize)
{
return qFd->read(maxSize);
}
//qint64 QSFile::Write(const QByteArray data)
//{
// return qFd->write(data);
//}
qint64 QSFile::Write(const QString data)
{
// QByteArray Array = byteArray.toUtf8();
// QByteArray Array = byteArray.toAscii();
QByteArray Array = data.toLocal8Bit();
return qFd->write((const QByteArray &) Array);
}
bool QSFile::putChar(int c)
{
return qFd->putChar((char ) c);
}
int QSFile::getChar()
{
int c = 0;
qFd->getChar((char *) &c);
return c;
}
void setupQSFile(QScriptEngine *engine)
{
QScriptValue fileClass = engine->scriptValueFromQMetaObject<QSFile>();
engine->globalObject().setProperty("QSFile", fileClass);
}