src/blfwk/StSRecordFile.h
/*
* Copyright (c) 2013-14, Freescale Semiconductor, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* o Neither the name of Freescale Semiconductor, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if !defined(_StSRecordFile_h_)
#define _StSRecordFile_h_
//#include <stdint.h>
#include "stdafx.h"
#include <istream>
#include <string>
#include <vector>
#include <stdexcept>
enum
{
SRECORD_START_CHAR = 'S',
SRECORD_MIN_LENGTH = 10,
SRECORD_ADDRESS_START_CHAR_INDEX = 4
};
class StSRecordFile
{
public:
struct SRecord
{
unsigned m_type;
unsigned m_count;
uint32_t m_address;
unsigned m_dataCount;
uint8_t *m_data;
uint8_t m_checksum;
};
typedef std::vector<SRecord>::const_iterator const_iterator;
public:
StSRecordFile(std::istream &inStream);
virtual ~StSRecordFile();
virtual void setName(const std::string &inName) { m_name = inName; }
virtual std::string getName() const { return m_name; }
virtual bool isSRecordFile();
virtual void parse();
inline unsigned getRecordCount() const { return static_cast<unsigned>(m_records.size()); }
inline const_iterator getBegin() const { return m_records.begin(); }
inline const_iterator getEnd() const { return m_records.end(); }
inline const SRecord &operator[](unsigned inIndex) { return m_records[inIndex]; }
protected:
std::istream &m_stream;
std::vector<SRecord> m_records;
std::string m_name;
virtual void parseLine(std::string &inLine);
bool isHexDigit(char c);
int hexDigitToInt(char digit);
int readHexByte(std::string &inString, int inIndex);
};
class StSRecordParseException : public std::runtime_error
{
public:
StSRecordParseException(const std::string &inMessage)
: std::runtime_error(inMessage)
{
}
};
#endif // _StSRecordFile_h_
StSRecordFile::SRecord::m_checksum
uint8_t m_checksum
The checksum byte present in the S-record.
Definition: apps/elftosb/common/StSRecordFile.h:51
StSRecordFile::SRecord
Definition: apps/elftosb/common/StSRecordFile.h:44
StSRecordFile::parse
virtual void parse()
Parses the entire S-record input stream.
Definition: apps/elftosb/common/StSRecordFile.cpp:54
StSRecordFile::SRecord::m_address
uint32_t m_address
The address specified as part of the S-record.
Definition: apps/elftosb/common/StSRecordFile.h:48
StSRecordFile::m_stream
std::istream & m_stream
The input stream for the S-record data.
Definition: apps/elftosb/common/StSRecordFile.h:94
std
STL namespace.
StSRecordFile::SRecord::m_dataCount
unsigned m_dataCount
Number of bytes of data.
Definition: apps/elftosb/common/StSRecordFile.h:49
StSRecordFile
S-record parser.
Definition: apps/elftosb/common/StSRecordFile.h:38
StSRecordFile::readHexByte
int readHexByte(std::string &inString, int inIndex)
Definition: apps/elftosb/common/StSRecordFile.cpp:108
StSRecordFile::m_name
std::string m_name
File name. (optional)
Definition: apps/elftosb/common/StSRecordFile.h:97
StSRecordParseException
Simple exception thrown to indicate an error in the input SRecord data format.
Definition: apps/elftosb/common/StSRecordFile.h:112
StSRecordFile::const_iterator
std::vector< SRecord >::const_iterator const_iterator
Iterator type.
Definition: src/blfwk/StSRecordFile.h:78
StSRecordFile::m_records
std::vector< SRecord > m_records
Vector of S-records in the input data.
Definition: apps/elftosb/common/StSRecordFile.h:95
StSRecordFile::getBegin
const_iterator getBegin() const
Definition: src/blfwk/StSRecordFile.h:107
StSRecordFile::getRecordCount
unsigned getRecordCount() const
Definition: apps/elftosb/common/StSRecordFile.h:82
StSRecordFile::SRecord::m_count
unsigned m_count
Number of character pairs (bytes) from address through checksum.
Definition: apps/elftosb/common/StSRecordFile.h:47
StSRecordFile::~StSRecordFile
virtual ~StSRecordFile()
Destructor.
Definition: apps/elftosb/common/StSRecordFile.cpp:18
StSRecordFile::StSRecordFile
StSRecordFile(std::istream &inStream)
Constructor.
Definition: apps/elftosb/common/StSRecordFile.cpp:12
StSRecordParseException::StSRecordParseException
StSRecordParseException(const std::string &inMessage)
Default constructor.
Definition: src/blfwk/StSRecordFile.h:139
StSRecordFile::SRecord::m_type
unsigned m_type
Record number type, such as 9 for "S9", 3 for "S3" and so on.
Definition: apps/elftosb/common/StSRecordFile.h:46
StSRecordFile::parseLine
virtual void parseLine(std::string &inLine)
Parses individual S-records.
Definition: apps/elftosb/common/StSRecordFile.cpp:128
StSRecordFile::isSRecordFile
virtual bool isSRecordFile()
Determine if the file is an S-record file.
Definition: apps/elftosb/common/StSRecordFile.cpp:33
StSRecordFile::SRecord::m_data
uint8_t * m_data
Pointer to data, or NULL if no data for this record type.
Definition: apps/elftosb/common/StSRecordFile.h:50