/*------------------------------------------------------------------------------ -- This file is a part of the SocExplorer Software -- Copyright (C) 2014, Plasma Physics Laboratory - CNRS -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by -- the Free Software Foundation; either version 2 of the License, or -- (at your option) any later version. -- -- This program is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- -- You should have received a copy of the GNU General Public License -- along with this program; if not, write to the Free Software -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -------------------------------------------------------------------------------*/ /*-- Author : Alexis Jeandet -- Mail : alexis.jeandet@member.fsf.org ----------------------------------------------------------------------------*/ #include "srecfile.h" #include #include "binaryfile.h" srecFile::srecFile() { } srecFile::srecFile(const QString &File) { openFile(File); } srecFile::srecFile(const QStringList &Files) { openFiles(Files); } srecFile::~srecFile() { } bool srecFile::openFile(const QString &File) { return openFiles(QStringList()<p_isSrec=true; this->p_isSrec &= isSREC(Files.at(i)); this->p_files.append(new QFile(Files.at(i))); this->p_files.at(i)->open(QIODevice::ReadOnly); parseFile(this->p_files.at(i)); } return true; } bool srecFile::isopened() { bool opened = true; for(int i=0;ip_files.count();i++) { opened &= p_files.at(i)->isOpen(); } return opened; } int srecFile::closeFile() { for(int i=0;idata); delete p_fragments.at(i); } p_fragments.clear(); p_files.clear(); p_fileName.clear(); return 0; } QList srecFile::getFragments() { return p_fragments; } bool srecFile::toSrec(QList fragments, const QString &File) { QString line; QFile file(File); file.open(QIODevice::WriteOnly); if(file.isOpen()) { QTextStream stream( &file ); //First build header line.append("S0"); line.append(QString("%1").arg(File.count()+3,2,16).replace(' ','0')); line.append("0000"); for(int i=0;isize);j+=16) { line.clear(); line.append("S315"); line.append(QString("%1").arg(fragment->address+j,8,16).replace(' ','0')); for(int k=0;k<16;k++) { line.append(QString("%1").arg((uchar)fragment->data[j+k],2,16).replace(' ','0')); } line.append(QString("%1").arg((uchar)srecFile::lineCheckSum(line),2,16).replace(' ','0')); line.append('\n'); stream << line.toUpper(); } int rem = fragment->size%16; if(rem) { line.clear(); line.append("S3"); line.append(QString("%1").arg(rem,2,16).replace(' ','0')); line.append(QString("%1").arg(fragment->address+fragment->size-rem,8,16).replace(' ','0')); for(int k=0;kdata[fragment->size-rem+k],2,16).replace(' ','0')); } line.append(QString("%1").arg((uchar)srecFile::lineCheckSum(line),2,16).replace(' ','0')); line.append('\n'); stream << line.toUpper(); } line.clear(); line.append("S705"); line.append(QString("%1").arg(fragment->address,8,16).replace(' ','0')); line.append(QString("%1").arg((uchar)srecFile::lineCheckSum(line),2,16).replace(' ','0')); line.append('\n'); stream << line.toUpper(); } file.close(); return true; } return false; } bool srecFile::toSrec(const QString &File) { return toSrec(p_fragments,File); } bool srecFile::toBinary(const QString &File) { return binaryFile::toBinary(p_fragments,File); } int srecFile::lineCount() { return p_lineCount; } int srecFile::getFragmentsCount() { return p_fragments.count(); } int srecFile::getFragmentAddress(int index) { if((index < p_fragments.count()) && (index>=0)) { return p_fragments.at(index)->address; } return 0; } int srecFile::getFragmentSize(int index) { if((index < p_fragments.count()) && (index>=0)) { return p_fragments.at(index)->size; } return 0; } codeFragment *srecFile::getFragment(int index) { if((index < p_fragments.count()) && (index>=0)) { return p_fragments.at(index); } return NULL; } QString srecFile::getFragmentHeader(int index) { if((index < p_fragments.count()) && (index>=0)) { return p_fragments.at(index)->header; } return ""; } bool srecFile::getFragmentData(int index, char **buffer) { if((index < p_fragments.count()) && (index>=0)) { *buffer = (char *)this->p_fragments.at(index)->data; return true; } return false; } bool srecFile::isSREC() { return p_isSrec & isopened(); } bool srecFile::isSREC(const QString &File) { QFile file(File); file.open(QIODevice::ReadOnly); if(file.isOpen()) { file.seek(0); QString line=file.readLine(); file.close(); return ((line.at(0)=='S')&&(line.at(1)=='0')); } return false; } void srecFile::parseFile(QFile *file) { if(file->isOpen()) { this->p_lineCount = 0; file->seek(0); codeFragment* fragment=NULL; QByteArray data; quint64 size=0; quint64 address=-1; QString header; while (!file->atEnd()) { QString line = file->readLine(); p_lineCount++; if(line.count()>4) { if(line.at(0)=='S') { bool ok; int count = line.mid(2,2).toInt(&ok,16); if(line.at(1)=='0') { header.clear(); for(int i=0;i<(count-3);i++) { header.append((char)line.mid((2*i)+8,2).toInt(&ok,16)); } } if(line.at(1)=='1') { } if(line.at(1)=='2') { } if(line.at(1)=='3') { int naddress =line.mid(4,8).toInt(&ok,16); if(address !=naddress) { if(fragment!=NULL) { fragment->size = data.size(); fragment->data = (char*)malloc(data.size()); for(int i =0;idata[i]=data.at(i); } data.clear(); p_fragments.append(fragment); } fragment = new codeFragment(); fragment->address = naddress; fragment->header = header; } address = naddress+count-5; for(int i=0;i<(count-5);i++) { data.append((char)line.mid((2*i)+12,2).toInt(&ok,16)); } } if(line.at(1)=='5') { } if(line.at(1)=='6') { } if(line.at(1)=='7') { } if(line.at(1)=='8') { } if(line.at(1)=='9') { } } } } if(data.size()!=0) { fragment->size = data.size(); fragment->data = (char*)malloc(data.size()); for(int i =0;idata[i]=data.at(i); } data.clear(); p_fragments.append(fragment); } } } char srecFile::lineCheckSum(const QString &line) { char sum=0; QString localLine = line; bool ok; if(localLine.at(0)=='S') // then should skip the first two digits { localLine.remove(0,2); } for(int i=0;i